interprocess_condition.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #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/detail/locks.hpp>
  21. #include <boost/interprocess/exceptions.hpp>
  22. #include <boost/limits.hpp>
  23. #include <boost/assert.hpp>
  24. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  25. #include <boost/interprocess/sync/posix/condition.hpp>
  26. #define BOOST_INTERPROCESS_USE_POSIX
  27. //Experimental...
  28. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  29. #include <boost/interprocess/sync/windows/condition.hpp>
  30. #define BOOST_INTERPROCESS_USE_WINDOWS
  31. #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  32. #include <boost/interprocess/sync/spin/condition.hpp>
  33. #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  34. #endif
  35. /// @endcond
  36. //!\file
  37. //!Describes process-shared variables interprocess_condition class
  38. namespace boost {
  39. namespace posix_time
  40. { class ptime; }
  41. namespace interprocess {
  42. class named_condition;
  43. //!This class is a condition variable that can be placed in shared memory or
  44. //!memory mapped files.
  45. //!Destroys the object of type std::condition_variable_any
  46. //!
  47. //!Unlike std::condition_variable in C++11, it is NOT safe to invoke the destructor if all
  48. //!threads have been only notified. It is required that they have exited their respective wait
  49. //!functions.
  50. class interprocess_condition
  51. {
  52. /// @cond
  53. //Non-copyable
  54. interprocess_condition(const interprocess_condition &);
  55. interprocess_condition &operator=(const interprocess_condition &);
  56. friend class named_condition;
  57. /// @endcond
  58. public:
  59. //!Constructs a interprocess_condition. On error throws interprocess_exception.
  60. interprocess_condition()
  61. {}
  62. //!Destroys *this
  63. //!liberating system resources.
  64. ~interprocess_condition()
  65. {}
  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_condition.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_condition.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. {
  80. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  81. m_condition.wait(internal_lock);
  82. }
  83. //!The same as:
  84. //!while (!pred()) wait(lock)
  85. template <typename L, typename Pr>
  86. void wait(L& lock, Pr pred)
  87. {
  88. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  89. m_condition.wait(internal_lock, pred);
  90. }
  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. {
  99. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  100. return m_condition.timed_wait(internal_lock, abs_time);
  101. }
  102. //!The same as: while (!pred()) {
  103. //! if (!timed_wait(lock, abs_time)) return pred();
  104. //! } return true;
  105. template <typename L, typename Pr>
  106. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  107. {
  108. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  109. return m_condition.timed_wait(internal_lock, abs_time, pred);
  110. }
  111. /// @cond
  112. private:
  113. #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  114. #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  115. ipcdetail::spin_condition m_condition;
  116. #elif defined(BOOST_INTERPROCESS_USE_POSIX)
  117. #undef BOOST_INTERPROCESS_USE_POSIX
  118. ipcdetail::posix_condition m_condition;
  119. #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
  120. #undef BOOST_INTERPROCESS_USE_WINDOWS
  121. ipcdetail::windows_condition m_condition;
  122. #else
  123. #error "Unknown platform for interprocess_mutex"
  124. #endif
  125. /// @endcond
  126. };
  127. } //namespace interprocess
  128. } // namespace boost
  129. #include <boost/interprocess/detail/config_end.hpp>
  130. #endif // BOOST_INTERPROCESS_CONDITION_HPP