named_condition.hpp 11 KB

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