-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondvar-stl_lib.cc
103 lines (97 loc) · 2.66 KB
/
condvar-stl_lib.cc
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
#include "condvar-stl.hh"
#include <unistd.h>
#include <cerrno>
#include <cstring>
namespace stl_condvar {
void StlCondvar::change_parity_or_block(const Parity new_parity) {
while (true) {
{
std::unique_lock<std::mutex> lock(mutex_, std::defer_lock);
if (!lock.try_lock()) {
continue;
}
if (CYCLES < ctr_) {
return;
}
if (Parity::even == new_parity) {
// Wait for odd ctr_.
while (is_even()) {
if (CYCLES < ctr_) {
return;
}
cond_.wait(lock);
// Yield if awoken spuriously or when signalled by waiter of same
// parity.
if (is_even()) {
continue;
}
}
ctr_++;
} else {
// Wait for even ctr_.
while (is_odd()) {
if (CYCLES < ctr_) {
return;
}
cond_.wait(lock);
if (is_odd()) {
continue;
}
}
ctr_++;
}
}
// Will quickly hang if notify_one() is used instead when two threads of the
// same parity signal each other and both block.
cond_.notify_all();
}
}
void StlCondvar::change_parity_or_timeout(const Parity new_parity) {
while (true) {
bool should_signal = false;
{
std::unique_lock<std::mutex> lock(mutex_, std::defer_lock);
if (!lock.try_lock()) {
continue;
}
if (CYCLES < ctr_) {
return;
}
// Initialize to no_timeout since the condition may be satisfied when the
// thread is entered.
bool succeeded = true;
if (Parity::even == new_parity) {
while (is_even()) {
if (CYCLES < ctr_) {
return;
}
// An inline function (is_odd()) apparently cannot serve as the
// predicate.
succeeded = cond_.wait_for(lock, TIMEOUT,
([&]() { return (1 == (ctr_ % 2)); }));
}
} else {
while (is_odd()) {
if (CYCLES < ctr_) {
return;
}
succeeded = cond_.wait_for(lock, TIMEOUT,
([&]() { return (0 == (ctr_ % 2)); }));
}
}
// should_signal is needed because we need to check succeeded inside the
// lock, but we want to signal outside it.
if (succeeded) {
ctr_++;
should_signal = true;
}
}
// Because the code makes progress even if the wrong waiter is awoken,
// signalling one works fine. However, notify_all() is much faster,
// perhaps by as much as a factor of 2.
if (should_signal) {
cond_.notify_all();
}
}
}
} // namespace stl_condvar