/Src/Dependencies/Boost/boost/interprocess/sync/interprocess_condition.hpp

http://hadesmem.googlecode.com/ · C++ Header · 179 lines · 107 code · 31 blank · 41 comment · 13 complexity · ce8a7dd9760f21e8ecd1e291838adce9 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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/scoped_lock.hpp>
  20. #include <boost/interprocess/sync/interprocess_condition.hpp>
  21. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  22. #include <boost/interprocess/exceptions.hpp>
  23. #include <boost/limits.hpp>
  24. #include <boost/assert.hpp>
  25. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  26. #include <pthread.h>
  27. #include <errno.h>
  28. #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
  29. #define BOOST_INTERPROCESS_USE_POSIX
  30. #else
  31. #include <boost/interprocess/detail/atomic.hpp>
  32. #include <boost/cstdint.hpp>
  33. #include <boost/interprocess/detail/os_thread_functions.hpp>
  34. #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  35. #endif
  36. /// @endcond
  37. //!\file
  38. //!Describes process-shared variables interprocess_condition class
  39. namespace boost {
  40. namespace posix_time
  41. { class ptime; }
  42. namespace interprocess {
  43. class named_condition;
  44. //!This class is a condition variable that can be placed in shared memory or
  45. //!memory mapped files.
  46. class interprocess_condition
  47. {
  48. /// @cond
  49. //Non-copyable
  50. interprocess_condition(const interprocess_condition &);
  51. interprocess_condition &operator=(const interprocess_condition &);
  52. friend class named_condition;
  53. /// @endcond
  54. public:
  55. //!Constructs a interprocess_condition. On error throws interprocess_exception.
  56. interprocess_condition();
  57. //!Destroys *this
  58. //!liberating system resources.
  59. ~interprocess_condition();
  60. //!If there is a thread waiting on *this, change that
  61. //!thread's state to ready. Otherwise there is no effect.
  62. void notify_one();
  63. //!Change the state of all threads waiting on *this to ready.
  64. //!If there are no waiting threads, notify_all() has no effect.
  65. void notify_all();
  66. //!Releases the lock on the interprocess_mutex object associated with lock, blocks
  67. //!the current thread of execution until readied by a call to
  68. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  69. template <typename L>
  70. void wait(L& lock)
  71. {
  72. if (!lock)
  73. throw lock_exception();
  74. do_wait(*lock.mutex());
  75. }
  76. //!The same as:
  77. //!while (!pred()) wait(lock)
  78. template <typename L, typename Pr>
  79. void wait(L& lock, Pr pred)
  80. {
  81. if (!lock)
  82. throw lock_exception();
  83. while (!pred())
  84. do_wait(*lock.mutex());
  85. }
  86. //!Releases the lock on the interprocess_mutex object associated with lock, blocks
  87. //!the current thread of execution until readied by a call to
  88. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  89. //!and then reacquires the lock.
  90. //!Returns: false if time abs_time is reached, otherwise true.
  91. template <typename L>
  92. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
  93. {
  94. if(abs_time == boost::posix_time::pos_infin){
  95. this->wait(lock);
  96. return true;
  97. }
  98. if (!lock)
  99. throw lock_exception();
  100. return do_timed_wait(abs_time, *lock.mutex());
  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. if(abs_time == boost::posix_time::pos_infin){
  109. this->wait(lock, pred);
  110. return true;
  111. }
  112. if (!lock)
  113. throw lock_exception();
  114. while (!pred()){
  115. if (!do_timed_wait(abs_time, *lock.mutex()))
  116. return pred();
  117. }
  118. return true;
  119. }
  120. /// @cond
  121. private:
  122. void do_wait(interprocess_mutex &mut);
  123. bool do_timed_wait(const boost::posix_time::ptime &abs_time, interprocess_mutex &mut);
  124. #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  125. enum { SLEEP = 0, NOTIFY_ONE, NOTIFY_ALL };
  126. interprocess_mutex m_enter_mut;
  127. //interprocess_mutex m_check_mut;
  128. volatile boost::uint32_t m_command;
  129. volatile boost::uint32_t m_num_waiters;
  130. bool do_timed_wait(bool tout_enabled, const boost::posix_time::ptime &abs_time, interprocess_mutex &mut);
  131. void notify(boost::uint32_t command);
  132. #elif defined(BOOST_INTERPROCESS_USE_POSIX)
  133. pthread_cond_t m_condition;
  134. #endif
  135. /// @endcond
  136. };
  137. } //namespace interprocess
  138. } // namespace boost
  139. #ifdef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  140. # undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  141. # include <boost/interprocess/sync/emulation/interprocess_condition.hpp>
  142. #endif
  143. #ifdef BOOST_INTERPROCESS_USE_POSIX
  144. # undef BOOST_INTERPROCESS_USE_POSIX
  145. # include <boost/interprocess/sync/posix/interprocess_condition.hpp>
  146. #endif
  147. #include <boost/interprocess/detail/config_end.hpp>
  148. #endif // BOOST_INTERPROCESS_CONDITION_HPP