barrier.cpp
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Oliver Kowalke 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "boost/fiber/barrier.hpp"
#include <mutex>
#include <system_error>
#include "boost/fiber/exceptions.hpp"
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
namespace boost {
namespace fibers {
barrier::barrier( std::size_t initial) :
initial_{ initial },
current_{ initial_ } {
if ( BOOST_UNLIKELY( 0 == initial) ) {
throw fiber_error{ std::make_error_code( std::errc::invalid_argument),
"boost fiber: zero initial barrier count" };
}
}
bool
barrier::wait() {
std::unique_lock< mutex > lk{ mtx_ };
const std::size_t cycle = cycle_;
if ( 0 == --current_) {
++cycle_;
current_ = initial_;
lk.unlock(); // no pessimization
cond_.notify_all();
return true;
}
cond_.wait( lk, [&]{ return cycle != cycle_; });
return false;
}
}}
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif