interprocess_condition_any.hpp 5.8 KB

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