named_condition.hpp 11 KB

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