blob: 32e98cb54c32fc13d2c5ae5c233d8f67357b4c7c (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "barrier.h"
namespace vespalib {
Barrier::Barrier(size_t n)
: _n(n),
_lock(),
_cond(),
_count(0),
_next(0)
{}
Barrier::~Barrier() = default;
bool
Barrier::await()
{
std::unique_lock guard(_lock);
if (_n == 0) {
return false;
}
if (_count == _next) {
_next += _n;
}
if (++_count == _next) {
_cond.notify_all();
} else {
size_t limit = _next;
while ((_count - limit) > _n) {
if (_n == 0) {
return false;
}
_cond.wait(guard);
}
}
return true;
}
void
Barrier::destroy()
{
std::lock_guard guard(_lock);
_n = 0;
_cond.notify_all();
}
} // namespace vespalib
|