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
|
#pragma once
#include "fwd.h"
#include "lfstack.h"
#include <util/generic/ptr.h>
#include <util/system/yassert.h>
#include <atomic>
struct TDefaultLFCounter {
template <class T>
void IncCount(const T& data) {
(void)data;
}
template <class T>
void DecCount(const T& data) {
(void)data;
}
};
// @brief lockfree queue
// @tparam T - the queue element, should be movable
// @tparam TCounter, a observer class to count number of items in queue
// be careful, IncCount and DecCount can be called on a moved object and
// it is TCounter class responsibility to check validity of passed object
template <class T, class TCounter>
class TLockFreeQueue: public TNonCopyable {
struct TListNode {
template <typename U>
TListNode(U&& u, TListNode* next)
: Next(next)
, Data(std::forward<U>(u))
{
}
template <typename U>
explicit TListNode(U&& u)
: Data(std::forward<U>(u))
{
}
std::atomic<TListNode*> Next;
T Data;
};
// using inheritance to be able to use 0 bytes for TCounter when we don't need one
struct TRootNode: public TCounter {
std::atomic<TListNode*> PushQueue = nullptr;
std::atomic<TListNode*> PopQueue = nullptr;
std::atomic<TListNode*> ToDelete = nullptr;
std::atomic<TRootNode*> NextFree = nullptr;
void CopyCounter(TRootNode* x) {
*(TCounter*)this = *(TCounter*)x;
}
};
static void EraseList(TListNode* n) {
while (n) {
TListNode* keepNext = n->Next.load(std::memory_order_acquire);
delete n;
n = keepNext;
}
}
alignas(64) std::atomic<TRootNode*> JobQueue;
alignas(64) std::atomic<size_t> FreememCounter;
alignas(64) std::atomic<size_t> FreeingTaskCounter;
alignas(64) std::atomic<TRootNode*> FreePtr;
void TryToFreeAsyncMemory() {
const auto keepCounter = FreeingTaskCounter.load();
TRootNode* current = FreePtr.load(std::memory_order_acquire);
if (current == nullptr)
return;
if (FreememCounter.load() == 1) {
// we are the last thread, try to cleanup
// check if another thread have cleaned up
if (keepCounter != FreeingTaskCounter.load()) {
return;
}
if (FreePtr.compare_exchange_strong(current, nullptr)) {
// free list
while (current) {
TRootNode* p = current->NextFree.load(std::memory_order_acquire);
EraseList(current->ToDelete.load(std::memory_order_acquire));
delete current;
current = p;
}
++FreeingTaskCounter;
}
}
}
void AsyncRef() {
++FreememCounter;
}
void AsyncUnref() {
TryToFreeAsyncMemory();
--FreememCounter;
}
void AsyncDel(TRootNode* toDelete, TListNode* lst) {
toDelete->ToDelete.store(lst, std::memory_order_release);
for (auto freePtr = FreePtr.load();;) {
toDelete->NextFree.store(freePtr, std::memory_order_release);
if (FreePtr.compare_exchange_weak(freePtr, toDelete))
break;
}
}
void AsyncUnref(TRootNode* toDelete, TListNode* lst) {
TryToFreeAsyncMemory();
if (--FreememCounter == 0) {
// no other operations in progress, can safely reclaim memory
EraseList(lst);
delete toDelete;
} else {
// Dequeue()s in progress, put node to free list
AsyncDel(toDelete, lst);
}
}
struct TListInvertor {
TListNode* Copy;
TListNode* Tail;
TListNode* PrevFirst;
TListInvertor()
: Copy(nullptr)
, Tail(nullptr)
, PrevFirst(nullptr)
{
}
~TListInvertor() {
EraseList(Copy);
}
void CopyWasUsed() {
Copy = nullptr;
Tail = nullptr;
PrevFirst = nullptr;
}
void DoCopy(TListNode* ptr) {
TListNode* newFirst = ptr;
TListNode* newCopy = nullptr;
TListNode* newTail = nullptr;
while (ptr) {
if (ptr == PrevFirst) {
// short cut, we have copied this part already
Tail->Next.store(newCopy, std::memory_order_release);
newCopy = Copy;
Copy = nullptr; // do not destroy prev try
if (!newTail)
newTail = Tail; // tried to invert same list
break;
}
TListNode* newElem = new TListNode(ptr->Data, newCopy);
newCopy = newElem;
ptr = ptr->Next.load(std::memory_order_acquire);
if (!newTail)
newTail = newElem;
}
EraseList(Copy); // copy was useless
Copy = newCopy;
PrevFirst = newFirst;
Tail = newTail;
}
};
void EnqueueImpl(TListNode* head, TListNode* tail) {
TRootNode* newRoot = new TRootNode;
AsyncRef();
newRoot->PushQueue.store(head, std::memory_order_release);
for (TRootNode* curRoot = JobQueue.load(std::memory_order_acquire);;) {
tail->Next.store(curRoot->PushQueue.load(std::memory_order_acquire), std::memory_order_release);
newRoot->PopQueue.store(curRoot->PopQueue.load(std::memory_order_acquire), std::memory_order_release);
newRoot->CopyCounter(curRoot);
for (TListNode* node = head;; node = node->Next.load(std::memory_order_acquire)) {
newRoot->IncCount(node->Data);
if (node == tail)
break;
}
if (JobQueue.compare_exchange_weak(curRoot, newRoot)) {
AsyncUnref(curRoot, nullptr);
break;
}
}
}
template <typename TCollection>
static void FillCollection(TListNode* lst, TCollection* res) {
while (lst) {
res->emplace_back(std::move(lst->Data));
lst = lst->Next.load(std::memory_order_acquire);
}
}
/** Traverses a given list simultaneously creating its inversed version.
* After that, fills a collection with a reversed version and returns the last visited lst's node.
*/
template <typename TCollection>
static TListNode* FillCollectionReverse(TListNode* lst, TCollection* res) {
if (!lst) {
return nullptr;
}
TListNode* newCopy = nullptr;
do {
TListNode* newElem = new TListNode(std::move(lst->Data), newCopy);
newCopy = newElem;
lst = lst->Next.load(std::memory_order_acquire);
} while (lst);
FillCollection(newCopy, res);
EraseList(newCopy);
return lst;
}
public:
TLockFreeQueue()
: JobQueue(new TRootNode)
, FreememCounter(0)
, FreeingTaskCounter(0)
, FreePtr(nullptr)
{
}
~TLockFreeQueue() {
AsyncRef();
AsyncUnref(); // should free FreeList
EraseList(JobQueue.load(std::memory_order_relaxed)->PushQueue.load(std::memory_order_relaxed));
EraseList(JobQueue.load(std::memory_order_relaxed)->PopQueue.load(std::memory_order_relaxed));
delete JobQueue;
}
template <typename U>
void Enqueue(U&& data) {
TListNode* newNode = new TListNode(std::forward<U>(data));
EnqueueImpl(newNode, newNode);
}
void Enqueue(T&& data) {
TListNode* newNode = new TListNode(std::move(data));
EnqueueImpl(newNode, newNode);
}
void Enqueue(const T& data) {
TListNode* newNode = new TListNode(data);
EnqueueImpl(newNode, newNode);
}
template <typename TCollection>
void EnqueueAll(const TCollection& data) {
EnqueueAll(data.begin(), data.end());
}
template <typename TIter>
void EnqueueAll(TIter dataBegin, TIter dataEnd) {
if (dataBegin == dataEnd)
return;
TIter i = dataBegin;
TListNode* node = new TListNode(*i);
TListNode* tail = node;
for (++i; i != dataEnd; ++i) {
TListNode* nextNode = node;
node = new TListNode(*i, nextNode);
}
EnqueueImpl(node, tail);
}
bool Dequeue(T* data) {
TRootNode* newRoot = nullptr;
TListInvertor listInvertor;
AsyncRef();
for (TRootNode* curRoot = JobQueue.load(std::memory_order_acquire);;) {
TListNode* tail = curRoot->PopQueue.load(std::memory_order_acquire);
if (tail) {
// has elems to pop
if (!newRoot)
newRoot = new TRootNode;
newRoot->PushQueue.store(curRoot->PushQueue.load(std::memory_order_acquire), std::memory_order_release);
newRoot->PopQueue.store(tail->Next.load(std::memory_order_acquire), std::memory_order_release);
newRoot->CopyCounter(curRoot);
newRoot->DecCount(tail->Data);
Y_ASSERT(curRoot->PopQueue.load() == tail);
if (JobQueue.compare_exchange_weak(curRoot, newRoot)) {
*data = std::move(tail->Data);
tail->Next.store(nullptr, std::memory_order_release);
AsyncUnref(curRoot, tail);
return true;
}
continue;
}
if (curRoot->PushQueue.load(std::memory_order_acquire) == nullptr) {
delete newRoot;
AsyncUnref();
return false; // no elems to pop
}
if (!newRoot)
newRoot = new TRootNode;
newRoot->PushQueue.store(nullptr, std::memory_order_release);
listInvertor.DoCopy(curRoot->PushQueue.load(std::memory_order_acquire));
newRoot->PopQueue.store(listInvertor.Copy, std::memory_order_release);
newRoot->CopyCounter(curRoot);
Y_ASSERT(curRoot->PopQueue.load() == nullptr);
if (JobQueue.compare_exchange_weak(curRoot, newRoot)) {
AsyncDel(curRoot, curRoot->PushQueue.load(std::memory_order_acquire));
curRoot = newRoot;
newRoot = nullptr;
listInvertor.CopyWasUsed();
} else {
newRoot->PopQueue.store(nullptr, std::memory_order_release);
}
}
}
template <typename TCollection>
void DequeueAll(TCollection* res) {
AsyncRef();
TRootNode* newRoot = new TRootNode;
TRootNode* curRoot = JobQueue.load(std::memory_order_acquire);
do {
} while (!JobQueue.compare_exchange_weak(curRoot, newRoot));
FillCollection(curRoot->PopQueue, res);
TListNode* toDeleteHead = curRoot->PushQueue;
TListNode* toDeleteTail = FillCollectionReverse(curRoot->PushQueue, res);
curRoot->PushQueue.store(nullptr, std::memory_order_release);
if (toDeleteTail) {
toDeleteTail->Next.store(curRoot->PopQueue.load());
} else {
toDeleteTail = curRoot->PopQueue;
}
curRoot->PopQueue.store(nullptr, std::memory_order_release);
AsyncUnref(curRoot, toDeleteHead);
}
bool IsEmpty() {
AsyncRef();
TRootNode* curRoot = JobQueue.load(std::memory_order_acquire);
bool res = curRoot->PushQueue.load(std::memory_order_acquire) == nullptr &&
curRoot->PopQueue.load(std::memory_order_acquire) == nullptr;
AsyncUnref();
return res;
}
TCounter GetCounter() {
AsyncRef();
TRootNode* curRoot = JobQueue.load(std::memory_order_acquire);
TCounter res = *(TCounter*)curRoot;
AsyncUnref();
return res;
}
};
template <class T, class TCounter>
class TAutoLockFreeQueue {
public:
using TRef = THolder<T>;
inline ~TAutoLockFreeQueue() {
TRef tmp;
while (Dequeue(&tmp)) {
}
}
inline bool Dequeue(TRef* t) {
T* res = nullptr;
if (Queue.Dequeue(&res)) {
t->Reset(res);
return true;
}
return false;
}
inline void Enqueue(TRef& t) {
Queue.Enqueue(t.Get());
Y_UNUSED(t.Release());
}
inline void Enqueue(TRef&& t) {
Queue.Enqueue(t.Get());
Y_UNUSED(t.Release());
}
inline bool IsEmpty() {
return Queue.IsEmpty();
}
inline TCounter GetCounter() {
return Queue.GetCounter();
}
private:
TLockFreeQueue<T*, TCounter> Queue;
};
|