named_mutex.hpp 9.6 KB

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