interprocess_sharable_mutex.hpp 11 KB

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