externally_locked.hpp 11 KB

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