2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-02 20:52:21 +00:00
Files
fiber/src/barrier.cpp

55 lines
969 B
C++

// Copyright Oliver Kowalke 2009.
// 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)
#define BOOST_FIBERS_SOURCE
#include "boost/fiber/barrier.hpp"
#include <stdexcept>
#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 ( initial == 0) throw std::invalid_argument("invalid barrier count"); }
bool
barrier::wait()
{
mutex::scoped_lock lk( mtx_);
bool cycle( cycle_);
if ( 0 == --current_)
{
cycle_ = ! cycle_;
current_ = initial_;
cond_.notify_all();
return true;
}
else
{
while ( cycle == cycle_)
cond_.wait( lk);
}
return false;
}
}}
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif