-
Notifications
You must be signed in to change notification settings - Fork 1
/
cyclic_barrier.h
108 lines (92 loc) · 2.6 KB
/
cyclic_barrier.h
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include <mutex>
#include <chrono>
class cyclic_barrier
{
enum barrier_state
{
broken = -1,
initialized,
waiting
};
private:
int parties_, waiting_parties_;
barrier_state state;
std::mutex m_;
std::condition_variable cv_;
public:
explicit cyclic_barrier(const int parties) : parties_(parties), waiting_parties_(0), state(initialized) {};
int await(); //Waits until all parties have invoked await on this barrier.
template <typename Rep, typename Period> //Waits until all parties have invoked await on this barrier, or the specified waiting time elapses.
int await(std::chrono::duration<Rep,Period> timeout);
int get_number_waiting(); //Returns the number of parties currently waiting at the barrier.
int get_parties(); //Returns the number of parties required to trip this barrier.
bool is_broken(); //Queries if this barrier is in a broken state.
void reset();
};
inline int cyclic_barrier::await()
{
std::unique_lock<std::mutex> ul(m_);
if (state == broken)
throw std::exception("broken_barrier_exception");
if (state == initialized) state = waiting;
const auto ret = parties_ - waiting_parties_;
waiting_parties_++;
if (waiting_parties_ < parties_)
cv_.wait(ul, [this]() {return (waiting_parties_ >= parties_) || (state != waiting); });
else
{
state = broken;
cv_.notify_all();
}
if (state == initialized)
throw std::exception("broken_barrier_exception");
return ret;
}
template <typename Rep, typename Period>
inline int cyclic_barrier::await(const std::chrono::duration<Rep,Period> timeout)
{
std::unique_lock<std::mutex> ul(m_);
if (state == broken)
throw std::exception("broken_barrier_exception");
if (state == initialized) state = waiting;
const auto ret = parties_ - waiting_parties_;
waiting_parties_++;
if (waiting_parties_ < parties_)
cv_.wait_for(ul, timeout, [this]() {return (waiting_parties_ >= parties_) || (state != waiting); });
else
{
state = broken;
cv_.notify_all();
}
if (state == initialized)
throw std::exception("broken_barrier_exception");
if(waiting_parties_<parties_)
{
waiting_parties_--;
throw std::exception("timeout_exception");
}
return ret;
}
inline int cyclic_barrier::get_number_waiting()
{
std::lock_guard<std::mutex> lg(m_);
return waiting_parties_;
}
inline int cyclic_barrier::get_parties()
{
std::lock_guard<std::mutex> lg(m_);
return parties_;
}
inline bool cyclic_barrier::is_broken()
{
std::lock_guard<std::mutex> lg(m_);
return state == broken;
}
inline void cyclic_barrier::reset()
{
std::lock_guard<std::mutex> lg(m_);
state = initialized;
waiting_parties_=0;
cv_.notify_all();
}