diff options
author | gulin <gulin@yandex-team.ru> | 2022-02-10 16:47:31 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:31 +0300 |
commit | c807aae441c17fc7f577c35757a4b6e0bd909802 (patch) | |
tree | fc3268f43edbf6f854c0266cd05b91952484179b /util/thread | |
parent | d06e6190fa85c1fb4b011631503d53ea39942ff9 (diff) | |
download | ydb-c807aae441c17fc7f577c35757a4b6e0bd909802.tar.gz |
Restoring authorship annotation for <gulin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/thread')
-rw-r--r-- | util/thread/lfqueue.h | 38 | ||||
-rw-r--r-- | util/thread/lfstack.h | 82 |
2 files changed, 60 insertions, 60 deletions
diff --git a/util/thread/lfqueue.h b/util/thread/lfqueue.h index ab523631e4..2717c56bb3 100644 --- a/util/thread/lfqueue.h +++ b/util/thread/lfqueue.h @@ -10,14 +10,14 @@ struct TDefaultLFCounter { template <class T> void IncCount(const T& data) { - (void)data; - } + (void)data; + } template <class T> void DecCount(const T& data) { - (void)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 @@ -43,7 +43,7 @@ class TLockFreeQueue: public TNonCopyable { T Data; }; - // using inheritance to be able to use 0 bytes for TCounter when we don't need one + // using inheritance to be able to use 0 bytes for TCounter when we don't need one struct TRootNode: public TCounter { TListNode* volatile PushQueue; TListNode* volatile PopQueue; @@ -58,8 +58,8 @@ class TLockFreeQueue: public TNonCopyable { { } void CopyCounter(TRootNode* x) { - *(TCounter*)this = *(TCounter*)x; - } + *(TCounter*)this = *(TCounter*)x; + } }; static void EraseList(TListNode* n) { @@ -233,8 +233,8 @@ public: { } ~TLockFreeQueue() { - AsyncRef(); - AsyncUnref(); // should free FreeList + AsyncRef(); + AsyncUnref(); // should free FreeList EraseList(JobQueue->PushQueue); EraseList(JobQueue->PopQueue); delete JobQueue; @@ -285,8 +285,8 @@ public: AtomicSet(newRoot->PushQueue, AtomicGet(curRoot->PushQueue)); AtomicSet(newRoot->PopQueue, AtomicGet(tail->Next)); - newRoot->CopyCounter(curRoot); - newRoot->DecCount(tail->Data); + newRoot->CopyCounter(curRoot); + newRoot->DecCount(tail->Data); Y_ASSERT(AtomicGet(curRoot->PopQueue) == tail); if (AtomicCas(&JobQueue, newRoot, curRoot)) { *data = std::move(tail->Data); @@ -307,7 +307,7 @@ public: AtomicSet(newRoot->PushQueue, nullptr); listInvertor.DoCopy(AtomicGet(curRoot->PushQueue)); AtomicSet(newRoot->PopQueue, listInvertor.Copy); - newRoot->CopyCounter(curRoot); + newRoot->CopyCounter(curRoot); Y_ASSERT(AtomicGet(curRoot->PopQueue) == nullptr); if (AtomicCas(&JobQueue, newRoot, curRoot)) { newRoot = nullptr; @@ -351,12 +351,12 @@ public: return res; } TCounter GetCounter() { - AsyncRef(); + AsyncRef(); TRootNode* curRoot = AtomicGet(JobQueue); - TCounter res = *(TCounter*)curRoot; - AsyncUnref(); - return res; - } + TCounter res = *(TCounter*)curRoot; + AsyncUnref(); + return res; + } }; template <class T, class TCounter> diff --git a/util/thread/lfstack.h b/util/thread/lfstack.h index ca3d95f3c3..98135e1ee9 100644 --- a/util/thread/lfstack.h +++ b/util/thread/lfstack.h @@ -121,66 +121,66 @@ public: AtomicAdd(DequeueCount, -1); return false; } - // add all elements to *res + // add all elements to *res // elements are returned in order of dequeue (top to bottom; see example in unittest) template <typename TCollection> void DequeueAll(TCollection* res) { - AtomicAdd(DequeueCount, 1); + AtomicAdd(DequeueCount, 1); for (TNode* current = AtomicGet(Head); current; current = AtomicGet(Head)) { if (AtomicCas(&Head, (TNode*)nullptr, current)) { for (TNode* x = current; x;) { res->push_back(std::move(x->Value)); - x = x->Next; - } - // EraseList(current); // ABA problem - // even more complex node deletion - TryToFreeMemory(); - if (AtomicAdd(DequeueCount, -1) == 0) { - // no other Dequeue()s, can safely reclaim memory - EraseList(current); - } else { - // Dequeue()s in progress, add nodes list to free list + x = x->Next; + } + // EraseList(current); // ABA problem + // even more complex node deletion + TryToFreeMemory(); + if (AtomicAdd(DequeueCount, -1) == 0) { + // no other Dequeue()s, can safely reclaim memory + EraseList(current); + } else { + // Dequeue()s in progress, add nodes list to free list TNode* currentLast = current; - while (currentLast->Next) { + while (currentLast->Next) { currentLast = currentLast->Next; - } - for (;;) { + } + for (;;) { AtomicSet(currentLast->Next, AtomicGet(FreePtr)); - if (AtomicCas(&FreePtr, current, currentLast->Next)) - break; - } - } - return; - } - } - TryToFreeMemory(); - AtomicAdd(DequeueCount, -1); - } + if (AtomicCas(&FreePtr, current, currentLast->Next)) + break; + } + } + return; + } + } + TryToFreeMemory(); + AtomicAdd(DequeueCount, -1); + } bool DequeueSingleConsumer(T* res) { for (TNode* current = AtomicGet(Head); current; current = AtomicGet(Head)) { - if (AtomicCas(&Head, current->Next, current)) { + if (AtomicCas(&Head, current->Next, current)) { *res = std::move(current->Value); - delete current; // with single consumer thread ABA does not happen - return true; - } - } - return false; - } - // add all elements to *res - // elements are returned in order of dequeue (top to bottom; see example in unittest) + delete current; // with single consumer thread ABA does not happen + return true; + } + } + return false; + } + // add all elements to *res + // elements are returned in order of dequeue (top to bottom; see example in unittest) template <typename TCollection> void DequeueAllSingleConsumer(TCollection* res) { for (TNode* current = AtomicGet(Head); current; current = AtomicGet(Head)) { if (AtomicCas(&Head, (TNode*)nullptr, current)) { for (TNode* x = current; x;) { res->push_back(std::move(x->Value)); - x = x->Next; - } - EraseList(current); // with single consumer thread ABA does not happen - return; - } - } - } + x = x->Next; + } + EraseList(current); // with single consumer thread ABA does not happen + return; + } + } + } bool IsEmpty() { AtomicAdd(DequeueCount, 0); // mem barrier return AtomicGet(Head) == nullptr; // without lock, so result is approximate |