interprocess_sharable_mutex.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/detail/posix_time_types_wrk.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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  86. //!Precondition: The thread must have exclusive ownership of the mutex.
  87. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  88. //!Throws: An exception derived from interprocess_exception on error.
  89. void unlock();
  90. //Sharable locking
  91. //!Requires: The calling thread does not own the mutex.
  92. //!
  93. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  94. //! and if another thread has exclusive ownership of the mutex,
  95. //! waits until it can obtain the ownership.
  96. //!Throws: interprocess_exception on error.
  97. //!
  98. //!Note: A program may deadlock if the thread that has ownership calls
  99. //! this function. If the implementation can detect the deadlock,
  100. //! an exception could be thrown.
  101. void lock_sharable();
  102. //!Requires: The calling thread does not own the mutex.
  103. //!
  104. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  105. //! without waiting. If no other thread has exclusive ownership
  106. //! of the mutex this succeeds.
  107. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  108. //! has to wait, returns false.
  109. //!Throws: interprocess_exception on error.
  110. //!
  111. //!Note: A program may deadlock if the thread that has ownership calls
  112. //! this function. If the implementation can detect the deadlock,
  113. //! an exception could be thrown.
  114. bool try_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. //! waiting if necessary until no other thread has exclusive
  119. //! ownership of the mutex or abs_time is reached.
  120. //!Returns: If acquires sharable ownership, returns true. Otherwise 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 timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  127. //!Precondition: The thread must have sharable ownership of the mutex.
  128. //!Effects: The calling thread releases the sharable ownership of the mutex.
  129. //!Throws: An exception derived from interprocess_exception on error.
  130. void unlock_sharable();
  131. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  132. private:
  133. typedef scoped_lock<interprocess_mutex> scoped_lock_t;
  134. //Pack all the control data in a word to be able
  135. //to use atomic instructions in the future
  136. struct control_word_t
  137. {
  138. unsigned exclusive_in : 1;
  139. unsigned num_shared : sizeof(unsigned)*CHAR_BIT-1;
  140. } m_ctrl;
  141. interprocess_mutex m_mut;
  142. interprocess_condition m_first_gate;
  143. interprocess_condition m_second_gate;
  144. private:
  145. //Rollback structures for exceptions or failure return values
  146. struct exclusive_rollback
  147. {
  148. exclusive_rollback(control_word_t &ctrl
  149. ,interprocess_condition &first_gate)
  150. : mp_ctrl(&ctrl), m_first_gate(first_gate)
  151. {}
  152. void release()
  153. { mp_ctrl = 0; }
  154. ~exclusive_rollback()
  155. {
  156. if(mp_ctrl){
  157. mp_ctrl->exclusive_in = 0;
  158. m_first_gate.notify_all();
  159. }
  160. }
  161. control_word_t *mp_ctrl;
  162. interprocess_condition &m_first_gate;
  163. };
  164. template<int Dummy>
  165. struct base_constants_t
  166. {
  167. static const unsigned max_readers
  168. = ~(unsigned(1) << (sizeof(unsigned)*CHAR_BIT-1));
  169. };
  170. typedef base_constants_t<0> constants;
  171. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  172. };
  173. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  174. template <int Dummy>
  175. const unsigned interprocess_sharable_mutex::base_constants_t<Dummy>::max_readers;
  176. inline interprocess_sharable_mutex::interprocess_sharable_mutex()
  177. {
  178. this->m_ctrl.exclusive_in = 0;
  179. this->m_ctrl.num_shared = 0;
  180. }
  181. inline interprocess_sharable_mutex::~interprocess_sharable_mutex()
  182. {}
  183. inline void interprocess_sharable_mutex::lock()
  184. {
  185. scoped_lock_t lck(m_mut);
  186. //The exclusive lock must block in the first gate
  187. //if an exclusive lock has been acquired
  188. while (this->m_ctrl.exclusive_in){
  189. this->m_first_gate.wait(lck);
  190. }
  191. //Mark that exclusive lock has been acquired
  192. this->m_ctrl.exclusive_in = 1;
  193. //Prepare rollback
  194. exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
  195. //Now wait until all readers are gone
  196. while (this->m_ctrl.num_shared){
  197. this->m_second_gate.wait(lck);
  198. }
  199. rollback.release();
  200. }
  201. inline bool interprocess_sharable_mutex::try_lock()
  202. {
  203. scoped_lock_t lck(m_mut, try_to_lock);
  204. //If we can't lock or any has there is any exclusive
  205. //or sharable mark return false;
  206. if(!lck.owns()
  207. || this->m_ctrl.exclusive_in
  208. || this->m_ctrl.num_shared){
  209. return false;
  210. }
  211. this->m_ctrl.exclusive_in = 1;
  212. return true;
  213. }
  214. inline bool interprocess_sharable_mutex::timed_lock
  215. (const boost::posix_time::ptime &abs_time)
  216. {
  217. scoped_lock_t lck(m_mut, abs_time);
  218. if(!lck.owns()) return false;
  219. //The exclusive lock must block in the first gate
  220. //if an exclusive lock has been acquired
  221. while (this->m_ctrl.exclusive_in){
  222. //Mutexes and condvars handle just fine infinite abs_times
  223. //so avoid checking it here
  224. if(!this->m_first_gate.timed_wait(lck, abs_time)){
  225. if(this->m_ctrl.exclusive_in){
  226. return false;
  227. }
  228. break;
  229. }
  230. }
  231. //Mark that exclusive lock has been acquired
  232. this->m_ctrl.exclusive_in = 1;
  233. //Prepare rollback
  234. exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
  235. //Now wait until all readers are gone
  236. while (this->m_ctrl.num_shared){
  237. //Mutexes and condvars handle just fine infinite abs_times
  238. //so avoid checking it here
  239. if(!this->m_second_gate.timed_wait(lck, abs_time)){
  240. if(this->m_ctrl.num_shared){
  241. return false;
  242. }
  243. break;
  244. }
  245. }
  246. rollback.release();
  247. return true;
  248. }
  249. inline void interprocess_sharable_mutex::unlock()
  250. {
  251. scoped_lock_t lck(m_mut);
  252. this->m_ctrl.exclusive_in = 0;
  253. this->m_first_gate.notify_all();
  254. }
  255. //Sharable locking
  256. inline void interprocess_sharable_mutex::lock_sharable()
  257. {
  258. scoped_lock_t lck(m_mut);
  259. //The sharable lock must block in the first gate
  260. //if an exclusive lock has been acquired
  261. //or there are too many sharable locks
  262. while(this->m_ctrl.exclusive_in
  263. || this->m_ctrl.num_shared == constants::max_readers){
  264. this->m_first_gate.wait(lck);
  265. }
  266. //Increment sharable count
  267. ++this->m_ctrl.num_shared;
  268. }
  269. inline bool interprocess_sharable_mutex::try_lock_sharable()
  270. {
  271. scoped_lock_t lck(m_mut, try_to_lock);
  272. //The sharable lock must fail
  273. //if an exclusive lock has been acquired
  274. //or there are too many sharable locks
  275. if(!lck.owns()
  276. || this->m_ctrl.exclusive_in
  277. || this->m_ctrl.num_shared == constants::max_readers){
  278. return false;
  279. }
  280. //Increment sharable count
  281. ++this->m_ctrl.num_shared;
  282. return true;
  283. }
  284. inline bool interprocess_sharable_mutex::timed_lock_sharable
  285. (const boost::posix_time::ptime &abs_time)
  286. {
  287. scoped_lock_t lck(m_mut, abs_time);
  288. if(!lck.owns()) return false;
  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. //Mutexes and condvars handle just fine infinite abs_times
  295. //so avoid checking it here
  296. if(!this->m_first_gate.timed_wait(lck, abs_time)){
  297. if(this->m_ctrl.exclusive_in
  298. || this->m_ctrl.num_shared == constants::max_readers){
  299. return false;
  300. }
  301. break;
  302. }
  303. }
  304. //Increment sharable count
  305. ++this->m_ctrl.num_shared;
  306. return true;
  307. }
  308. inline void interprocess_sharable_mutex::unlock_sharable()
  309. {
  310. scoped_lock_t lck(m_mut);
  311. //Decrement sharable count
  312. --this->m_ctrl.num_shared;
  313. if (this->m_ctrl.num_shared == 0){
  314. this->m_second_gate.notify_one();
  315. }
  316. //Check if there are blocked sharables because of
  317. //there were too many sharables
  318. else if(this->m_ctrl.num_shared == (constants::max_readers-1)){
  319. this->m_first_gate.notify_all();
  320. }
  321. }
  322. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  323. } //namespace interprocess {
  324. } //namespace boost {
  325. #include <boost/interprocess/detail/config_end.hpp>
  326. #endif //BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP