named_sharable_mutex.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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_SHARABLE_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_NAMED_SHARABLE_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/timed_utils.hpp>
  25. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  26. #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
  27. #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
  28. #include <boost/interprocess/permissions.hpp>
  29. //!\file
  30. //!Describes a named sharable 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 sharable 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 sharable mutex.
  40. class named_sharable_mutex
  41. {
  42. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  43. //Non-copyable
  44. named_sharable_mutex();
  45. named_sharable_mutex(const named_sharable_mutex &);
  46. named_sharable_mutex &operator=(const named_sharable_mutex &);
  47. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  48. public:
  49. //!Creates a global sharable mutex with a name.
  50. //!If the sharable mutex can't be created throws interprocess_exception
  51. named_sharable_mutex(create_only_t, const char *name, const permissions &perm = permissions());
  52. //!Opens or creates a global sharable mutex with a name.
  53. //!If the sharable mutex is created, this call is equivalent to
  54. //!named_sharable_mutex(create_only_t, ...)
  55. //!If the sharable mutex is already created, this call is equivalent to
  56. //!named_sharable_mutex(open_only_t, ... ).
  57. named_sharable_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
  58. //!Opens a global sharable mutex with a name if that sharable mutex
  59. //!is previously.
  60. //!created. If it is not previously created this function throws
  61. //!interprocess_exception.
  62. named_sharable_mutex(open_only_t, const char *name);
  63. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  64. //!Creates a global sharable mutex with a name.
  65. //!If the sharable mutex can't be created throws interprocess_exception
  66. //!
  67. //!Note: This function is only available on operating systems with
  68. //! native wchar_t APIs (e.g. Windows).
  69. named_sharable_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
  70. //!Opens or creates a global sharable mutex with a name.
  71. //!If the sharable mutex is created, this call is equivalent to
  72. //!named_sharable_mutex(create_only_t, ...)
  73. //!If the sharable mutex is already created, this call is equivalent to
  74. //!named_sharable_mutex(open_only_t, ... ).
  75. //!
  76. //!Note: This function is only available on operating systems with
  77. //! native wchar_t APIs (e.g. Windows).
  78. named_sharable_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
  79. //!Opens a global sharable mutex with a name if that sharable mutex
  80. //!is previously.
  81. //!created. If it is not previously created this function throws
  82. //!interprocess_exception.
  83. //!
  84. //!Note: This function is only available on operating systems with
  85. //! native wchar_t APIs (e.g. Windows).
  86. named_sharable_mutex(open_only_t, const wchar_t *name);
  87. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  88. //!Destroys *this and indicates that the calling process is finished using
  89. //!the resource. The destructor function will deallocate
  90. //!any system resources allocated by the system for use by this process for
  91. //!this resource. The resource can still be opened again calling
  92. //!the open constructor overload. To erase the resource from the system
  93. //!use remove().
  94. ~named_sharable_mutex();
  95. //Exclusive locking
  96. //!Requires: The calling thread does not own the mutex.
  97. //!
  98. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  99. //! and if another thread has exclusive or sharable ownership of
  100. //! the mutex, it waits until it can obtain the ownership.
  101. //!Throws: interprocess_exception on error.
  102. //!
  103. //!Note: A program may deadlock if the thread that has ownership calls
  104. //! this function. If the implementation can detect the deadlock,
  105. //! an exception could be thrown.
  106. void lock();
  107. //!Requires: The calling thread does not own the mutex.
  108. //!
  109. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  110. //! without waiting. If no other thread has exclusive or sharable
  111. //! ownership of the mutex this succeeds.
  112. //!Returns: If it can acquire exclusive ownership immediately returns true.
  113. //! If it has to wait, returns false.
  114. //!Throws: interprocess_exception on error.
  115. //!
  116. //!Note: A program may deadlock if the thread that has ownership calls
  117. //! this function. If the implementation can detect the deadlock,
  118. //! an exception could be thrown.
  119. bool try_lock();
  120. //!Requires: The calling thread does not own the mutex.
  121. //!
  122. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  123. //! waiting if necessary until no other thread has exclusive, or sharable
  124. //! ownership of the mutex or abs_time is reached.
  125. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  126. //!Throws: interprocess_exception on error.
  127. //!
  128. //!Note: A program may deadlock if the thread that has ownership calls
  129. //! this function. If the implementation can detect the deadlock,
  130. //! an exception could be thrown.
  131. template<class TimePoint>
  132. bool timed_lock(const TimePoint &abs_time);
  133. //!Same as `timed_lock`, but this function is modeled after the
  134. //!standard library interface.
  135. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  136. { return this->timed_lock(abs_time); }
  137. //!Same as `timed_lock`, but this function is modeled after the
  138. //!standard library interface.
  139. template<class Duration> bool try_lock_for(const Duration &dur)
  140. { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
  141. //!Precondition: The thread must have exclusive ownership of the mutex.
  142. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  143. //!Throws: An exception derived from interprocess_exception on error.
  144. void unlock();
  145. //Sharable locking
  146. //!Requires: The calling thread does not own the mutex.
  147. //!
  148. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  149. //! and if another thread has exclusive ownership of the mutex,
  150. //! waits until it can obtain the ownership.
  151. //!Throws: interprocess_exception on error.
  152. //!
  153. //!Note: A program may deadlock if the thread that has ownership calls
  154. //! this function. If the implementation can detect the deadlock,
  155. //! an exception could be thrown
  156. void lock_sharable();
  157. //!Same as `lock_sharable` but with a std-compatible interface
  158. //!
  159. void lock_shared()
  160. { this->lock_sharable(); }
  161. //!Requires: The calling thread does not own the mutex.
  162. //!
  163. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  164. //! without waiting. If no other thread has exclusive ownership
  165. //! of the mutex this succeeds.
  166. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  167. //! has to wait, 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 try_lock_sharable();
  174. //!Same as `try_lock_sharable` but with a std-compatible interface
  175. //!
  176. bool try_lock_shared()
  177. { return this->try_lock_sharable(); }
  178. //!Requires: The calling thread does not own the mutex.
  179. //!
  180. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  181. //! waiting if necessary until no other thread has exclusive
  182. //! ownership of the mutex or abs_time is reached.
  183. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  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. template<class TimePoint>
  190. bool timed_lock_sharable(const TimePoint &abs_time);
  191. //!Same as `timed_lock_sharable`, but this function is modeled after the
  192. //!standard library interface.
  193. template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
  194. { return this->timed_lock_sharable(abs_time); }
  195. //!Same as `timed_lock_sharable`, but this function is modeled after the
  196. //!standard library interface.
  197. template<class Duration> bool try_lock_shared_for(const Duration &dur)
  198. { return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
  199. //!Precondition: The thread must have sharable ownership of the mutex.
  200. //!Effects: The calling thread releases the sharable ownership of the mutex.
  201. //!Throws: An exception derived from interprocess_exception on error.
  202. void unlock_sharable();
  203. //!Same as `unlock_sharable` but with a std-compatible interface
  204. //!
  205. void unlock_shared()
  206. { this->unlock_sharable(); }
  207. //!Erases a named sharable mutex from the system.
  208. //!Returns false on error. Never throws.
  209. static bool remove(const char *name);
  210. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  211. //!Erases a named sharable mutex from the system.
  212. //!Returns false on error. Never throws.
  213. static bool remove(const wchar_t *name);
  214. #endif
  215. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  216. private:
  217. friend class ipcdetail::interprocess_tester;
  218. void dont_close_on_destruction();
  219. interprocess_sharable_mutex *mutex() const
  220. { return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
  221. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  222. open_create_impl_t m_shmem;
  223. typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
  224. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  225. };
  226. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  227. inline named_sharable_mutex::~named_sharable_mutex()
  228. {}
  229. inline named_sharable_mutex::named_sharable_mutex
  230. (create_only_t, const char *name, const permissions &perm)
  231. : m_shmem (create_only
  232. ,name
  233. ,sizeof(interprocess_sharable_mutex) +
  234. open_create_impl_t::ManagedOpenOrCreateUserOffset
  235. ,read_write
  236. ,0
  237. ,construct_func_t(ipcdetail::DoCreate)
  238. ,perm)
  239. {}
  240. inline named_sharable_mutex::named_sharable_mutex
  241. (open_or_create_t, const char *name, const permissions &perm)
  242. : m_shmem (open_or_create
  243. ,name
  244. ,sizeof(interprocess_sharable_mutex) +
  245. open_create_impl_t::ManagedOpenOrCreateUserOffset
  246. ,read_write
  247. ,0
  248. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  249. ,perm)
  250. {}
  251. inline named_sharable_mutex::named_sharable_mutex
  252. (open_only_t, const char *name)
  253. : m_shmem (open_only
  254. ,name
  255. ,read_write
  256. ,0
  257. ,construct_func_t(ipcdetail::DoOpen))
  258. {}
  259. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  260. inline named_sharable_mutex::named_sharable_mutex
  261. (create_only_t, const wchar_t *name, const permissions &perm)
  262. : m_shmem (create_only
  263. ,name
  264. ,sizeof(interprocess_sharable_mutex) +
  265. open_create_impl_t::ManagedOpenOrCreateUserOffset
  266. ,read_write
  267. ,0
  268. ,construct_func_t(ipcdetail::DoCreate)
  269. ,perm)
  270. {}
  271. inline named_sharable_mutex::named_sharable_mutex
  272. (open_or_create_t, const wchar_t *name, const permissions &perm)
  273. : m_shmem (open_or_create
  274. ,name
  275. ,sizeof(interprocess_sharable_mutex) +
  276. open_create_impl_t::ManagedOpenOrCreateUserOffset
  277. ,read_write
  278. ,0
  279. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  280. ,perm)
  281. {}
  282. inline named_sharable_mutex::named_sharable_mutex
  283. (open_only_t, const wchar_t *name)
  284. : m_shmem (open_only
  285. ,name
  286. ,read_write
  287. ,0
  288. ,construct_func_t(ipcdetail::DoOpen))
  289. {}
  290. #endif
  291. inline void named_sharable_mutex::dont_close_on_destruction()
  292. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  293. inline void named_sharable_mutex::lock()
  294. { this->mutex()->lock(); }
  295. inline void named_sharable_mutex::unlock()
  296. { this->mutex()->unlock(); }
  297. inline bool named_sharable_mutex::try_lock()
  298. { return this->mutex()->try_lock(); }
  299. template<class TimePoint>
  300. inline bool named_sharable_mutex::timed_lock
  301. (const TimePoint &abs_time)
  302. { return this->mutex()->timed_lock(abs_time); }
  303. inline void named_sharable_mutex::lock_sharable()
  304. { this->mutex()->lock_sharable(); }
  305. inline void named_sharable_mutex::unlock_sharable()
  306. { this->mutex()->unlock_sharable(); }
  307. inline bool named_sharable_mutex::try_lock_sharable()
  308. { return this->mutex()->try_lock_sharable(); }
  309. template<class TimePoint>
  310. inline bool named_sharable_mutex::timed_lock_sharable
  311. (const TimePoint &abs_time)
  312. { return this->mutex()->timed_lock_sharable(abs_time); }
  313. inline bool named_sharable_mutex::remove(const char *name)
  314. { return shared_memory_object::remove(name); }
  315. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  316. inline bool named_sharable_mutex::remove(const wchar_t *name)
  317. { return shared_memory_object::remove(name); }
  318. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  319. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  320. } //namespace interprocess {
  321. } //namespace boost {
  322. #include <boost/interprocess/detail/config_end.hpp>
  323. #endif //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP