2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_FIBERS_CONDITION_VARIABLE_H
8 #define BOOST_FIBERS_CONDITION_VARIABLE_H
16 #include <boost/assert.hpp>
17 #include <boost/config.hpp>
18 #include <boost/context/detail/config.hpp>
20 #include <boost/fiber/context.hpp>
21 #include <boost/fiber/detail/config.hpp>
22 #include <boost/fiber/detail/convert.hpp>
23 #include <boost/fiber/detail/spinlock.hpp>
24 #include <boost/fiber/exceptions.hpp>
25 #include <boost/fiber/mutex.hpp>
26 #include <boost/fiber/operations.hpp>
27 #include <boost/fiber/waker.hpp>
29 #ifdef BOOST_HAS_ABI_HEADERS
30 # include BOOST_ABI_PREFIX
34 # pragma warning(push)
35 //# pragma warning(disable:4251)
41 enum class cv_status {
46 class BOOST_FIBERS_DECL condition_variable_any {
48 detail::spinlock wait_queue_splk_{};
49 wait_queue wait_queue_{};
52 condition_variable_any() = default;
54 ~condition_variable_any() {
55 BOOST_ASSERT( wait_queue_.empty() );
58 condition_variable_any( condition_variable_any const&) = delete;
59 condition_variable_any & operator=( condition_variable_any const&) = delete;
61 void notify_one() noexcept;
63 void notify_all() noexcept;
65 template< typename LockType >
66 void wait( LockType & lt) {
67 context * active_ctx = context::active();
68 // atomically call lt.unlock() and block on *this
69 // store this fiber in waiting-queue
70 detail::spinlock_lock lk{ wait_queue_splk_ };
72 wait_queue_.suspend_and_wait( lk, active_ctx);
74 // relock external again before returning
77 #if defined(BOOST_CONTEXT_HAS_CXXABI_H)
78 } catch ( abi::__forced_unwind const&) {
86 template< typename LockType, typename Pred >
87 void wait( LockType & lt, Pred pred) {
93 template< typename LockType, typename Clock, typename Duration >
94 cv_status wait_until( LockType & lt, std::chrono::time_point< Clock, Duration > const& timeout_time_) {
95 context * active_ctx = context::active();
96 cv_status status = cv_status::no_timeout;
97 std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_);
98 // atomically call lt.unlock() and block on *this
99 // store this fiber in waiting-queue
100 detail::spinlock_lock lk{ wait_queue_splk_ };
101 // unlock external lt
103 if ( ! wait_queue_.suspend_and_wait_until( lk, active_ctx, timeout_time)) {
104 status = cv_status::timeout;
106 // relock external again before returning
109 #if defined(BOOST_CONTEXT_HAS_CXXABI_H)
110 } catch ( abi::__forced_unwind const&) {
119 template< typename LockType, typename Clock, typename Duration, typename Pred >
120 bool wait_until( LockType & lt,
121 std::chrono::time_point< Clock, Duration > const& timeout_time, Pred pred) {
123 if ( cv_status::timeout == wait_until( lt, timeout_time) ) {
130 template< typename LockType, typename Rep, typename Period >
131 cv_status wait_for( LockType & lt, std::chrono::duration< Rep, Period > const& timeout_duration) {
132 return wait_until( lt,
133 std::chrono::steady_clock::now() + timeout_duration);
136 template< typename LockType, typename Rep, typename Period, typename Pred >
137 bool wait_for( LockType & lt, std::chrono::duration< Rep, Period > const& timeout_duration, Pred pred) {
138 return wait_until( lt,
139 std::chrono::steady_clock::now() + timeout_duration,
144 class BOOST_FIBERS_DECL condition_variable {
146 condition_variable_any cnd_;
149 condition_variable() = default;
151 condition_variable( condition_variable const&) = delete;
152 condition_variable & operator=( condition_variable const&) = delete;
154 void notify_one() noexcept {
158 void notify_all() noexcept {
162 void wait( std::unique_lock< mutex > & lt) {
164 BOOST_ASSERT( lt.owns_lock() );
165 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
168 BOOST_ASSERT( lt.owns_lock() );
169 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
172 template< typename Pred >
173 void wait( std::unique_lock< mutex > & lt, Pred pred) {
175 BOOST_ASSERT( lt.owns_lock() );
176 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
177 cnd_.wait( lt, pred);
179 BOOST_ASSERT( lt.owns_lock() );
180 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
183 template< typename Clock, typename Duration >
184 cv_status wait_until( std::unique_lock< mutex > & lt,
185 std::chrono::time_point< Clock, Duration > const& timeout_time) {
187 BOOST_ASSERT( lt.owns_lock() );
188 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
189 cv_status result = cnd_.wait_until( lt, timeout_time);
191 BOOST_ASSERT( lt.owns_lock() );
192 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
196 template< typename Clock, typename Duration, typename Pred >
197 bool wait_until( std::unique_lock< mutex > & lt,
198 std::chrono::time_point< Clock, Duration > const& timeout_time, Pred pred) {
200 BOOST_ASSERT( lt.owns_lock() );
201 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
202 bool result = cnd_.wait_until( lt, timeout_time, pred);
204 BOOST_ASSERT( lt.owns_lock() );
205 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
209 template< typename Rep, typename Period >
210 cv_status wait_for( std::unique_lock< mutex > & lt,
211 std::chrono::duration< Rep, Period > const& timeout_duration) {
213 BOOST_ASSERT( lt.owns_lock() );
214 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
215 cv_status result = cnd_.wait_for( lt, timeout_duration);
217 BOOST_ASSERT( lt.owns_lock() );
218 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
222 template< typename Rep, typename Period, typename Pred >
223 bool wait_for( std::unique_lock< mutex > & lt,
224 std::chrono::duration< Rep, Period > const& timeout_duration, Pred pred) {
226 BOOST_ASSERT( lt.owns_lock() );
227 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
228 bool result = cnd_.wait_for( lt, timeout_duration, pred);
230 BOOST_ASSERT( lt.owns_lock() );
231 BOOST_ASSERT( context::active() == lt.mutex()->owner_);
239 # pragma warning(pop)
242 #ifdef BOOST_HAS_ABI_HEADERS
243 # include BOOST_ABI_SUFFIX
246 #endif // BOOST_FIBERS_CONDITION_VARIABLE_H