upgradable_lock.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  33. //!\file
  34. //!Describes the upgradable_lock class that serves to acquire the upgradable
  35. //!lock of a mutex.
  36. namespace boost {
  37. namespace interprocess {
  38. //!upgradable_lock is meant to carry out the tasks for read-locking, unlocking,
  39. //!try-read-locking and timed-read-locking (recursive or not) for the Mutex.
  40. //!Additionally the upgradable_lock can transfer ownership to a scoped_lock
  41. //!using transfer_lock syntax. The Mutex need not supply all of the functionality.
  42. //!If the client of upgradable_lock<Mutex> does not use functionality which the
  43. //!Mutex does not supply, no harm is done. Mutex ownership can be shared among
  44. //!read_locks, and a single upgradable_lock. upgradable_lock does not support
  45. //!copy semantics. However upgradable_lock supports ownership transfer from
  46. //!a upgradable_locks or scoped_locks via transfer_lock syntax.
  47. template <class UpgradableMutex>
  48. class upgradable_lock
  49. {
  50. public:
  51. typedef UpgradableMutex mutex_type;
  52. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  53. private:
  54. typedef upgradable_lock<UpgradableMutex> this_type;
  55. explicit upgradable_lock(scoped_lock<mutex_type>&);
  56. typedef bool this_type::*unspecified_bool_type;
  57. BOOST_MOVABLE_BUT_NOT_COPYABLE(upgradable_lock)
  58. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  59. public:
  60. //!Effects: Default constructs a upgradable_lock.
  61. //!Postconditions: owns() == false and mutex() == 0.
  62. upgradable_lock() BOOST_NOEXCEPT
  63. : mp_mutex(0), m_locked(false)
  64. {}
  65. explicit upgradable_lock(mutex_type& m)
  66. : mp_mutex(&m), m_locked(false)
  67. { mp_mutex->lock_upgradable(); m_locked = true; }
  68. //!Postconditions: owns() == false, and mutex() == &m.
  69. //!Notes: The constructor will not take ownership of the mutex. There is no effect
  70. //! required on the referenced mutex.
  71. upgradable_lock(mutex_type& m, defer_lock_type)
  72. : mp_mutex(&m), m_locked(false)
  73. {}
  74. //!Postconditions: owns() == true, and mutex() == &m.
  75. //!Notes: The constructor will suppose that the mutex is already upgradable
  76. //! locked. There is no effect required on the referenced mutex.
  77. upgradable_lock(mutex_type& m, accept_ownership_type)
  78. : mp_mutex(&m), m_locked(true)
  79. {}
  80. //!Effects: m.try_lock_upgradable().
  81. //!Postconditions: mutex() == &m. owns() == the return value of the
  82. //! m.try_lock_upgradable() executed within the constructor.
  83. //!Notes: The constructor will take upgradable-ownership of the mutex
  84. //! if it can do so without waiting. Whether or not this constructor
  85. //! handles recursive locking depends upon the mutex. If the mutex_type
  86. //! does not support try_lock_upgradable, this constructor will fail at
  87. //! compile time if instantiated, but otherwise have no effect.
  88. upgradable_lock(mutex_type& m, try_to_lock_type)
  89. : mp_mutex(&m), m_locked(false)
  90. { m_locked = mp_mutex->try_lock_upgradable(); }
  91. //!Effects: m.timed_lock_upgradable(abs_time)
  92. //!Postconditions: mutex() == &m. owns() == the return value of the
  93. //! m.timed_lock_upgradable() executed within the constructor.
  94. //!Notes: The constructor will take upgradable-ownership of the mutex if it
  95. //! can do so within the time specified. Whether or not this constructor
  96. //! handles recursive locking depends upon the mutex. If the mutex_type
  97. //! does not support timed_lock_upgradable, this constructor will fail
  98. //! at compile time if instantiated, but otherwise have no effect.
  99. upgradable_lock(mutex_type& m, const boost::posix_time::ptime& 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. try{
  171. if(m_locked && mp_mutex) mp_mutex->unlock_upgradable();
  172. }
  173. catch(...){}
  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. bool timed_lock(const boost::posix_time::ptime& abs_time)
  226. {
  227. if(!mp_mutex || m_locked)
  228. throw lock_exception();
  229. m_locked = mp_mutex->timed_lock_upgradable(abs_time);
  230. return m_locked;
  231. }
  232. //!Effects: If mutex() == 0 or if not locked, throws a lock_exception()
  233. //! exception. Calls unlock_upgradable() on the referenced mutex.
  234. //!Postconditions: owns() == false.
  235. //!Notes: The upgradable_lock changes from a state of owning the mutex,
  236. //! to not owning the mutex.
  237. void unlock()
  238. {
  239. if(!mp_mutex || !m_locked)
  240. throw lock_exception();
  241. mp_mutex->unlock_upgradable();
  242. m_locked = false;
  243. }
  244. //!Effects: Returns true if this scoped_lock has acquired the
  245. //!referenced mutex.
  246. bool owns() const BOOST_NOEXCEPT
  247. { return m_locked && mp_mutex; }
  248. //!Conversion to bool.
  249. //!Returns owns().
  250. operator unspecified_bool_type() const BOOST_NOEXCEPT
  251. { return m_locked? &this_type::m_locked : 0; }
  252. //!Effects: Returns a pointer to the referenced mutex, or 0 if
  253. //!there is no mutex to reference.
  254. mutex_type* mutex() const BOOST_NOEXCEPT
  255. { return mp_mutex; }
  256. //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
  257. //! mutex to reference.
  258. //!Postconditions: mutex() == 0 and owns() == false.
  259. mutex_type* release() BOOST_NOEXCEPT
  260. {
  261. mutex_type *mut = mp_mutex;
  262. mp_mutex = 0;
  263. m_locked = false;
  264. return mut;
  265. }
  266. //!Effects: Swaps state with moved lock.
  267. //!Throws: Nothing.
  268. void swap(upgradable_lock<mutex_type> &other) BOOST_NOEXCEPT
  269. {
  270. (simple_swap)(mp_mutex, other.mp_mutex);
  271. (simple_swap)(m_locked, other.m_locked);
  272. }
  273. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  274. private:
  275. mutex_type *mp_mutex;
  276. bool m_locked;
  277. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  278. };
  279. } // namespace interprocess
  280. } // namespace boost
  281. #include <boost/interprocess/detail/config_end.hpp>
  282. #endif // BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP