scoped_lock.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_SCOPED_LOCK_HPP
  16. #define BOOST_INTERPROCESS_SCOPED_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 scoped_lock class.
  31. namespace boost {
  32. namespace interprocess {
  33. //!scoped_lock is meant to carry out the tasks for locking, unlocking, try-locking
  34. //!and timed-locking (recursive or not) for the Mutex. The Mutex need not supply all
  35. //!of this functionality. If the client of scoped_lock<Mutex> does not use
  36. //!functionality which the Mutex does not supply, no harm is done. Mutex ownership
  37. //!transfer is supported through the syntax of move semantics. Ownership transfer
  38. //!is allowed both by construction and assignment. The scoped_lock does not support
  39. //!copy semantics. A compile time error results if copy construction or copy
  40. //!assignment is attempted. Mutex ownership can also be moved from an
  41. //!upgradable_lock and sharable_lock via constructor. In this role, scoped_lock
  42. //!shares the same functionality as a write_lock.
  43. template <class Mutex>
  44. class scoped_lock
  45. {
  46. /// @cond
  47. private:
  48. typedef scoped_lock<Mutex> this_type;
  49. BOOST_MOVABLE_BUT_NOT_COPYABLE(scoped_lock)
  50. typedef bool this_type::*unspecified_bool_type;
  51. /// @endcond
  52. public:
  53. typedef Mutex mutex_type;
  54. //!Effects: Default constructs a scoped_lock.
  55. //!Postconditions: owns() == false and mutex() == 0.
  56. scoped_lock()
  57. : mp_mutex(0), m_locked(false)
  58. {}
  59. //!Effects: m.lock().
  60. //!Postconditions: owns() == true and mutex() == &m.
  61. //!Notes: The constructor will take ownership of the mutex. If another thread
  62. //! already owns the mutex, this thread will block until the mutex is released.
  63. //! Whether or not this constructor handles recursive locking depends upon the mutex.
  64. explicit scoped_lock(mutex_type& m)
  65. : mp_mutex(&m), m_locked(false)
  66. { mp_mutex->lock(); m_locked = true; }
  67. //!Postconditions: owns() == false, and mutex() == &m.
  68. //!Notes: The constructor will not take ownership of the mutex. There is no effect
  69. //! required on the referenced mutex.
  70. scoped_lock(mutex_type& m, defer_lock_type)
  71. : mp_mutex(&m), m_locked(false)
  72. {}
  73. //!Postconditions: owns() == true, and mutex() == &m.
  74. //!Notes: The constructor will suppose that the mutex is already locked. There
  75. //! is no effect required on the referenced mutex.
  76. scoped_lock(mutex_type& m, accept_ownership_type)
  77. : mp_mutex(&m), m_locked(true)
  78. {}
  79. //!Effects: m.try_lock().
  80. //!Postconditions: mutex() == &m. owns() == the return value of the
  81. //! m.try_lock() executed within the constructor.
  82. //!Notes: The constructor will take ownership of the mutex if it can do
  83. //! so without waiting. Whether or not this constructor handles recursive
  84. //! locking depends upon the mutex. If the mutex_type does not support try_lock,
  85. //! this constructor will fail at compile time if instantiated, but otherwise
  86. //! have no effect.
  87. scoped_lock(mutex_type& m, try_to_lock_type)
  88. : mp_mutex(&m), m_locked(mp_mutex->try_lock())
  89. {}
  90. //!Effects: m.timed_lock(abs_time).
  91. //!Postconditions: mutex() == &m. owns() == the return value of the
  92. //! m.timed_lock(abs_time) executed within the constructor.
  93. //!Notes: The constructor will take ownership of the mutex if it can do
  94. //! it until abs_time is reached. Whether or not this constructor
  95. //! handles recursive locking depends upon the mutex. If the mutex_type
  96. //! does not support try_lock, this constructor will fail at compile
  97. //! time if instantiated, but otherwise have no effect.
  98. scoped_lock(mutex_type& m, const boost::posix_time::ptime& abs_time)
  99. : mp_mutex(&m), m_locked(mp_mutex->timed_lock(abs_time))
  100. {}
  101. //!Postconditions: mutex() == the value scop.mutex() had before the
  102. //! constructor executes. s1.mutex() == 0. owns() == the value of
  103. //! scop.owns() before the constructor executes. scop.owns().
  104. //!Notes: If the scop scoped_lock owns the mutex, ownership is moved
  105. //! to thisscoped_lock with no blocking. If the scop scoped_lock does not
  106. //! own the mutex, then neither will this scoped_lock. Only a moved
  107. //! scoped_lock's will match this signature. An non-moved scoped_lock
  108. //! can be moved with the expression: "boost::move(lock);". This
  109. //! constructor does not alter the state of the mutex, only potentially
  110. //! who owns it.
  111. scoped_lock(BOOST_RV_REF(scoped_lock) scop)
  112. : mp_mutex(0), m_locked(scop.owns())
  113. { mp_mutex = scop.release(); }
  114. //!Effects: If upgr.owns() then calls unlock_upgradable_and_lock() on the
  115. //! referenced mutex. upgr.release() is called.
  116. //!Postconditions: mutex() == the value upgr.mutex() had before the construction.
  117. //! upgr.mutex() == 0. owns() == upgr.owns() before the construction.
  118. //! upgr.owns() == false after the construction.
  119. //!Notes: If upgr is locked, this constructor will lock this scoped_lock while
  120. //! unlocking upgr. If upgr is unlocked, then this scoped_lock will be
  121. //! unlocked as well. Only a moved upgradable_lock's will match this
  122. //! signature. An non-moved upgradable_lock can be moved with
  123. //! the expression: "boost::move(lock);" This constructor may block if
  124. //! other threads hold a sharable_lock on this mutex (sharable_lock's can
  125. //! share ownership with an upgradable_lock).
  126. template<class T>
  127. explicit scoped_lock(BOOST_RV_REF(upgradable_lock<T>) upgr
  128. , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
  129. : mp_mutex(0), m_locked(false)
  130. {
  131. upgradable_lock<mutex_type> &u_lock = upgr;
  132. if(u_lock.owns()){
  133. u_lock.mutex()->unlock_upgradable_and_lock();
  134. m_locked = true;
  135. }
  136. mp_mutex = u_lock.release();
  137. }
  138. //!Effects: If upgr.owns() then calls try_unlock_upgradable_and_lock() on the
  139. //!referenced mutex:
  140. //! a)if try_unlock_upgradable_and_lock() returns true then mutex() obtains
  141. //! the value from upgr.release() and owns() is set to true.
  142. //! b)if try_unlock_upgradable_and_lock() returns false then upgr is
  143. //! unaffected and this scoped_lock construction as the same effects as
  144. //! a default construction.
  145. //! c)Else upgr.owns() is false. mutex() obtains the value from upgr.release()
  146. //! and owns() is set to false
  147. //!Notes: This construction will not block. It will try to obtain mutex
  148. //! ownership from upgr immediately, while changing the lock type from a
  149. //! "read lock" to a "write lock". If the "read lock" isn't held in the
  150. //! first place, the mutex merely changes type to an unlocked "write lock".
  151. //! If the "read lock" is held, then mutex transfer occurs only if it can
  152. //! do so in a non-blocking manner.
  153. template<class T>
  154. scoped_lock(BOOST_RV_REF(upgradable_lock<T>) upgr, try_to_lock_type
  155. , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
  156. : mp_mutex(0), m_locked(false)
  157. {
  158. upgradable_lock<mutex_type> &u_lock = upgr;
  159. if(u_lock.owns()){
  160. if((m_locked = u_lock.mutex()->try_unlock_upgradable_and_lock()) == true){
  161. mp_mutex = u_lock.release();
  162. }
  163. }
  164. else{
  165. u_lock.release();
  166. }
  167. }
  168. //!Effects: If upgr.owns() then calls timed_unlock_upgradable_and_lock(abs_time)
  169. //! on the referenced mutex:
  170. //! a)if timed_unlock_upgradable_and_lock(abs_time) returns true then mutex()
  171. //! obtains the value from upgr.release() and owns() is set to true.
  172. //! b)if timed_unlock_upgradable_and_lock(abs_time) returns false then upgr
  173. //! is unaffected and this scoped_lock construction as the same effects
  174. //! as a default construction.
  175. //! c)Else upgr.owns() is false. mutex() obtains the value from upgr.release()
  176. //! and owns() is set to false
  177. //!Notes: This construction will not block. It will try to obtain mutex ownership
  178. //! from upgr immediately, while changing the lock type from a "read lock" to a
  179. //! "write lock". If the "read lock" isn't held in the first place, the mutex
  180. //! merely changes type to an unlocked "write lock". If the "read lock" is held,
  181. //! then mutex transfer occurs only if it can do so in a non-blocking manner.
  182. template<class T>
  183. scoped_lock(BOOST_RV_REF(upgradable_lock<T>) upgr, boost::posix_time::ptime &abs_time
  184. , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
  185. : mp_mutex(0), m_locked(false)
  186. {
  187. upgradable_lock<mutex_type> &u_lock = upgr;
  188. if(u_lock.owns()){
  189. if((m_locked = u_lock.mutex()->timed_unlock_upgradable_and_lock(abs_time)) == true){
  190. mp_mutex = u_lock.release();
  191. }
  192. }
  193. else{
  194. u_lock.release();
  195. }
  196. }
  197. //!Effects: If shar.owns() then calls try_unlock_sharable_and_lock() on the
  198. //!referenced mutex.
  199. //! a)if try_unlock_sharable_and_lock() returns true then mutex() obtains
  200. //! the value from shar.release() and owns() is set to true.
  201. //! b)if try_unlock_sharable_and_lock() returns false then shar is
  202. //! unaffected and this scoped_lock construction has the same
  203. //! effects as a default construction.
  204. //! c)Else shar.owns() is false. mutex() obtains the value from
  205. //! shar.release() and owns() is set to false
  206. //!Notes: This construction will not block. It will try to obtain mutex
  207. //! ownership from shar immediately, while changing the lock type from a
  208. //! "read lock" to a "write lock". If the "read lock" isn't held in the
  209. //! first place, the mutex merely changes type to an unlocked "write lock".
  210. //! If the "read lock" is held, then mutex transfer occurs only if it can
  211. //! do so in a non-blocking manner.
  212. template<class T>
  213. scoped_lock(BOOST_RV_REF(sharable_lock<T>) shar, try_to_lock_type
  214. , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
  215. : mp_mutex(0), m_locked(false)
  216. {
  217. sharable_lock<mutex_type> &s_lock = shar;
  218. if(s_lock.owns()){
  219. if((m_locked = s_lock.mutex()->try_unlock_sharable_and_lock()) == true){
  220. mp_mutex = s_lock.release();
  221. }
  222. }
  223. else{
  224. s_lock.release();
  225. }
  226. }
  227. //!Effects: if (owns()) mp_mutex->unlock().
  228. //!Notes: The destructor behavior ensures that the mutex lock is not leaked.*/
  229. ~scoped_lock()
  230. {
  231. try{ if(m_locked && mp_mutex) mp_mutex->unlock(); }
  232. catch(...){}
  233. }
  234. //!Effects: If owns() before the call, then unlock() is called on mutex().
  235. //! *this gets the state of scop and scop gets set to a default constructed state.
  236. //!Notes: With a recursive mutex it is possible that both this and scop own
  237. //! the same mutex before the assignment. In this case, this will own the
  238. //! mutex after the assignment (and scop will not), but the mutex's lock
  239. //! count will be decremented by one.
  240. scoped_lock &operator=(BOOST_RV_REF(scoped_lock) scop)
  241. {
  242. if(this->owns())
  243. this->unlock();
  244. m_locked = scop.owns();
  245. mp_mutex = scop.release();
  246. return *this;
  247. }
  248. //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
  249. //! exception. Calls lock() on the referenced mutex.
  250. //!Postconditions: owns() == true.
  251. //!Notes: The scoped_lock changes from a state of not owning the mutex, to
  252. //! owning the mutex, blocking if necessary.
  253. void lock()
  254. {
  255. if(!mp_mutex || m_locked)
  256. throw lock_exception();
  257. mp_mutex->lock();
  258. m_locked = true;
  259. }
  260. //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
  261. //! exception. Calls try_lock() on the referenced mutex.
  262. //!Postconditions: owns() == the value returned from mutex()->try_lock().
  263. //!Notes: The scoped_lock changes from a state of not owning the mutex, to
  264. //! owning the mutex, but only if blocking was not required. If the
  265. //! mutex_type does not support try_lock(), this function will fail at
  266. //! compile time if instantiated, but otherwise have no effect.*/
  267. bool try_lock()
  268. {
  269. if(!mp_mutex || m_locked)
  270. throw lock_exception();
  271. m_locked = mp_mutex->try_lock();
  272. return m_locked;
  273. }
  274. //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
  275. //! exception. Calls timed_lock(abs_time) on the referenced mutex.
  276. //!Postconditions: owns() == the value returned from mutex()-> timed_lock(abs_time).
  277. //!Notes: The scoped_lock changes from a state of not owning the mutex, to
  278. //! owning the mutex, but only if it can obtain ownership by the specified
  279. //! time. If the mutex_type does not support timed_lock (), this function
  280. //! will fail at compile time if instantiated, but otherwise have no effect.*/
  281. bool timed_lock(const boost::posix_time::ptime& abs_time)
  282. {
  283. if(!mp_mutex || m_locked)
  284. throw lock_exception();
  285. m_locked = mp_mutex->timed_lock(abs_time);
  286. return m_locked;
  287. }
  288. //!Effects: If mutex() == 0 or if not locked, throws a lock_exception()
  289. //! exception. Calls unlock() on the referenced mutex.
  290. //!Postconditions: owns() == false.
  291. //!Notes: The scoped_lock changes from a state of owning the mutex, to not
  292. //! owning the mutex.*/
  293. void unlock()
  294. {
  295. if(!mp_mutex || !m_locked)
  296. throw lock_exception();
  297. mp_mutex->unlock();
  298. m_locked = false;
  299. }
  300. //!Effects: Returns true if this scoped_lock has acquired
  301. //!the referenced mutex.
  302. bool owns() const
  303. { return m_locked && mp_mutex; }
  304. //!Conversion to bool.
  305. //!Returns owns().
  306. operator unspecified_bool_type() const
  307. { return m_locked? &this_type::m_locked : 0; }
  308. //!Effects: Returns a pointer to the referenced mutex, or 0 if
  309. //!there is no mutex to reference.
  310. mutex_type* mutex() const
  311. { return mp_mutex; }
  312. //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
  313. //! mutex to reference.
  314. //!Postconditions: mutex() == 0 and owns() == false.
  315. mutex_type* release()
  316. {
  317. mutex_type *mut = mp_mutex;
  318. mp_mutex = 0;
  319. m_locked = false;
  320. return mut;
  321. }
  322. //!Effects: Swaps state with moved lock.
  323. //!Throws: Nothing.
  324. void swap( scoped_lock<mutex_type> &other)
  325. {
  326. std::swap(mp_mutex, other.mp_mutex);
  327. std::swap(m_locked, other.m_locked);
  328. }
  329. /// @cond
  330. private:
  331. mutex_type *mp_mutex;
  332. bool m_locked;
  333. /// @endcond
  334. };
  335. } // namespace interprocess
  336. } // namespace boost
  337. #include <boost/interprocess/detail/config_end.hpp>
  338. #endif // BOOST_INTERPROCESS_SCOPED_LOCK_HPP