interprocess_condition.hpp 6.4 KB

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