-
Notifications
You must be signed in to change notification settings - Fork 35
/
wsq.hpp
242 lines (194 loc) · 5.85 KB
/
wsq.hpp
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
#pragma once
#include <atomic>
#include <vector>
#include <optional>
#include <cassert>
/**
@class: WorkStealingQueue
@tparam T data type
@brief Lock-free unbounded single-producer multiple-consumer queue.
This class implements the work stealing queue described in the paper,
"Correct and Efficient Work-Stealing for Weak Memory Models,"
available at https://www.di.ens.fr/~zappa/readings/ppopp13.pdf.
Only the queue owner can perform pop and push operations,
while others can steal data from the queue.
*/
template <typename T>
class WorkStealingQueue {
struct Array {
int64_t C;
int64_t M;
std::atomic<T>* S;
explicit Array(int64_t c) :
C {c},
M {c-1},
S {new std::atomic<T>[static_cast<size_t>(C)]} {
}
~Array() {
delete [] S;
}
int64_t capacity() const noexcept {
return C;
}
template <typename O>
void push(int64_t i, O&& o) noexcept {
S[i & M].store(std::forward<O>(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;
}
};
std::atomic<int64_t> _top;
std::atomic<int64_t> _bottom;
std::atomic<Array*> _array;
std::vector<Array*> _garbage;
public:
/**
@brief constructs the queue with a given capacity
@param capacity the capacity of the queue (must be power of 2)
*/
explicit WorkStealingQueue(int64_t capacity = 1024);
/**
@brief destructs the queue
*/
~WorkStealingQueue();
/**
@brief queries if the queue is empty at the time of this call
*/
bool empty() const noexcept;
/**
@brief queries the number of items at the time of this call
*/
size_t size() const noexcept;
/**
@brief queries the capacity of the queue
*/
int64_t capacity() const noexcept;
/**
@brief inserts an item to the queue
Only the owner thread can insert an item to the queue.
The operation can trigger the queue to resize its capacity
if more space is required.
@tparam O data type
@param item the item to perfect-forward to the queue
*/
template <typename O>
void push(O&& item);
/**
@brief pops out an item from the queue
Only the owner thread can pop out an item from the queue.
The return can be a @std_nullopt if this operation failed (empty queue).
*/
std::optional<T> pop();
/**
@brief steals an item from the queue
Any threads can try to steal an item from the queue.
The return can be a @std_nullopt if this operation failed (not necessary empty).
*/
std::optional<T> steal();
};
// Constructor
template <typename T>
WorkStealingQueue<T>::WorkStealingQueue(int64_t c) {
assert(c && (!(c & (c-1))));
_top.store(0, std::memory_order_relaxed);
_bottom.store(0, std::memory_order_relaxed);
_array.store(new Array{c}, std::memory_order_relaxed);
_garbage.reserve(32);
}
// Destructor
template <typename T>
WorkStealingQueue<T>::~WorkStealingQueue() {
for(auto a : _garbage) {
delete a;
}
delete _array.load();
}
// Function: empty
template <typename T>
bool WorkStealingQueue<T>::empty() const noexcept {
int64_t b = _bottom.load(std::memory_order_relaxed);
int64_t t = _top.load(std::memory_order_relaxed);
return b <= t;
}
// Function: size
template <typename T>
size_t WorkStealingQueue<T>::size() const noexcept {
int64_t b = _bottom.load(std::memory_order_relaxed);
int64_t t = _top.load(std::memory_order_relaxed);
return static_cast<size_t>(b >= t ? b - t : 0);
}
// Function: push
template <typename T>
template <typename O>
void WorkStealingQueue<T>::push(O&& o) {
int64_t b = _bottom.load(std::memory_order_relaxed);
int64_t t = _top.load(std::memory_order_acquire);
Array* a = _array.load(std::memory_order_relaxed);
// queue is full
if(a->capacity() - 1 < (b - t)) {
Array* tmp = a->resize(b, t);
_garbage.push_back(a);
std::swap(a, tmp);
_array.store(a, std::memory_order_relaxed);
}
a->push(b, std::forward<O>(o));
std::atomic_thread_fence(std::memory_order_release);
_bottom.store(b + 1, std::memory_order_relaxed);
}
// Function: pop
template <typename T>
std::optional<T> WorkStealingQueue<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);
std::optional<T> item;
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 = std::nullopt;
}
_bottom.store(b + 1, std::memory_order_relaxed);
}
}
else {
_bottom.store(b + 1, std::memory_order_relaxed);
}
return item;
}
// Function: steal
template <typename T>
std::optional<T> WorkStealingQueue<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);
std::optional<T> item;
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 std::nullopt;
}
}
return item;
}
// Function: capacity
template <typename T>
int64_t WorkStealingQueue<T>::capacity() const noexcept {
return _array.load(std::memory_order_relaxed)->capacity();
}