named_condition_any.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_CONDITION_ANY_HPP
  11. #define BOOST_INTERPROCESS_NAMED_CONDITION_ANY_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/sync/cv_status.hpp>
  22. #include <boost/interprocess/creation_tags.hpp>
  23. #include <boost/interprocess/exceptions.hpp>
  24. #include <boost/interprocess/timed_utils.hpp>
  25. #include <boost/interprocess/detail/interprocess_tester.hpp>
  26. #include <boost/interprocess/permissions.hpp>
  27. #include <boost/interprocess/sync/detail/locks.hpp>
  28. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  29. #include <boost/interprocess/sync/windows/named_condition_any.hpp>
  30. #define BOOST_INTERPROCESS_NAMED_CONDITION_ANY_USE_WINAPI
  31. #else
  32. #include <boost/interprocess/sync/shm/named_condition_any.hpp>
  33. #endif
  34. //!\file
  35. //!Describes a named condition class for inter-process synchronization
  36. namespace boost {
  37. namespace interprocess {
  38. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  39. namespace ipcdetail{ class interprocess_tester; }
  40. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  41. //! A global condition variable that can be created by name.
  42. //! This condition variable is designed to work with named_mutex and
  43. //! can't be placed in shared memory or memory mapped files.
  44. class named_condition_any
  45. {
  46. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  47. //Non-copyable
  48. named_condition_any();
  49. named_condition_any(const named_condition_any &);
  50. named_condition_any &operator=(const named_condition_any &);
  51. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  52. public:
  53. //!Creates a global condition with a name.
  54. //!If the condition can't be created throws interprocess_exception
  55. named_condition_any(create_only_t, const char *name, const permissions &perm = permissions())
  56. : m_cond(create_only_t(), name, perm)
  57. {}
  58. //!Opens or creates a global condition with a name.
  59. //!If the condition is created, this call is equivalent to
  60. //!named_condition_any(create_only_t, ... )
  61. //!If the condition is already created, this call is equivalent
  62. //!named_condition_any(open_only_t, ... )
  63. //!Does not throw
  64. named_condition_any(open_or_create_t, const char *name, const permissions &perm = permissions())
  65. : m_cond(open_or_create_t(), name, perm)
  66. {}
  67. //!Opens a global condition with a name if that condition is previously
  68. //!created. If it is not previously created this function throws
  69. //!interprocess_exception.
  70. named_condition_any(open_only_t, const char *name)
  71. : m_cond(open_only_t(), name)
  72. {}
  73. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  74. //!Creates a global condition with a name.
  75. //!If the condition can't be created throws interprocess_exception
  76. //!
  77. //!Note: This function is only available on operating systems with
  78. //! native wchar_t APIs (e.g. Windows).
  79. named_condition_any(create_only_t, const wchar_t *name, const permissions &perm = permissions())
  80. : m_cond(create_only_t(), name, perm)
  81. {}
  82. //!Opens or creates a global condition with a name.
  83. //!If the condition is created, this call is equivalent to
  84. //!named_condition_any(create_only_t, ... )
  85. //!If the condition is already created, this call is equivalent
  86. //!named_condition_any(open_only_t, ... )
  87. //!Does not throw
  88. //!
  89. //!Note: This function is only available on operating systems with
  90. //! native wchar_t APIs (e.g. Windows).
  91. named_condition_any(open_or_create_t, const wchar_t *name, const permissions &perm = permissions())
  92. : m_cond(open_or_create_t(), name, perm)
  93. {}
  94. //!Opens a global condition with a name if that condition is previously
  95. //!created. If it is not previously created this function throws
  96. //!interprocess_exception.
  97. //!
  98. //!Note: This function is only available on operating systems with
  99. //! native wchar_t APIs (e.g. Windows).
  100. named_condition_any(open_only_t, const wchar_t *name)
  101. : m_cond(open_only_t(), name)
  102. {}
  103. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  104. //!Destroys *this and indicates that the calling process is finished using
  105. //!the resource. The destructor function will deallocate
  106. //!any system resources allocated by the system for use by this process for
  107. //!this resource. The resource can still be opened again calling
  108. //!the open constructor overload. To erase the resource from the system
  109. //!use remove().
  110. ~named_condition_any()
  111. {}
  112. //!If there is a thread waiting on *this, change that
  113. //!thread's state to ready. Otherwise there is no effect.*/
  114. void notify_one()
  115. { m_cond.notify_one(); }
  116. //!Change the state of all threads waiting on *this to ready.
  117. //!If there are no waiting threads, notify_all() has no effect.
  118. void notify_all()
  119. { m_cond.notify_all(); }
  120. //!Releases the lock on the named_mutex object associated with lock, blocks
  121. //!the current thread of execution until readied by a call to
  122. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  123. template <typename L>
  124. void wait(L& lock)
  125. { return m_cond.wait(lock); }
  126. //!The same as:
  127. //!while (!pred()) wait(lock)
  128. template <typename L, typename Pr>
  129. void wait(L& lock, Pr pred)
  130. { return m_cond.wait(lock, pred); }
  131. //!Releases the lock on the named_mutex object associated with lock, blocks
  132. //!the current thread of execution until readied by a call to
  133. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  134. //!and then reacquires the lock.
  135. //!Returns: false if time abs_time is reached, otherwise true.
  136. template <typename L, typename TimePoint>
  137. bool timed_wait(L& lock, const TimePoint &abs_time)
  138. { return m_cond.timed_wait(lock, abs_time); }
  139. //!The same as: while (!pred()) {
  140. //! if (!timed_wait(lock, abs_time)) return pred();
  141. //! } return true;
  142. template <typename L, typename TimePoint, typename Pr>
  143. bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
  144. { return m_cond.timed_wait(lock, abs_time, pred); }
  145. //!Same as `timed_wait`, but this function is modeled after the
  146. //!standard library interface.
  147. template <typename L, class TimePoint>
  148. cv_status wait_until(L& lock, const TimePoint &abs_time)
  149. { return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
  150. //!Same as `timed_wait`, but this function is modeled after the
  151. //!standard library interface.
  152. template <typename L, class TimePoint, typename Pr>
  153. bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
  154. { return this->timed_wait(lock, abs_time, pred); }
  155. //!Same as `timed_wait`, but this function is modeled after the
  156. //!standard library interface and uses relative timeouts.
  157. template <typename L, class Duration>
  158. cv_status wait_for(L& lock, const Duration &dur)
  159. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
  160. //!Same as `timed_wait`, but this function is modeled after the
  161. //!standard library interface and uses relative timeouts
  162. template <typename L, class Duration, typename Pr>
  163. bool wait_for(L& lock, const Duration &dur, Pr pred)
  164. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
  165. //!Erases a named condition from the system.
  166. //!Returns false on error. Never throws.
  167. static bool remove(const char *name)
  168. { return condition_any_type::remove(name); }
  169. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  170. //!Erases a named condition from the system.
  171. //!Returns false on error. Never throws.
  172. //!
  173. //!Note: This function is only available on operating systems with
  174. //! native wchar_t APIs (e.g. Windows).
  175. static bool remove(const wchar_t *name)
  176. { return condition_any_type::remove(name); }
  177. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  178. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  179. private:
  180. #if defined(BOOST_INTERPROCESS_NAMED_CONDITION_ANY_USE_WINAPI)
  181. typedef ipcdetail::winapi_named_condition_any condition_any_type;
  182. #else
  183. typedef ipcdetail::shm_named_condition_any condition_any_type;
  184. #endif
  185. condition_any_type m_cond;
  186. friend class ipcdetail::interprocess_tester;
  187. void dont_close_on_destruction()
  188. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); }
  189. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  190. };
  191. } //namespace interprocess
  192. } //namespace boost
  193. #include <boost/interprocess/detail/config_end.hpp>
  194. #endif // BOOST_INTERPROCESS_NAMED_CONDITION_ANY_HPP