named_mutex.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_NAMED_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/detail/interprocess_tester.hpp>
  24. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  25. #include <boost/interprocess/permissions.hpp>
  26. #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  27. #include <boost/interprocess/sync/posix/named_mutex.hpp>
  28. #define BOOST_INTERPROCESS_NAMED_MUTEX_USE_POSIX
  29. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  30. #include <boost/interprocess/sync/windows/named_mutex.hpp>
  31. #define BOOST_INTERPROCESS_NAMED_MUTEX_USE_WINAPI
  32. #else
  33. #include <boost/interprocess/sync/shm/named_mutex.hpp>
  34. #endif
  35. //!\file
  36. //!Describes a named mutex class for inter-process synchronization
  37. namespace boost {
  38. namespace interprocess {
  39. class named_condition;
  40. //!A mutex with a global name, so it can be found from different
  41. //!processes. This mutex can't be placed in shared memory, and
  42. //!each process should have it's own named_mutex.
  43. class named_mutex
  44. {
  45. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  46. //Non-copyable
  47. named_mutex();
  48. named_mutex(const named_mutex &);
  49. named_mutex &operator=(const named_mutex &);
  50. friend class named_condition;
  51. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  52. public:
  53. //!Creates a global mutex with a name.
  54. //!Throws interprocess_exception on error.
  55. named_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
  56. //!Opens or creates a global mutex with a name.
  57. //!If the mutex is created, this call is equivalent to
  58. //!named_mutex(create_only_t, ... )
  59. //!If the mutex is already created, this call is equivalent
  60. //!named_mutex(open_only_t, ... )
  61. //!Does not throw
  62. named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  63. //!Opens a global mutex with a name if that mutex is previously
  64. //!created. If it is not previously created this function throws
  65. //!interprocess_exception.
  66. named_mutex(open_only_t open_only, const char *name);
  67. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  68. //!Creates a global mutex with a name.
  69. //!Throws interprocess_exception on error.
  70. //!
  71. //!Note: This function is only available on operating systems with
  72. //! native wchar_t APIs (e.g. Windows).
  73. named_mutex(create_only_t create_only, const wchar_t *name, const permissions &perm = permissions());
  74. //!Opens or creates a global mutex with a name.
  75. //!If the mutex is created, this call is equivalent to
  76. //!named_mutex(create_only_t, ... )
  77. //!If the mutex is already created, this call is equivalent
  78. //!named_mutex(open_only_t, ... )
  79. //!Does not throw
  80. //!
  81. //!Note: This function is only available on operating systems with
  82. //! native wchar_t APIs (e.g. Windows).
  83. named_mutex(open_or_create_t open_or_create, const wchar_t *name, const permissions &perm = permissions());
  84. //!Opens a global mutex with a name if that mutex is previously
  85. //!created. If it is not previously created this function throws
  86. //!interprocess_exception.
  87. //!
  88. //!Note: This function is only available on operating systems with
  89. //! native wchar_t APIs (e.g. Windows).
  90. named_mutex(open_only_t open_only, const wchar_t *name);
  91. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  92. //!Destroys *this and indicates that the calling process is finished using
  93. //!the resource. The destructor function will deallocate
  94. //!any system resources allocated by the system for use by this process for
  95. //!this resource. The resource can still be opened again calling
  96. //!the open constructor overload. To erase the resource from the system
  97. //!use remove().
  98. ~named_mutex();
  99. //!Unlocks a previously locked
  100. //!mutex.
  101. void unlock();
  102. //!Requires: The calling thread does not own the mutex.
  103. //!
  104. //!Locks the mutex, sleeps when the mutex is already locked.
  105. //!Throws interprocess_exception if a severe error is found
  106. //!
  107. //!Note: A program may deadlock if the thread that has ownership calls
  108. //! this function. If the implementation can detect the deadlock,
  109. //! an exception could be thrown.
  110. void lock();
  111. //!Requires: The calling thread does not own the mutex.
  112. //!
  113. //!Tries to lock the mutex, returns false when the mutex
  114. //!is already locked, returns true when success.
  115. //!Throws interprocess_exception if a severe error is found
  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. //!Tries to lock the the mutex until time abs_time,
  124. //!Returns false when timeout expires, returns true when locks.
  125. //!Throws interprocess_exception if a severe error is found
  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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  131. //!Erases a named mutex from the system.
  132. //!Returns false on error. Never throws.
  133. static bool remove(const char *name);
  134. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  135. //!Erases a named mutex from the system.
  136. //!Returns false on error. Never throws.
  137. //!
  138. //!Note: This function is only available on operating systems with
  139. //! native wchar_t APIs (e.g. Windows).
  140. static bool remove(const wchar_t *name);
  141. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  142. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  143. private:
  144. friend class ipcdetail::interprocess_tester;
  145. void dont_close_on_destruction();
  146. public:
  147. #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USE_POSIX)
  148. typedef ipcdetail::posix_named_mutex internal_mutex_type;
  149. #elif defined(BOOST_INTERPROCESS_NAMED_MUTEX_USE_WINAPI)
  150. typedef ipcdetail::winapi_named_mutex internal_mutex_type;
  151. #else
  152. typedef ipcdetail::shm_named_mutex internal_mutex_type;
  153. #endif
  154. internal_mutex_type &internal_mutex()
  155. { return m_mut; }
  156. internal_mutex_type m_mut;
  157. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  158. };
  159. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  160. inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
  161. : m_mut(create_only_t(), name, perm)
  162. {}
  163. inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
  164. : m_mut(open_or_create_t(), name, perm)
  165. {}
  166. inline named_mutex::named_mutex(open_only_t, const char *name)
  167. : m_mut(open_only_t(), name)
  168. {}
  169. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  170. inline named_mutex::named_mutex(create_only_t, const wchar_t *name, const permissions &perm)
  171. : m_mut(create_only_t(), name, perm)
  172. {}
  173. inline named_mutex::named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
  174. : m_mut(open_or_create_t(), name, perm)
  175. {}
  176. inline named_mutex::named_mutex(open_only_t, const wchar_t *name)
  177. : m_mut(open_only_t(), name)
  178. {}
  179. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  180. inline void named_mutex::dont_close_on_destruction()
  181. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); }
  182. inline named_mutex::~named_mutex()
  183. {}
  184. inline void named_mutex::lock()
  185. { m_mut.lock(); }
  186. inline void named_mutex::unlock()
  187. { m_mut.unlock(); }
  188. inline bool named_mutex::try_lock()
  189. { return m_mut.try_lock(); }
  190. inline bool named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  191. { return m_mut.timed_lock(abs_time); }
  192. inline bool named_mutex::remove(const char *name)
  193. { return internal_mutex_type::remove(name); }
  194. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  195. inline bool named_mutex::remove(const wchar_t *name)
  196. { return internal_mutex_type::remove(name); }
  197. #endif
  198. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  199. } //namespace interprocess {
  200. } //namespace boost {
  201. #include <boost/interprocess/detail/config_end.hpp>
  202. #endif //BOOST_INTERPROCESS_NAMED_MUTEX_HPP