interprocess_condition_any.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-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_ANY_HPP
  11. #define BOOST_INTERPROCESS_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. #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/timed_utils.hpp>
  24. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  25. #include <boost/interprocess/sync/interprocess_condition.hpp>
  26. #include <boost/interprocess/exceptions.hpp>
  27. #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
  28. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  29. //!\file
  30. //!Describes process-shared variables interprocess_condition_any class
  31. namespace boost {
  32. namespace interprocess {
  33. //!This class is a condition variable that can be placed in shared memory or
  34. //!memory mapped files.
  35. //!
  36. //!The interprocess_condition_any class is a generalization of interprocess_condition.
  37. //!Whereas interprocess_condition works only on Locks with mutex_type == interprocess_mutex
  38. //!interprocess_condition_any can operate on any user-defined lock that meets the BasicLockable
  39. //!requirements (lock()/unlock() member functions).
  40. //!
  41. //!Unlike std::condition_variable_any in C++11, it is NOT safe to invoke the destructor if all
  42. //!threads have been only notified. It is required that they have exited their respective wait
  43. //!functions.
  44. class interprocess_condition_any
  45. {
  46. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  47. //Non-copyable
  48. interprocess_condition_any(const interprocess_condition_any &);
  49. interprocess_condition_any &operator=(const interprocess_condition_any &);
  50. class members
  51. {
  52. public:
  53. typedef interprocess_condition condvar_type;
  54. typedef interprocess_mutex mutex_type;
  55. condvar_type &get_condvar() { return m_cond; }
  56. mutex_type &get_mutex() { return m_mut; }
  57. private:
  58. condvar_type m_cond;
  59. mutex_type m_mut;
  60. };
  61. ipcdetail::condition_any_wrapper<members> m_cond;
  62. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  63. public:
  64. //!Constructs a interprocess_condition_any. On error throws interprocess_exception.
  65. interprocess_condition_any(){}
  66. //!Destroys *this
  67. //!liberating system resources.
  68. ~interprocess_condition_any(){}
  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_cond.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_cond.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. { m_cond.wait(lock); }
  83. //!The same as:
  84. //!while (!pred()) wait(lock)
  85. template <typename L, typename Pr>
  86. void wait(L& lock, Pr pred)
  87. { m_cond.wait(lock, pred); }
  88. //!Releases the lock on the interprocess_mutex object associated with lock, blocks
  89. //!the current thread of execution until readied by a call to
  90. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  91. //!and then reacquires the lock.
  92. //!Returns: false if time abs_time is reached, otherwise true.
  93. template <typename L, class TimePoint>
  94. bool timed_wait(L& lock, const TimePoint &abs_time)
  95. { return m_cond.timed_wait(lock, abs_time); }
  96. //!The same as: while (!pred()) {
  97. //! if (!timed_wait(lock, abs_time)) return pred();
  98. //! } return true;
  99. template <typename L, class TimePoint, typename Pr>
  100. bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
  101. { return m_cond.timed_wait(lock, abs_time, pred); }
  102. //!Same as `timed_wait`, but this function is modeled after the
  103. //!standard library interface.
  104. template <typename L, class TimePoint>
  105. cv_status wait_until(L& lock, const TimePoint &abs_time)
  106. { return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
  107. //!Same as `timed_wait`, but this function is modeled after the
  108. //!standard library interface.
  109. template <typename L, class TimePoint, typename Pr>
  110. bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
  111. { return this->timed_wait(lock, abs_time, pred); }
  112. //!Same as `timed_wait`, but this function is modeled after the
  113. //!standard library interface and uses relative timeouts.
  114. template <typename L, class Duration>
  115. cv_status wait_for(L& lock, const Duration &dur)
  116. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
  117. //!Same as `timed_wait`, but this function is modeled after the
  118. //!standard library interface and uses relative timeouts
  119. template <typename L, class Duration, typename Pr>
  120. bool wait_for(L& lock, const Duration &dur, Pr pred)
  121. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
  122. };
  123. } //namespace interprocess
  124. } // namespace boost
  125. #include <boost/interprocess/detail/config_end.hpp>
  126. #endif // BOOST_INTERPROCESS_CONDITION_ANY_HPP