interprocess_condition.hpp 5.5 KB

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