upgradable_lock.hpp 13 KB

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