-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathwsq.hpp
More file actions
483 lines (385 loc) · 13.6 KB
/
wsq.hpp
File metadata and controls
483 lines (385 loc) · 13.6 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#pragma once
/**
@file wsq.hpp
@brief standalone work-stealing queue implementation extracted from the Taskflow project
Reference: Vyukov, "Bounded MPMC Queue", 1024cores.net, 2009.
Lê et al., "Correct and Efficient Work-Stealing for Weak Memory
Models", PPoPP 2013, §push.
*/
#include <atomic>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <type_traits>
#include <vector>
#ifndef WSQ_CACHELINE_SIZE
#define WSQ_CACHELINE_SIZE 64
#endif
#ifndef WSQ_DEFAULT_BOUNDED_LOG_SIZE
#define WSQ_DEFAULT_BOUNDED_LOG_SIZE 8
#endif
#ifndef WSQ_DEFAULT_UNBOUNDED_LOG_SIZE
#define WSQ_DEFAULT_UNBOUNDED_LOG_SIZE 10
#endif
namespace wsq {
// ----------------------------------------------------------------------------
// UnboundedWSQ
// ----------------------------------------------------------------------------
template <typename T>
class UnboundedWSQ {
struct Array {
size_t C;
size_t M;
std::atomic<T>* S;
explicit Array(size_t c) :
C {c},
M {c-1},
S {new std::atomic<T>[C]} {
}
~Array() {
delete [] S;
}
size_t capacity() const noexcept {
return C;
}
void push(int64_t i, T o) noexcept {
S[i & M].store(o, std::memory_order_relaxed);
}
T pop(int64_t i) noexcept {
return S[i & M].load(std::memory_order_relaxed);
}
Array* resize(int64_t b, int64_t t) {
Array* ptr = new Array{2*C};
for(int64_t i=t; i!=b; ++i) {
ptr->push(i, pop(i));
}
return ptr;
}
Array* resize(int64_t b, int64_t t, size_t N) {
Array* ptr = new Array{std::bit_ceil(C + N)};
for(int64_t i=t; i!=b; ++i) {
ptr->push(i, pop(i));
}
return ptr;
}
};
alignas(WSQ_CACHELINE_SIZE) std::atomic<int64_t> _top;
alignas(WSQ_CACHELINE_SIZE) std::atomic<int64_t> _bottom;
// Owner-private cached upper bound on _top. Never read by thieves.
// Because _top is never decremented, the real occupancy can only be
// smaller than what is computed using this cached value, so using it
// for the overflow check is always safe.
int64_t _cached_top {0};
// _array on its own cache line: avoids false-sharing with _bottom when
// thieves load _array (consume) after reading _bottom (acquire).
alignas(WSQ_CACHELINE_SIZE) std::atomic<Array*> _array;
std::vector<Array*> _garbage;
Array* _resize_array(Array* a, int64_t b, int64_t t);
Array* _resize_array(Array* a, int64_t b, int64_t t, size_t N);
public:
using value_type = std::conditional_t<std::is_pointer_v<T>, T, std::optional<T>>;
explicit UnboundedWSQ(int64_t LogSize = WSQ_DEFAULT_UNBOUNDED_LOG_SIZE);
~UnboundedWSQ();
UnboundedWSQ(const UnboundedWSQ&) = delete;
UnboundedWSQ& operator=(const UnboundedWSQ&) = delete;
bool empty() const noexcept;
size_t size() const noexcept;
size_t capacity() const noexcept;
void push(T item);
template <typename I>
void bulk_push(I first, size_t N);
value_type pop();
value_type steal();
value_type steal_with_feedback(size_t& num_empty_steals);
static constexpr value_type empty_value() noexcept {
if constexpr (std::is_pointer_v<T>) return T{nullptr};
else return std::optional<T>{std::nullopt};
}
};
// --- UnboundedWSQ implementation ---
template <typename T>
UnboundedWSQ<T>::UnboundedWSQ(int64_t LogSize) {
_top.store(0, std::memory_order_relaxed);
_bottom.store(0, std::memory_order_relaxed);
_array.store(new Array{(size_t{1} << LogSize)}, std::memory_order_relaxed);
_garbage.reserve(32);
}
template <typename T>
UnboundedWSQ<T>::~UnboundedWSQ() {
for(auto a : _garbage) delete a;
delete _array.load();
}
template <typename T>
bool UnboundedWSQ<T>::empty() const noexcept {
int64_t t = _top.load(std::memory_order_relaxed);
int64_t b = _bottom.load(std::memory_order_relaxed);
return (b <= t);
}
template <typename T>
size_t UnboundedWSQ<T>::size() const noexcept {
int64_t t = _top.load(std::memory_order_relaxed);
int64_t b = _bottom.load(std::memory_order_relaxed);
return static_cast<size_t>(b >= t ? b - t : 0);
}
template <typename T>
size_t UnboundedWSQ<T>::capacity() const noexcept {
return _array.load(std::memory_order_relaxed)->capacity();
}
template <typename T>
void UnboundedWSQ<T>::push(T o) {
int64_t b = _bottom.load(std::memory_order_relaxed);
Array* a = _array.load(std::memory_order_relaxed);
// Use cached upper bound of _top — avoids cross-core acquire on common path.
// Refresh only when cached value suggests the array may be full.
if(a->capacity() < static_cast<size_t>(b - _cached_top + 1)) [[unlikely]] {
_cached_top = _top.load(std::memory_order_acquire);
if(a->capacity() < static_cast<size_t>(b - _cached_top + 1)) [[unlikely]] {
a = _resize_array(a, b, _cached_top);
}
}
a->push(b, o);
std::atomic_thread_fence(std::memory_order_release);
_bottom.store(b + 1, std::memory_order_release);
}
template <typename T>
template <typename I>
void UnboundedWSQ<T>::bulk_push(I first, size_t N) {
if(N == 0) return;
int64_t b = _bottom.load(std::memory_order_relaxed);
Array* a = _array.load(std::memory_order_relaxed);
// queue is full with N additional items
if((b - _cached_top + N) > a->capacity()) [[unlikely]] {
_cached_top = _top.load(std::memory_order_acquire);
if((b - _cached_top + N) > a->capacity()) [[unlikely]] {
a = _resize_array(a, b, _cached_top, N);
}
}
for(size_t i=0; i<N; ++i) {
a->push(b++, first[i]);
}
std::atomic_thread_fence(std::memory_order_release);
_bottom.store(b, std::memory_order_release);
}
template <typename T>
typename UnboundedWSQ<T>::value_type UnboundedWSQ<T>::pop() {
int64_t b = _bottom.load(std::memory_order_relaxed) - 1;
Array* a = _array.load(std::memory_order_relaxed);
_bottom.store(b, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_seq_cst);
int64_t t = _top.load(std::memory_order_relaxed);
auto item = empty_value();
if(t <= b) {
item = a->pop(b);
if(t == b) {
// the last item just got stolen
if(!_top.compare_exchange_strong(t, t + 1,
std::memory_order_seq_cst,
std::memory_order_relaxed)) {
item = empty_value();
}
_bottom.store(b + 1, std::memory_order_relaxed);
}
}
else {
_bottom.store(b + 1, std::memory_order_relaxed);
}
return item;
}
template <typename T>
typename UnboundedWSQ<T>::value_type UnboundedWSQ<T>::steal() {
int64_t t = _top.load(std::memory_order_acquire);
std::atomic_thread_fence(std::memory_order_seq_cst);
int64_t b = _bottom.load(std::memory_order_acquire);
auto item = empty_value();
if(t < b) {
Array* a = _array.load(std::memory_order_consume);
item = a->pop(t);
if(!_top.compare_exchange_strong(t, t+1,
std::memory_order_seq_cst, std::memory_order_relaxed)) {
return empty_value();
}
}
return item;
}
template <typename T>
typename UnboundedWSQ<T>::value_type
UnboundedWSQ<T>::steal_with_feedback(size_t& num_empty_steals) {
int64_t t = _top.load(std::memory_order_acquire);
std::atomic_thread_fence(std::memory_order_seq_cst);
int64_t b = _bottom.load(std::memory_order_acquire);
auto item = empty_value();
if(t < b) {
num_empty_steals = 0;
Array* a = _array.load(std::memory_order_consume);
item = a->pop(t);
if(!_top.compare_exchange_strong(t, t+1,
std::memory_order_seq_cst, std::memory_order_relaxed)) {
return empty_value();
}
}
else {
++num_empty_steals;
}
return item;
}
template <typename T>
typename UnboundedWSQ<T>::Array*
UnboundedWSQ<T>::_resize_array(Array* a, int64_t b, int64_t t) {
Array* tmp = a->resize(b, t);
_garbage.push_back(a);
_array.store(tmp, std::memory_order_release);
return tmp;
}
template <typename T>
typename UnboundedWSQ<T>::Array*
UnboundedWSQ<T>::_resize_array(Array* a, int64_t b, int64_t t, size_t N) {
Array* tmp = a->resize(b, t, N);
_garbage.push_back(a);
_array.store(tmp, std::memory_order_release);
return tmp;
}
// ----------------------------------------------------------------------------
// BoundedWSQ
// ----------------------------------------------------------------------------
template <typename T, size_t LogSize = WSQ_DEFAULT_BOUNDED_LOG_SIZE>
class BoundedWSQ {
static constexpr size_t BufferSize = size_t{1} << LogSize;
static constexpr size_t BufferMask = BufferSize - 1;
static_assert(BufferSize >= 2 && (BufferSize & BufferMask) == 0);
alignas(WSQ_CACHELINE_SIZE) std::atomic<int64_t> _top {0};
alignas(WSQ_CACHELINE_SIZE) std::atomic<int64_t> _bottom {0};
int64_t _cached_top {0};
alignas(WSQ_CACHELINE_SIZE) std::atomic<T> _buffer[BufferSize];
public:
using value_type = std::conditional_t<std::is_pointer_v<T>, T, std::optional<T>>;
BoundedWSQ() = default;
~BoundedWSQ() = default;
BoundedWSQ(const BoundedWSQ&) = delete;
BoundedWSQ& operator=(const BoundedWSQ&) = delete;
bool empty() const noexcept;
size_t size() const noexcept;
static constexpr size_t capacity() noexcept { return BufferSize; }
template <typename O>
bool try_push(O&& item);
template <typename I>
size_t try_bulk_push(I first, size_t N);
value_type pop();
value_type steal();
value_type steal_with_feedback(size_t& num_empty_steals);
static constexpr value_type empty_value() noexcept {
if constexpr (std::is_pointer_v<T>) return T{nullptr};
else return std::optional<T>{std::nullopt};
}
};
// --- BoundedWSQ implementation ---
template <typename T, size_t LogSize>
bool BoundedWSQ<T, LogSize>::empty() const noexcept {
int64_t t = _top.load(std::memory_order_relaxed);
int64_t b = _bottom.load(std::memory_order_relaxed);
return b <= t;
}
template <typename T, size_t LogSize>
size_t BoundedWSQ<T, LogSize>::size() const noexcept {
int64_t t = _top.load(std::memory_order_relaxed);
int64_t b = _bottom.load(std::memory_order_relaxed);
return static_cast<size_t>(b >= t ? b - t : 0);
}
template <typename T, size_t LogSize>
template <typename O>
bool BoundedWSQ<T, LogSize>::try_push(O&& o) {
int64_t b = _bottom.load(std::memory_order_relaxed);
// Optimistic check using cached _top — no cross-core traffic on common path.
// Refresh and re-check before returning false: thieves may have advanced
// _top since the last refresh, so the queue may have room.
if(static_cast<size_t>(b - _cached_top + 1) > BufferSize) {
_cached_top = _top.load(std::memory_order_acquire);
if(static_cast<size_t>(b - _cached_top + 1) > BufferSize) {
return false;
}
}
_buffer[b & BufferMask].store(std::forward<O>(o), std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);
_bottom.store(b + 1, std::memory_order_release);
return true;
}
template <typename T, size_t LogSize>
template <typename I>
size_t BoundedWSQ<T, LogSize>::try_bulk_push(I first, size_t N) {
if(N == 0) return 0;
int64_t b = _bottom.load(std::memory_order_relaxed);
// Use cached _top for capacity estimate; refresh only when it shows zero room
// to avoid returning 0 based on a stale estimate alone.
size_t r = BufferSize - static_cast<size_t>(b - _cached_top);
if(r == 0) [[unlikely]] {
_cached_top = _top.load(std::memory_order_acquire);
r = BufferSize - static_cast<size_t>(b - _cached_top);
}
size_t n = std::min(N, r);
if(n > 0) {
for(size_t i=0; i<n; ++i) {
_buffer[b++ & BufferMask].store(first[i], std::memory_order_relaxed);
}
std::atomic_thread_fence(std::memory_order_release);
_bottom.store(b, std::memory_order_release);
}
return n;
}
template <typename T, size_t LogSize>
typename BoundedWSQ<T, LogSize>::value_type BoundedWSQ<T, LogSize>::pop() {
int64_t b = _bottom.load(std::memory_order_relaxed) - 1;
_bottom.store(b, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_seq_cst);
int64_t t = _top.load(std::memory_order_relaxed);
auto item = empty_value();
if(t <= b) {
item = _buffer[b & BufferMask].load(std::memory_order_relaxed);
if(t == b) {
if(!_top.compare_exchange_strong(t, t+1,
std::memory_order_seq_cst, std::memory_order_relaxed)) {
item = empty_value();
}
_bottom.store(b + 1, std::memory_order_relaxed);
}
}
else {
_bottom.store(b + 1, std::memory_order_relaxed);
}
return item;
}
template <typename T, size_t LogSize>
typename BoundedWSQ<T, LogSize>::value_type BoundedWSQ<T, LogSize>::steal() {
int64_t t = _top.load(std::memory_order_acquire);
std::atomic_thread_fence(std::memory_order_seq_cst);
int64_t b = _bottom.load(std::memory_order_acquire);
auto item = empty_value();
if(t < b) {
item = _buffer[t & BufferMask].load(std::memory_order_relaxed);
if(!_top.compare_exchange_strong(t, t+1,
std::memory_order_seq_cst, std::memory_order_relaxed)) {
return empty_value();
}
}
return item;
}
template <typename T, size_t LogSize>
typename BoundedWSQ<T, LogSize>::value_type
BoundedWSQ<T, LogSize>::steal_with_feedback(size_t& num_empty_steals) {
int64_t t = _top.load(std::memory_order_acquire);
std::atomic_thread_fence(std::memory_order_seq_cst);
int64_t b = _bottom.load(std::memory_order_acquire);
auto item = empty_value();
if(t < b) {
num_empty_steals = 0;
item = _buffer[t & BufferMask].load(std::memory_order_relaxed);
if(!_top.compare_exchange_strong(t, t+1,
std::memory_order_seq_cst, std::memory_order_relaxed)) {
return empty_value();
}
}
else {
++num_empty_steals;
}
return item;
}
} // namespace wsq