named_upgradable_mutex.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  65. //!Creates a global upgradable mutex with a name.
  66. //!If the upgradable mutex can't be created throws interprocess_exception
  67. //!
  68. //!Note: This function is only available on operating systems with
  69. //! native wchar_t APIs (e.g. Windows).
  70. named_upgradable_mutex(create_only_t create_only, const wchar_t *name, const permissions &perm = permissions());
  71. //!Opens or creates a global upgradable mutex with a name.
  72. //!If the upgradable mutex is created, this call is equivalent to
  73. //!named_upgradable_mutex(create_only_t, ...)
  74. //!If the upgradable mutex is already created, this call is equivalent to
  75. //!named_upgradable_mutex(open_only_t, ... ).
  76. //!
  77. //!Note: This function is only available on operating systems with
  78. //! native wchar_t APIs (e.g. Windows).
  79. named_upgradable_mutex(open_or_create_t open_or_create, const wchar_t *name, const permissions &perm = permissions());
  80. //!Opens a global upgradable mutex with a name if that upgradable mutex
  81. //!is previously.
  82. //!created. If it is not previously created this function throws
  83. //!interprocess_exception.
  84. //!
  85. //!Note: This function is only available on operating systems with
  86. //! native wchar_t APIs (e.g. Windows).
  87. named_upgradable_mutex(open_only_t open_only, const wchar_t *name);
  88. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  89. //!Destroys *this and indicates that the calling process is finished using
  90. //!the resource. The destructor function will deallocate
  91. //!any system resources allocated by the system for use by this process for
  92. //!this resource. The resource can still be opened again calling
  93. //!the open constructor overload. To erase the resource from the system
  94. //!use remove().
  95. ~named_upgradable_mutex();
  96. //Exclusive locking
  97. //!Requires: The calling thread does not own the mutex.
  98. //!
  99. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  100. //! and if another thread has exclusive, sharable or upgradable ownership of
  101. //! the mutex, it waits until it can obtain the ownership.
  102. //!Throws: interprocess_exception on error.
  103. //!
  104. //!Note: A program may deadlock if the thread that has ownership calls
  105. //! this function. If the implementation can detect the deadlock,
  106. //! an exception could be thrown
  107. void lock();
  108. //!Requires: The calling thread does not own the mutex.
  109. //!
  110. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  111. //! without waiting. If no other thread has exclusive, sharable or upgradable
  112. //! ownership of the mutex this succeeds.
  113. //!Returns: If it can acquire exclusive ownership immediately returns true.
  114. //! If it has to wait, returns false.
  115. //!Throws: interprocess_exception on error.
  116. //!
  117. //!Note: A program may deadlock if the thread that has ownership calls
  118. //! this function. If the implementation can detect the deadlock,
  119. //! an exception could be thrown
  120. bool try_lock();
  121. //!Requires: The calling thread does not own the mutex.
  122. //!
  123. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  124. //! waiting if necessary until no other thread has exclusive, sharable or
  125. //! upgradable ownership of the mutex or abs_time is reached.
  126. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  127. //!Throws: interprocess_exception on error.
  128. //!
  129. //!Note: A program may deadlock if the thread that has ownership calls
  130. //! this function. If the implementation can detect the deadlock,
  131. //! an exception could be thrown
  132. bool timed_lock(const boost::posix_time::ptime &abs_time);
  133. //!Precondition: The thread must have exclusive ownership of the mutex.
  134. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  135. //!Throws: An exception derived from interprocess_exception on error.
  136. void unlock();
  137. //Sharable locking
  138. //!Requires: The calling thread does not own the mutex.
  139. //!
  140. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  141. //! and if another thread has exclusive ownership of the mutex,
  142. //! waits until it can obtain the ownership.
  143. //!Throws: interprocess_exception on error.
  144. //!
  145. //!Note: A program may deadlock if the thread that has ownership calls
  146. //! this function. If the implementation can detect the deadlock,
  147. //! an exception could be thrown
  148. void lock_sharable();
  149. //!Requires: The calling thread does not own the mutex.
  150. //!
  151. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  152. //! without waiting. If no other thread has exclusive ownership
  153. //! of the mutex this succeeds.
  154. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  155. //! has to wait, returns false.
  156. //!Throws: interprocess_exception on error.
  157. //!
  158. //!Note: A program may deadlock if the thread that has ownership calls
  159. //! this function. If the implementation can detect the deadlock,
  160. //! an exception could be thrown
  161. bool try_lock_sharable();
  162. //!Requires: The calling thread does not own the mutex.
  163. //!
  164. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  165. //! waiting if necessary until no other thread has exclusive
  166. //! ownership of the mutex or abs_time is reached.
  167. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  168. //!Throws: interprocess_exception on error.
  169. //!
  170. //!Note: A program may deadlock if the thread that has ownership calls
  171. //! this function. If the implementation can detect the deadlock,
  172. //! an exception could be thrown
  173. bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  174. //!Precondition: The thread must have sharable ownership of the mutex.
  175. //!Effects: The calling thread releases the sharable ownership of the mutex.
  176. //!Throws: An exception derived from interprocess_exception on error.
  177. void unlock_sharable();
  178. //Upgradable locking
  179. //!Requires: The calling thread does not own the mutex.
  180. //!
  181. //!Effects: The calling thread tries to obtain upgradable ownership of the mutex,
  182. //! and if another thread has exclusive or upgradable ownership of the mutex,
  183. //! waits until it can obtain the ownership.
  184. //!Throws: interprocess_exception on error.
  185. //!
  186. //!Note: A program may deadlock if the thread that has ownership calls
  187. //! this function. If the implementation can detect the deadlock,
  188. //! an exception could be thrown
  189. void lock_upgradable();
  190. //!Requires: The calling thread does not own the mutex.
  191. //!
  192. //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
  193. //! without waiting. If no other thread has exclusive or upgradable ownership
  194. //! of the mutex this succeeds.
  195. //!Returns: If it can acquire upgradable ownership immediately returns true.
  196. //! If it has to wait, returns false.
  197. //!Throws: interprocess_exception on error.
  198. //!
  199. //!Note: A program may deadlock if the thread that has ownership calls
  200. //! this function. If the implementation can detect the deadlock,
  201. //! an exception could be thrown
  202. bool try_lock_upgradable();
  203. //!Requires: The calling thread does not own the mutex.
  204. //!
  205. //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
  206. //! waiting if necessary until no other thread has exclusive or upgradable
  207. //! ownership of the mutex or abs_time is reached.
  208. //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false.
  209. //!Throws: interprocess_exception on error.
  210. //!
  211. //!Note: A program may deadlock if the thread that has ownership calls
  212. //! this function. If the implementation can detect the deadlock,
  213. //! an exception could be thrown
  214. bool timed_lock_upgradable(const boost::posix_time::ptime &abs_time);
  215. //!Precondition: The thread must have upgradable ownership of the mutex.
  216. //!Effects: The calling thread releases the upgradable ownership of the mutex.
  217. //!Throws: An exception derived from interprocess_exception on error.
  218. void unlock_upgradable();
  219. //Demotions
  220. //!Precondition: The thread must have exclusive ownership of the mutex.
  221. //!Effects: The thread atomically releases exclusive ownership and acquires
  222. //! upgradable ownership. This operation is non-blocking.
  223. //!Throws: An exception derived from interprocess_exception on error.
  224. void unlock_and_lock_upgradable();
  225. //!Precondition: The thread must have exclusive ownership of the mutex.
  226. //!Effects: The thread atomically releases exclusive ownership and acquires
  227. //! sharable ownership. This operation is non-blocking.
  228. //!Throws: An exception derived from interprocess_exception on error.
  229. void unlock_and_lock_sharable();
  230. //!Precondition: The thread must have upgradable ownership of the mutex.
  231. //!Effects: The thread atomically releases upgradable ownership and acquires
  232. //! sharable ownership. This operation is non-blocking.
  233. //!Throws: An exception derived from interprocess_exception on error.
  234. void unlock_upgradable_and_lock_sharable();
  235. //Promotions
  236. //!Precondition: The thread must have upgradable ownership of the mutex.
  237. //!Effects: The thread atomically releases upgradable ownership and acquires
  238. //! exclusive ownership. This operation will block until all threads with
  239. //! sharable ownership release it.
  240. //!Throws: An exception derived from interprocess_exception on error.
  241. void unlock_upgradable_and_lock();
  242. //!Precondition: The thread must have upgradable ownership of the mutex.
  243. //!Effects: The thread atomically releases upgradable ownership and tries to
  244. //! acquire exclusive ownership. This operation will fail if there are threads
  245. //! with sharable ownership, but it will maintain upgradable ownership.
  246. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  247. //!Throws: An exception derived from interprocess_exception on error.
  248. bool try_unlock_upgradable_and_lock();
  249. //!Precondition: The thread must have upgradable ownership of the mutex.
  250. //!Effects: The thread atomically releases upgradable ownership and tries to acquire
  251. //! exclusive ownership, waiting if necessary until abs_time. This operation will
  252. //! fail if there are threads with sharable ownership or timeout reaches, but it
  253. //! will maintain upgradable ownership.
  254. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  255. //!Throws: An exception derived from interprocess_exception on error.
  256. bool timed_unlock_upgradable_and_lock(const boost::posix_time::ptime &abs_time);
  257. //!Precondition: The thread must have sharable ownership of the mutex.
  258. //!Effects: The thread atomically releases sharable ownership and tries to acquire
  259. //! exclusive ownership. This operation will fail if there are threads with sharable
  260. //! or upgradable ownership, but it will maintain sharable ownership.
  261. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  262. //!Throws: An exception derived from interprocess_exception on error.
  263. bool try_unlock_sharable_and_lock();
  264. //!Precondition: The thread must have sharable ownership of the mutex.
  265. //!Effects: The thread atomically releases sharable ownership and tries to acquire
  266. //! upgradable ownership. This operation will fail if there are threads with sharable
  267. //! or upgradable ownership, but it will maintain sharable ownership.
  268. //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false.
  269. //!Throws: An exception derived from interprocess_exception on error.
  270. bool try_unlock_sharable_and_lock_upgradable();
  271. //!Erases a named upgradable mutex from the system.
  272. //!Returns false on error. Never throws.
  273. static bool remove(const char *name);
  274. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  275. //!Erases a named upgradable mutex from the system.
  276. //!Returns false on error. Never throws.
  277. //!
  278. //!Note: This function is only available on operating systems with
  279. //! native wchar_t APIs (e.g. Windows).
  280. static bool remove(const wchar_t *name);
  281. #endif
  282. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  283. private:
  284. friend class ipcdetail::interprocess_tester;
  285. void dont_close_on_destruction();
  286. interprocess_upgradable_mutex *mutex() const
  287. { return static_cast<interprocess_upgradable_mutex*>(m_shmem.get_user_address()); }
  288. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  289. open_create_impl_t m_shmem;
  290. typedef ipcdetail::named_creation_functor<interprocess_upgradable_mutex> construct_func_t;
  291. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  292. };
  293. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  294. inline named_upgradable_mutex::~named_upgradable_mutex()
  295. {}
  296. inline named_upgradable_mutex::named_upgradable_mutex
  297. (create_only_t, const char *name, const permissions &perm)
  298. : m_shmem (create_only
  299. ,name
  300. ,sizeof(interprocess_upgradable_mutex) +
  301. open_create_impl_t::ManagedOpenOrCreateUserOffset
  302. ,read_write
  303. ,0
  304. ,construct_func_t(ipcdetail::DoCreate)
  305. ,perm)
  306. {}
  307. inline named_upgradable_mutex::named_upgradable_mutex
  308. (open_or_create_t, const char *name, const permissions &perm)
  309. : m_shmem (open_or_create
  310. ,name
  311. ,sizeof(interprocess_upgradable_mutex) +
  312. open_create_impl_t::ManagedOpenOrCreateUserOffset
  313. ,read_write
  314. ,0
  315. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  316. ,perm)
  317. {}
  318. inline named_upgradable_mutex::named_upgradable_mutex
  319. (open_only_t, const char *name)
  320. : m_shmem (open_only
  321. ,name
  322. ,read_write
  323. ,0
  324. ,construct_func_t(ipcdetail::DoOpen))
  325. {}
  326. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  327. inline named_upgradable_mutex::named_upgradable_mutex
  328. (create_only_t, const wchar_t *name, const permissions &perm)
  329. : m_shmem (create_only
  330. ,name
  331. ,sizeof(interprocess_upgradable_mutex) +
  332. open_create_impl_t::ManagedOpenOrCreateUserOffset
  333. ,read_write
  334. ,0
  335. ,construct_func_t(ipcdetail::DoCreate)
  336. ,perm)
  337. {}
  338. inline named_upgradable_mutex::named_upgradable_mutex
  339. (open_or_create_t, const wchar_t *name, const permissions &perm)
  340. : m_shmem (open_or_create
  341. ,name
  342. ,sizeof(interprocess_upgradable_mutex) +
  343. open_create_impl_t::ManagedOpenOrCreateUserOffset
  344. ,read_write
  345. ,0
  346. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  347. ,perm)
  348. {}
  349. inline named_upgradable_mutex::named_upgradable_mutex
  350. (open_only_t, const wchar_t *name)
  351. : m_shmem (open_only
  352. ,name
  353. ,read_write
  354. ,0
  355. ,construct_func_t(ipcdetail::DoOpen))
  356. {}
  357. #endif
  358. inline void named_upgradable_mutex::dont_close_on_destruction()
  359. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  360. inline void named_upgradable_mutex::lock()
  361. { this->mutex()->lock(); }
  362. inline void named_upgradable_mutex::unlock()
  363. { this->mutex()->unlock(); }
  364. inline bool named_upgradable_mutex::try_lock()
  365. { return this->mutex()->try_lock(); }
  366. inline bool named_upgradable_mutex::timed_lock
  367. (const boost::posix_time::ptime &abs_time)
  368. { return this->mutex()->timed_lock(abs_time); }
  369. inline void named_upgradable_mutex::lock_upgradable()
  370. { this->mutex()->lock_upgradable(); }
  371. inline void named_upgradable_mutex::unlock_upgradable()
  372. { this->mutex()->unlock_upgradable(); }
  373. inline bool named_upgradable_mutex::try_lock_upgradable()
  374. { return this->mutex()->try_lock_upgradable(); }
  375. inline bool named_upgradable_mutex::timed_lock_upgradable
  376. (const boost::posix_time::ptime &abs_time)
  377. { return this->mutex()->timed_lock_upgradable(abs_time); }
  378. inline void named_upgradable_mutex::lock_sharable()
  379. { this->mutex()->lock_sharable(); }
  380. inline void named_upgradable_mutex::unlock_sharable()
  381. { this->mutex()->unlock_sharable(); }
  382. inline bool named_upgradable_mutex::try_lock_sharable()
  383. { return this->mutex()->try_lock_sharable(); }
  384. inline bool named_upgradable_mutex::timed_lock_sharable
  385. (const boost::posix_time::ptime &abs_time)
  386. { return this->mutex()->timed_lock_sharable(abs_time); }
  387. inline void named_upgradable_mutex::unlock_and_lock_upgradable()
  388. { this->mutex()->unlock_and_lock_upgradable(); }
  389. inline void named_upgradable_mutex::unlock_and_lock_sharable()
  390. { this->mutex()->unlock_and_lock_sharable(); }
  391. inline void named_upgradable_mutex::unlock_upgradable_and_lock_sharable()
  392. { this->mutex()->unlock_upgradable_and_lock_sharable(); }
  393. inline void named_upgradable_mutex::unlock_upgradable_and_lock()
  394. { this->mutex()->unlock_upgradable_and_lock(); }
  395. inline bool named_upgradable_mutex::try_unlock_upgradable_and_lock()
  396. { return this->mutex()->try_unlock_upgradable_and_lock(); }
  397. inline bool named_upgradable_mutex::timed_unlock_upgradable_and_lock
  398. (const boost::posix_time::ptime &abs_time)
  399. { return this->mutex()->timed_unlock_upgradable_and_lock(abs_time); }
  400. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock()
  401. { return this->mutex()->try_unlock_sharable_and_lock(); }
  402. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock_upgradable()
  403. { return this->mutex()->try_unlock_sharable_and_lock_upgradable(); }
  404. inline bool named_upgradable_mutex::remove(const char *name)
  405. { return shared_memory_object::remove(name); }
  406. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  407. inline bool named_upgradable_mutex::remove(const wchar_t *name)
  408. { return shared_memory_object::remove(name); }
  409. #endif
  410. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  411. } //namespace interprocess {
  412. } //namespace boost {
  413. #include <boost/interprocess/detail/config_end.hpp>
  414. #endif //BOOST_INTERPROCESS_named_upgradable_mutex_HPP