sharable_lock.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. //!\file
  34. //!Describes the upgradable_lock class that serves to acquire the upgradable
  35. //!lock of a mutex.
  36. namespace boost {
  37. namespace interprocess {
  38. //!sharable_lock is meant to carry out the tasks for sharable-locking
  39. //!(such as read-locking), unlocking, try-sharable-locking and timed-sharable-locking
  40. //!(recursive or not) for the Mutex. The Mutex need not supply all of this
  41. //!functionality. If the client of sharable_lock<Mutex> does not use functionality which
  42. //!the Mutex does not supply, no harm is done. Mutex ownership can be shared among
  43. //!sharable_locks, and a single upgradable_lock. sharable_lock does not support
  44. //!copy semantics. But sharable_lock supports ownership transfer from an sharable_lock,
  45. //!upgradable_lock and scoped_lock via transfer_lock syntax.*/
  46. template <class SharableMutex>
  47. class sharable_lock
  48. {
  49. public:
  50. typedef SharableMutex mutex_type;
  51. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  52. private:
  53. typedef sharable_lock<SharableMutex> this_type;
  54. explicit sharable_lock(scoped_lock<mutex_type>&);
  55. typedef bool this_type::*unspecified_bool_type;
  56. BOOST_MOVABLE_BUT_NOT_COPYABLE(sharable_lock)
  57. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  58. public:
  59. //!Effects: Default constructs a sharable_lock.
  60. //!Postconditions: owns() == false and mutex() == 0.
  61. sharable_lock() BOOST_NOEXCEPT
  62. : mp_mutex(0), m_locked(false)
  63. {}
  64. //!Effects: m.lock_sharable().
  65. //!Postconditions: owns() == true and mutex() == &m.
  66. //!Notes: The constructor will take sharable-ownership of the mutex. If
  67. //! another thread already owns the mutex with exclusive ownership
  68. //! (scoped_lock), this thread will block until the mutex is released.
  69. //! If another thread owns the mutex with sharable or upgradable ownership,
  70. //! then no blocking will occur. Whether or not this constructor handles
  71. //! recursive locking depends upon the mutex.
  72. explicit sharable_lock(mutex_type& m)
  73. : mp_mutex(&m), m_locked(false)
  74. { mp_mutex->lock_sharable(); m_locked = true; }
  75. //!Postconditions: owns() == false, and mutex() == &m.
  76. //!Notes: The constructor will not take ownership of the mutex. There is no effect
  77. //! required on the referenced mutex.
  78. sharable_lock(mutex_type& m, defer_lock_type)
  79. : mp_mutex(&m), m_locked(false)
  80. {}
  81. //!Postconditions: owns() == true, and mutex() == &m.
  82. //!Notes: The constructor will suppose that the mutex is already sharable
  83. //! locked. There is no effect required on the referenced mutex.
  84. sharable_lock(mutex_type& m, accept_ownership_type)
  85. : mp_mutex(&m), m_locked(true)
  86. {}
  87. //!Effects: m.try_lock_sharable()
  88. //!Postconditions: mutex() == &m. owns() == the return value of the
  89. //! m.try_lock_sharable() executed within the constructor.
  90. //!Notes: The constructor will take sharable-ownership of the mutex if it
  91. //! can do so without waiting. Whether or not this constructor handles
  92. //! recursive locking depends upon the mutex. If the mutex_type does not
  93. //! support try_lock_sharable, this constructor will fail at compile
  94. //! time if instantiated, but otherwise have no effect.
  95. sharable_lock(mutex_type& m, try_to_lock_type)
  96. : mp_mutex(&m), m_locked(false)
  97. { m_locked = mp_mutex->try_lock_sharable(); }
  98. //!Effects: m.timed_lock_sharable(abs_time)
  99. //!Postconditions: mutex() == &m. owns() == the return value of the
  100. //! m.timed_lock_sharable() executed within the constructor.
  101. //!Notes: The constructor will take sharable-ownership of the mutex if it
  102. //! can do so within the time specified. Whether or not this constructor
  103. //! handles recursive locking depends upon the mutex. If the mutex_type
  104. //! does not support timed_lock_sharable, this constructor will fail at
  105. //! compile time if instantiated, but otherwise have no effect.
  106. template<class TimePoint>
  107. sharable_lock(mutex_type& m, const TimePoint& 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. BOOST_TRY{
  169. if(m_locked && mp_mutex) mp_mutex->unlock_sharable();
  170. }
  171. BOOST_CATCH(...){} BOOST_CATCH_END
  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. template<class TimePoint>
  223. bool timed_lock(const TimePoint& abs_time)
  224. {
  225. if(!mp_mutex || m_locked)
  226. throw lock_exception();
  227. m_locked = mp_mutex->timed_lock_sharable(abs_time);
  228. return m_locked;
  229. }
  230. //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
  231. //! exception. Calls try_lock_shared_until(abs_time) on the referenced mutex.
  232. //!Postconditions: owns() == the value returned from
  233. //! mutex()->timed_lock_sharable(elps_time).
  234. //!Notes: The sharable_lock changes from a state of not owning the mutex,
  235. //! to owning the mutex, but only if it can obtain ownership within the
  236. //! specified time interval. If the mutex_type does not support
  237. //! timed_lock_sharable(), this function will fail at compile time if
  238. //! instantiated, but otherwise have no effect.
  239. //!
  240. //!Note: Similar to timed_lock, but with a std-like interface
  241. template<class TimePoint>
  242. bool try_lock_until(const TimePoint& abs_time)
  243. {
  244. if(!mp_mutex || m_locked)
  245. throw lock_exception();
  246. m_locked = mp_mutex->try_lock_shared_until(abs_time);
  247. return m_locked;
  248. }
  249. //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
  250. //! exception. Calls try_lock_shared_until(abs_time) on the referenced mutex.
  251. //!Postconditions: owns() == the value returned from
  252. //! mutex()->timed_lock_sharable(elps_time).
  253. //!Notes: The sharable_lock changes from a state of not owning the mutex,
  254. //! to owning the mutex, but only if it can obtain ownership within the
  255. //! specified time interval. If the mutex_type does not support
  256. //! timed_lock_sharable(), this function will fail at compile time if
  257. //! instantiated, but otherwise have no effect.
  258. //!
  259. //!Note: Similar to timed_lock, but with a std-like interface
  260. template<class Duration>
  261. bool try_lock_for(const Duration& dur)
  262. {
  263. if(!mp_mutex || m_locked)
  264. throw lock_exception();
  265. m_locked = mp_mutex->try_lock_shared_for(dur);
  266. return m_locked;
  267. }
  268. //!Effects: If mutex() == 0 or not locked, throws a lock_exception() exception.
  269. //! Calls unlock_sharable() on the referenced mutex.
  270. //!Postconditions: owns() == false.
  271. //!Notes: The sharable_lock changes from a state of owning the mutex, to
  272. //! not owning the mutex.
  273. void unlock()
  274. {
  275. if(!mp_mutex || !m_locked)
  276. throw lock_exception();
  277. mp_mutex->unlock_sharable();
  278. m_locked = false;
  279. }
  280. //!Effects: Returns true if this scoped_lock has
  281. //!acquired the referenced mutex.
  282. bool owns() const BOOST_NOEXCEPT
  283. { return m_locked && mp_mutex; }
  284. //!Conversion to bool.
  285. //!Returns owns().
  286. operator unspecified_bool_type() const BOOST_NOEXCEPT
  287. { return m_locked? &this_type::m_locked : 0; }
  288. //!Effects: Returns a pointer to the referenced mutex, or 0 if
  289. //!there is no mutex to reference.
  290. mutex_type* mutex() const BOOST_NOEXCEPT
  291. { return mp_mutex; }
  292. //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
  293. //! mutex to reference.
  294. //!Postconditions: mutex() == 0 and owns() == false.
  295. mutex_type* release() BOOST_NOEXCEPT
  296. {
  297. mutex_type *mut = mp_mutex;
  298. mp_mutex = 0;
  299. m_locked = false;
  300. return mut;
  301. }
  302. //!Effects: Swaps state with moved lock.
  303. //!Throws: Nothing.
  304. void swap(sharable_lock<mutex_type> &other) BOOST_NOEXCEPT
  305. {
  306. (simple_swap)(mp_mutex, other.mp_mutex);
  307. (simple_swap)(m_locked, other.m_locked);
  308. }
  309. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  310. private:
  311. mutex_type *mp_mutex;
  312. bool m_locked;
  313. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  314. };
  315. } // namespace interprocess
  316. } // namespace boost
  317. #include <boost/interprocess/detail/config_end.hpp>
  318. #endif // BOOST_INTERPROCESS_SHARABLE_LOCK_HPP