named_upgradable_mutex.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.
  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. //!Precondition: The thread must have sharable ownership of the mutex.
  182. //!Effects: The thread atomically releases sharable ownership and tries to acquire
  183. //! upgradable ownership. This operation will fail if there are threads with sharable
  184. //! or upgradable ownership, but it will maintain sharable ownership.
  185. //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false.
  186. //!Throws: An exception derived from interprocess_exception on error.
  187. bool try_unlock_sharable_and_lock_upgradable();
  188. //!Erases a named upgradable mutex from the system.
  189. //!Returns false on error. Never throws.
  190. static bool remove(const char *name);
  191. /// @cond
  192. private:
  193. friend class ipcdetail::interprocess_tester;
  194. void dont_close_on_destruction();
  195. interprocess_upgradable_mutex *mutex() const
  196. { return static_cast<interprocess_upgradable_mutex*>(m_shmem.get_user_address()); }
  197. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  198. open_create_impl_t m_shmem;
  199. typedef ipcdetail::named_creation_functor<interprocess_upgradable_mutex> construct_func_t;
  200. /// @endcond
  201. };
  202. /// @cond
  203. inline named_upgradable_mutex::~named_upgradable_mutex()
  204. {}
  205. inline named_upgradable_mutex::named_upgradable_mutex
  206. (create_only_t, const char *name, const permissions &perm)
  207. : m_shmem (create_only
  208. ,name
  209. ,sizeof(interprocess_upgradable_mutex) +
  210. open_create_impl_t::ManagedOpenOrCreateUserOffset
  211. ,read_write
  212. ,0
  213. ,construct_func_t(ipcdetail::DoCreate)
  214. ,perm)
  215. {}
  216. inline named_upgradable_mutex::named_upgradable_mutex
  217. (open_or_create_t, const char *name, const permissions &perm)
  218. : m_shmem (open_or_create
  219. ,name
  220. ,sizeof(interprocess_upgradable_mutex) +
  221. open_create_impl_t::ManagedOpenOrCreateUserOffset
  222. ,read_write
  223. ,0
  224. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  225. ,perm)
  226. {}
  227. inline named_upgradable_mutex::named_upgradable_mutex
  228. (open_only_t, const char *name)
  229. : m_shmem (open_only
  230. ,name
  231. ,read_write
  232. ,0
  233. ,construct_func_t(ipcdetail::DoOpen))
  234. {}
  235. inline void named_upgradable_mutex::dont_close_on_destruction()
  236. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  237. inline void named_upgradable_mutex::lock()
  238. { this->mutex()->lock(); }
  239. inline void named_upgradable_mutex::unlock()
  240. { this->mutex()->unlock(); }
  241. inline bool named_upgradable_mutex::try_lock()
  242. { return this->mutex()->try_lock(); }
  243. inline bool named_upgradable_mutex::timed_lock
  244. (const boost::posix_time::ptime &abs_time)
  245. { return this->mutex()->timed_lock(abs_time); }
  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. { return this->mutex()->timed_lock_upgradable(abs_time); }
  255. inline void named_upgradable_mutex::lock_sharable()
  256. { this->mutex()->lock_sharable(); }
  257. inline void named_upgradable_mutex::unlock_sharable()
  258. { this->mutex()->unlock_sharable(); }
  259. inline bool named_upgradable_mutex::try_lock_sharable()
  260. { return this->mutex()->try_lock_sharable(); }
  261. inline bool named_upgradable_mutex::timed_lock_sharable
  262. (const boost::posix_time::ptime &abs_time)
  263. { return this->mutex()->timed_lock_sharable(abs_time); }
  264. inline void named_upgradable_mutex::unlock_and_lock_upgradable()
  265. { this->mutex()->unlock_and_lock_upgradable(); }
  266. inline void named_upgradable_mutex::unlock_and_lock_sharable()
  267. { this->mutex()->unlock_and_lock_sharable(); }
  268. inline void named_upgradable_mutex::unlock_upgradable_and_lock_sharable()
  269. { this->mutex()->unlock_upgradable_and_lock_sharable(); }
  270. inline void named_upgradable_mutex::unlock_upgradable_and_lock()
  271. { this->mutex()->unlock_upgradable_and_lock(); }
  272. inline bool named_upgradable_mutex::try_unlock_upgradable_and_lock()
  273. { return this->mutex()->try_unlock_upgradable_and_lock(); }
  274. inline bool named_upgradable_mutex::timed_unlock_upgradable_and_lock
  275. (const boost::posix_time::ptime &abs_time)
  276. { return this->mutex()->timed_unlock_upgradable_and_lock(abs_time); }
  277. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock()
  278. { return this->mutex()->try_unlock_sharable_and_lock(); }
  279. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock_upgradable()
  280. { return this->mutex()->try_unlock_sharable_and_lock_upgradable(); }
  281. inline bool named_upgradable_mutex::remove(const char *name)
  282. { return shared_memory_object::remove(name); }
  283. /// @endcond
  284. } //namespace interprocess {
  285. } //namespace boost {
  286. #include <boost/interprocess/detail/config_end.hpp>
  287. #endif //BOOST_INTERPROCESS_named_upgradable_mutex_HPP