interprocess_sharable_mutex.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Code based on Howard Hinnant's shared_mutex class
  3. //
  4. // (C) Copyright Howard Hinnant 2007-2010. Distributed under the Boost
  5. // Software License, Version 1.0. (see http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // See http://www.boost.org/libs/interprocess for documentation.
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. #ifndef BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP
  15. #define BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP
  16. #ifndef BOOST_CONFIG_HPP
  17. # include <boost/config.hpp>
  18. #endif
  19. #
  20. #if defined(BOOST_HAS_PRAGMA_ONCE)
  21. # pragma once
  22. #endif
  23. #include <boost/interprocess/detail/config_begin.hpp>
  24. #include <boost/interprocess/detail/workaround.hpp>
  25. #include <boost/interprocess/sync/scoped_lock.hpp>
  26. #include <boost/interprocess/timed_utils.hpp>
  27. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  28. #include <boost/interprocess/sync/interprocess_condition.hpp>
  29. #include <climits>
  30. //!\file
  31. //!Describes interprocess_sharable_mutex class
  32. namespace boost {
  33. namespace interprocess {
  34. //!Wraps a interprocess_sharable_mutex that can be placed in shared memory and can be
  35. //!shared between processes. Allows timed lock tries
  36. class interprocess_sharable_mutex
  37. {
  38. //Non-copyable
  39. interprocess_sharable_mutex(const interprocess_sharable_mutex &);
  40. interprocess_sharable_mutex &operator=(const interprocess_sharable_mutex &);
  41. friend class interprocess_condition;
  42. public:
  43. //!Constructs the sharable lock.
  44. //!Throws interprocess_exception on error.
  45. interprocess_sharable_mutex();
  46. //!Destroys the sharable lock.
  47. //!Does not throw.
  48. ~interprocess_sharable_mutex();
  49. //Exclusive locking
  50. //!Requires: The calling thread does not own the mutex.
  51. //!
  52. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  53. //! and if another thread has exclusive or sharable ownership of
  54. //! the mutex, it waits until it can obtain the ownership.
  55. //!Throws: interprocess_exception on error.
  56. //!
  57. //!Note: A program may deadlock if the thread that has ownership calls
  58. //! this function. If the implementation can detect the deadlock,
  59. //! an exception could be thrown.
  60. void lock();
  61. //!Requires: The calling thread does not own the mutex.
  62. //!
  63. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  64. //! without waiting. If no other thread has exclusive or sharable
  65. //! ownership of the mutex this succeeds.
  66. //!Returns: If it can acquire exclusive ownership immediately returns true.
  67. //! If it has to wait, returns false.
  68. //!Throws: interprocess_exception on error.
  69. //!
  70. //!Note: A program may deadlock if the thread that has ownership calls
  71. //! this function. If the implementation can detect the deadlock,
  72. //! an exception could be thrown.
  73. bool try_lock();
  74. //!Requires: The calling thread does not own the mutex.
  75. //!
  76. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  77. //! waiting if necessary until no other thread has exclusive or sharable
  78. //! ownership of the mutex or abs_time is reached.
  79. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  80. //!Throws: interprocess_exception on error.
  81. //!
  82. //!Note: A program may deadlock if the thread that has ownership calls
  83. //! this function. If the implementation can detect the deadlock,
  84. //! an exception could be thrown.
  85. template<class TimePoint>
  86. bool timed_lock(const TimePoint &abs_time);
  87. //!Same as `timed_lock`, but this function is modeled after the
  88. //!standard library interface.
  89. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  90. { return this->timed_lock(abs_time); }
  91. //!Same as `timed_lock`, but this function is modeled after the
  92. //!standard library interface.
  93. template<class Duration> bool try_lock_for(const Duration &dur)
  94. { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
  95. //!Precondition: The thread must have exclusive ownership of the mutex.
  96. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  97. //!Throws: An exception derived from interprocess_exception on error.
  98. void unlock();
  99. //Sharable locking
  100. //!Requires: The calling thread does not own the mutex.
  101. //!
  102. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  103. //! and if another thread has exclusive ownership of the mutex,
  104. //! waits until it can obtain the ownership.
  105. //!Throws: interprocess_exception on error.
  106. //!
  107. //!Note: A program may deadlock if the thread that has ownership calls
  108. //! this function. If the implementation can detect the deadlock,
  109. //! an exception could be thrown.
  110. void lock_sharable();
  111. //!Same as `lock_sharable` but with a std-compatible interface
  112. //!
  113. void lock_shared()
  114. { this->lock_sharable(); }
  115. //!Requires: The calling thread does not own the mutex.
  116. //!
  117. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  118. //! without waiting. If no other thread has exclusive ownership
  119. //! of the mutex this succeeds.
  120. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  121. //! has to wait, returns false.
  122. //!Throws: interprocess_exception on error.
  123. //!
  124. //!Note: A program may deadlock if the thread that has ownership calls
  125. //! this function. If the implementation can detect the deadlock,
  126. //! an exception could be thrown.
  127. bool try_lock_sharable();
  128. //!Same as `try_lock_sharable` but with a std-compatible interface
  129. //!
  130. bool try_lock_shared()
  131. { return this->try_lock_sharable(); }
  132. //!Requires: The calling thread does not own the mutex.
  133. //!
  134. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  135. //! waiting if necessary until no other thread has exclusive
  136. //! ownership of the mutex or abs_time is reached.
  137. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  138. //!Throws: interprocess_exception on error.
  139. //!
  140. //!Note: A program may deadlock if the thread that has ownership calls
  141. //! this function. If the implementation can detect the deadlock,
  142. //! an exception could be thrown.
  143. template<class TimePoint>
  144. bool timed_lock_sharable(const TimePoint &abs_time);
  145. //!Same as `timed_lock_sharable`, but this function is modeled after the
  146. //!standard library interface.
  147. template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
  148. { return this->timed_lock_sharable(abs_time); }
  149. //!Same as `timed_lock_sharable`, but this function is modeled after the
  150. //!standard library interface.
  151. template<class Duration> bool try_lock_shared_for(const Duration &dur)
  152. { return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
  153. //!Precondition: The thread must have sharable ownership of the mutex.
  154. //!Effects: The calling thread releases the sharable ownership of the mutex.
  155. //!Throws: An exception derived from interprocess_exception on error.
  156. void unlock_sharable();
  157. //!Same as `unlock_sharable` but with a std-compatible interface
  158. //!
  159. void unlock_shared()
  160. { this->unlock_sharable(); }
  161. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  162. private:
  163. typedef scoped_lock<interprocess_mutex> scoped_lock_t;
  164. //Pack all the control data in a word to be able
  165. //to use atomic instructions in the future
  166. struct control_word_t
  167. {
  168. unsigned exclusive_in : 1;
  169. unsigned num_shared : sizeof(unsigned)*CHAR_BIT-1;
  170. } m_ctrl;
  171. interprocess_mutex m_mut;
  172. interprocess_condition m_first_gate;
  173. interprocess_condition m_second_gate;
  174. private:
  175. //Rollback structures for exceptions or failure return values
  176. struct exclusive_rollback
  177. {
  178. exclusive_rollback(control_word_t &ctrl
  179. ,interprocess_condition &first_gate)
  180. : mp_ctrl(&ctrl), m_first_gate(first_gate)
  181. {}
  182. void release()
  183. { mp_ctrl = 0; }
  184. ~exclusive_rollback()
  185. {
  186. if(mp_ctrl){
  187. mp_ctrl->exclusive_in = 0;
  188. m_first_gate.notify_all();
  189. }
  190. }
  191. control_word_t *mp_ctrl;
  192. interprocess_condition &m_first_gate;
  193. };
  194. template<int Dummy>
  195. struct base_constants_t
  196. {
  197. static const unsigned max_readers
  198. = ~(unsigned(1) << (sizeof(unsigned)*CHAR_BIT-1));
  199. };
  200. typedef base_constants_t<0> constants;
  201. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  202. };
  203. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  204. template <int Dummy>
  205. const unsigned interprocess_sharable_mutex::base_constants_t<Dummy>::max_readers;
  206. inline interprocess_sharable_mutex::interprocess_sharable_mutex()
  207. {
  208. this->m_ctrl.exclusive_in = 0;
  209. this->m_ctrl.num_shared = 0;
  210. }
  211. inline interprocess_sharable_mutex::~interprocess_sharable_mutex()
  212. {}
  213. inline void interprocess_sharable_mutex::lock()
  214. {
  215. scoped_lock_t lck(m_mut);
  216. //The exclusive lock must block in the first gate
  217. //if an exclusive lock has been acquired
  218. while (this->m_ctrl.exclusive_in){
  219. this->m_first_gate.wait(lck);
  220. }
  221. //Mark that exclusive lock has been acquired
  222. this->m_ctrl.exclusive_in = 1;
  223. //Prepare rollback
  224. exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
  225. //Now wait until all readers are gone
  226. while (this->m_ctrl.num_shared){
  227. this->m_second_gate.wait(lck);
  228. }
  229. rollback.release();
  230. }
  231. inline bool interprocess_sharable_mutex::try_lock()
  232. {
  233. scoped_lock_t lck(m_mut, try_to_lock);
  234. //If we can't lock or any has there is any exclusive
  235. //or sharable mark return false;
  236. if(!lck.owns()
  237. || this->m_ctrl.exclusive_in
  238. || this->m_ctrl.num_shared){
  239. return false;
  240. }
  241. this->m_ctrl.exclusive_in = 1;
  242. return true;
  243. }
  244. template<class TimePoint>
  245. inline bool interprocess_sharable_mutex::timed_lock
  246. (const TimePoint &abs_time)
  247. {
  248. scoped_lock_t lck(m_mut, abs_time);
  249. if(!lck.owns()) return false;
  250. //The exclusive lock must block in the first gate
  251. //if an exclusive lock has been acquired
  252. while (this->m_ctrl.exclusive_in){
  253. //Mutexes and condvars handle just fine infinite abs_times
  254. //so avoid checking it here
  255. if(!this->m_first_gate.timed_wait(lck, abs_time)){
  256. if(this->m_ctrl.exclusive_in){
  257. return false;
  258. }
  259. break;
  260. }
  261. }
  262. //Mark that exclusive lock has been acquired
  263. this->m_ctrl.exclusive_in = 1;
  264. //Prepare rollback
  265. exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
  266. //Now wait until all readers are gone
  267. while (this->m_ctrl.num_shared){
  268. //Mutexes and condvars handle just fine infinite abs_times
  269. //so avoid checking it here
  270. if(!this->m_second_gate.timed_wait(lck, abs_time)){
  271. if(this->m_ctrl.num_shared){
  272. return false;
  273. }
  274. break;
  275. }
  276. }
  277. rollback.release();
  278. return true;
  279. }
  280. inline void interprocess_sharable_mutex::unlock()
  281. {
  282. scoped_lock_t lck(m_mut);
  283. this->m_ctrl.exclusive_in = 0;
  284. this->m_first_gate.notify_all();
  285. }
  286. //Sharable locking
  287. inline void interprocess_sharable_mutex::lock_sharable()
  288. {
  289. scoped_lock_t lck(m_mut);
  290. //The sharable lock must block in the first gate
  291. //if an exclusive lock has been acquired
  292. //or there are too many sharable locks
  293. while(this->m_ctrl.exclusive_in
  294. || this->m_ctrl.num_shared == constants::max_readers){
  295. this->m_first_gate.wait(lck);
  296. }
  297. //Increment sharable count
  298. ++this->m_ctrl.num_shared;
  299. }
  300. inline bool interprocess_sharable_mutex::try_lock_sharable()
  301. {
  302. scoped_lock_t lck(m_mut, try_to_lock);
  303. //The sharable lock must fail
  304. //if an exclusive lock has been acquired
  305. //or there are too many sharable locks
  306. if(!lck.owns()
  307. || this->m_ctrl.exclusive_in
  308. || this->m_ctrl.num_shared == constants::max_readers){
  309. return false;
  310. }
  311. //Increment sharable count
  312. ++this->m_ctrl.num_shared;
  313. return true;
  314. }
  315. template<class TimePoint>
  316. inline bool interprocess_sharable_mutex::timed_lock_sharable
  317. (const TimePoint &abs_time)
  318. {
  319. scoped_lock_t lck(m_mut, abs_time);
  320. if(!lck.owns()) return false;
  321. //The sharable lock must block in the first gate
  322. //if an exclusive lock has been acquired
  323. //or there are too many sharable locks
  324. while (this->m_ctrl.exclusive_in
  325. || this->m_ctrl.num_shared == constants::max_readers){
  326. //Mutexes and condvars handle just fine infinite abs_times
  327. //so avoid checking it here
  328. if(!this->m_first_gate.timed_wait(lck, abs_time)){
  329. if(this->m_ctrl.exclusive_in
  330. || this->m_ctrl.num_shared == constants::max_readers){
  331. return false;
  332. }
  333. break;
  334. }
  335. }
  336. //Increment sharable count
  337. ++this->m_ctrl.num_shared;
  338. return true;
  339. }
  340. inline void interprocess_sharable_mutex::unlock_sharable()
  341. {
  342. scoped_lock_t lck(m_mut);
  343. //Decrement sharable count
  344. --this->m_ctrl.num_shared;
  345. if (this->m_ctrl.num_shared == 0){
  346. this->m_second_gate.notify_one();
  347. }
  348. //Check if there are blocked sharables because of
  349. //there were too many sharables
  350. else if(this->m_ctrl.num_shared == (constants::max_readers-1)){
  351. this->m_first_gate.notify_all();
  352. }
  353. }
  354. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  355. } //namespace interprocess {
  356. } //namespace boost {
  357. #include <boost/interprocess/detail/config_end.hpp>
  358. #endif //BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP