condition_variable.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright Oliver Kowalke 2013.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_FIBERS_CONDITION_VARIABLE_H
  6. #define BOOST_FIBERS_CONDITION_VARIABLE_H
  7. #include <algorithm>
  8. #include <atomic>
  9. #include <chrono>
  10. #include <functional>
  11. #include <mutex>
  12. #include <boost/assert.hpp>
  13. #include <boost/config.hpp>
  14. #include <boost/fiber/context.hpp>
  15. #include <boost/fiber/detail/config.hpp>
  16. #include <boost/fiber/detail/convert.hpp>
  17. #include <boost/fiber/detail/spinlock.hpp>
  18. #include <boost/fiber/exceptions.hpp>
  19. #include <boost/fiber/mutex.hpp>
  20. #include <boost/fiber/operations.hpp>
  21. #ifdef BOOST_HAS_ABI_HEADERS
  22. # include BOOST_ABI_PREFIX
  23. #endif
  24. #ifdef _MSC_VER
  25. # pragma warning(push)
  26. //# pragma warning(disable:4251)
  27. #endif
  28. namespace boost {
  29. namespace fibers {
  30. enum class cv_status {
  31. no_timeout = 1,
  32. timeout
  33. };
  34. class BOOST_FIBERS_DECL condition_variable_any {
  35. private:
  36. typedef context::wait_queue_t wait_queue_t;
  37. detail::spinlock wait_queue_splk_{};
  38. wait_queue_t wait_queue_{};
  39. public:
  40. condition_variable_any() = default;
  41. ~condition_variable_any() {
  42. BOOST_ASSERT( wait_queue_.empty() );
  43. }
  44. condition_variable_any( condition_variable_any const&) = delete;
  45. condition_variable_any & operator=( condition_variable_any const&) = delete;
  46. void notify_one() noexcept;
  47. void notify_all() noexcept;
  48. template< typename LockType >
  49. void wait( LockType & lt) {
  50. context * active_ctx = context::active();
  51. // atomically call lt.unlock() and block on *this
  52. // store this fiber in waiting-queue
  53. detail::spinlock_lock lk{ wait_queue_splk_ };
  54. BOOST_ASSERT( ! active_ctx->wait_is_linked() );
  55. active_ctx->wait_link( wait_queue_);
  56. active_ctx->twstatus.store( static_cast< std::intptr_t >( 0), std::memory_order_release);
  57. // unlock external lt
  58. lt.unlock();
  59. // suspend this fiber
  60. active_ctx->suspend( lk);
  61. // relock external again before returning
  62. try {
  63. lt.lock();
  64. } catch (...) {
  65. std::terminate();
  66. }
  67. // post-conditions
  68. BOOST_ASSERT( ! active_ctx->wait_is_linked() );
  69. }
  70. template< typename LockType, typename Pred >
  71. void wait( LockType & lt, Pred pred) {
  72. while ( ! pred() ) {
  73. wait( lt);
  74. }
  75. }
  76. template< typename LockType, typename Clock, typename Duration >
  77. cv_status wait_until( LockType & lt, std::chrono::time_point< Clock, Duration > const& timeout_time_) {
  78. context * active_ctx = context::active();
  79. cv_status status = cv_status::no_timeout;
  80. std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_);
  81. // atomically call lt.unlock() and block on *this
  82. // store this fiber in waiting-queue
  83. detail::spinlock_lock lk{ wait_queue_splk_ };
  84. BOOST_ASSERT( ! active_ctx->wait_is_linked() );
  85. active_ctx->wait_link( wait_queue_);
  86. active_ctx->twstatus.store( reinterpret_cast< std::intptr_t >( this), std::memory_order_release);
  87. // unlock external lt
  88. lt.unlock();
  89. // suspend this fiber
  90. if ( ! active_ctx->wait_until( timeout_time, lk) ) {
  91. status = cv_status::timeout;
  92. // relock local lk
  93. lk.lock();
  94. // remove from waiting-queue
  95. wait_queue_.remove( * active_ctx);
  96. // unlock local lk
  97. lk.unlock();
  98. }
  99. // relock external again before returning
  100. try {
  101. lt.lock();
  102. } catch (...) {
  103. std::terminate();
  104. }
  105. // post-conditions
  106. BOOST_ASSERT( ! active_ctx->wait_is_linked() );
  107. return status;
  108. }
  109. template< typename LockType, typename Clock, typename Duration, typename Pred >
  110. bool wait_until( LockType & lt,
  111. std::chrono::time_point< Clock, Duration > const& timeout_time, Pred pred) {
  112. while ( ! pred() ) {
  113. if ( cv_status::timeout == wait_until( lt, timeout_time) ) {
  114. return pred();
  115. }
  116. }
  117. return true;
  118. }
  119. template< typename LockType, typename Rep, typename Period >
  120. cv_status wait_for( LockType & lt, std::chrono::duration< Rep, Period > const& timeout_duration) {
  121. return wait_until( lt,
  122. std::chrono::steady_clock::now() + timeout_duration);
  123. }
  124. template< typename LockType, typename Rep, typename Period, typename Pred >
  125. bool wait_for( LockType & lt, std::chrono::duration< Rep, Period > const& timeout_duration, Pred pred) {
  126. return wait_until( lt,
  127. std::chrono::steady_clock::now() + timeout_duration,
  128. pred);
  129. }
  130. };
  131. class BOOST_FIBERS_DECL condition_variable {
  132. private:
  133. condition_variable_any cnd_;
  134. public:
  135. condition_variable() = default;
  136. condition_variable( condition_variable const&) = delete;
  137. condition_variable & operator=( condition_variable const&) = delete;
  138. void notify_one() noexcept {
  139. cnd_.notify_one();
  140. }
  141. void notify_all() noexcept {
  142. cnd_.notify_all();
  143. }
  144. void wait( std::unique_lock< mutex > & lt) {
  145. // pre-condition
  146. BOOST_ASSERT( lt.owns_lock() );
  147. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  148. cnd_.wait( lt);
  149. // post-condition
  150. BOOST_ASSERT( lt.owns_lock() );
  151. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  152. }
  153. template< typename Pred >
  154. void wait( std::unique_lock< mutex > & lt, Pred pred) {
  155. // pre-condition
  156. BOOST_ASSERT( lt.owns_lock() );
  157. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  158. cnd_.wait( lt, pred);
  159. // post-condition
  160. BOOST_ASSERT( lt.owns_lock() );
  161. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  162. }
  163. template< typename Clock, typename Duration >
  164. cv_status wait_until( std::unique_lock< mutex > & lt,
  165. std::chrono::time_point< Clock, Duration > const& timeout_time) {
  166. // pre-condition
  167. BOOST_ASSERT( lt.owns_lock() );
  168. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  169. cv_status result = cnd_.wait_until( lt, timeout_time);
  170. // post-condition
  171. BOOST_ASSERT( lt.owns_lock() );
  172. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  173. return result;
  174. }
  175. template< typename Clock, typename Duration, typename Pred >
  176. bool wait_until( std::unique_lock< mutex > & lt,
  177. std::chrono::time_point< Clock, Duration > const& timeout_time, Pred pred) {
  178. // pre-condition
  179. BOOST_ASSERT( lt.owns_lock() );
  180. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  181. bool result = cnd_.wait_until( lt, timeout_time, pred);
  182. // post-condition
  183. BOOST_ASSERT( lt.owns_lock() );
  184. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  185. return result;
  186. }
  187. template< typename Rep, typename Period >
  188. cv_status wait_for( std::unique_lock< mutex > & lt,
  189. std::chrono::duration< Rep, Period > const& timeout_duration) {
  190. // pre-condition
  191. BOOST_ASSERT( lt.owns_lock() );
  192. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  193. cv_status result = cnd_.wait_for( lt, timeout_duration);
  194. // post-condition
  195. BOOST_ASSERT( lt.owns_lock() );
  196. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  197. return result;
  198. }
  199. template< typename Rep, typename Period, typename Pred >
  200. bool wait_for( std::unique_lock< mutex > & lt,
  201. std::chrono::duration< Rep, Period > const& timeout_duration, Pred pred) {
  202. // pre-condition
  203. BOOST_ASSERT( lt.owns_lock() );
  204. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  205. bool result = cnd_.wait_for( lt, timeout_duration, pred);
  206. // post-condition
  207. BOOST_ASSERT( lt.owns_lock() );
  208. BOOST_ASSERT( context::active() == lt.mutex()->owner_);
  209. return result;
  210. }
  211. };
  212. }}
  213. #ifdef _MSC_VER
  214. # pragma warning(pop)
  215. #endif
  216. #ifdef BOOST_HAS_ABI_HEADERS
  217. # include BOOST_ABI_SUFFIX
  218. #endif
  219. #endif // BOOST_FIBERS_CONDITION_VARIABLE_H