scoped_lock.hpp 17 KB

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