diff options
Diffstat (limited to 'src/boost/libs/fiber/performance/fiber/barrier.hpp')
-rw-r--r-- | src/boost/libs/fiber/performance/fiber/barrier.hpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/boost/libs/fiber/performance/fiber/barrier.hpp b/src/boost/libs/fiber/performance/fiber/barrier.hpp new file mode 100644 index 00000000..dcbd7e98 --- /dev/null +++ b/src/boost/libs/fiber/performance/fiber/barrier.hpp @@ -0,0 +1,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) + +#ifndef BARRIER_H +#define BARRIER_H + +#include <cstddef> +#include <condition_variable> +#include <mutex> + +#include <boost/assert.hpp> + +class barrier { +private: + std::size_t initial_; + std::size_t current_; + bool cycle_{ true }; + std::mutex mtx_{}; + std::condition_variable cond_{}; + +public: + explicit barrier( std::size_t initial) : + initial_{ initial }, + current_{ initial_ } { + BOOST_ASSERT ( 0 != initial); + } + + barrier( barrier const&) = delete; + barrier & operator=( barrier const&) = delete; + + bool wait() { + std::unique_lock< std::mutex > lk( mtx_); + const bool cycle = cycle_; + if ( 0 == --current_) { + cycle_ = ! cycle_; + current_ = initial_; + lk.unlock(); // no pessimization + cond_.notify_all(); + return true; + } else { + cond_.wait( lk, [&](){ return cycle != cycle_; }); + } + return false; + } +}; + +#endif // BARRIER_H |