named_recursive_mutex.hpp 9.4 KB

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