named_recursive_mutex.hpp 9.9 KB

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