condition_variable.hpp 8.5 KB

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