scoped_lock.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/posix_time_types_wrk.hpp>
  33. #include <boost/interprocess/detail/simple_swap.hpp>
  34. //!\file
  35. //!Describes the scoped_lock class.
  36. namespace boost {
  37. namespace interprocess {
  38. //!scoped_lock is meant to carry out the tasks for locking, unlocking, try-locking
  39. //!and timed-locking (recursive or not) for the Mutex. The Mutex need not supply all
  40. //!of this functionality. If the client of scoped_lock<Mutex> does not use
  41. //!functionality which the Mutex does not supply, no harm is done. Mutex ownership
  42. //!transfer is supported through the syntax of move semantics. Ownership transfer
  43. //!is allowed both by construction and assignment. The scoped_lock does not support
  44. //!copy semantics. A compile time error results if copy construction or copy
  45. //!assignment is attempted. Mutex ownership can also be moved from an
  46. //!upgradable_lock and sharable_lock via constructor. In this role, scoped_lock
  47. //!shares the same functionality as a write_lock.
  48. template <class Mutex>
  49. class scoped_lock
  50. {
  51. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  52. private:
  53. typedef scoped_lock<Mutex> this_type;
  54. BOOST_MOVABLE_BUT_NOT_COPYABLE(scoped_lock)
  55. typedef bool this_type::*unspecified_bool_type;
  56. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  57. public:
  58. typedef Mutex mutex_type;
  59. //!Effects: Default constructs a scoped_lock.
  60. //!Postconditions: owns() == false and mutex() == 0.
  61. scoped_lock()
  62. : mp_mutex(0), m_locked(false)
  63. {}
  64. //!Effects: m.lock().
  65. //!Postconditions: owns() == true and mutex() == &m.
  66. //!Notes: The constructor will take ownership of the mutex. If another thread
  67. //! already owns the mutex, this thread will block until the mutex is released.
  68. //! Whether or not this constructor handles recursive locking depends upon the mutex.
  69. explicit scoped_lock(mutex_type& m)
  70. : mp_mutex(&m), m_locked(false)
  71. { mp_mutex->lock(); m_locked = true; }
  72. //!Postconditions: owns() == false, and mutex() == &m.
  73. //!Notes: The constructor will not take ownership of the mutex. There is no effect
  74. //! required on the referenced mutex.
  75. scoped_lock(mutex_type& m, defer_lock_type)
  76. : mp_mutex(&m), m_locked(false)
  77. {}
  78. //!Postconditions: owns() == true, and mutex() == &m.
  79. //!Notes: The constructor will suppose that the mutex is already locked. There
  80. //! is no effect required on the referenced mutex.
  81. scoped_lock(mutex_type& m, accept_ownership_type)
  82. : mp_mutex(&m), m_locked(true)
  83. {}
  84. //!Effects: m.try_lock().
  85. //!Postconditions: mutex() == &m. owns() == the return value of the
  86. //! m.try_lock() executed within the constructor.
  87. //!Notes: The constructor will take ownership of the mutex if it can do
  88. //! so without waiting. Whether or not this constructor handles recursive
  89. //! locking depends upon the mutex. If the mutex_type does not support try_lock,
  90. //! this constructor will fail at compile time if instantiated, but otherwise
  91. //! have no effect.
  92. scoped_lock(mutex_type& m, try_to_lock_type)
  93. : mp_mutex(&m), m_locked(mp_mutex->try_lock())
  94. {}
  95. //!Effects: m.timed_lock(abs_time).
  96. //!Postconditions: mutex() == &m. owns() == the return value of the
  97. //! m.timed_lock(abs_time) executed within the constructor.
  98. //!Notes: The constructor will take ownership of the mutex if it can do
  99. //! it until abs_time is reached. Whether or not this constructor
  100. //! handles recursive locking depends upon the mutex. If the mutex_type
  101. //! does not support try_lock, this constructor will fail at compile
  102. //! time if instantiated, but otherwise have no effect.
  103. scoped_lock(mutex_type& m, const boost::posix_time::ptime& 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)
  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>
  188. scoped_lock(BOOST_RV_REF(upgradable_lock<T>) upgr, boost::posix_time::ptime &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. try{ if(m_locked && mp_mutex) mp_mutex->unlock(); }
  237. catch(...){}
  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. bool timed_lock(const boost::posix_time::ptime& abs_time)
  287. {
  288. if(!mp_mutex || m_locked)
  289. throw lock_exception();
  290. m_locked = mp_mutex->timed_lock(abs_time);
  291. return m_locked;
  292. }
  293. //!Effects: If mutex() == 0 or if not locked, throws a lock_exception()
  294. //! exception. Calls unlock() on the referenced mutex.
  295. //!Postconditions: owns() == false.
  296. //!Notes: The scoped_lock changes from a state of owning the mutex, to not
  297. //! owning the mutex.*/
  298. void unlock()
  299. {
  300. if(!mp_mutex || !m_locked)
  301. throw lock_exception();
  302. mp_mutex->unlock();
  303. m_locked = false;
  304. }
  305. //!Effects: Returns true if this scoped_lock has acquired
  306. //!the referenced mutex.
  307. bool owns() const
  308. { return m_locked && mp_mutex; }
  309. //!Conversion to bool.
  310. //!Returns owns().
  311. operator unspecified_bool_type() const
  312. { return m_locked? &this_type::m_locked : 0; }
  313. //!Effects: Returns a pointer to the referenced mutex, or 0 if
  314. //!there is no mutex to reference.
  315. mutex_type* mutex() const
  316. { return mp_mutex; }
  317. //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
  318. //! mutex to reference.
  319. //!Postconditions: mutex() == 0 and owns() == false.
  320. mutex_type* release()
  321. {
  322. mutex_type *mut = mp_mutex;
  323. mp_mutex = 0;
  324. m_locked = false;
  325. return mut;
  326. }
  327. //!Effects: Swaps state with moved lock.
  328. //!Throws: Nothing.
  329. void swap( scoped_lock<mutex_type> &other)
  330. {
  331. (simple_swap)(mp_mutex, other.mp_mutex);
  332. (simple_swap)(m_locked, other.m_locked);
  333. }
  334. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  335. private:
  336. mutex_type *mp_mutex;
  337. bool m_locked;
  338. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  339. };
  340. } // namespace interprocess
  341. } // namespace boost
  342. #include <boost/interprocess/detail/config_end.hpp>
  343. #endif // BOOST_INTERPROCESS_SCOPED_LOCK_HPP