7173fde594d176d733653666ec280ed36180cd67
[ardour.git] / libs / pbd / pbd / spinlock.h
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #ifndef _pbd_spinlock_h_
20 #define _pbd_spinlock_h_
21
22 #include <boost/smart_ptr/detail/spinlock.hpp>
23 #include "pbd/libpbd_visibility.h"
24
25 namespace PBD {
26
27 /* struct boost::detail::spinlock {
28  *   void lock();
29  *   bool try_lock();
30  *   void unlock();
31  * };
32  *
33  * initialize with BOOST_DETAIL_SPINLOCK_INIT
34  */
35 static boost::detail::spinlock sl_init = BOOST_DETAIL_SPINLOCK_INIT;
36
37 struct spinlock_t {
38 public:
39         spinlock_t () : l (sl_init) {};
40         void lock () { l.lock (); }
41         void unlock () { l.unlock (); }
42         bool try_lock () { return l.try_lock (); }
43 private:
44         boost::detail::spinlock l;
45 };
46
47 /* RAII wrapper */
48 class LIBPBD_API SpinLock {
49
50 public:
51         SpinLock (spinlock_t&);
52         ~SpinLock ();
53
54 private:
55         spinlock_t& _lock;
56 };
57
58 } /* namespace */
59
60 #endif /* _pbd_spinlock_h__ */