named_mutex.hpp 9.8 KB

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