interprocess_condition_any.hpp 4.7 KB

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