condition_any_algorithm.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_DETAIL_CONDITION_ANY_ALGORITHM_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_CONDITION_ANY_ALGORITHM_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. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/sync/scoped_lock.hpp>
  22. #include <boost/interprocess/sync/detail/locks.hpp>
  23. #include <limits>
  24. namespace boost {
  25. namespace interprocess {
  26. namespace ipcdetail {
  27. ////////////////////////////////////////////////////////////////////////
  28. ////////////////////////////////////////////////////////////////////////
  29. ////////////////////////////////////////////////////////////////////////
  30. //
  31. // Condition variable 'any' (able to use any type of external mutex)
  32. //
  33. // The code is based on Howard E. Hinnant's ISO C++ N2406 paper.
  34. // Many thanks to Howard for his support and comments.
  35. ////////////////////////////////////////////////////////////////////////
  36. ////////////////////////////////////////////////////////////////////////
  37. ////////////////////////////////////////////////////////////////////////
  38. // Required interface for ConditionAnyMembers
  39. // class ConditionAnyMembers
  40. // {
  41. // typedef implementation_defined mutex_type;
  42. // typedef implementation_defined condvar_type;
  43. //
  44. // condvar &get_condvar()
  45. // mutex_type &get_mutex()
  46. // };
  47. //
  48. // Must be initialized as following
  49. //
  50. // get_condvar() [no threads blocked]
  51. // get_mutex() [unlocked]
  52. template<class ConditionAnyMembers>
  53. class condition_any_algorithm
  54. {
  55. private:
  56. condition_any_algorithm();
  57. ~condition_any_algorithm();
  58. condition_any_algorithm(const condition_any_algorithm &);
  59. condition_any_algorithm &operator=(const condition_any_algorithm &);
  60. typedef typename ConditionAnyMembers::mutex_type mutex_type;
  61. typedef typename ConditionAnyMembers::condvar_type condvar_type;
  62. public:
  63. template <class Lock>
  64. static void wait(ConditionAnyMembers& data, Lock& lock)
  65. {
  66. //lock internal before unlocking external to avoid race with a notifier
  67. scoped_lock<mutex_type> internal_lock(data.get_mutex());
  68. {
  69. lock_inverter<Lock> inverted_lock(lock);
  70. scoped_lock<lock_inverter<Lock> > external_unlock(inverted_lock);
  71. { //unlock internal first to avoid deadlock with near simultaneous waits
  72. scoped_lock<mutex_type> internal_unlock;
  73. internal_lock.swap(internal_unlock);
  74. data.get_condvar().wait(internal_unlock);
  75. }
  76. }
  77. }
  78. template <class Lock, class TimePoint>
  79. static bool timed_wait(ConditionAnyMembers &data, Lock& lock, const TimePoint &abs_time)
  80. {
  81. //lock internal before unlocking external to avoid race with a notifier
  82. scoped_lock<mutex_type> internal_lock(data.get_mutex());
  83. {
  84. //Unlock external lock and program for relock
  85. lock_inverter<Lock> inverted_lock(lock);
  86. scoped_lock<lock_inverter<Lock> > external_unlock(inverted_lock);
  87. { //unlock internal first to avoid deadlock with near simultaneous waits
  88. scoped_lock<mutex_type> internal_unlock;
  89. internal_lock.swap(internal_unlock);
  90. return data.get_condvar().timed_wait(internal_unlock, abs_time);
  91. }
  92. }
  93. }
  94. static void signal(ConditionAnyMembers& data, bool broadcast)
  95. {
  96. scoped_lock<mutex_type> internal_lock(data.get_mutex());
  97. if(broadcast){
  98. data.get_condvar().notify_all();
  99. }
  100. else{
  101. data.get_condvar().notify_one();
  102. }
  103. }
  104. };
  105. template<class ConditionAnyMembers>
  106. class condition_any_wrapper
  107. {
  108. //Non-copyable
  109. condition_any_wrapper(const condition_any_wrapper &);
  110. condition_any_wrapper &operator=(const condition_any_wrapper &);
  111. ConditionAnyMembers m_data;
  112. typedef ipcdetail::condition_any_algorithm<ConditionAnyMembers> algo_type;
  113. public:
  114. condition_any_wrapper(){}
  115. ~condition_any_wrapper(){}
  116. ConditionAnyMembers & get_members()
  117. { return m_data; }
  118. const ConditionAnyMembers & get_members() const
  119. { return m_data; }
  120. void notify_one()
  121. { algo_type::signal(m_data, false); }
  122. void notify_all()
  123. { algo_type::signal(m_data, true); }
  124. template <typename Lock>
  125. void wait(Lock& lock)
  126. {
  127. if (!lock)
  128. throw lock_exception();
  129. algo_type::wait(m_data, lock);
  130. }
  131. template <typename L, typename Pr>
  132. void wait(L& lock, Pr pred)
  133. {
  134. if (!lock)
  135. throw lock_exception();
  136. while (!pred())
  137. algo_type::wait(m_data, lock);
  138. }
  139. template <typename L, typename TimePoint>
  140. bool timed_wait(L& lock, const TimePoint &abs_time)
  141. {
  142. if (!lock)
  143. throw lock_exception();
  144. return algo_type::timed_wait(m_data, lock, abs_time);
  145. }
  146. template <typename L, typename TimePoint, typename Pr>
  147. bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
  148. {
  149. if (!lock)
  150. throw lock_exception();
  151. while (!pred()){
  152. if (!algo_type::timed_wait(m_data, lock, abs_time))
  153. return pred();
  154. }
  155. return true;
  156. }
  157. };
  158. } //namespace ipcdetail
  159. } //namespace interprocess
  160. } //namespace boost
  161. #include <boost/interprocess/detail/config_end.hpp>
  162. #endif //BOOST_INTERPROCESS_DETAIL_CONDITION_ANY_ALGORITHM_HPP