| 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 | #include "boost/fiber/barrier.hpp" |
| 8 | |
| 9 | #include <mutex> |
| 10 | #include <system_error> |
| 11 | |
| 12 | #include "boost/fiber/exceptions.hpp" |
| 13 | |
| 14 | #ifdef BOOST_HAS_ABI_HEADERS |
| 15 | # include BOOST_ABI_PREFIX |
| 16 | #endif |
| 17 | |
| 18 | namespace boost { |
| 19 | namespace fibers { |
| 20 | |
| 21 | barrier::barrier( std::size_t initial) : |
| 22 | initial_{ initial }, |
| 23 | current_{ initial_ } { |
| 24 | if ( BOOST_UNLIKELY( 0 == initial) ) { |
| 25 | throw fiber_error{ std::make_error_code( e: std::errc::invalid_argument), |
| 26 | "boost fiber: zero initial barrier count"}; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | bool |
| 31 | barrier::wait() { |
| 32 | std::unique_lock< mutex > lk{ mtx_ }; |
| 33 | const std::size_t cycle = cycle_; |
| 34 | if ( 0 == --current_) { |
| 35 | ++cycle_; |
| 36 | current_ = initial_; |
| 37 | lk.unlock(); // no pessimization |
| 38 | cond_.notify_all(); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | cond_.wait( lt&: lk, pred: [&]{ return cycle != cycle_; }); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | }} |
| 47 | |
| 48 | #ifdef BOOST_HAS_ABI_HEADERS |
| 49 | # include BOOST_ABI_SUFFIX |
| 50 | #endif |
| 51 |
