named_condition_any.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/creation_tags.hpp>
  22. #include <boost/interprocess/exceptions.hpp>
  23. #include <boost/interprocess/detail/interprocess_tester.hpp>
  24. #include <boost/interprocess/permissions.hpp>
  25. #include <boost/interprocess/detail/posix_time_types_wrk.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_any.hpp>
  29. #define BOOST_INTERPROCESS_NAMED_CONDITION_ANY_USE_WINAPI
  30. #else
  31. #include <boost/interprocess/sync/shm/named_condition_any.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_any
  44. {
  45. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  46. //Non-copyable
  47. named_condition_any();
  48. named_condition_any(const named_condition_any &);
  49. named_condition_any &operator=(const named_condition_any &);
  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_any(create_only_t, const char *name, const permissions &perm = permissions())
  55. : m_cond(create_only_t(), name, perm)
  56. {}
  57. //!Opens or creates a global condition with a name.
  58. //!If the condition is created, this call is equivalent to
  59. //!named_condition_any(create_only_t, ... )
  60. //!If the condition is already created, this call is equivalent
  61. //!named_condition_any(open_only_t, ... )
  62. //!Does not throw
  63. named_condition_any(open_or_create_t, const char *name, const permissions &perm = permissions())
  64. : m_cond(open_or_create_t(), name, perm)
  65. {}
  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. named_condition_any(open_only_t, const char *name)
  70. : m_cond(open_only_t(), name)
  71. {}
  72. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  73. //!Creates a global condition with a name.
  74. //!If the condition can't be created throws interprocess_exception
  75. //!
  76. //!Note: This function is only available on operating systems with
  77. //! native wchar_t APIs (e.g. Windows).
  78. named_condition_any(create_only_t, const wchar_t *name, const permissions &perm = permissions())
  79. : m_cond(create_only_t(), name, perm)
  80. {}
  81. //!Opens or creates a global condition with a name.
  82. //!If the condition is created, this call is equivalent to
  83. //!named_condition_any(create_only_t, ... )
  84. //!If the condition is already created, this call is equivalent
  85. //!named_condition_any(open_only_t, ... )
  86. //!Does not throw
  87. //!
  88. //!Note: This function is only available on operating systems with
  89. //! native wchar_t APIs (e.g. Windows).
  90. named_condition_any(open_or_create_t, const wchar_t *name, const permissions &perm = permissions())
  91. : m_cond(open_or_create_t(), name, perm)
  92. {}
  93. //!Opens a global condition with a name if that condition is previously
  94. //!created. If it is not previously created this function throws
  95. //!interprocess_exception.
  96. //!
  97. //!Note: This function is only available on operating systems with
  98. //! native wchar_t APIs (e.g. Windows).
  99. named_condition_any(open_only_t, const wchar_t *name)
  100. : m_cond(open_only_t(), name)
  101. {}
  102. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  103. //!Destroys *this and indicates that the calling process is finished using
  104. //!the resource. The destructor function will deallocate
  105. //!any system resources allocated by the system for use by this process for
  106. //!this resource. The resource can still be opened again calling
  107. //!the open constructor overload. To erase the resource from the system
  108. //!use remove().
  109. ~named_condition_any()
  110. {}
  111. //!If there is a thread waiting on *this, change that
  112. //!thread's state to ready. Otherwise there is no effect.*/
  113. void notify_one()
  114. { m_cond.notify_one(); }
  115. //!Change the state of all threads waiting on *this to ready.
  116. //!If there are no waiting threads, notify_all() has no effect.
  117. void notify_all()
  118. { m_cond.notify_all(); }
  119. //!Releases the lock on the named_mutex object associated with lock, blocks
  120. //!the current thread of execution until readied by a call to
  121. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  122. template <typename L>
  123. void wait(L& lock)
  124. { return m_cond.wait(lock); }
  125. //!The same as:
  126. //!while (!pred()) wait(lock)
  127. template <typename L, typename Pr>
  128. void wait(L& lock, Pr pred)
  129. { return m_cond.wait(lock, pred); }
  130. //!Releases the lock on the named_mutex object associated with lock, blocks
  131. //!the current thread of execution until readied by a call to
  132. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  133. //!and then reacquires the lock.
  134. //!Returns: false if time abs_time is reached, otherwise true.
  135. template <typename L>
  136. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
  137. { return m_cond.timed_wait(lock, abs_time); }
  138. //!The same as: while (!pred()) {
  139. //! if (!timed_wait(lock, abs_time)) return pred();
  140. //! } return true;
  141. template <typename L, typename Pr>
  142. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  143. { return m_cond.timed_wait(lock, abs_time, pred); }
  144. //!Erases a named condition from the system.
  145. //!Returns false on error. Never throws.
  146. static bool remove(const char *name)
  147. { return condition_any_type::remove(name); }
  148. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  149. //!Erases a named condition from the system.
  150. //!Returns false on error. Never throws.
  151. //!
  152. //!Note: This function is only available on operating systems with
  153. //! native wchar_t APIs (e.g. Windows).
  154. static bool remove(const wchar_t *name)
  155. { return condition_any_type::remove(name); }
  156. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  157. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  158. private:
  159. #if defined(BOOST_INTERPROCESS_NAMED_CONDITION_ANY_USE_WINAPI)
  160. typedef ipcdetail::winapi_named_condition_any condition_any_type;
  161. #else
  162. typedef ipcdetail::shm_named_condition_any condition_any_type;
  163. #endif
  164. condition_any_type m_cond;
  165. friend class ipcdetail::interprocess_tester;
  166. void dont_close_on_destruction()
  167. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); }
  168. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  169. };
  170. } //namespace interprocess
  171. } //namespace boost
  172. #include <boost/interprocess/detail/config_end.hpp>
  173. #endif // BOOST_INTERPROCESS_NAMED_CONDITION_ANY_HPP