named_recursive_mutex.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/detail/posix_time_types_wrk.hpp>
  23. #include <boost/interprocess/permissions.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 create_only, 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 open_or_create, 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 open_only, 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 create_only, 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 open_or_create, 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 open_only, 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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  118. //!Erases a named recursive mutex
  119. //!from the system
  120. static bool remove(const char *name);
  121. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  122. //!Erases a named recursive mutex
  123. //!from the system
  124. //!
  125. //!Note: This function is only available on operating systems with
  126. //! native wchar_t APIs (e.g. Windows).
  127. static bool remove(const wchar_t *name);
  128. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  129. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  130. private:
  131. friend class ipcdetail::interprocess_tester;
  132. void dont_close_on_destruction();
  133. #if defined(BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_USE_WINAPI)
  134. typedef ipcdetail::winapi_named_recursive_mutex impl_t;
  135. #else
  136. typedef ipcdetail::shm_named_recursive_mutex impl_t;
  137. #endif
  138. impl_t m_mut;
  139. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  140. };
  141. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  142. inline named_recursive_mutex::~named_recursive_mutex()
  143. {}
  144. inline void named_recursive_mutex::dont_close_on_destruction()
  145. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); }
  146. inline named_recursive_mutex::named_recursive_mutex(create_only_t, const char *name, const permissions &perm)
  147. : m_mut (create_only, name, perm)
  148. {}
  149. inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm)
  150. : m_mut (open_or_create, name, perm)
  151. {}
  152. inline named_recursive_mutex::named_recursive_mutex(open_only_t, const char *name)
  153. : m_mut (open_only, name)
  154. {}
  155. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  156. inline named_recursive_mutex::named_recursive_mutex(create_only_t, const wchar_t *name, const permissions &perm)
  157. : m_mut (create_only, name, perm)
  158. {}
  159. inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
  160. : m_mut (open_or_create, name, perm)
  161. {}
  162. inline named_recursive_mutex::named_recursive_mutex(open_only_t, const wchar_t *name)
  163. : m_mut (open_only, name)
  164. {}
  165. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  166. inline void named_recursive_mutex::lock()
  167. { m_mut.lock(); }
  168. inline void named_recursive_mutex::unlock()
  169. { m_mut.unlock(); }
  170. inline bool named_recursive_mutex::try_lock()
  171. { return m_mut.try_lock(); }
  172. inline bool named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  173. { return m_mut.timed_lock(abs_time); }
  174. inline bool named_recursive_mutex::remove(const char *name)
  175. { return impl_t::remove(name); }
  176. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  177. inline bool named_recursive_mutex::remove(const wchar_t *name)
  178. { return impl_t::remove(name); }
  179. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  180. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  181. } //namespace interprocess {
  182. } //namespace boost {
  183. #include <boost/interprocess/detail/config_end.hpp>
  184. #endif //BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_HPP