Chris@16
|
1 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
2 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
3 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
4 // (C) Copyright 2008-2009,2012 Vicente J. Botet Escriba
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_THREAD_STRICT_LOCK_HPP
|
Chris@16
|
7 #define BOOST_THREAD_STRICT_LOCK_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #include <boost/thread/detail/config.hpp>
|
Chris@16
|
10 #include <boost/thread/detail/delete.hpp>
|
Chris@16
|
11 #include <boost/thread/detail/lockable_wrapper.hpp>
|
Chris@16
|
12 #include <boost/thread/lock_options.hpp>
|
Chris@16
|
13 #include <boost/thread/lock_traits.hpp>
|
Chris@16
|
14 #include <boost/thread/lockable_traits.hpp>
|
Chris@16
|
15 #include <boost/thread/lockable_concepts.hpp>
|
Chris@16
|
16 #include <boost/thread/lock_concepts.hpp>
|
Chris@16
|
17 #include <boost/thread/exceptions.hpp>
|
Chris@16
|
18 #include <boost/throw_exception.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/config/abi_prefix.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost
|
Chris@16
|
23 {
|
Chris@16
|
24
|
Chris@16
|
25
|
Chris@16
|
26 //[strict_lock
|
Chris@16
|
27 template <typename Lockable>
|
Chris@16
|
28 class strict_lock
|
Chris@16
|
29 {
|
Chris@16
|
30
|
Chris@16
|
31 BOOST_CONCEPT_ASSERT(( BasicLockable<Lockable> ));
|
Chris@16
|
32 public:
|
Chris@16
|
33 typedef Lockable mutex_type;
|
Chris@16
|
34
|
Chris@16
|
35 // construct/copy/destroy:
|
Chris@16
|
36
|
Chris@16
|
37 BOOST_THREAD_NO_COPYABLE( strict_lock)
|
Chris@16
|
38
|
Chris@16
|
39 /**
|
Chris@16
|
40 * Constructor from a mutex reference.
|
Chris@16
|
41 *
|
Chris@16
|
42 * @param mtx the mutex to lock.
|
Chris@16
|
43 *
|
Chris@16
|
44 * __Effects: Stores a reference to the mutex to lock and locks it.
|
Chris@16
|
45 * __Throws: Any exception BasicMutex::lock() can throw.
|
Chris@16
|
46 */
|
Chris@16
|
47 explicit strict_lock(mutex_type& mtx) :
|
Chris@16
|
48 mtx_(mtx)
|
Chris@16
|
49 {
|
Chris@16
|
50 mtx.lock();
|
Chris@16
|
51 } /*< locks on construction >*/
|
Chris@16
|
52
|
Chris@16
|
53
|
Chris@16
|
54 #if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
|
Chris@16
|
55 strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lockable> > l_) :
|
Chris@16
|
56 mtx_(*(const_cast<thread_detail::lockable_wrapper<Lockable>*>(l_.begin())->m))
|
Chris@16
|
57 {
|
Chris@16
|
58 mtx_.lock();
|
Chris@16
|
59 }
|
Chris@16
|
60 #endif
|
Chris@16
|
61
|
Chris@16
|
62 /**
|
Chris@16
|
63 * Destructor
|
Chris@16
|
64 *
|
Chris@16
|
65 * __Effects: unlocks the stored mutex.
|
Chris@16
|
66 *
|
Chris@16
|
67 * __Throws
|
Chris@16
|
68 */
|
Chris@16
|
69 ~strict_lock()
|
Chris@16
|
70 {
|
Chris@16
|
71 mtx_.unlock();
|
Chris@16
|
72 } /*< unlocks on destruction >*/
|
Chris@16
|
73
|
Chris@16
|
74
|
Chris@16
|
75 // observers
|
Chris@16
|
76
|
Chris@16
|
77 /**
|
Chris@16
|
78 * @return the owned mutex.
|
Chris@16
|
79 */
|
Chris@16
|
80 mutex_type* mutex() const BOOST_NOEXCEPT
|
Chris@16
|
81 {
|
Chris@16
|
82 return &mtx_;
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 /**
|
Chris@16
|
86 * @return whether this lock is locking a mutex.
|
Chris@16
|
87 */
|
Chris@16
|
88 bool owns_lock() const BOOST_NOEXCEPT
|
Chris@16
|
89 {
|
Chris@16
|
90 return true;
|
Chris@16
|
91 }
|
Chris@16
|
92
|
Chris@16
|
93 /**
|
Chris@16
|
94 * @return whether this lock is locking that mutex.
|
Chris@16
|
95 */
|
Chris@16
|
96 bool owns_lock(const mutex_type* l) const BOOST_NOEXCEPT
|
Chris@16
|
97 {
|
Chris@16
|
98 return l == mutex();
|
Chris@16
|
99 } /*< strict locks specific function >*/
|
Chris@16
|
100
|
Chris@16
|
101 //BOOST_ADRESS_OF_DELETE(strict_lock) /*< disable aliasing >*/
|
Chris@16
|
102 //BOOST_HEAP_ALLOCATION_DELETE(strict_lock) /*< disable heap allocation >*/
|
Chris@16
|
103
|
Chris@16
|
104 /*< no possibility to unlock >*/
|
Chris@16
|
105
|
Chris@16
|
106 private:
|
Chris@16
|
107 mutex_type& mtx_;
|
Chris@16
|
108 };
|
Chris@16
|
109 //]
|
Chris@16
|
110 template <typename Lockable>
|
Chris@16
|
111 struct is_strict_lock_sur_parole<strict_lock<Lockable> > : true_type
|
Chris@16
|
112 {
|
Chris@16
|
113 };
|
Chris@16
|
114
|
Chris@16
|
115 /**
|
Chris@16
|
116 * A nested strict lock is a scoped lock guard ensuring the mutex is locked on its
|
Chris@16
|
117 * scope, by taking ownership of an nesting lock, locking the mutex on construction if not already locked
|
Chris@16
|
118 * and restoring the ownership to the nesting lock on destruction.
|
Chris@16
|
119 */
|
Chris@16
|
120 //[nested_strict_lock
|
Chris@16
|
121 template <typename Lock>
|
Chris@16
|
122 class nested_strict_lock
|
Chris@16
|
123 {
|
Chris@16
|
124 BOOST_CONCEPT_ASSERT(( BasicLock<Lock> )); /*< The Lock must be a movable lock >*/
|
Chris@16
|
125 public:
|
Chris@16
|
126 typedef typename Lock::mutex_type mutex_type; /*< Name the lockable type locked by Lock >*/
|
Chris@16
|
127
|
Chris@16
|
128 BOOST_THREAD_NO_COPYABLE( nested_strict_lock)
|
Chris@16
|
129
|
Chris@16
|
130 /**
|
Chris@16
|
131 * Constructor from a nesting @c Lock.
|
Chris@16
|
132 *
|
Chris@16
|
133 * @param lk the nesting lock
|
Chris@16
|
134 *
|
Chris@16
|
135 * __Requires: <c>lk.mutex() != null_ptr</c>
|
Chris@16
|
136 * __Effects: Stores the reference to the lock parameter and takes ownership on it.
|
Chris@16
|
137 * If the lock doesn't owns the mutex @c mtx lock it.
|
Chris@16
|
138 * __Postconditions: @c owns_lock(lk.mutex())
|
Chris@16
|
139 * __StrongException
|
Chris@16
|
140 * __Throws:
|
Chris@16
|
141 *
|
Chris@16
|
142 * - lock_error when BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is defined and lk.mutex() == null_ptr
|
Chris@16
|
143 *
|
Chris@16
|
144 * - Any exception that @c lk.lock() can throw.
|
Chris@16
|
145 *
|
Chris@16
|
146 */
|
Chris@16
|
147 explicit nested_strict_lock(Lock& lk) :
|
Chris@16
|
148 lk_(lk) /*< Store reference to lk >*/
|
Chris@16
|
149 {
|
Chris@16
|
150 /*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/
|
Chris@16
|
151 BOOST_THREAD_ASSERT_PRECONDITION( lk.mutex() != 0,
|
Chris@16
|
152 lock_error()
|
Chris@16
|
153 );
|
Chris@16
|
154 if (!lk.owns_lock()) lk.lock(); /*< ensures it is locked >*/
|
Chris@16
|
155 tmp_lk_ = move(lk); /*< Move ownership to temporary lk >*/
|
Chris@16
|
156 }
|
Chris@16
|
157
|
Chris@16
|
158 #if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
|
Chris@16
|
159 nested_strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lock> > l_) :
|
Chris@16
|
160 lk_(*(const_cast<thread_detail::lockable_wrapper<Lock>*>(l_.begin())->m))
|
Chris@16
|
161 {
|
Chris@16
|
162 /*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/
|
Chris@16
|
163 BOOST_THREAD_ASSERT_PRECONDITION( lk_.mutex() != 0,
|
Chris@16
|
164 lock_error()
|
Chris@16
|
165 );
|
Chris@16
|
166 if (!lk_.owns_lock()) lk_.lock(); /*< ensures it is locked >*/
|
Chris@16
|
167 tmp_lk_ = move(lk_); /*< Move ownership to temporary lk >*/
|
Chris@16
|
168 }
|
Chris@16
|
169 #endif
|
Chris@16
|
170
|
Chris@16
|
171 /**
|
Chris@16
|
172 * Destructor
|
Chris@16
|
173 *
|
Chris@16
|
174 * __Effects: Restores ownership to the nesting lock.
|
Chris@16
|
175 */
|
Chris@16
|
176 ~nested_strict_lock()BOOST_NOEXCEPT
|
Chris@16
|
177 {
|
Chris@16
|
178 lk_ = move(tmp_lk_); /*< Move ownership to nesting lock >*/
|
Chris@16
|
179 }
|
Chris@16
|
180
|
Chris@16
|
181 // observers
|
Chris@16
|
182 /**
|
Chris@16
|
183 * return @c the owned mutex.
|
Chris@16
|
184 */
|
Chris@16
|
185 mutex_type* mutex() const BOOST_NOEXCEPT
|
Chris@16
|
186 {
|
Chris@16
|
187 return tmp_lk_.mutex();
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 /**
|
Chris@16
|
191 * @return whether this lock is locking a mutex.
|
Chris@16
|
192 */
|
Chris@16
|
193 bool owns_lock() const BOOST_NOEXCEPT
|
Chris@16
|
194 {
|
Chris@16
|
195 return true;
|
Chris@16
|
196 }
|
Chris@16
|
197
|
Chris@16
|
198 /**
|
Chris@16
|
199 * @return whether if this lock is locking that mutex.
|
Chris@16
|
200 */
|
Chris@16
|
201 bool owns_lock(mutex_type const* l) const BOOST_NOEXCEPT
|
Chris@16
|
202 {
|
Chris@16
|
203 return l == mutex();
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 //BOOST_ADRESS_OF_DELETE(nested_strict_lock)
|
Chris@16
|
207 //BOOST_HEAP_ALLOCATEION_DELETE(nested_strict_lock)
|
Chris@16
|
208
|
Chris@16
|
209 private:
|
Chris@16
|
210 Lock& lk_;
|
Chris@16
|
211 Lock tmp_lk_;
|
Chris@16
|
212 };
|
Chris@16
|
213 //]
|
Chris@16
|
214
|
Chris@16
|
215 template <typename Lock>
|
Chris@16
|
216 struct is_strict_lock_sur_parole<nested_strict_lock<Lock> > : true_type
|
Chris@16
|
217 {
|
Chris@16
|
218 };
|
Chris@16
|
219
|
Chris@16
|
220 #if ! defined BOOST_THREAD_NO_MAKE_STRICT_LOCK
|
Chris@16
|
221 template <typename Lockable>
|
Chris@16
|
222 strict_lock<Lockable> make_strict_lock(Lockable& mtx)
|
Chris@16
|
223 {
|
Chris@16
|
224 return { thread_detail::lockable_wrapper<Lockable>(mtx) };
|
Chris@16
|
225 }
|
Chris@16
|
226 template <typename Lock>
|
Chris@16
|
227 nested_strict_lock<Lock> make_nested_strict_lock(Lock& lk)
|
Chris@16
|
228 {
|
Chris@16
|
229 return { thread_detail::lockable_wrapper<Lock>(lk) };
|
Chris@16
|
230 }
|
Chris@16
|
231 #endif
|
Chris@16
|
232 }
|
Chris@16
|
233 #include <boost/config/abi_suffix.hpp>
|
Chris@16
|
234
|
Chris@16
|
235 #endif
|