named_recursive_mutex.hpp 9.7 KB

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