named_condition.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/creation_tags.hpp>
  18. #include <boost/interprocess/exceptions.hpp>
  19. #include <boost/interprocess/detail/interprocess_tester.hpp>
  20. #include <boost/interprocess/permissions.hpp>
  21. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  22. #include <boost/interprocess/sync/detail/locks.hpp>
  23. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  24. #include <boost/interprocess/sync/windows/named_condition.hpp>
  25. #define BOOST_INTERPROCESS_USE_WINDOWS
  26. #else
  27. #include <boost/interprocess/sync/shm/named_condition.hpp>
  28. #endif
  29. //!\file
  30. //!Describes a named condition class for inter-process synchronization
  31. namespace boost {
  32. namespace interprocess {
  33. /// @cond
  34. namespace ipcdetail{ class interprocess_tester; }
  35. /// @endcond
  36. //! A global condition variable that can be created by name.
  37. //! This condition variable is designed to work with named_mutex and
  38. //! can't be placed in shared memory or memory mapped files.
  39. class named_condition
  40. {
  41. /// @cond
  42. //Non-copyable
  43. named_condition();
  44. named_condition(const named_condition &);
  45. named_condition &operator=(const named_condition &);
  46. /// @endcond
  47. public:
  48. //!Creates a global condition with a name.
  49. //!If the condition can't be created throws interprocess_exception
  50. named_condition(create_only_t create_only, const char *name, const permissions &perm = permissions());
  51. //!Opens or creates a global condition with a name.
  52. //!If the condition is created, this call is equivalent to
  53. //!named_condition(create_only_t, ... )
  54. //!If the condition is already created, this call is equivalent
  55. //!named_condition(open_only_t, ... )
  56. //!Does not throw
  57. named_condition(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  58. //!Opens a global condition with a name if that condition is previously
  59. //!created. If it is not previously created this function throws
  60. //!interprocess_exception.
  61. named_condition(open_only_t open_only, const char *name);
  62. //!Destroys *this and indicates that the calling process is finished using
  63. //!the resource. The destructor function will deallocate
  64. //!any system resources allocated by the system for use by this process for
  65. //!this resource. The resource can still be opened again calling
  66. //!the open constructor overload. To erase the resource from the system
  67. //!use remove().
  68. ~named_condition();
  69. //!If there is a thread waiting on *this, change that
  70. //!thread's state to ready. Otherwise there is no effect.*/
  71. void notify_one();
  72. //!Change the state of all threads waiting on *this to ready.
  73. //!If there are no waiting threads, notify_all() has no effect.
  74. void notify_all();
  75. //!Releases the lock on the named_mutex object associated with lock, blocks
  76. //!the current thread of execution until readied by a call to
  77. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  78. template <typename L>
  79. void wait(L& lock);
  80. //!The same as:
  81. //!while (!pred()) wait(lock)
  82. template <typename L, typename Pr>
  83. void wait(L& lock, Pr pred);
  84. //!Releases the lock on the named_mutex object associated with lock, blocks
  85. //!the current thread of execution until readied by a call to
  86. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  87. //!and then reacquires the lock.
  88. //!Returns: false if time abs_time is reached, otherwise true.
  89. template <typename L>
  90. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time);
  91. //!The same as: while (!pred()) {
  92. //! if (!timed_wait(lock, abs_time)) return pred();
  93. //! } return true;
  94. template <typename L, typename Pr>
  95. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred);
  96. //!Erases a named condition from the system.
  97. //!Returns false on error. Never throws.
  98. static bool remove(const char *name);
  99. /// @cond
  100. private:
  101. #if defined(BOOST_INTERPROCESS_USE_WINDOWS)
  102. typedef ipcdetail::windows_named_condition condition_type;
  103. #else
  104. typedef ipcdetail::shm_named_condition condition_type;
  105. #endif
  106. condition_type m_cond;
  107. friend class ipcdetail::interprocess_tester;
  108. void dont_close_on_destruction()
  109. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); }
  110. /// @endcond
  111. };
  112. /// @cond
  113. inline named_condition::~named_condition()
  114. {}
  115. inline named_condition::named_condition(create_only_t, const char *name, const permissions &perm)
  116. : m_cond(create_only_t(), name, perm)
  117. {}
  118. inline named_condition::named_condition(open_or_create_t, const char *name, const permissions &perm)
  119. : m_cond(open_or_create_t(), name, perm)
  120. {}
  121. inline named_condition::named_condition(open_only_t, const char *name)
  122. : m_cond(open_only_t(), name)
  123. {}
  124. inline void named_condition::notify_one()
  125. { m_cond.notify_one(); }
  126. inline void named_condition::notify_all()
  127. { m_cond.notify_all(); }
  128. template <typename L>
  129. inline void named_condition::wait(L& lock)
  130. {
  131. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  132. m_cond.wait(internal_lock);
  133. }
  134. template <typename L, typename Pr>
  135. inline void named_condition::wait(L& lock, Pr pred)
  136. {
  137. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  138. m_cond.wait(internal_lock, pred);
  139. }
  140. template <typename L>
  141. inline bool named_condition::timed_wait
  142. (L& lock, const boost::posix_time::ptime &abs_time)
  143. {
  144. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  145. return m_cond.timed_wait(internal_lock, abs_time);
  146. }
  147. template <typename L, typename Pr>
  148. inline bool named_condition::timed_wait
  149. (L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  150. {
  151. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  152. return m_cond.timed_wait(internal_lock, abs_time, pred);
  153. }
  154. inline bool named_condition::remove(const char *name)
  155. {
  156. return condition_type::remove(name);
  157. }
  158. /// @endcond
  159. } //namespace interprocess
  160. } //namespace boost
  161. #include <boost/interprocess/detail/config_end.hpp>
  162. #endif // BOOST_INTERPROCESS_NAMED_CONDITION_HPP