named_sharable_mutex.hpp 15 KB

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