sharable_lock.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. // This interface is inspired by Howard Hinnant's lock proposal.
  12. // http://home.twcny.rr.com/hinnant/cpp_extensions/threads_move.html
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #ifndef BOOST_INTERPROCESS_SHARABLE_LOCK_HPP
  16. #define BOOST_INTERPROCESS_SHARABLE_LOCK_HPP
  17. #ifndef BOOST_CONFIG_HPP
  18. # include <boost/config.hpp>
  19. #endif
  20. #
  21. #if defined(BOOST_HAS_PRAGMA_ONCE)
  22. # pragma once
  23. #endif
  24. #include <boost/interprocess/detail/config_begin.hpp>
  25. #include <boost/interprocess/detail/workaround.hpp>
  26. #include <boost/interprocess/interprocess_fwd.hpp>
  27. #include <boost/interprocess/sync/lock_options.hpp>
  28. #include <boost/interprocess/exceptions.hpp>
  29. #include <boost/interprocess/detail/mpl.hpp>
  30. #include <boost/interprocess/detail/type_traits.hpp>
  31. #include <boost/interprocess/detail/simple_swap.hpp>
  32. #include <boost/move/utility_core.hpp>
  33. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  34. //!\file
  35. //!Describes the upgradable_lock class that serves to acquire the upgradable
  36. //!lock of a mutex.
  37. namespace boost {
  38. namespace interprocess {
  39. //!sharable_lock is meant to carry out the tasks for sharable-locking
  40. //!(such as read-locking), unlocking, try-sharable-locking and timed-sharable-locking
  41. //!(recursive or not) for the Mutex. The Mutex need not supply all of this
  42. //!functionality. If the client of sharable_lock<Mutex> does not use functionality which
  43. //!the Mutex does not supply, no harm is done. Mutex ownership can be shared among
  44. //!sharable_locks, and a single upgradable_lock. sharable_lock does not support
  45. //!copy semantics. But sharable_lock supports ownership transfer from an sharable_lock,
  46. //!upgradable_lock and scoped_lock via transfer_lock syntax.*/
  47. template <class SharableMutex>
  48. class sharable_lock
  49. {
  50. public:
  51. typedef SharableMutex mutex_type;
  52. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  53. private:
  54. typedef sharable_lock<SharableMutex> this_type;
  55. explicit sharable_lock(scoped_lock<mutex_type>&);
  56. typedef bool this_type::*unspecified_bool_type;
  57. BOOST_MOVABLE_BUT_NOT_COPYABLE(sharable_lock)
  58. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  59. public:
  60. //!Effects: Default constructs a sharable_lock.
  61. //!Postconditions: owns() == false and mutex() == 0.
  62. sharable_lock() BOOST_NOEXCEPT
  63. : mp_mutex(0), m_locked(false)
  64. {}
  65. //!Effects: m.lock_sharable().
  66. //!Postconditions: owns() == true and mutex() == &m.
  67. //!Notes: The constructor will take sharable-ownership of the mutex. If
  68. //! another thread already owns the mutex with exclusive ownership
  69. //! (scoped_lock), this thread will block until the mutex is released.
  70. //! If another thread owns the mutex with sharable or upgradable ownership,
  71. //! then no blocking will occur. Whether or not this constructor handles
  72. //! recursive locking depends upon the mutex.
  73. explicit sharable_lock(mutex_type& m)
  74. : mp_mutex(&m), m_locked(false)
  75. { mp_mutex->lock_sharable(); m_locked = true; }
  76. //!Postconditions: owns() == false, and mutex() == &m.
  77. //!Notes: The constructor will not take ownership of the mutex. There is no effect
  78. //! required on the referenced mutex.
  79. sharable_lock(mutex_type& m, defer_lock_type)
  80. : mp_mutex(&m), m_locked(false)
  81. {}
  82. //!Postconditions: owns() == true, and mutex() == &m.
  83. //!Notes: The constructor will suppose that the mutex is already sharable
  84. //! locked. There is no effect required on the referenced mutex.
  85. sharable_lock(mutex_type& m, accept_ownership_type)
  86. : mp_mutex(&m), m_locked(true)
  87. {}
  88. //!Effects: m.try_lock_sharable()
  89. //!Postconditions: mutex() == &m. owns() == the return value of the
  90. //! m.try_lock_sharable() executed within the constructor.
  91. //!Notes: The constructor will take sharable-ownership of the mutex if it
  92. //! can do so without waiting. Whether or not this constructor handles
  93. //! recursive locking depends upon the mutex. If the mutex_type does not
  94. //! support try_lock_sharable, this constructor will fail at compile
  95. //! time if instantiated, but otherwise have no effect.
  96. sharable_lock(mutex_type& m, try_to_lock_type)
  97. : mp_mutex(&m), m_locked(false)
  98. { m_locked = mp_mutex->try_lock_sharable(); }
  99. //!Effects: m.timed_lock_sharable(abs_time)
  100. //!Postconditions: mutex() == &m. owns() == the return value of the
  101. //! m.timed_lock_sharable() executed within the constructor.
  102. //!Notes: The constructor will take sharable-ownership of the mutex if it
  103. //! can do so within the time specified. Whether or not this constructor
  104. //! handles recursive locking depends upon the mutex. If the mutex_type
  105. //! does not support timed_lock_sharable, this constructor will fail at
  106. //! compile time if instantiated, but otherwise have no effect.
  107. sharable_lock(mutex_type& m, const boost::posix_time::ptime& abs_time)
  108. : mp_mutex(&m), m_locked(false)
  109. { m_locked = mp_mutex->timed_lock_sharable(abs_time); }
  110. //!Postconditions: mutex() == upgr.mutex(). owns() == the value of upgr.owns()
  111. //! before the construction. upgr.owns() == false after the construction.
  112. //!Notes: If the upgr sharable_lock owns the mutex, ownership is moved to this
  113. //! sharable_lock with no blocking. If the upgr sharable_lock does not own the mutex, then
  114. //! neither will this sharable_lock. Only a moved sharable_lock's will match this
  115. //! signature. An non-moved sharable_lock can be moved with the expression:
  116. //! "boost::move(lock);". This constructor does not alter the state of the mutex,
  117. //! only potentially who owns it.
  118. sharable_lock(BOOST_RV_REF(sharable_lock<mutex_type>) upgr) BOOST_NOEXCEPT
  119. : mp_mutex(0), m_locked(upgr.owns())
  120. { mp_mutex = upgr.release(); }
  121. //!Effects: If upgr.owns() then calls unlock_upgradable_and_lock_sharable() on the
  122. //! referenced mutex.
  123. //!Postconditions: mutex() == the value upgr.mutex() had before the construction.
  124. //! upgr.mutex() == 0 owns() == the value of upgr.owns() before construction.
  125. //! upgr.owns() == false after the construction.
  126. //!Notes: If upgr is locked, this constructor will lock this sharable_lock while
  127. //! unlocking upgr. Only a moved sharable_lock's will match this
  128. //! signature. An non-moved upgradable_lock can be moved with the expression:
  129. //! "boost::move(lock);".*/
  130. template<class T>
  131. sharable_lock(BOOST_RV_REF(upgradable_lock<T>) upgr
  132. , typename ipcdetail::enable_if< ipcdetail::is_same<T, SharableMutex> >::type * = 0)
  133. : mp_mutex(0), m_locked(false)
  134. {
  135. upgradable_lock<mutex_type> &u_lock = upgr;
  136. if(u_lock.owns()){
  137. u_lock.mutex()->unlock_upgradable_and_lock_sharable();
  138. m_locked = true;
  139. }
  140. mp_mutex = u_lock.release();
  141. }
  142. //!Effects: If scop.owns() then calls unlock_and_lock_sharable() on the
  143. //! referenced mutex.
  144. //!Postconditions: mutex() == the value scop.mutex() had before the construction.
  145. //! scop.mutex() == 0 owns() == scop.owns() before the constructor. After the
  146. //! construction, scop.owns() == false.
  147. //!Notes: If scop is locked, this constructor will transfer the exclusive ownership
  148. //! to a sharable-ownership of this sharable_lock.
  149. //! Only a moved scoped_lock's will match this
  150. //! signature. An non-moved scoped_lock can be moved with the expression:
  151. //! "boost::move(lock);".
  152. template<class T>
  153. sharable_lock(BOOST_RV_REF(scoped_lock<T>) scop
  154. , typename ipcdetail::enable_if< ipcdetail::is_same<T, SharableMutex> >::type * = 0)
  155. : mp_mutex(0), m_locked(false)
  156. {
  157. scoped_lock<mutex_type> &e_lock = scop;
  158. if(e_lock.owns()){
  159. e_lock.mutex()->unlock_and_lock_sharable();
  160. m_locked = true;
  161. }
  162. mp_mutex = e_lock.release();
  163. }
  164. //!Effects: if (owns()) mp_mutex->unlock_sharable().
  165. //!Notes: The destructor behavior ensures that the mutex lock is not leaked.
  166. ~sharable_lock()
  167. {
  168. try{
  169. if(m_locked && mp_mutex) mp_mutex->unlock_sharable();
  170. }
  171. catch(...){}
  172. }
  173. //!Effects: If owns() before the call, then unlock_sharable() is called on mutex().
  174. //! *this gets the state of upgr and upgr gets set to a default constructed state.
  175. //!Notes: With a recursive mutex it is possible that both this and upgr own the mutex
  176. //! before the assignment. In this case, this will own the mutex after the assignment
  177. //! (and upgr will not), but the mutex's lock count will be decremented by one.
  178. sharable_lock &operator=(BOOST_RV_REF(sharable_lock<mutex_type>) upgr)
  179. {
  180. if(this->owns())
  181. this->unlock();
  182. m_locked = upgr.owns();
  183. mp_mutex = upgr.release();
  184. return *this;
  185. }
  186. //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
  187. //! exception. Calls lock_sharable() on the referenced mutex.
  188. //!Postconditions: owns() == true.
  189. //!Notes: The sharable_lock changes from a state of not owning the
  190. //! mutex, to owning the mutex, blocking if necessary.
  191. void lock()
  192. {
  193. if(!mp_mutex || m_locked)
  194. throw lock_exception();
  195. mp_mutex->lock_sharable();
  196. m_locked = true;
  197. }
  198. //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
  199. //! exception. Calls try_lock_sharable() on the referenced mutex.
  200. //!Postconditions: owns() == the value returned from
  201. //! mutex()->try_lock_sharable().
  202. //!Notes: The sharable_lock changes from a state of not owning the mutex,
  203. //! to owning the mutex, but only if blocking was not required. If the
  204. //! mutex_type does not support try_lock_sharable(), this function will
  205. //! fail at compile time if instantiated, but otherwise have no effect.
  206. bool try_lock()
  207. {
  208. if(!mp_mutex || m_locked)
  209. throw lock_exception();
  210. m_locked = mp_mutex->try_lock_sharable();
  211. return m_locked;
  212. }
  213. //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
  214. //! exception. Calls timed_lock_sharable(abs_time) on the referenced mutex.
  215. //!Postconditions: owns() == the value returned from
  216. //! mutex()->timed_lock_sharable(elps_time).
  217. //!Notes: The sharable_lock changes from a state of not owning the mutex,
  218. //! to owning the mutex, but only if it can obtain ownership within the
  219. //! specified time interval. If the mutex_type does not support
  220. //! timed_lock_sharable(), this function will fail at compile time if
  221. //! instantiated, but otherwise have no effect.
  222. bool timed_lock(const boost::posix_time::ptime& abs_time)
  223. {
  224. if(!mp_mutex || m_locked)
  225. throw lock_exception();
  226. m_locked = mp_mutex->timed_lock_sharable(abs_time);
  227. return m_locked;
  228. }
  229. //!Effects: If mutex() == 0 or not locked, throws a lock_exception() exception.
  230. //! Calls unlock_sharable() on the referenced mutex.
  231. //!Postconditions: owns() == false.
  232. //!Notes: The sharable_lock changes from a state of owning the mutex, to
  233. //! not owning the mutex.
  234. void unlock()
  235. {
  236. if(!mp_mutex || !m_locked)
  237. throw lock_exception();
  238. mp_mutex->unlock_sharable();
  239. m_locked = false;
  240. }
  241. //!Effects: Returns true if this scoped_lock has
  242. //!acquired the referenced mutex.
  243. bool owns() const BOOST_NOEXCEPT
  244. { return m_locked && mp_mutex; }
  245. //!Conversion to bool.
  246. //!Returns owns().
  247. operator unspecified_bool_type() const BOOST_NOEXCEPT
  248. { return m_locked? &this_type::m_locked : 0; }
  249. //!Effects: Returns a pointer to the referenced mutex, or 0 if
  250. //!there is no mutex to reference.
  251. mutex_type* mutex() const BOOST_NOEXCEPT
  252. { return mp_mutex; }
  253. //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
  254. //! mutex to reference.
  255. //!Postconditions: mutex() == 0 and owns() == false.
  256. mutex_type* release() BOOST_NOEXCEPT
  257. {
  258. mutex_type *mut = mp_mutex;
  259. mp_mutex = 0;
  260. m_locked = false;
  261. return mut;
  262. }
  263. //!Effects: Swaps state with moved lock.
  264. //!Throws: Nothing.
  265. void swap(sharable_lock<mutex_type> &other) BOOST_NOEXCEPT
  266. {
  267. (simple_swap)(mp_mutex, other.mp_mutex);
  268. (simple_swap)(m_locked, other.m_locked);
  269. }
  270. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  271. private:
  272. mutex_type *mp_mutex;
  273. bool m_locked;
  274. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  275. };
  276. } // namespace interprocess
  277. } // namespace boost
  278. #include <boost/interprocess/detail/config_end.hpp>
  279. #endif // BOOST_INTERPROCESS_SHARABLE_LOCK_HPP