| 1 | |
| 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) |
| 6 | |
| 7 | #ifndef BOOST_FIBERS_CONDITION_VARIABLE_H |
| 8 | #define BOOST_FIBERS_CONDITION_VARIABLE_H |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <atomic> |
| 12 | #include <chrono> |
| 13 | #include <functional> |
| 14 | #include <mutex> |
| 15 | |
| 16 | #include <boost/assert.hpp> |
| 17 | #include <boost/config.hpp> |
| 18 | #include <boost/context/detail/config.hpp> |
| 19 | |
| 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> |
| 28 | |
| 29 | #ifdef BOOST_HAS_ABI_HEADERS |
| 30 | # include BOOST_ABI_PREFIX |
| 31 | #endif |
| 32 | |
| 33 | #ifdef _MSC_VER |
| 34 | # pragma warning(push) |
| 35 | //# pragma warning(disable:4251) |
| 36 | #endif |
| 37 | |
| 38 | namespace boost { |
| 39 | namespace fibers { |
| 40 | |
| 41 | enum class cv_status { |
| 42 | no_timeout = 1, |
| 43 | timeout |
| 44 | }; |
| 45 | |
| 46 | class BOOST_FIBERS_DECL condition_variable_any { |
| 47 | private: |
| 48 | detail::spinlock wait_queue_splk_{}; |
| 49 | wait_queue wait_queue_{}; |
| 50 | |
| 51 | public: |
| 52 | condition_variable_any() = default; |
| 53 | |
| 54 | ~condition_variable_any() { |
| 55 | BOOST_ASSERT( wait_queue_.empty() ); |
| 56 | } |
| 57 | |
| 58 | condition_variable_any( condition_variable_any const&) = delete; |
| 59 | condition_variable_any & operator=( condition_variable_any const&) = delete; |
| 60 | |
| 61 | void notify_one() noexcept; |
| 62 | |
| 63 | void notify_all() noexcept; |
| 64 | |
| 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_ }; |
| 71 | lt.unlock(); |
| 72 | wait_queue_.suspend_and_wait( lk, active_ctx); |
| 73 | |
| 74 | // relock external again before returning |
| 75 | try { |
| 76 | lt.lock(); |
| 77 | #if defined(BOOST_CONTEXT_HAS_CXXABI_H) |
| 78 | } catch ( abi::__forced_unwind const&) { |
| 79 | throw; |
| 80 | #endif |
| 81 | } catch (...) { |
| 82 | std::terminate(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | template< typename LockType, typename Pred > |
| 87 | void wait( LockType & lt, Pred pred) { |
| 88 | while ( ! pred() ) { |
| 89 | wait( lt); |
| 90 | } |
| 91 | } |
| 92 | |
| 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 |
| 102 | lt.unlock(); |
| 103 | if ( ! wait_queue_.suspend_and_wait_until( lk, active_ctx, timeout_time)) { |
| 104 | status = cv_status::timeout; |
| 105 | } |
| 106 | // relock external again before returning |
| 107 | try { |
| 108 | lt.lock(); |
| 109 | #if defined(BOOST_CONTEXT_HAS_CXXABI_H) |
| 110 | } catch ( abi::__forced_unwind const&) { |
| 111 | throw; |
| 112 | #endif |
| 113 | } catch (...) { |
| 114 | std::terminate(); |
| 115 | } |
| 116 | return status; |
| 117 | } |
| 118 | |
| 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) { |
| 122 | while ( ! pred() ) { |
| 123 | if ( cv_status::timeout == wait_until( lt, timeout_time) ) { |
| 124 | return pred(); |
| 125 | } |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 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); |
| 134 | } |
| 135 | |
| 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, |
| 140 | pred); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | class BOOST_FIBERS_DECL condition_variable { |
| 145 | private: |
| 146 | condition_variable_any cnd_; |
| 147 | |
| 148 | public: |
| 149 | condition_variable() = default; |
| 150 | |
| 151 | condition_variable( condition_variable const&) = delete; |
| 152 | condition_variable & operator=( condition_variable const&) = delete; |
| 153 | |
| 154 | void notify_one() noexcept { |
| 155 | cnd_.notify_one(); |
| 156 | } |
| 157 | |
| 158 | void notify_all() noexcept { |
| 159 | cnd_.notify_all(); |
| 160 | } |
| 161 | |
| 162 | void wait( std::unique_lock< mutex > & lt) { |
| 163 | // pre-condition |
| 164 | BOOST_ASSERT( lt.owns_lock() ); |
| 165 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 166 | cnd_.wait( lt); |
| 167 | // post-condition |
| 168 | BOOST_ASSERT( lt.owns_lock() ); |
| 169 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 170 | } |
| 171 | |
| 172 | template< typename Pred > |
| 173 | void wait( std::unique_lock< mutex > & lt, Pred pred) { |
| 174 | // pre-condition |
| 175 | BOOST_ASSERT( lt.owns_lock() ); |
| 176 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 177 | cnd_.wait( lt, pred); |
| 178 | // post-condition |
| 179 | BOOST_ASSERT( lt.owns_lock() ); |
| 180 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 181 | } |
| 182 | |
| 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) { |
| 186 | // pre-condition |
| 187 | BOOST_ASSERT( lt.owns_lock() ); |
| 188 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 189 | cv_status result = cnd_.wait_until( lt, timeout_time); |
| 190 | // post-condition |
| 191 | BOOST_ASSERT( lt.owns_lock() ); |
| 192 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 193 | return result; |
| 194 | } |
| 195 | |
| 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) { |
| 199 | // pre-condition |
| 200 | BOOST_ASSERT( lt.owns_lock() ); |
| 201 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 202 | bool result = cnd_.wait_until( lt, timeout_time, pred); |
| 203 | // post-condition |
| 204 | BOOST_ASSERT( lt.owns_lock() ); |
| 205 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 206 | return result; |
| 207 | } |
| 208 | |
| 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) { |
| 212 | // pre-condition |
| 213 | BOOST_ASSERT( lt.owns_lock() ); |
| 214 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 215 | cv_status result = cnd_.wait_for( lt, timeout_duration); |
| 216 | // post-condition |
| 217 | BOOST_ASSERT( lt.owns_lock() ); |
| 218 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 219 | return result; |
| 220 | } |
| 221 | |
| 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) { |
| 225 | // pre-condition |
| 226 | BOOST_ASSERT( lt.owns_lock() ); |
| 227 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 228 | bool result = cnd_.wait_for( lt, timeout_duration, pred); |
| 229 | // post-condition |
| 230 | BOOST_ASSERT( lt.owns_lock() ); |
| 231 | BOOST_ASSERT( context::active() == lt.mutex()->owner_); |
| 232 | return result; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | }} |
| 237 | |
| 238 | #ifdef _MSC_VER |
| 239 | # pragma warning(pop) |
| 240 | #endif |
| 241 | |
| 242 | #ifdef BOOST_HAS_ABI_HEADERS |
| 243 | # include BOOST_ABI_SUFFIX |
| 244 | #endif |
| 245 | |
| 246 | #endif // BOOST_FIBERS_CONDITION_VARIABLE_H |
| 247 | |