]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/fiber/examples/thread_barrier.hpp
Add patch for failing prerm scripts
[ceph.git] / ceph / src / boost / libs / fiber / examples / thread_barrier.hpp
CommitLineData
7c673cae
FG
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
b32b8144
FG
7#ifndef THREAD_BARRIER_H
8#define THREAD_BARRIER_H
7c673cae
FG
9
10#include <cstddef>
11#include <condition_variable>
12#include <mutex>
13
14#include <boost/assert.hpp>
15
b32b8144 16class thread_barrier {
7c673cae
FG
17private:
18 std::size_t initial_;
19 std::size_t current_;
20 bool cycle_{ true };
21 std::mutex mtx_{};
22 std::condition_variable cond_{};
23
24public:
b32b8144 25 explicit thread_barrier( std::size_t initial) :
7c673cae
FG
26 initial_{ initial },
27 current_{ initial_ } {
28 BOOST_ASSERT ( 0 != initial);
29 }
30
b32b8144
FG
31 thread_barrier( thread_barrier const&) = delete;
32 thread_barrier & operator=( thread_barrier const&) = delete;
7c673cae
FG
33
34 bool wait() {
35 std::unique_lock< std::mutex > lk( mtx_);
36 const bool cycle = cycle_;
37 if ( 0 == --current_) {
38 cycle_ = ! cycle_;
39 current_ = initial_;
40 lk.unlock(); // no pessimization
41 cond_.notify_all();
42 return true;
43 } else {
44 cond_.wait( lk, [&](){ return cycle != cycle_; });
45 }
46 return false;
47 }
48};
49
b32b8144 50#endif // THREAD_BARRIER_H