strict_lock.hpp 6.1 KB

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