]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Distributed under the Boost Software License, Version 1.0. (See |
2 | // accompanying file LICENSE_1_0.txt or copy at | |
3 | // http://www.boost.org/LICENSE_1_0.txt) | |
4 | // (C) Copyright 2012 Vicente J. Botet Escriba | |
5 | ||
6 | #ifndef BOOST_THREAD_REVERSE_LOCK_HPP | |
7 | #define BOOST_THREAD_REVERSE_LOCK_HPP | |
8 | #include <boost/thread/detail/config.hpp> | |
9 | #include <boost/thread/detail/move.hpp> | |
10 | #include <boost/thread/lockable_traits.hpp> | |
11 | #include <boost/thread/lock_options.hpp> | |
12 | #include <boost/thread/detail/delete.hpp> | |
13 | ||
14 | namespace boost | |
15 | { | |
16 | ||
17 | template<typename Lock> | |
18 | class reverse_lock | |
19 | { | |
20 | public: | |
21 | typedef typename Lock::mutex_type mutex_type; | |
22 | BOOST_THREAD_NO_COPYABLE(reverse_lock) | |
23 | ||
24 | explicit reverse_lock(Lock& m_) | |
25 | : m(m_), mtx(0) | |
26 | { | |
27 | if (m.owns_lock()) | |
28 | { | |
29 | m.unlock(); | |
30 | } | |
31 | mtx=m.release(); | |
32 | } | |
33 | ~reverse_lock() | |
34 | { | |
35 | if (mtx) { | |
36 | mtx->lock(); | |
37 | m = BOOST_THREAD_MAKE_RV_REF(Lock(*mtx, adopt_lock)); | |
38 | } | |
39 | } | |
40 | ||
41 | private: | |
42 | Lock& m; | |
43 | mutex_type* mtx; | |
44 | }; | |
45 | ||
46 | ||
47 | #ifdef BOOST_THREAD_NO_AUTO_DETECT_MUTEX_TYPES | |
48 | template<typename T> | |
49 | struct is_mutex_type<reverse_lock<T> > | |
50 | { | |
51 | BOOST_STATIC_CONSTANT(bool, value = true); | |
52 | }; | |
53 | ||
54 | #endif | |
55 | ||
56 | ||
57 | } | |
58 | ||
59 | #endif // header |