interprocess_sharable_mutex.hpp 14 KB

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