Ardour  9.0-pre0-350-gf17a656217
spinlock.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 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 <cstring>
24 
25 #include "pbd/libpbd_visibility.h"
26 
27 namespace PBD {
28 
29 /* struct boost::detail::spinlock {
30  * void lock();
31  * bool try_lock();
32  * void unlock();
33  * };
34  *
35  * initialize with BOOST_DETAIL_SPINLOCK_INIT
36  */
37 
38 struct spinlock_t {
39 public:
40 #ifdef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
41  /* C++11 non-static data member initialization,
42  * with non-copyable std::atomic ATOMIC_FLAG_INIT
43  */
44  spinlock_t () {}
45 #else
46  /* default C++ assign struct's first member */
48  {
49  boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
50  std::memcpy (&l, &init, sizeof (init));
51  }
52 #endif
53  void lock () { l.lock (); }
54  void unlock () { l.unlock (); }
55  bool try_lock () { return l.try_lock (); }
56 
57 private:
58 #ifdef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
59  boost::detail::spinlock l = BOOST_DETAIL_SPINLOCK_INIT;
60 #else
61  boost::detail::spinlock l;
62 #endif
63 
64  /* prevent copy construction */
66 };
67 
68 /* RAII wrapper */
70 
71 public:
74 
75 private:
77 };
78 
79 } /* namespace */
80 
81 #endif /* _pbd_spinlock_h__ */
SpinLock(spinlock_t &)
spinlock_t & _lock
Definition: spinlock.h:76
#define LIBPBD_API
Definition: axis_view.h:42
bool init()
void lock()
Definition: spinlock.h:53
bool try_lock()
Definition: spinlock.h:55
spinlock_t(const spinlock_t &)
boost::detail::spinlock l
Definition: spinlock.h:61
void unlock()
Definition: spinlock.h:54