named_upgradable_mutex.hpp 15 KB

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