upgradable_lock.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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_UPGRADABLE_LOCK_HPP
  16. #define BOOST_INTERPROCESS_UPGRADABLE_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/detail/mpl.hpp>
  29. #include <boost/interprocess/detail/type_traits.hpp>
  30. #include <boost/interprocess/exceptions.hpp>
  31. #include <boost/move/utility_core.hpp>
  32. //!\file
  33. //!Describes the upgradable_lock class that serves to acquire the upgradable
  34. //!lock of a mutex.
  35. namespace boost {
  36. namespace interprocess {
  37. //!upgradable_lock is meant to carry out the tasks for read-locking, unlocking,
  38. //!try-read-locking and timed-read-locking (recursive or not) for the Mutex.
  39. //!Additionally the upgradable_lock can transfer ownership to a scoped_lock
  40. //!using transfer_lock syntax. The Mutex need not supply all of the functionality.
  41. //!If the client of upgradable_lock<Mutex> does not use functionality which the
  42. //!Mutex does not supply, no harm is done. Mutex ownership can be shared among
  43. //!read_locks, and a single upgradable_lock. upgradable_lock does not support
  44. //!copy semantics. However upgradable_lock supports ownership transfer from
  45. //!a upgradable_locks or scoped_locks via transfer_lock syntax.
  46. template <class UpgradableMutex>
  47. class upgradable_lock
  48. {
  49. public:
  50. typedef UpgradableMutex mutex_type;
  51. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  52. private:
  53. typedef upgradable_lock<UpgradableMutex> this_type;
  54. explicit upgradable_lock(scoped_lock<mutex_type>&);
  55. typedef bool this_type::*unspecified_bool_type;
  56. BOOST_MOVABLE_BUT_NOT_COPYABLE(upgradable_lock)
  57. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  58. public:
  59. //!Effects: Default constructs a upgradable_lock.
  60. //!Postconditions: owns() == false and mutex() == 0.
  61. upgradable_lock() BOOST_NOEXCEPT
  62. : mp_mutex(0), m_locked(false)
  63. {}
  64. explicit upgradable_lock(mutex_type& m)
  65. : mp_mutex(&m), m_locked(false)
  66. { mp_mutex->lock_upgradable(); 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. upgradable_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 upgradable
  75. //! locked. There is no effect required on the referenced mutex.
  76. upgradable_lock(mutex_type& m, accept_ownership_type)
  77. : mp_mutex(&m), m_locked(true)
  78. {}
  79. //!Effects: m.try_lock_upgradable().
  80. //!Postconditions: mutex() == &m. owns() == the return value of the
  81. //! m.try_lock_upgradable() executed within the constructor.
  82. //!Notes: The constructor will take upgradable-ownership of the mutex
  83. //! if it can do so without waiting. Whether or not this constructor
  84. //! handles recursive locking depends upon the mutex. If the mutex_type
  85. //! does not support try_lock_upgradable, this constructor will fail at
  86. //! compile time if instantiated, but otherwise have no effect.
  87. upgradable_lock(mutex_type& m, try_to_lock_type)
  88. : mp_mutex(&m), m_locked(false)
  89. { m_locked = mp_mutex->try_lock_upgradable(); }
  90. //!Effects: m.timed_lock_upgradable(abs_time)
  91. //!Postconditions: mutex() == &m. owns() == the return value of the
  92. //! m.timed_lock_upgradable() executed within the constructor.
  93. //!Notes: The constructor will take upgradable-ownership of the mutex if it
  94. //! can do so within the time specified. Whether or not this constructor
  95. //! handles recursive locking depends upon the mutex. If the mutex_type
  96. //! does not support timed_lock_upgradable, this constructor will fail
  97. //! at compile time if instantiated, but otherwise have no effect.
  98. template<class TimePoint>
  99. upgradable_lock(mutex_type& m, const TimePoint& abs_time)
  100. : mp_mutex(&m), m_locked(false)
  101. { m_locked = mp_mutex->timed_lock_upgradable(abs_time); }
  102. //!Effects: No effects on the underlying mutex.
  103. //!Postconditions: mutex() == the value upgr.mutex() had before the
  104. //! construction. upgr.mutex() == 0. owns() == upgr.owns() before the
  105. //! construction. upgr.owns() == false.
  106. //!Notes: If upgr is locked, this constructor will lock this upgradable_lock
  107. //! while unlocking upgr. If upgr is unlocked, then this upgradable_lock will
  108. //! be unlocked as well. Only a moved upgradable_lock's will match this
  109. //! signature. An non-moved upgradable_lock can be moved with the
  110. //! expression: "boost::move(lock);". This constructor does not alter the
  111. //! state of the mutex, only potentially who owns it.
  112. upgradable_lock(BOOST_RV_REF(upgradable_lock<mutex_type>) upgr) BOOST_NOEXCEPT
  113. : mp_mutex(0), m_locked(upgr.owns())
  114. { mp_mutex = upgr.release(); }
  115. //!Effects: If scop.owns(), m_.unlock_and_lock_upgradable().
  116. //!Postconditions: mutex() == the value scop.mutex() had before the construction.
  117. //! scop.mutex() == 0. owns() == scop.owns() before the constructor. After the
  118. //! construction, scop.owns() == false.
  119. //!Notes: If scop is locked, this constructor will transfer the exclusive-ownership
  120. //! to an upgradable-ownership of this upgradable_lock.
  121. //! Only a moved sharable_lock's will match this
  122. //! signature. An non-moved sharable_lock can be moved with the
  123. //! expression: "boost::move(lock);".
  124. template<class T>
  125. upgradable_lock(BOOST_RV_REF(scoped_lock<T>) scop
  126. , typename ipcdetail::enable_if< ipcdetail::is_same<T, UpgradableMutex> >::type * = 0)
  127. : mp_mutex(0), m_locked(false)
  128. {
  129. scoped_lock<mutex_type> &u_lock = scop;
  130. if(u_lock.owns()){
  131. u_lock.mutex()->unlock_and_lock_upgradable();
  132. m_locked = true;
  133. }
  134. mp_mutex = u_lock.release();
  135. }
  136. //!Effects: If shar.owns() then calls try_unlock_sharable_and_lock_upgradable()
  137. //! on the referenced mutex.
  138. //! a)if try_unlock_sharable_and_lock_upgradable() returns true then mutex()
  139. //! obtains the value from shar.release() and owns() is set to true.
  140. //! b)if try_unlock_sharable_and_lock_upgradable() returns false then shar is
  141. //! unaffected and this upgradable_lock construction has the same
  142. //! effects as a default construction.
  143. //! c)Else shar.owns() is false. mutex() obtains the value from shar.release()
  144. //! and owns() is set to false.
  145. //!Notes: This construction will not block. It will try to obtain mutex
  146. //! ownership from shar immediately, while changing the lock type from a
  147. //! "read lock" to an "upgradable lock". If the "read lock" isn't held
  148. //! in the first place, the mutex merely changes type to an unlocked
  149. //! "upgradable lock". If the "read lock" is held, then mutex transfer
  150. //! occurs only if it can do so in a non-blocking manner.
  151. template<class T>
  152. upgradable_lock( BOOST_RV_REF(sharable_lock<T>) shar, try_to_lock_type
  153. , typename ipcdetail::enable_if< ipcdetail::is_same<T, UpgradableMutex> >::type * = 0)
  154. : mp_mutex(0), m_locked(false)
  155. {
  156. sharable_lock<mutex_type> &s_lock = shar;
  157. if(s_lock.owns()){
  158. if((m_locked = s_lock.mutex()->try_unlock_sharable_and_lock_upgradable()) == true){
  159. mp_mutex = s_lock.release();
  160. }
  161. }
  162. else{
  163. s_lock.release();
  164. }
  165. }
  166. //!Effects: if (owns()) m_->unlock_upgradable().
  167. //!Notes: The destructor behavior ensures that the mutex lock is not leaked.
  168. ~upgradable_lock()
  169. {
  170. BOOST_TRY{
  171. if(m_locked && mp_mutex) mp_mutex->unlock_upgradable();
  172. }
  173. BOOST_CATCH(...){} BOOST_CATCH_END
  174. }
  175. //!Effects: If owns(), then unlock_upgradable() is called on mutex().
  176. //! *this gets the state of upgr and upgr gets set to a default constructed state.
  177. //!Notes: With a recursive mutex it is possible that both this and upgr own the
  178. //! mutex before the assignment. In this case, this will own the mutex
  179. //! after the assignment (and upgr will not), but the mutex's upgradable lock
  180. //! count will be decremented by one.
  181. upgradable_lock &operator=(BOOST_RV_REF(upgradable_lock) upgr)
  182. {
  183. if(this->owns())
  184. this->unlock();
  185. m_locked = upgr.owns();
  186. mp_mutex = upgr.release();
  187. return *this;
  188. }
  189. //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
  190. //! exception. Calls lock_upgradable() on the referenced mutex.
  191. //!Postconditions: owns() == true.
  192. //!Notes: The sharable_lock changes from a state of not owning the mutex,
  193. //! to owning the mutex, blocking if necessary.
  194. void lock()
  195. {
  196. if(!mp_mutex || m_locked)
  197. throw lock_exception();
  198. mp_mutex->lock_upgradable();
  199. m_locked = true;
  200. }
  201. //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
  202. //! exception. Calls try_lock_upgradable() on the referenced mutex.
  203. //!Postconditions: owns() == the value returned from
  204. //! mutex()->try_lock_upgradable().
  205. //!Notes: The upgradable_lock changes from a state of not owning the mutex,
  206. //! to owning the mutex, but only if blocking was not required. If the
  207. //! mutex_type does not support try_lock_upgradable(), this function will
  208. //! fail at compile time if instantiated, but otherwise have no effect.
  209. bool try_lock()
  210. {
  211. if(!mp_mutex || m_locked)
  212. throw lock_exception();
  213. m_locked = mp_mutex->try_lock_upgradable();
  214. return m_locked;
  215. }
  216. //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
  217. //! exception. Calls timed_lock_upgradable(abs_time) on the referenced mutex.
  218. //!Postconditions: owns() == the value returned from
  219. //! mutex()->timed_lock_upgradable(abs_time).
  220. //!Notes: The upgradable_lock changes from a state of not owning the mutex,
  221. //! to owning the mutex, but only if it can obtain ownership within the
  222. //! specified time. If the mutex_type does not support
  223. //! timed_lock_upgradable(abs_time), this function will fail at compile
  224. //! time if instantiated, but otherwise have no effect.
  225. template<class TimePoint>
  226. bool timed_lock(const TimePoint& abs_time)
  227. {
  228. if(!mp_mutex || m_locked)
  229. throw lock_exception();
  230. m_locked = mp_mutex->timed_lock_upgradable(abs_time);
  231. return m_locked;
  232. }
  233. //!Effects: If mutex() == 0 or if not locked, throws a lock_exception()
  234. //! exception. Calls unlock_upgradable() on the referenced mutex.
  235. //!Postconditions: owns() == false.
  236. //!Notes: The upgradable_lock changes from a state of owning the mutex,
  237. //! to not owning the mutex.
  238. void unlock()
  239. {
  240. if(!mp_mutex || !m_locked)
  241. throw lock_exception();
  242. mp_mutex->unlock_upgradable();
  243. m_locked = false;
  244. }
  245. //!Effects: Returns true if this scoped_lock has acquired the
  246. //!referenced mutex.
  247. bool owns() const BOOST_NOEXCEPT
  248. { return m_locked && mp_mutex; }
  249. //!Conversion to bool.
  250. //!Returns owns().
  251. operator unspecified_bool_type() const BOOST_NOEXCEPT
  252. { return m_locked? &this_type::m_locked : 0; }
  253. //!Effects: Returns a pointer to the referenced mutex, or 0 if
  254. //!there is no mutex to reference.
  255. mutex_type* mutex() const BOOST_NOEXCEPT
  256. { return mp_mutex; }
  257. //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
  258. //! mutex to reference.
  259. //!Postconditions: mutex() == 0 and owns() == false.
  260. mutex_type* release() BOOST_NOEXCEPT
  261. {
  262. mutex_type *mut = mp_mutex;
  263. mp_mutex = 0;
  264. m_locked = false;
  265. return mut;
  266. }
  267. //!Effects: Swaps state with moved lock.
  268. //!Throws: Nothing.
  269. void swap(upgradable_lock<mutex_type> &other) BOOST_NOEXCEPT
  270. {
  271. (simple_swap)(mp_mutex, other.mp_mutex);
  272. (simple_swap)(m_locked, other.m_locked);
  273. }
  274. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  275. private:
  276. mutex_type *mp_mutex;
  277. bool m_locked;
  278. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  279. };
  280. } // namespace interprocess
  281. } // namespace boost
  282. #include <boost/interprocess/detail/config_end.hpp>
  283. #endif // BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP