condition_variable
6.46 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#pragma once
#if __cplusplus < 201103L
#error "C++ version lower than C++11"
#endif
#include <pthread.h>
#include <system_error>
#include <chrono>
#include <utility>
#include <functional>
#include <memory>
#include "__utils.h"
#include "mutex"
#define rt_cpp_cond_var pthread_cond_t
namespace std
{
enum class cv_status
{
no_timeout,
timeout
};
class condition_variable
{
public:
typedef rt_cpp_cond_var *native_handle_type;
condition_variable(const condition_variable &) = delete;
condition_variable &operator=(const condition_variable &) = delete;
condition_variable() = default;
~condition_variable()
{
pthread_cond_destroy(&_m_cond);
}
void wait(unique_lock<mutex> &lock);
void notify_one() noexcept
{
pthread_cond_signal(&_m_cond);
}
void notify_all() noexcept
{
pthread_cond_broadcast(&_m_cond);
}
template <class Predicate>
void wait(unique_lock<mutex> &lock, Predicate pred)
{
while (!pred())
wait(lock);
}
template <class Clock, class Duration>
cv_status wait_until(unique_lock<mutex> &lock,
const chrono::time_point<Clock, Duration> &abs_time)
{
if (!lock.owns_lock())
throw_system_error((int)errc::operation_not_permitted,
"condition_variable::wailt_until: waiting on unlocked lock");
auto secs = chrono::time_point_cast<chrono::seconds>(abs_time);
auto nano_secs = chrono::duration_cast<chrono::nanoseconds>(abs_time - secs);
struct timespec c_abs_time = {static_cast<time_t>(secs.time_since_epoch().count()),
static_cast<long>(nano_secs.count())};
pthread_cond_timedwait(&_m_cond, lock.mutex()->native_handle(), &c_abs_time);
return (Clock::now() < abs_time) ? cv_status::no_timeout : cv_status::timeout;
}
template <class Clock, class Duration, class Predicate>
bool wait_until(unique_lock<mutex> &lock,
const chrono::time_point<Clock, Duration> &abs_time,
Predicate pred)
{
while (!pred())
if (wait_until(lock, abs_time) == cv_status::timeout)
return pred();
return true;
}
template <class Rep, class Period>
cv_status wait_for(unique_lock<mutex> &lock,
const chrono::duration<Rep, Period> &rel_time)
{
return wait_until(lock, real_time_clock::now() + rel_time);
}
template <class Rep, class Period, class Predicate>
bool wait_for(unique_lock<mutex> &lock,
const chrono::duration<Rep, Period> &rel_time,
Predicate pred)
{
return wait_until(lock, real_time_clock::now() + rel_time, std::move(pred));
}
native_handle_type native_handle()
{
return &_m_cond;
}
private:
rt_cpp_cond_var _m_cond = PTHREAD_COND_INITIALIZER;
};
// Lockable is only required to have `lock()` and `unlock()`
class condition_variable_any
{
private:
condition_variable _m_cond;
shared_ptr<mutex> _m_mtx;
// so that Lockable automatically unlocks when waiting and locks after waiting
template <class Lockable>
struct unlocker
{
Lockable &_m_lock;
explicit unlocker(Lockable &lk)
: _m_lock(lk)
{
_m_lock.unlock();
}
~unlocker()
{
_m_lock.lock();
}
unlocker(const unlocker &) = delete;
unlocker &operator=(const unlocker &) = delete;
};
public:
condition_variable_any() : _m_mtx(std::make_shared<mutex>()) {}
~condition_variable_any() = default;
condition_variable_any(const condition_variable_any &) = delete;
condition_variable_any &operator=(const condition_variable_any &) = delete;
void notify_one() noexcept
{
lock_guard<mutex> lk(*_m_mtx);
_m_cond.notify_one();
}
void notify_all() noexcept
{
lock_guard<mutex> lk(*_m_mtx);
_m_cond.notify_all();
}
template <class Lock>
void wait(Lock &lock)
{
shared_ptr<mutex> mut = _m_mtx;
unique_lock<mutex> lk(*mut);
unlocker<Lock> auto_lk(lock); // unlock here
unique_lock<mutex> lk2(std::move(lk));
_m_cond.wait(lk2);
} // mut.unlock(); lock.lock();
template <class Lock, class Predicate>
void wait(Lock &lock, Predicate pred)
{
while (!pred())
wait(lock);
}
template <class Lock, class Clock, class Duration>
cv_status wait_until(Lock &lock,
const chrono::time_point<Clock, Duration> &abs_time)
{
shared_ptr<mutex> mut = _m_mtx;
unique_lock<mutex> lk(*mut);
unlocker<Lock> auto_lk(lock); // unlock here
unique_lock<mutex> lk2(std::move(lk));
return _m_cond.wait_until(lk2, abs_time);
}
template <class Lock, class Clock, class Duration, class Predicate>
bool wait_until(Lock &lock,
const chrono::time_point<Clock, Duration> &abs_time,
Predicate pred)
{
while (!pred())
if (wait_until(lock, abs_time) == cv_status::timeout)
return pred();
return true;
}
template <class Lock, class Rep, class Period>
cv_status wait_for(Lock &lock,
const chrono::duration<Rep, Period> &rel_time)
{
return wait_until(lock, real_time_clock::now() + rel_time);
}
template <class Lock, class Rep, class Period, class Predicate>
bool wait_for(Lock &lock,
const chrono::duration<Rep, Period> &rel_time,
Predicate pred)
{
return wait_until(lock, real_time_clock::now() + rel_time, std::move(pred));
}
};
void notify_all_at_thread_exit(condition_variable &cond, unique_lock<mutex> lk);
} // namespace std