named_sharable_mutex.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/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 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 create_only, 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 open_or_create, 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 open_only, 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 create_only, 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 open_or_create, 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 open_only, 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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  132. //!Precondition: The thread must have exclusive ownership of the mutex.
  133. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  134. //!Throws: An exception derived from interprocess_exception on error.
  135. void unlock();
  136. //Sharable locking
  137. //!Requires: The calling thread does not own the mutex.
  138. //!
  139. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  140. //! and if another thread has exclusive ownership of the mutex,
  141. //! waits until it can obtain the ownership.
  142. //!Throws: interprocess_exception on error.
  143. //!
  144. //!Note: A program may deadlock if the thread that has ownership calls
  145. //! this function. If the implementation can detect the deadlock,
  146. //! an exception could be thrown
  147. void lock_sharable();
  148. //!Requires: The calling thread does not own the mutex.
  149. //!
  150. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  151. //! without waiting. If no other thread has exclusive ownership
  152. //! of the mutex this succeeds.
  153. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  154. //! has to wait, returns false.
  155. //!Throws: interprocess_exception on error.
  156. //!
  157. //!Note: A program may deadlock if the thread that has ownership calls
  158. //! this function. If the implementation can detect the deadlock,
  159. //! an exception could be thrown
  160. bool try_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. //! waiting if necessary until no other thread has exclusive
  165. //! ownership of the mutex or abs_time is reached.
  166. //!Returns: If acquires sharable ownership, returns true. Otherwise 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 timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  173. //!Precondition: The thread must have sharable ownership of the mutex.
  174. //!Effects: The calling thread releases the sharable ownership of the mutex.
  175. //!Throws: An exception derived from interprocess_exception on error.
  176. void unlock_sharable();
  177. //!Erases a named sharable mutex from the system.
  178. //!Returns false on error. Never throws.
  179. static bool remove(const char *name);
  180. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  181. //!Erases a named sharable mutex from the system.
  182. //!Returns false on error. Never throws.
  183. static bool remove(const wchar_t *name);
  184. #endif
  185. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  186. private:
  187. friend class ipcdetail::interprocess_tester;
  188. void dont_close_on_destruction();
  189. interprocess_sharable_mutex *mutex() const
  190. { return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
  191. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  192. open_create_impl_t m_shmem;
  193. typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
  194. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  195. };
  196. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  197. inline named_sharable_mutex::~named_sharable_mutex()
  198. {}
  199. inline named_sharable_mutex::named_sharable_mutex
  200. (create_only_t, const char *name, const permissions &perm)
  201. : m_shmem (create_only
  202. ,name
  203. ,sizeof(interprocess_sharable_mutex) +
  204. open_create_impl_t::ManagedOpenOrCreateUserOffset
  205. ,read_write
  206. ,0
  207. ,construct_func_t(ipcdetail::DoCreate)
  208. ,perm)
  209. {}
  210. inline named_sharable_mutex::named_sharable_mutex
  211. (open_or_create_t, const char *name, const permissions &perm)
  212. : m_shmem (open_or_create
  213. ,name
  214. ,sizeof(interprocess_sharable_mutex) +
  215. open_create_impl_t::ManagedOpenOrCreateUserOffset
  216. ,read_write
  217. ,0
  218. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  219. ,perm)
  220. {}
  221. inline named_sharable_mutex::named_sharable_mutex
  222. (open_only_t, const char *name)
  223. : m_shmem (open_only
  224. ,name
  225. ,read_write
  226. ,0
  227. ,construct_func_t(ipcdetail::DoOpen))
  228. {}
  229. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  230. inline named_sharable_mutex::named_sharable_mutex
  231. (create_only_t, const wchar_t *name, const permissions &perm)
  232. : m_shmem (create_only
  233. ,name
  234. ,sizeof(interprocess_sharable_mutex) +
  235. open_create_impl_t::ManagedOpenOrCreateUserOffset
  236. ,read_write
  237. ,0
  238. ,construct_func_t(ipcdetail::DoCreate)
  239. ,perm)
  240. {}
  241. inline named_sharable_mutex::named_sharable_mutex
  242. (open_or_create_t, const wchar_t *name, const permissions &perm)
  243. : m_shmem (open_or_create
  244. ,name
  245. ,sizeof(interprocess_sharable_mutex) +
  246. open_create_impl_t::ManagedOpenOrCreateUserOffset
  247. ,read_write
  248. ,0
  249. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  250. ,perm)
  251. {}
  252. inline named_sharable_mutex::named_sharable_mutex
  253. (open_only_t, const wchar_t *name)
  254. : m_shmem (open_only
  255. ,name
  256. ,read_write
  257. ,0
  258. ,construct_func_t(ipcdetail::DoOpen))
  259. {}
  260. #endif
  261. inline void named_sharable_mutex::dont_close_on_destruction()
  262. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  263. inline void named_sharable_mutex::lock()
  264. { this->mutex()->lock(); }
  265. inline void named_sharable_mutex::unlock()
  266. { this->mutex()->unlock(); }
  267. inline bool named_sharable_mutex::try_lock()
  268. { return this->mutex()->try_lock(); }
  269. inline bool named_sharable_mutex::timed_lock
  270. (const boost::posix_time::ptime &abs_time)
  271. { return this->mutex()->timed_lock(abs_time); }
  272. inline void named_sharable_mutex::lock_sharable()
  273. { this->mutex()->lock_sharable(); }
  274. inline void named_sharable_mutex::unlock_sharable()
  275. { this->mutex()->unlock_sharable(); }
  276. inline bool named_sharable_mutex::try_lock_sharable()
  277. { return this->mutex()->try_lock_sharable(); }
  278. inline bool named_sharable_mutex::timed_lock_sharable
  279. (const boost::posix_time::ptime &abs_time)
  280. { return this->mutex()->timed_lock_sharable(abs_time); }
  281. inline bool named_sharable_mutex::remove(const char *name)
  282. { return shared_memory_object::remove(name); }
  283. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  284. inline bool named_sharable_mutex::remove(const wchar_t *name)
  285. { return shared_memory_object::remove(name); }
  286. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  287. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  288. } //namespace interprocess {
  289. } //namespace boost {
  290. #include <boost/interprocess/detail/config_end.hpp>
  291. #endif //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP