named_upgradable_mutex.hpp 16 KB

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