comparison DEPENDENCIES/generic/include/boost/thread/externally_locked.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 // (C) Copyright 2012 Vicente J. Botet Escriba
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6
7 #ifndef BOOST_THREAD_EXTERNALLY_LOCKED_HPP
8 #define BOOST_THREAD_EXTERNALLY_LOCKED_HPP
9
10 #include <boost/thread/detail/config.hpp>
11
12 #include <boost/thread/exceptions.hpp>
13 #include <boost/thread/lock_concepts.hpp>
14 #include <boost/thread/lock_traits.hpp>
15 #include <boost/thread/lockable_concepts.hpp>
16 #include <boost/thread/strict_lock.hpp>
17
18 #include <boost/static_assert.hpp>
19 #include <boost/type_traits/is_same.hpp>
20 #include <boost/throw_exception.hpp>
21 #include <boost/utility/swap.hpp>
22
23 #include <boost/config/abi_prefix.hpp>
24
25 namespace boost
26 {
27
28 /**
29 * externally_locked cloaks an object of type T, and actually provides full
30 * access to that object through the get and set member functions, provided you
31 * pass a reference to a strict lock object
32 */
33
34 //[externally_locked
35 template <typename T, typename MutexType = boost::mutex>
36 class externally_locked;
37 template <typename T, typename MutexType>
38 class externally_locked
39 {
40 //BOOST_CONCEPT_ASSERT(( CopyConstructible<T> ));
41 BOOST_CONCEPT_ASSERT(( BasicLockable<MutexType> ));
42
43 public:
44 typedef MutexType mutex_type;
45
46 BOOST_THREAD_COPYABLE_AND_MOVABLE( externally_locked )
47 /**
48 * Requires: T is a model of CopyConstructible.
49 * Effects: Constructs an externally locked object copying the cloaked type.
50 */
51 externally_locked(mutex_type& mtx, const T& obj) :
52 obj_(obj), mtx_(&mtx)
53 {
54 }
55
56 /**
57 * Requires: T is a model of Movable.
58 * Effects: Constructs an externally locked object by moving the cloaked type.
59 */
60 externally_locked(mutex_type& mtx, BOOST_THREAD_RV_REF(T) obj) :
61 obj_(move(obj)), mtx_(&mtx)
62 {
63 }
64
65 /**
66 * Requires: T is a model of DefaultConstructible.
67 * Effects: Constructs an externally locked object initializing the cloaked type with the default constructor.
68 */
69 externally_locked(mutex_type& mtx) // BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T()))
70 : obj_(), mtx_(&mtx)
71 {
72 }
73
74 /**
75 * Copy constructor
76 */
77 externally_locked(externally_locked const& rhs) //BOOST_NOEXCEPT
78 : obj_(rhs.obj_), mtx_(rhs.mtx_)
79 {
80 }
81 /**
82 * Move constructor
83 */
84 externally_locked(BOOST_THREAD_RV_REF(externally_locked) rhs) //BOOST_NOEXCEPT
85 : obj_(move(rhs.obj_)), mtx_(rhs.mtx_)
86 {
87 }
88
89 /// assignment
90 externally_locked& operator=(externally_locked const& rhs) //BOOST_NOEXCEPT
91 {
92 obj_=rhs.obj_;
93 mtx_=rhs.mtx_;
94 return *this;
95 }
96
97 /// move assignment
98 externally_locked& operator=(BOOST_THREAD_RV_REF(externally_locked) rhs) // BOOST_NOEXCEPT
99 {
100 obj_=move(rhs.obj_);
101 mtx_=rhs.mtx_;
102 return *this;
103 }
104
105 void swap(externally_locked& rhs) //BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR)
106 {
107 swap(obj_, rhs.obj_);
108 swap(mtx_, rhs.mtx_);
109 }
110
111 /**
112 * Requires: The lk parameter must be locking the associated mtx.
113 *
114 * Returns: The address of the cloaked object..
115 *
116 * Throws: lock_error if BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is not defined and the lk parameter doesn't satisfy the preconditions
117 */
118 T& get(strict_lock<mutex_type>& lk)
119 {
120 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
121 return obj_;
122 }
123
124 const T& get(strict_lock<mutex_type>& lk) const
125 {
126 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
127 return obj_;
128 }
129
130 template <class Lock>
131 T& get(nested_strict_lock<Lock>& lk)
132 {
133 BOOST_STATIC_ASSERT( (is_same<mutex_type, typename Lock::mutex_type>::value)); /*< that locks the same type >*/
134 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
135 return obj_;
136 }
137
138 template <class Lock>
139 const T& get(nested_strict_lock<Lock>& lk) const
140 {
141 BOOST_STATIC_ASSERT( (is_same<mutex_type, typename Lock::mutex_type>::value)); /*< that locks the same type >*/
142 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
143 return obj_;
144 }
145
146 /**
147 * Requires: The lk parameter must be locking the associated mtx.
148 * Returns: The address of the cloaked object..
149 *
150 * Throws: lock_error if BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is not defined and the lk parameter doesn't satisfy the preconditions
151 */
152 template <class Lock>
153 T& get(Lock& lk)
154 {
155 BOOST_CONCEPT_ASSERT(( StrictLock<Lock> ));
156 BOOST_STATIC_ASSERT( (is_strict_lock<Lock>::value)); /*< lk is a strict lock "sur parolle" >*/
157 BOOST_STATIC_ASSERT( (is_same<mutex_type, typename Lock::mutex_type>::value)); /*< that locks the same type >*/
158
159 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
160
161 return obj_;
162 }
163
164 mutex_type* mutex() const BOOST_NOEXCEPT
165 {
166 return mtx_;
167 }
168
169 // modifiers
170
171 void lock()
172 {
173 mtx_->lock();
174 }
175 void unlock()
176 {
177 mtx_->unlock();
178 }
179 bool try_lock()
180 {
181 return mtx_->try_lock();
182 }
183 // todo add time related functions
184
185 private:
186 T obj_;
187 mutex_type* mtx_;
188 };
189 //]
190
191 /**
192 * externally_locked<T&,M> specialization for T& that cloaks an reference to an object of type T, and actually
193 * provides full access to that object through the get and set member functions, provided you
194 * pass a reference to a strict lock object.
195 */
196
197 //[externally_locked_ref
198 template <typename T, typename MutexType>
199 class externally_locked<T&, MutexType>
200 {
201 //BOOST_CONCEPT_ASSERT(( CopyConstructible<T> ));
202 BOOST_CONCEPT_ASSERT(( BasicLockable<MutexType> ));
203
204 public:
205 typedef MutexType mutex_type;
206
207 BOOST_THREAD_COPYABLE_AND_MOVABLE( externally_locked )
208
209 /**
210 * Effects: Constructs an externally locked object storing the cloaked reference object.
211 */
212 externally_locked(T& obj, mutex_type& mtx) BOOST_NOEXCEPT :
213 obj_(&obj), mtx_(&mtx)
214 {
215 }
216
217 /// copy constructor
218 externally_locked(externally_locked const& rhs) BOOST_NOEXCEPT :
219 obj_(rhs.obj_), mtx_(rhs.mtx_)
220 {
221 }
222
223 /// move constructor
224 externally_locked(BOOST_THREAD_RV_REF(externally_locked) rhs) BOOST_NOEXCEPT :
225 obj_(rhs.obj_), mtx_(rhs.mtx_)
226 {
227 }
228
229 /// assignment
230 externally_locked& operator=(externally_locked const& rhs) BOOST_NOEXCEPT
231 {
232 obj_=rhs.obj_;
233 mtx_=rhs.mtx_;
234 return *this;
235 }
236
237 /// move assignment
238 externally_locked& operator=(BOOST_THREAD_RV_REF(externally_locked) rhs) BOOST_NOEXCEPT
239 {
240 obj_=rhs.obj_;
241 mtx_=rhs.mtx_;
242 return *this;
243 }
244
245 void swap(externally_locked& rhs) BOOST_NOEXCEPT
246 {
247 swap(obj_, rhs.obj_);
248 swap(mtx_, rhs.mtx_);
249 }
250 /**
251 * Requires: The lk parameter must be locking the associated mtx.
252 *
253 * Returns: The address of the cloaked object..
254 *
255 * Throws: lock_error if BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is not defined and the lk parameter doesn't satisfy the preconditions
256 */
257 T& get(strict_lock<mutex_type> const& lk)
258 {
259 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
260 return *obj_;
261 }
262
263 const T& get(strict_lock<mutex_type> const& lk) const
264 {
265 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
266 return *obj_;
267 }
268
269 template <class Lock>
270 T& get(nested_strict_lock<Lock> const& lk)
271 {
272 BOOST_STATIC_ASSERT( (is_same<mutex_type, typename Lock::mutex_type>::value)); /*< that locks the same type >*/
273 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
274 return *obj_;
275 }
276
277 template <class Lock>
278 const T& get(nested_strict_lock<Lock> const& lk) const
279 {
280 BOOST_STATIC_ASSERT( (is_same<mutex_type, typename Lock::mutex_type>::value)); /*< that locks the same type >*/
281 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
282 return *obj_;
283 }
284
285 /**
286 * Requires: The lk parameter must be locking the associated mtx.
287 * Returns: The address of the cloaked object..
288 *
289 * Throws: lock_error if BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is not defined and the lk parameter doesn't satisfy the preconditions
290 */
291 template <class Lock>
292 T& get(Lock const& lk)
293 {
294 BOOST_CONCEPT_ASSERT(( StrictLock<Lock> ));
295 BOOST_STATIC_ASSERT( (is_strict_lock<Lock>::value)); /*< lk is a strict lock "sur parolle" >*/
296 BOOST_STATIC_ASSERT( (is_same<mutex_type, typename Lock::mutex_type>::value)); /*< that locks the same type >*/
297 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
298 return *obj_;
299 }
300
301 /**
302 * Requires: The lk parameter must be locking the associated mtx.
303 * Returns: The address of the cloaked object..
304 *
305 * Throws: lock_error if BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is not defined and the lk parameter doesn't satisfy the preconditions
306 */
307 template <class Lock>
308 T const& get(Lock const& lk) const
309 {
310 BOOST_CONCEPT_ASSERT(( StrictLock<Lock> ));
311 BOOST_STATIC_ASSERT( (is_strict_lock<Lock>::value)); /*< lk is a strict lock "sur parolle" >*/
312 BOOST_STATIC_ASSERT( (is_same<mutex_type, typename Lock::mutex_type>::value)); /*< that locks the same type >*/
313 BOOST_THREAD_ASSERT_PRECONDITION( lk.owns_lock(mtx_), lock_error() ); /*< run time check throw if not locks the same >*/
314 return *obj_;
315 }
316 mutex_type* mutex() const BOOST_NOEXCEPT
317 {
318 return mtx_;
319 }
320
321 void lock()
322 {
323 mtx_->lock();
324 }
325 void unlock()
326 {
327 mtx_->unlock();
328 }
329 bool try_lock()
330 {
331 return mtx_->try_lock();
332 }
333 // todo add time related functions
334
335 protected:
336 T* obj_;
337 mutex_type* mtx_;
338 };
339 //]
340
341 template <typename T, typename MutexType>
342 void swap(externally_locked<T, MutexType> & lhs, externally_locked<T, MutexType> & rhs) // BOOST_NOEXCEPT
343 {
344 lhs.swap(rhs);
345 }
346
347 }
348
349 #include <boost/config/abi_suffix.hpp>
350
351 #endif // header