named_recursive_mutex.hpp 9.6 KB

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