interprocess_condition.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_CONDITION_HPP
  11. #define BOOST_INTERPROCESS_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. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  20. #include <boost/interprocess/detail/config_begin.hpp>
  21. #include <boost/interprocess/detail/workaround.hpp>
  22. #include <boost/interprocess/sync/cv_status.hpp>
  23. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  24. #include <boost/interprocess/sync/detail/locks.hpp>
  25. #include <boost/interprocess/timed_utils.hpp>
  26. #include <boost/interprocess/exceptions.hpp>
  27. #include <boost/limits.hpp>
  28. #include <boost/assert.hpp>
  29. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  30. #include <boost/interprocess/sync/posix/condition.hpp>
  31. #define BOOST_INTERPROCESS_CONDITION_USE_POSIX
  32. //Experimental...
  33. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  34. #include <boost/interprocess/sync/windows/condition.hpp>
  35. #define BOOST_INTERPROCESS_CONDITION_USE_WINAPI
  36. #else
  37. //spin_condition is used
  38. #include <boost/interprocess/sync/spin/condition.hpp>
  39. #endif
  40. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  41. //!\file
  42. //!Describes process-shared variables interprocess_condition class
  43. namespace boost {
  44. namespace interprocess {
  45. class named_condition;
  46. //!This class is a condition variable that can be placed in shared memory or
  47. //!memory mapped files.
  48. //!Destroys the object of type std::condition_variable_any
  49. //!
  50. //!Unlike std::condition_variable in C++11, it is NOT safe to invoke the destructor if all
  51. //!threads have been only notified. It is required that they have exited their respective wait
  52. //!functions.
  53. class interprocess_condition
  54. {
  55. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  56. //Non-copyable
  57. interprocess_condition(const interprocess_condition &);
  58. interprocess_condition &operator=(const interprocess_condition &);
  59. friend class named_condition;
  60. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  61. public:
  62. //!Constructs a interprocess_condition. On error throws interprocess_exception.
  63. interprocess_condition()
  64. {}
  65. //!Destroys *this
  66. //!liberating system resources.
  67. ~interprocess_condition()
  68. {}
  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. { m_condition.notify_one(); }
  73. //!Change the state of all threads waiting on *this to ready.
  74. //!If there are no waiting threads, notify_all() has no effect.
  75. void notify_all()
  76. { m_condition.notify_all(); }
  77. //!Releases the lock on the interprocess_mutex object associated with lock, blocks
  78. //!the current thread of execution until readied by a call to
  79. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  80. template <typename L>
  81. void wait(L& lock)
  82. {
  83. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  84. m_condition.wait(internal_lock);
  85. }
  86. //!The same as:
  87. //!while (!pred()) wait(lock)
  88. template <typename L, typename Pr>
  89. void wait(L& lock, Pr pred)
  90. {
  91. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  92. m_condition.wait(internal_lock, pred);
  93. }
  94. //!Releases the lock on the interprocess_mutex object associated with lock, blocks
  95. //!the current thread of execution until readied by a call to
  96. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  97. //!and then reacquires the lock.
  98. //!Returns: false if time abs_time is reached, otherwise true.
  99. template <typename L, class TimePoint>
  100. bool timed_wait(L& lock, const TimePoint &abs_time)
  101. {
  102. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  103. return m_condition.timed_wait(internal_lock, abs_time);
  104. }
  105. //!The same as: while (!pred()) {
  106. //! if (!timed_wait(lock, abs_time)) return pred();
  107. //! } return true;
  108. template <typename L, class TimePoint, typename Pr>
  109. bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
  110. {
  111. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  112. return m_condition.timed_wait(internal_lock, abs_time, pred);
  113. }
  114. //!Same as `timed_wait`, but this function is modeled after the
  115. //!standard library interface.
  116. template <typename L, class TimePoint>
  117. cv_status wait_until(L& lock, const TimePoint &abs_time)
  118. { return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
  119. //!Same as `timed_wait`, but this function is modeled after the
  120. //!standard library interface.
  121. template <typename L, class TimePoint, typename Pr>
  122. bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
  123. { return this->timed_wait(lock, abs_time, pred); }
  124. //!Same as `timed_wait`, but this function is modeled after the
  125. //!standard library interface and uses relative timeouts.
  126. template <typename L, class Duration>
  127. cv_status wait_for(L& lock, const Duration &dur)
  128. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
  129. //!Same as `timed_wait`, but this function is modeled after the
  130. //!standard library interface and uses relative timeouts
  131. template <typename L, class Duration, typename Pr>
  132. bool wait_for(L& lock, const Duration &dur, Pr pred)
  133. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
  134. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  135. private:
  136. #if defined(BOOST_INTERPROCESS_CONDITION_USE_POSIX)
  137. ipcdetail::posix_condition m_condition;
  138. #elif defined(BOOST_INTERPROCESS_CONDITION_USE_WINAPI)
  139. ipcdetail::winapi_condition m_condition;
  140. #else
  141. ipcdetail::spin_condition m_condition;
  142. #endif
  143. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  144. };
  145. } //namespace interprocess
  146. } // namespace boost
  147. #include <boost/interprocess/detail/config_end.hpp>
  148. #endif // BOOST_INTERPROCESS_CONDITION_HPP