-
Notifications
You must be signed in to change notification settings - Fork 2
/
semaphore_waiting_strategy.h
362 lines (314 loc) · 10.3 KB
/
semaphore_waiting_strategy.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#ifndef THEARTFUL_BROADCAST_QUEUE_SEMAPHORE_WAITER
#define THEARTFUL_BROADCAST_QUEUE_SEMAPHORE_WAITER
#include <atomic>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <system_error>
#include <thread>
#include <vector>
#if defined(__linux__)
#include <semaphore.h>
#include <time.h>
#elif defined(_WIN32) || defined(__MSYS__)
#include <windows.h>
#elif defined(__MACH__)
#include <mach/mach_init.h> // for mach_task_self
#include <mach/semaphore.h> // for the rest of the semaphore interface
#include <mach/task.h> // for semaphore_create and semaphore_destroy
#endif
namespace broadcast_queue {
#if defined(__linux__)
class semaphore {
public:
semaphore(int initial_value = 0) { sem_init(&m_semaphore, 0, initial_value); }
~semaphore() { sem_destroy(&m_semaphore); }
void release(int val = 1) {
for (int i = 0; i < val; i++) {
if (sem_post(&m_semaphore) != 0) {
throw std::system_error(errno, std::system_category());
}
}
}
void acquire() {
if (sem_wait(&m_semaphore) == 0) {
return;
} else {
throw std::system_error(errno, std::system_category());
}
}
bool try_acquire() {
if (sem_trywait(&m_semaphore) == 0) {
return true;
} else {
if (errno == EAGAIN || errno == EINTR) {
return false;
} else {
throw std::system_error(errno, std::system_category());
}
}
}
template <typename Rep, typename Period>
bool try_acquire_for(const std::chrono::duration<Rep, Period> &timeout) {
struct timespec timeout_spec;
clock_gettime(CLOCK_MONOTONIC, &timeout_spec);
auto secs = std::chrono::duration_cast<std::chrono::seconds>(timeout);
timeout_spec.tv_sec += secs.count();
timeout_spec.tv_nsec +=
std::chrono::duration_cast<std::chrono::nanoseconds>(timeout - secs)
.count();
// make sure that tv_nsec is less than 1e9
if (timeout_spec.tv_nsec > 1000000000) {
timeout_spec.tv_sec++;
timeout_spec.tv_nsec -= 1000000000;
}
if (sem_clockwait(&m_semaphore, CLOCK_MONOTONIC, &timeout_spec) == 0) {
return true;
} else {
if (errno == ETIMEDOUT || errno == EINTR) {
return false;
} else {
throw std::system_error(errno, std::system_category());
}
}
}
template <typename Clock, typename Duration>
bool
try_acquire_until(const std::chrono::time_point<Clock, Duration> &until) {
auto now = Clock::now();
if (now > until)
return false;
return try_acquire_for(until - now);
}
int value() {
int value;
if (sem_getvalue(&m_semaphore, &value) == 0) {
return value;
} else {
throw std::system_error(errno, std::system_category());
}
}
private:
sem_t m_semaphore;
};
#elif defined(_WIN32) || defined(__MSYS__)
class semaphore {
public:
semaphore(int initial_value = 0) {
m_semaphore = CreateSemaphore(NULL, initial_value, INT_MAX, NULL);
m_semaphore_value = 0;
}
~semaphore() { CloseHandle(m_semaphore); }
void release(int val = 1) {
m_semaphore_value.fetch_add(val, std::memory_order_relaxed);
ReleaseSemaphore(m_semaphore, val, NULL);
}
void acquire() {
switch (WaitForSingleObject(m_semaphore, INFINITE)) {
case WAIT_OBJECT_0:
m_semaphore_value.fetch_sub(1, std::memory_order_relaxed);
return;
case WAIT_ABANDONED:
case WAIT_TIMEOUT:
case WAIT_FAILED:
default:
return;
}
}
bool try_acquire() {
switch (WaitForSingleObject(m_semaphore, 0)) {
case WAIT_OBJECT_0:
m_semaphore_value.fetch_sub(1, std::memory_order_relaxed);
return true;
case WAIT_ABANDONED:
case WAIT_TIMEOUT:
case WAIT_FAILED:
default:
return false;
}
}
template <typename Rep, typename Period>
bool try_acquire_for(const std::chrono::duration<Rep, Period> &timeout) {
DWORD millis = static_cast<DWORD>(
std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count());
switch (WaitForSingleObject(m_semaphore, millis)) {
case WAIT_OBJECT_0:
m_semaphore_value.fetch_sub(1, std::memory_order_relaxed);
return true;
case WAIT_ABANDONED:
case WAIT_TIMEOUT:
case WAIT_FAILED:
default:
return false;
}
}
template <typename Clock, typename Duration>
bool
try_acquire_until(const std::chrono::time_point<Clock, Duration> &until) {
auto now = Clock::now();
if (now > until)
return false;
return try_acquire_for(until - now);
}
int value() { return m_semaphore_value.load(std::memory_order_relaxed); }
private:
HANDLE m_semaphore;
std::atomic<uint32_t> m_semaphore_value;
};
#elif defined(__MACH__)
class semaphore {
public:
semaphore(int initial_value = 0) {
kern_return_t result = semaphore_create(mach_task_self(), &m_semaphore,
SYNC_POLICY_FIFO, initial_value);
switch (result) {
case KERN_SUCCESS:
// hooray
break;
case KERN_INVALID_ARGUMENT:
throw std::logic_error("semaphore_create invalid argument!");
break;
case KERN_RESOURCE_SHORTAGE:
throw std::runtime_error("semaphore_create resource shortage!");
break;
default:
throw std::runtime_error("semaphore_create unknown error!");
break;
}
}
~semaphore() { semaphore_destroy(mach_task_self(), m_semaphore); }
void release(int val = 1) {
m_semaphore_value.fetch_add(val, std::memory_order_relaxed);
for (int i = 0; i < val; i++) {
switch (semaphore_signal(m_semaphore)) {
case KERN_SUCCESS:
// hooray
break;
case KERN_INVALID_ARGUMENT:
throw std::logic_error("semaphore_signal invalid argument!");
break;
case KERN_TERMINATED:
throw std::runtime_error(
"semaphore_signal semaphore has been terminated!");
break;
}
}
}
void acquire() {
kern_return_t result;
while ((result = semaphore_wait(m_semaphore)) != KERN_SUCCESS) {
switch (result) {
case KERN_TERMINATED:
throw std::runtime_error(
"semaphore_signal semaphore has been terminated!");
break;
case KERN_INVALID_ARGUMENT:
throw std::logic_error("semaphore_signal invalid argument!");
break;
case KERN_ABORTED:
// spurious wake
break;
}
}
m_semaphore_value.fetch_sub(1, std::memory_order_relaxed);
}
bool try_acquire() { return try_acquire_for(std::chrono::seconds(0)); }
template <typename Rep, typename Period>
bool try_acquire_for(const std::chrono::duration<Rep, Period> &timeout) {
mach_timespec_t timeout_spec;
auto secs = std::chrono::duration_cast<std::chrono::seconds>(timeout);
timeout_spec.tv_sec = secs.count();
timeout_spec.tv_nsec =
std::chrono::duration_cast<std::chrono::nanoseconds>(timeout - secs)
.count();
switch (semaphore_timedwait(m_semaphore, timeout_spec)) {
case KERN_SUCCESS:
// hooray
m_semaphore_value.fetch_sub(1, std::memory_order_relaxed);
return true;
case KERN_TERMINATED:
throw std::runtime_error(
"semaphore_signal semaphore has been terminated!");
return false;
case KERN_INVALID_ARGUMENT:
throw std::logic_error("semaphore_signal invalid argument!");
return false;
case KERN_INVALID_VALUE:
throw std::logic_error("semaphore_signal invalid time value!");
return false;
case KERN_ABORTED: // spurious wake
case KERN_OPERATION_TIMED_OUT:
default:
return false;
}
}
template <typename Clock, typename Duration>
bool
try_acquire_until(const std::chrono::time_point<Clock, Duration> &until) {
auto now = Clock::now();
if (now > until)
return false;
return try_acquire_for(until - now);
}
int value() { return m_semaphore_value.load(std::memory_order_relaxed); }
private:
semaphore_t m_semaphore;
std::atomic<uint32_t> m_semaphore_value;
};
#endif
class semaphore_waiting_strategy {
public:
semaphore_waiting_strategy() : m_semaphore{0}, m_waiters{0} {}
~semaphore_waiting_strategy() {}
void notify(std::atomic<uint32_t> &sequence_number) {
// getting the value is ok even though it might change because if it changes
// it would mean that a reader decremented it and read the new value that
// we're notifying on, and it's okay if we overcount him
int semaphore_value = m_semaphore.value();
// the number of waiters isn't accurate because:
// 1. it might decrease from the point we loaded it here, until the point
// we use it. this is ok since this would mean we overcounted a waiter,
// and it this won't cause problems
// 2. it might increase from point we loaded it here, until the point we use
// it, but this is ok, since the reader will check again on the sequence
// number after he has increased the number of waiters, and in that case
// he would find that the sequence number has changed and wouldn't need
// to wait
auto waiters = m_waiters.load(std::memory_order_relaxed);
if (waiters > semaphore_value)
m_semaphore.release(waiters - semaphore_value);
}
template <typename Rep, typename Period>
bool wait(std::atomic<uint32_t> &sequence_number,
uint32_t old_sequence_number,
const std::chrono::duration<Rep, Period> &timeout) {
m_waiters.fetch_add(1, std::memory_order_acq_rel);
// we have to check again on the sequence number after we've incremented
// m_waiters because the writer might miss our increment if he was already
// in the notify method
// but if he's in the notify method, then the sequence_number has changed
if (sequence_number.load(std::memory_order_relaxed) !=
old_sequence_number) {
m_waiters.fetch_sub(1, std::memory_order_relaxed);
return true;
}
auto until = std::chrono::steady_clock::now() + timeout;
do {
if (m_semaphore.try_acquire_for(timeout)) {
if (sequence_number.load(std::memory_order_relaxed) !=
old_sequence_number) {
m_waiters.fetch_sub(1, std::memory_order_relaxed);
return true;
}
}
} while (std::chrono::steady_clock::now() < until);
// timed out
m_waiters.fetch_sub(1, std::memory_order_relaxed);
return false;
}
private:
semaphore m_semaphore;
std::atomic<uint64_t> m_waiters;
};
} // namespace broadcast_queue
#endif // THEARTFUL_BROADCAST_QUEUE_SEMAPHORE_WAITER