interprocess_condition_any.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. /// @cond
  16. #include <boost/interprocess/detail/config_begin.hpp>
  17. #include <boost/interprocess/detail/workaround.hpp>
  18. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  19. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  20. #include <boost/interprocess/sync/interprocess_condition.hpp>
  21. #include <boost/interprocess/exceptions.hpp>
  22. #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
  23. /// @endcond
  24. //!\file
  25. //!Describes process-shared variables interprocess_condition_any class
  26. namespace boost {
  27. namespace posix_time
  28. { class ptime; }
  29. namespace interprocess {
  30. //!This class is a condition variable that can be placed in shared memory or
  31. //!memory mapped files.
  32. //!
  33. //!The interprocess_condition_any class is a generalization of interprocess_condition.
  34. //!Whereas interprocess_condition works only on Locks with mutex_type == interprocess_mutex
  35. //!interprocess_condition_any can operate on any user-defined lock that meets the BasicLockable
  36. //!requirements (lock()/unlock() member functions).
  37. //!
  38. //!Unlike std::condition_variable_any in C++11, it is NOT safe to invoke the destructor if all
  39. //!threads have been only notified. It is required that they have exited their respective wait
  40. //!functions.
  41. class interprocess_condition_any
  42. {
  43. /// @cond
  44. //Non-copyable
  45. interprocess_condition_any(const interprocess_condition_any &);
  46. interprocess_condition_any &operator=(const interprocess_condition_any &);
  47. class members
  48. {
  49. public:
  50. typedef interprocess_condition condvar_type;
  51. typedef interprocess_mutex mutex_type;
  52. condvar_type &get_condvar() { return m_cond; }
  53. mutex_type &get_mutex() { return m_mut; }
  54. private:
  55. condvar_type m_cond;
  56. mutex_type m_mut;
  57. };
  58. ipcdetail::condition_any_wrapper<members> m_cond;
  59. /// @endcond
  60. public:
  61. //!Constructs a interprocess_condition_any. On error throws interprocess_exception.
  62. interprocess_condition_any(){}
  63. //!Destroys *this
  64. //!liberating system resources.
  65. ~interprocess_condition_any(){}
  66. //!If there is a thread waiting on *this, change that
  67. //!thread's state to ready. Otherwise there is no effect.
  68. void notify_one()
  69. { m_cond.notify_one(); }
  70. //!Change the state of all threads waiting on *this to ready.
  71. //!If there are no waiting threads, notify_all() has no effect.
  72. void notify_all()
  73. { m_cond.notify_all(); }
  74. //!Releases the lock on the interprocess_mutex object associated with lock, blocks
  75. //!the current thread of execution until readied by a call to
  76. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  77. template <typename L>
  78. void wait(L& lock)
  79. { m_cond.wait(lock); }
  80. //!The same as:
  81. //!while (!pred()) wait(lock)
  82. template <typename L, typename Pr>
  83. void wait(L& lock, Pr pred)
  84. { m_cond.wait(lock, pred); }
  85. //!Releases the lock on the interprocess_mutex object associated with lock, blocks
  86. //!the current thread of execution until readied by a call to
  87. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  88. //!and then reacquires the lock.
  89. //!Returns: false if time abs_time is reached, otherwise true.
  90. template <typename L>
  91. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
  92. { return m_cond.timed_wait(lock, abs_time); }
  93. //!The same as: while (!pred()) {
  94. //! if (!timed_wait(lock, abs_time)) return pred();
  95. //! } return true;
  96. template <typename L, typename Pr>
  97. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  98. { return m_cond.timed_wait(lock, abs_time, pred); }
  99. };
  100. } //namespace interprocess
  101. } // namespace boost
  102. #include <boost/interprocess/detail/config_end.hpp>
  103. #endif // BOOST_INTERPROCESS_CONDITION_ANY_HPP