completion_latch.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Distributed under the Boost Software License, Version 1.0. (See
  2. // accompanying file LICENSE_1_0.txt or copy at
  3. // http://www.boost.org/LICENSE_1_0.txt)
  4. // (C) Copyright 2013 Vicente J. Botet Escriba
  5. #ifndef BOOST_THREAD_COMPLETION_LATCH_HPP
  6. #define BOOST_THREAD_COMPLETION_LATCH_HPP
  7. #include <boost/thread/detail/config.hpp>
  8. #include <boost/thread/detail/delete.hpp>
  9. #include <boost/thread/detail/counter.hpp>
  10. #include <boost/thread/mutex.hpp>
  11. #include <boost/thread/lock_types.hpp>
  12. #include <boost/thread/condition_variable.hpp>
  13. #include <boost/chrono/duration.hpp>
  14. #include <boost/chrono/time_point.hpp>
  15. #include <boost/assert.hpp>
  16. #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
  17. #include <boost/function.hpp>
  18. #else
  19. #include <functional>
  20. #endif
  21. //#include <boost/thread/latch.hpp>
  22. #include <boost/config/abi_prefix.hpp>
  23. namespace boost
  24. {
  25. namespace thread_detail
  26. {
  27. void noop()
  28. {
  29. }
  30. }
  31. class completion_latch
  32. {
  33. public:
  34. /// the implementation defined completion function type
  35. #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
  36. typedef function<void()> completion_function;
  37. #else
  38. typedef std::function<void()> completion_function;
  39. #endif
  40. /// noop completion function factory
  41. static completion_function noop()
  42. {
  43. return completion_function(&thread_detail::noop);
  44. }
  45. private:
  46. struct around_wait;
  47. friend struct around_wait;
  48. struct around_wait
  49. {
  50. completion_latch &that_;
  51. boost::unique_lock<boost::mutex> &lk_;
  52. around_wait(completion_latch &that, boost::unique_lock<boost::mutex> &lk)
  53. : that_(that), lk_(lk)
  54. {
  55. that_.leavers_.cond_.wait(lk, detail::counter_is_zero(that_.leavers_));
  56. that_.waiters_.inc_and_notify_all();
  57. that_.leavers_.cond_.wait(lk, detail::counter_is_not_zero(that_.leavers_));
  58. }
  59. ~around_wait()
  60. {
  61. that_.waiters_.dec_and_notify_all();
  62. }
  63. };
  64. bool count_down(unique_lock<mutex> &lk)
  65. {
  66. BOOST_ASSERT(count_ > 0);
  67. if (--count_ == 0)
  68. {
  69. waiters_.cond_.wait(lk, detail::counter_is_not_zero(waiters_));
  70. leavers_.assign_and_notify_all(waiters_);
  71. count_.cond_.notify_all();
  72. waiters_.cond_.wait(lk, detail::counter_is_zero(waiters_));
  73. leavers_.assign_and_notify_all(0);
  74. lk.unlock();
  75. funct_();
  76. return true;
  77. }
  78. return false;
  79. }
  80. public:
  81. BOOST_THREAD_NO_COPYABLE( completion_latch )
  82. /// Constructs a latch with a given count.
  83. completion_latch(std::size_t count) :
  84. count_(count), funct_(noop()), waiters_(0), leavers_(0)
  85. {
  86. }
  87. /// Constructs a latch with a given count and a completion function.
  88. template <typename F>
  89. completion_latch(std::size_t count, BOOST_THREAD_RV_REF(F) funct) :
  90. count_(count),
  91. funct_(boost::move(funct)),
  92. waiters_(0),
  93. leavers_(0)
  94. {
  95. }
  96. template <typename F>
  97. completion_latch(std::size_t count, void(*funct)()) :
  98. count_(count), funct_(funct), waiters_(0), leavers_(0)
  99. {
  100. }
  101. ///
  102. ~completion_latch()
  103. {
  104. }
  105. /// Blocks until the latch has counted down to zero.
  106. void wait()
  107. {
  108. boost::unique_lock<boost::mutex> lk(mutex_);
  109. around_wait aw(*this, lk);
  110. count_.cond_.wait(lk, detail::counter_is_zero(count_));
  111. }
  112. /// @return true if the internal counter is already 0, false otherwise
  113. bool try_wait()
  114. {
  115. boost::unique_lock<boost::mutex> lk(mutex_);
  116. around_wait aw(*this, lk);
  117. return (count_ == 0);
  118. }
  119. /// try to wait for a specified amount of time
  120. /// @return whether there is a timeout or not.
  121. template <class Rep, class Period>
  122. cv_status wait_for(const chrono::duration<Rep, Period>& rel_time)
  123. {
  124. boost::unique_lock<boost::mutex> lk(mutex_);
  125. around_wait aw(*this, lk);
  126. return count_.cond_.wait_for(lk, rel_time, detail::counter_is_zero(count_))
  127. ? cv_status::no_timeout
  128. : cv_status::timeout;
  129. }
  130. /// try to wait until the specified time_point is reached
  131. /// @return whether there is a timeout or not.
  132. template <class Clock, class Duration>
  133. cv_status wait_until(const chrono::time_point<Clock, Duration>& abs_time)
  134. {
  135. boost::unique_lock<boost::mutex> lk(mutex_);
  136. around_wait aw(*this, lk);
  137. return count_.cond_.wait_until(lk, abs_time, detail::counter_is_zero(count_))
  138. ? cv_status::no_timeout
  139. : cv_status::timeout;
  140. }
  141. /// Decrement the count and notify anyone waiting if we reach zero.
  142. /// @Requires count must be greater than 0
  143. void count_down()
  144. {
  145. unique_lock<mutex> lk(mutex_);
  146. count_down(lk);
  147. }
  148. void signal()
  149. {
  150. count_down();
  151. }
  152. /// Decrement the count and notify anyone waiting if we reach zero.
  153. /// Blocks until the latch has counted down to zero.
  154. /// @Requires count must be greater than 0
  155. void count_down_and_wait()
  156. {
  157. boost::unique_lock<boost::mutex> lk(mutex_);
  158. if (count_down(lk))
  159. {
  160. return;
  161. }
  162. around_wait aw(*this, lk);
  163. count_.cond_.wait(lk, detail::counter_is_zero(count_));
  164. }
  165. void sync()
  166. {
  167. count_down_and_wait();
  168. }
  169. /// Reset the counter
  170. /// #Requires This method may only be invoked when there are no other threads currently inside the count_down_and_wait() method.
  171. void reset(std::size_t count)
  172. {
  173. boost::lock_guard<boost::mutex> lk(mutex_);
  174. //BOOST_ASSERT(count_ == 0);
  175. count_ = count;
  176. }
  177. /// Resets the latch with the new completion function.
  178. /// The next time the internal count reaches 0, this function will be invoked.
  179. /// This completion function may only be invoked when there are no other threads
  180. /// currently inside the count_down and wait related functions.
  181. /// It may also be invoked from within the registered completion function.
  182. /// @Returns the old completion function if any or noop if
  183. #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
  184. template <typename F>
  185. completion_function then(BOOST_THREAD_RV_REF(F) funct)
  186. {
  187. boost::lock_guard<boost::mutex> lk(mutex_);
  188. completion_function tmp(funct_);
  189. funct_ = boost::move(funct);
  190. return tmp;
  191. }
  192. #endif
  193. completion_function then(void(*funct)())
  194. {
  195. boost::lock_guard<boost::mutex> lk(mutex_);
  196. completion_function tmp(funct_);
  197. funct_ = completion_function(funct);
  198. return tmp;
  199. }
  200. private:
  201. mutex mutex_;
  202. detail::counter count_;
  203. completion_function funct_;
  204. detail::counter waiters_;
  205. detail::counter leavers_;
  206. };
  207. } // namespace boost
  208. #include <boost/config/abi_suffix.hpp>
  209. #endif