named_upgradable_mutex.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #ifndef BOOST_INTERPROCESS_NAMED_UPGRADABLE_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_NAMED_UPGRADABLE_MUTEX_HPP
  12. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/creation_tags.hpp>
  18. #include <boost/interprocess/exceptions.hpp>
  19. #include <boost/interprocess/shared_memory_object.hpp>
  20. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  21. #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
  22. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  23. #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
  24. #include <boost/interprocess/permissions.hpp>
  25. //!\file
  26. //!Describes a named upgradable mutex class for inter-process synchronization
  27. namespace boost {
  28. namespace interprocess {
  29. /// @cond
  30. namespace ipcdetail{ class interprocess_tester; }
  31. /// @endcond
  32. class named_condition;
  33. //!A upgradable mutex with a global name, so it can be found from different
  34. //!processes. This mutex can't be placed in shared memory, and
  35. //!each process should have it's own named upgradable mutex.
  36. class named_upgradable_mutex
  37. {
  38. /// @cond
  39. //Non-copyable
  40. named_upgradable_mutex();
  41. named_upgradable_mutex(const named_upgradable_mutex &);
  42. named_upgradable_mutex &operator=(const named_upgradable_mutex &);
  43. friend class named_condition;
  44. /// @endcond
  45. public:
  46. //!Creates a global upgradable mutex with a name.
  47. //!If the upgradable mutex can't be created throws interprocess_exception
  48. named_upgradable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
  49. //!Opens or creates a global upgradable mutex with a name, and an initial count.
  50. //!If the upgradable mutex is created, this call is equivalent to
  51. //!named_upgradable_mutex(create_only_t, ...)
  52. //!If the upgradable mutex is already created, this call is equivalent to
  53. //!named_upgradable_mutex(open_only_t, ... ).
  54. named_upgradable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  55. //!Opens a global upgradable mutex with a name if that upgradable mutex
  56. //!is previously.
  57. //!created. If it is not previously created this function throws
  58. //!interprocess_exception.
  59. named_upgradable_mutex(open_only_t open_only, const char *name);
  60. //!Destroys *this and indicates that the calling process is finished using
  61. //!the resource. The destructor function will deallocate
  62. //!any system resources allocated by the system for use by this process for
  63. //!this resource. The resource can still be opened again calling
  64. //!the open constructor overload. To erase the resource from the system
  65. //!use remove().
  66. ~named_upgradable_mutex();
  67. //Exclusive locking
  68. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  69. //! and if another thread has exclusive, sharable or upgradable ownership of
  70. //! the mutex, it waits until it can obtain the ownership.
  71. //!Throws: interprocess_exception on error.
  72. void lock();
  73. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  74. //! without waiting. If no other thread has exclusive, sharable or upgradable
  75. //! ownership of the mutex this succeeds.
  76. //!Returns: If it can acquire exclusive ownership immediately returns true.
  77. //! If it has to wait, returns false.
  78. //!Throws: interprocess_exception on error.
  79. bool try_lock();
  80. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  81. //! waiting if necessary until no other thread has exclusive, sharable or
  82. //! upgradable ownership of the mutex or abs_time is reached.
  83. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  84. //!Throws: interprocess_exception on error.
  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. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  92. //! and if another thread has exclusive ownership of the mutex,
  93. //! waits until it can obtain the ownership.
  94. //!Throws: interprocess_exception on error.
  95. void lock_sharable();
  96. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  97. //! without waiting. If no other thread has exclusive ownership
  98. //! of the mutex this succeeds.
  99. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  100. //! has to wait, returns false.
  101. //!Throws: interprocess_exception on error.
  102. bool try_lock_sharable();
  103. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  104. //! waiting if necessary until no other thread has exclusive
  105. //! ownership of the mutex or abs_time is reached.
  106. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  107. //!Throws: interprocess_exception on error.
  108. bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  109. //!Precondition: The thread must have sharable ownership of the mutex.
  110. //!Effects: The calling thread releases the sharable ownership of the mutex.
  111. //!Throws: An exception derived from interprocess_exception on error.
  112. void unlock_sharable();
  113. //Upgradable locking
  114. //!Effects: The calling thread tries to obtain upgradable ownership of the mutex,
  115. //! and if another thread has exclusive or upgradable ownership of the mutex,
  116. //! waits until it can obtain the ownership.
  117. //!Throws: interprocess_exception on error.
  118. void lock_upgradable();
  119. //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
  120. //! without waiting. If no other thread has exclusive or upgradable ownership
  121. //! of the mutex this succeeds.
  122. //!Returns: If it can acquire upgradable ownership immediately returns true.
  123. //! If it has to wait, returns false.
  124. //!Throws: interprocess_exception on error.
  125. bool try_lock_upgradable();
  126. //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
  127. //! waiting if necessary until no other thread has exclusive or upgradable
  128. //! ownership of the mutex or abs_time is reached.
  129. //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false.
  130. //!Throws: interprocess_exception on error.
  131. bool timed_lock_upgradable(const boost::posix_time::ptime &abs_time);
  132. //!Precondition: The thread must have upgradable ownership of the mutex.
  133. //!Effects: The calling thread releases the upgradable ownership of the mutex.
  134. //!Throws: An exception derived from interprocess_exception on error.
  135. void unlock_upgradable();
  136. //Demotions
  137. //!Precondition: The thread must have exclusive ownership of the mutex.
  138. //!Effects: The thread atomically releases exclusive ownership and acquires
  139. //! upgradable ownership. This operation is non-blocking.
  140. //!Throws: An exception derived from interprocess_exception on error.
  141. void unlock_and_lock_upgradable();
  142. //!Precondition: The thread must have exclusive ownership of the mutex.
  143. //!Effects: The thread atomically releases exclusive ownership and acquires
  144. //! sharable ownership. This operation is non-blocking.
  145. //!Throws: An exception derived from interprocess_exception on error.
  146. void unlock_and_lock_sharable();
  147. //!Precondition: The thread must have upgradable ownership of the mutex.
  148. //!Effects: The thread atomically releases upgradable ownership and acquires
  149. //! sharable ownership. This operation is non-blocking.
  150. //!Throws: An exception derived from interprocess_exception on error.
  151. void unlock_upgradable_and_lock_sharable();
  152. //Promotions
  153. //!Precondition: The thread must have upgradable ownership of the mutex.
  154. //!Effects: The thread atomically releases upgradable ownership and acquires
  155. //! exclusive ownership. This operation will block until all threads with
  156. //! sharable ownership release it.
  157. //!Throws: An exception derived from interprocess_exception on error.
  158. void unlock_upgradable_and_lock();
  159. //!Precondition: The thread must have upgradable ownership of the mutex.
  160. //!Effects: The thread atomically releases upgradable ownership and tries to
  161. //! acquire exclusive ownership. This operation will fail if there are threads
  162. //! with sharable ownership, but it will maintain upgradable ownership.
  163. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  164. //!Throws: An exception derived from interprocess_exception on error.
  165. bool try_unlock_upgradable_and_lock();
  166. //!Precondition: The thread must have upgradable ownership of the mutex.
  167. //!Effects: The thread atomically releases upgradable ownership and tries to acquire
  168. //! exclusive ownership, waiting if necessary until abs_time. This operation will
  169. //! fail if there are threads with sharable ownership or timeout reaches, but it
  170. //! will maintain upgradable ownership.
  171. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  172. //!Throws: An exception derived from interprocess_exception on error.
  173. bool timed_unlock_upgradable_and_lock(const boost::posix_time::ptime &abs_time);
  174. //!Precondition: The thread must have sharable ownership of the mutex.
  175. //!Effects: The thread atomically releases sharable ownership and tries to acquire
  176. //! exclusive ownership. This operation will fail if there are threads with sharable
  177. //! or upgradable ownership, but it will maintain sharable ownership.
  178. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  179. //!Throws: An exception derived from interprocess_exception on error.
  180. bool try_unlock_sharable_and_lock();
  181. bool try_unlock_sharable_and_lock_upgradable();
  182. //!Erases a named upgradable mutex from the system.
  183. //!Returns false on error. Never throws.
  184. static bool remove(const char *name);
  185. /// @cond
  186. private:
  187. friend class ipcdetail::interprocess_tester;
  188. void dont_close_on_destruction();
  189. interprocess_upgradable_mutex *mutex() const
  190. { return static_cast<interprocess_upgradable_mutex*>(m_shmem.get_user_address()); }
  191. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  192. open_create_impl_t m_shmem;
  193. typedef ipcdetail::named_creation_functor<interprocess_upgradable_mutex> construct_func_t;
  194. /// @endcond
  195. };
  196. /// @cond
  197. inline named_upgradable_mutex::~named_upgradable_mutex()
  198. {}
  199. inline named_upgradable_mutex::named_upgradable_mutex
  200. (create_only_t, const char *name, const permissions &perm)
  201. : m_shmem (create_only
  202. ,name
  203. ,sizeof(interprocess_upgradable_mutex) +
  204. open_create_impl_t::ManagedOpenOrCreateUserOffset
  205. ,read_write
  206. ,0
  207. ,construct_func_t(ipcdetail::DoCreate)
  208. ,perm)
  209. {}
  210. inline named_upgradable_mutex::named_upgradable_mutex
  211. (open_or_create_t, const char *name, const permissions &perm)
  212. : m_shmem (open_or_create
  213. ,name
  214. ,sizeof(interprocess_upgradable_mutex) +
  215. open_create_impl_t::ManagedOpenOrCreateUserOffset
  216. ,read_write
  217. ,0
  218. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  219. ,perm)
  220. {}
  221. inline named_upgradable_mutex::named_upgradable_mutex
  222. (open_only_t, const char *name)
  223. : m_shmem (open_only
  224. ,name
  225. ,read_write
  226. ,0
  227. ,construct_func_t(ipcdetail::DoOpen))
  228. {}
  229. inline void named_upgradable_mutex::dont_close_on_destruction()
  230. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  231. inline void named_upgradable_mutex::lock()
  232. { this->mutex()->lock(); }
  233. inline void named_upgradable_mutex::unlock()
  234. { this->mutex()->unlock(); }
  235. inline bool named_upgradable_mutex::try_lock()
  236. { return this->mutex()->try_lock(); }
  237. inline bool named_upgradable_mutex::timed_lock
  238. (const boost::posix_time::ptime &abs_time)
  239. {
  240. if(abs_time == boost::posix_time::pos_infin){
  241. this->lock();
  242. return true;
  243. }
  244. return this->mutex()->timed_lock(abs_time);
  245. }
  246. inline void named_upgradable_mutex::lock_upgradable()
  247. { this->mutex()->lock_upgradable(); }
  248. inline void named_upgradable_mutex::unlock_upgradable()
  249. { this->mutex()->unlock_upgradable(); }
  250. inline bool named_upgradable_mutex::try_lock_upgradable()
  251. { return this->mutex()->try_lock_upgradable(); }
  252. inline bool named_upgradable_mutex::timed_lock_upgradable
  253. (const boost::posix_time::ptime &abs_time)
  254. {
  255. if(abs_time == boost::posix_time::pos_infin){
  256. this->lock_upgradable();
  257. return true;
  258. }
  259. return this->mutex()->timed_lock_upgradable(abs_time);
  260. }
  261. inline void named_upgradable_mutex::lock_sharable()
  262. { this->mutex()->lock_sharable(); }
  263. inline void named_upgradable_mutex::unlock_sharable()
  264. { this->mutex()->unlock_sharable(); }
  265. inline bool named_upgradable_mutex::try_lock_sharable()
  266. { return this->mutex()->try_lock_sharable(); }
  267. inline bool named_upgradable_mutex::timed_lock_sharable
  268. (const boost::posix_time::ptime &abs_time)
  269. {
  270. if(abs_time == boost::posix_time::pos_infin){
  271. this->lock_sharable();
  272. return true;
  273. }
  274. return this->mutex()->timed_lock_sharable(abs_time);
  275. }
  276. inline void named_upgradable_mutex::unlock_and_lock_upgradable()
  277. { this->mutex()->unlock_and_lock_upgradable(); }
  278. inline void named_upgradable_mutex::unlock_and_lock_sharable()
  279. { this->mutex()->unlock_and_lock_sharable(); }
  280. inline void named_upgradable_mutex::unlock_upgradable_and_lock_sharable()
  281. { this->mutex()->unlock_upgradable_and_lock_sharable(); }
  282. inline void named_upgradable_mutex::unlock_upgradable_and_lock()
  283. { this->mutex()->unlock_upgradable_and_lock(); }
  284. inline bool named_upgradable_mutex::try_unlock_upgradable_and_lock()
  285. { return this->mutex()->try_unlock_upgradable_and_lock(); }
  286. inline bool named_upgradable_mutex::timed_unlock_upgradable_and_lock
  287. (const boost::posix_time::ptime &abs_time)
  288. { return this->mutex()->timed_unlock_upgradable_and_lock(abs_time); }
  289. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock()
  290. { return this->mutex()->try_unlock_sharable_and_lock(); }
  291. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock_upgradable()
  292. { return this->mutex()->try_unlock_sharable_and_lock_upgradable(); }
  293. inline bool named_upgradable_mutex::remove(const char *name)
  294. { return shared_memory_object::remove(name); }
  295. /// @endcond
  296. } //namespace interprocess {
  297. } //namespace boost {
  298. #include <boost/interprocess/detail/config_end.hpp>
  299. #endif //BOOST_INTERPROCESS_NAMED_UPGRADABLE_MUTEX_HPP