named_mutex.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/timed_utils.hpp>
  24. #include <boost/interprocess/detail/interprocess_tester.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, 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, 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, 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, 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, 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, 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. 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. //!Erases a named mutex from the system.
  141. //!Returns false on error. Never throws.
  142. static bool remove(const char *name);
  143. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  144. //!Erases a named mutex from the system.
  145. //!Returns false on error. Never throws.
  146. //!
  147. //!Note: This function is only available on operating systems with
  148. //! native wchar_t APIs (e.g. Windows).
  149. static bool remove(const wchar_t *name);
  150. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  151. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  152. private:
  153. friend class ipcdetail::interprocess_tester;
  154. void dont_close_on_destruction();
  155. public:
  156. #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USE_POSIX)
  157. typedef ipcdetail::posix_named_mutex internal_mutex_type;
  158. #elif defined(BOOST_INTERPROCESS_NAMED_MUTEX_USE_WINAPI)
  159. typedef ipcdetail::winapi_named_mutex internal_mutex_type;
  160. #else
  161. typedef ipcdetail::shm_named_mutex internal_mutex_type;
  162. #endif
  163. internal_mutex_type &internal_mutex()
  164. { return m_mut; }
  165. internal_mutex_type m_mut;
  166. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  167. };
  168. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  169. inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
  170. : m_mut(create_only_t(), name, perm)
  171. {}
  172. inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
  173. : m_mut(open_or_create_t(), name, perm)
  174. {}
  175. inline named_mutex::named_mutex(open_only_t, const char *name)
  176. : m_mut(open_only_t(), name)
  177. {}
  178. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  179. inline named_mutex::named_mutex(create_only_t, const wchar_t *name, const permissions &perm)
  180. : m_mut(create_only_t(), name, perm)
  181. {}
  182. inline named_mutex::named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
  183. : m_mut(open_or_create_t(), name, perm)
  184. {}
  185. inline named_mutex::named_mutex(open_only_t, const wchar_t *name)
  186. : m_mut(open_only_t(), name)
  187. {}
  188. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  189. inline void named_mutex::dont_close_on_destruction()
  190. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); }
  191. inline named_mutex::~named_mutex()
  192. {}
  193. inline void named_mutex::lock()
  194. { m_mut.lock(); }
  195. inline void named_mutex::unlock()
  196. { m_mut.unlock(); }
  197. inline bool named_mutex::try_lock()
  198. { return m_mut.try_lock(); }
  199. template<class TimePoint>
  200. inline bool named_mutex::timed_lock(const TimePoint &abs_time)
  201. { return m_mut.timed_lock(abs_time); }
  202. inline bool named_mutex::remove(const char *name)
  203. { return internal_mutex_type::remove(name); }
  204. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  205. inline bool named_mutex::remove(const wchar_t *name)
  206. { return internal_mutex_type::remove(name); }
  207. #endif
  208. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  209. } //namespace interprocess {
  210. } //namespace boost {
  211. #include <boost/interprocess/detail/config_end.hpp>
  212. #endif //BOOST_INTERPROCESS_NAMED_MUTEX_HPP