2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-02 08:52:07 +00:00
Files
fiber/src/barrier.cpp
2014-06-29 13:31:01 +02:00

63 lines
1.2 KiB
C++

// 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 <boost/exception/all.hpp>
#include <boost/thread/locks.hpp>
#include "boost/fiber/exceptions.hpp"
#include "boost/fiber/operations.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_),
cycle_( true),
mtx_(),
cond_()
{
if ( 0 == initial)
boost::throw_exception(
invalid_argument(
system::errc::invalid_argument,
"boost fiber: zero initial barrier count") );
}
bool
barrier::wait()
{
boost::unique_lock< mutex > lk( mtx_);
bool cycle( cycle_);
if ( 0 == --current_)
{
cycle_ = ! cycle_;
current_ = initial_;
cond_.notify_all();
return true;
}
else
{
while ( cycle == cycle_)
//FIXME: what happend if fiber is interrupted?
// ++current_ ?
cond_.wait( lk);
}
return false;
}
}}
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif