sharable_lock.hpp 13 KB

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