diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/actors/util | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/actors/util')
-rw-r--r-- | library/cpp/actors/util/funnel_queue.h | 128 | ||||
-rw-r--r-- | library/cpp/actors/util/queue_chunk.h | 4 | ||||
-rw-r--r-- | library/cpp/actors/util/recentwnd.h | 48 | ||||
-rw-r--r-- | library/cpp/actors/util/rope.h | 2 | ||||
-rw-r--r-- | library/cpp/actors/util/threadparkpad.cpp | 152 | ||||
-rw-r--r-- | library/cpp/actors/util/unordered_cache.h | 4 |
6 files changed, 169 insertions, 169 deletions
diff --git a/library/cpp/actors/util/funnel_queue.h b/library/cpp/actors/util/funnel_queue.h index 0e21e2617c..855ed4bee1 100644 --- a/library/cpp/actors/util/funnel_queue.h +++ b/library/cpp/actors/util/funnel_queue.h @@ -3,33 +3,33 @@ #include <util/system/atomic.h> #include <util/generic/noncopyable.h> -template <typename ElementType> -class TFunnelQueue: private TNonCopyable { +template <typename ElementType> +class TFunnelQueue: private TNonCopyable { public: TFunnelQueue() noexcept - : Front(nullptr) - , Back(nullptr) - { - } + : Front(nullptr) + , Back(nullptr) + { + } - virtual ~TFunnelQueue() noexcept { + virtual ~TFunnelQueue() noexcept { for (auto entry = Front; entry; entry = DeleteEntry(entry)) continue; } /// Push element. Can be used from many threads. Return true if is first element. bool - Push(ElementType&& element) noexcept { - TEntry* const next = NewEntry(static_cast<ElementType&&>(element)); - TEntry* const prev = AtomicSwap(&Back, next); + Push(ElementType&& element) noexcept { + TEntry* const next = NewEntry(static_cast<ElementType&&>(element)); + TEntry* const prev = AtomicSwap(&Back, next); AtomicSet(prev ? prev->Next : Front, next); return !prev; } /// Extract top element. Must be used only from one thread. Return true if have more. bool - Pop() noexcept { - if (TEntry* const top = AtomicGet(Front)) { + Pop() noexcept { + if (TEntry* const top = AtomicGet(Front)) { const auto last = AtomicCas(&Back, nullptr, top); if (last) // This is last element in queue. Queue is empty now. AtomicCas(&Front, nullptr, top); @@ -51,42 +51,42 @@ public: /// Peek top element. Must be used only from one thread. ElementType& - Top() const noexcept { + Top() const noexcept { return AtomicGet(Front)->Data; } bool - IsEmpty() const noexcept { + IsEmpty() const noexcept { return !AtomicGet(Front); } protected: - class TEntry: private TNonCopyable { - friend class TFunnelQueue; - + class TEntry: private TNonCopyable { + friend class TFunnelQueue; + private: explicit TEntry(ElementType&& element) noexcept - : Data(static_cast<ElementType&&>(element)) - , Next(nullptr) - { - } + : Data(static_cast<ElementType&&>(element)) + , Next(nullptr) + { + } - ~TEntry() noexcept { - } + ~TEntry() noexcept { + } public: ElementType Data; - TEntry* volatile Next; + TEntry* volatile Next; }; - TEntry* volatile Front; - TEntry* volatile Back; + TEntry* volatile Front; + TEntry* volatile Back; - virtual TEntry* NewEntry(ElementType&& element) noexcept { + virtual TEntry* NewEntry(ElementType&& element) noexcept { return new TEntry(static_cast<ElementType&&>(element)); } - virtual TEntry* DeleteEntry(TEntry* entry) noexcept { + virtual TEntry* DeleteEntry(TEntry* entry) noexcept { const auto next = entry->Next; delete entry; return next; @@ -147,36 +147,36 @@ public: using const_iterator = TConstEntryIter; using iterator = TEntryIter; - iterator begin() { - return {AtomicGet(Front)}; - } - const_iterator cbegin() { - return {AtomicGet(Front)}; - } - const_iterator begin() const { - return {AtomicGet(Front)}; - } - - iterator end() { - return {nullptr}; - } - const_iterator cend() { - return {nullptr}; - } - const_iterator end() const { - return {nullptr}; - } + iterator begin() { + return {AtomicGet(Front)}; + } + const_iterator cbegin() { + return {AtomicGet(Front)}; + } + const_iterator begin() const { + return {AtomicGet(Front)}; + } + + iterator end() { + return {nullptr}; + } + const_iterator cend() { + return {nullptr}; + } + const_iterator end() const { + return {nullptr}; + } }; -template <typename ElementType> -class TPooledFunnelQueue: public TFunnelQueue<ElementType> { +template <typename ElementType> +class TPooledFunnelQueue: public TFunnelQueue<ElementType> { public: TPooledFunnelQueue() noexcept : Stack(nullptr) - { - } + { + } - virtual ~TPooledFunnelQueue() noexcept override { + virtual ~TPooledFunnelQueue() noexcept override { for (auto entry = TBase::Front; entry; entry = TBase::DeleteEntry(entry)) continue; for (auto entry = Stack; entry; entry = TBase::DeleteEntry(entry)) @@ -187,10 +187,10 @@ public: private: typedef TFunnelQueue<ElementType> TBase; - typename TBase::TEntry* volatile Stack; + typename TBase::TEntry* volatile Stack; protected: - virtual typename TBase::TEntry* NewEntry(ElementType&& element) noexcept override { + virtual typename TBase::TEntry* NewEntry(ElementType&& element) noexcept override { while (const auto top = AtomicGet(Stack)) if (AtomicCas(&Stack, top->Next, top)) { top->Data = static_cast<ElementType&&>(element); @@ -201,37 +201,37 @@ protected: return TBase::NewEntry(static_cast<ElementType&&>(element)); } - virtual typename TBase::TEntry* DeleteEntry(typename TBase::TEntry* entry) noexcept override { + virtual typename TBase::TEntry* DeleteEntry(typename TBase::TEntry* entry) noexcept override { entry->Data = ElementType(); const auto next = entry->Next; - do - AtomicSet(entry->Next, AtomicGet(Stack)); + do + AtomicSet(entry->Next, AtomicGet(Stack)); while (!AtomicCas(&Stack, entry, entry->Next)); return next; } }; -template <typename ElementType, template <typename T> class TQueueType = TFunnelQueue> -class TCountedFunnelQueue: public TQueueType<ElementType> { +template <typename ElementType, template <typename T> class TQueueType = TFunnelQueue> +class TCountedFunnelQueue: public TQueueType<ElementType> { public: TCountedFunnelQueue() noexcept : Count(0) - { - } + { + } - TAtomicBase GetSize() const noexcept { + TAtomicBase GetSize() const noexcept { return AtomicGet(Count); } private: typedef TQueueType<ElementType> TBase; - virtual typename TBase::TEntry* NewEntry(ElementType&& element) noexcept override { + virtual typename TBase::TEntry* NewEntry(ElementType&& element) noexcept override { AtomicAdd(Count, 1); return TBase::NewEntry(static_cast<ElementType&&>(element)); } - virtual typename TBase::TEntry* DeleteEntry(typename TBase::TEntry* entry) noexcept override { + virtual typename TBase::TEntry* DeleteEntry(typename TBase::TEntry* entry) noexcept override { AtomicSub(Count, 1); return TBase::DeleteEntry(entry); } diff --git a/library/cpp/actors/util/queue_chunk.h b/library/cpp/actors/util/queue_chunk.h index 8a4e02d8cb..b56f0c1d5c 100644 --- a/library/cpp/actors/util/queue_chunk.h +++ b/library/cpp/actors/util/queue_chunk.h @@ -5,7 +5,7 @@ template <typename T, ui32 TSize, typename TDerived> struct TQueueChunkDerived { static const ui32 EntriesCount = (TSize - sizeof(TQueueChunkDerived*)) / sizeof(T); - static_assert(EntriesCount > 0, "expect EntriesCount > 0"); + static_assert(EntriesCount > 0, "expect EntriesCount > 0"); volatile T Entries[EntriesCount]; TDerived* volatile Next; @@ -18,7 +18,7 @@ struct TQueueChunkDerived { template <typename T, ui32 TSize> struct TQueueChunk { static const ui32 EntriesCount = (TSize - sizeof(TQueueChunk*)) / sizeof(T); - static_assert(EntriesCount > 0, "expect EntriesCount > 0"); + static_assert(EntriesCount > 0, "expect EntriesCount > 0"); volatile T Entries[EntriesCount]; TQueueChunk* volatile Next; diff --git a/library/cpp/actors/util/recentwnd.h b/library/cpp/actors/util/recentwnd.h index ba1ede6f29..6135814f50 100644 --- a/library/cpp/actors/util/recentwnd.h +++ b/library/cpp/actors/util/recentwnd.h @@ -1,15 +1,15 @@ #pragma once - + #include <util/generic/deque.h> template <typename TElem, template <typename, typename...> class TContainer = TDeque> class TRecentWnd { public: - TRecentWnd(ui32 wndSize) - : MaxWndSize_(wndSize) - { - } + TRecentWnd(ui32 wndSize) + : MaxWndSize_(wndSize) + { + } void Push(const TElem& elem) { if (Window_.size() == MaxWndSize_) @@ -23,27 +23,27 @@ public: Window_.emplace_back(std::move(elem)); } - TElem& Last() { - return Window_.back(); - } - const TElem& Last() const { - return Window_.back(); - } - bool Full() const { - return Window_.size() == MaxWndSize_; - } - ui64 Size() const { - return Window_.size(); - } + TElem& Last() { + return Window_.back(); + } + const TElem& Last() const { + return Window_.back(); + } + bool Full() const { + return Window_.size() == MaxWndSize_; + } + ui64 Size() const { + return Window_.size(); + } using const_iterator = typename TContainer<TElem>::const_iterator; - const_iterator begin() { - return Window_.begin(); - } - const_iterator end() { - return Window_.end(); - } + const_iterator begin() { + return Window_.begin(); + } + const_iterator end() { + return Window_.end(); + } void Reset(ui32 wndSize = 0) { Window_.clear(); @@ -60,7 +60,7 @@ public: Window_.begin() + Window_.size() - MaxWndSize_); } } - + private: TContainer<TElem> Window_; ui32 MaxWndSize_; diff --git a/library/cpp/actors/util/rope.h b/library/cpp/actors/util/rope.h index f5595efbaa..305a035448 100644 --- a/library/cpp/actors/util/rope.h +++ b/library/cpp/actors/util/rope.h @@ -574,7 +574,7 @@ public: TRope(TString s) { if (s) { Size = s.size(); - s.reserve(32); + s.reserve(32); Chain.PutToEnd(std::move(s)); } } diff --git a/library/cpp/actors/util/threadparkpad.cpp b/library/cpp/actors/util/threadparkpad.cpp index 74069ff15b..471be44031 100644 --- a/library/cpp/actors/util/threadparkpad.cpp +++ b/library/cpp/actors/util/threadparkpad.cpp @@ -6,143 +6,143 @@ #include "futex.h" namespace NActors { - class TThreadParkPad::TImpl { - volatile bool Interrupted; - int Futex; - - public: - TImpl() - : Interrupted(false) - , Futex(0) - { - } - ~TImpl() { - } + class TThreadParkPad::TImpl { + volatile bool Interrupted; + int Futex; + + public: + TImpl() + : Interrupted(false) + , Futex(0) + { + } + ~TImpl() { + } bool Park() noexcept { - __atomic_fetch_sub(&Futex, 1, __ATOMIC_SEQ_CST); - while (__atomic_load_n(&Futex, __ATOMIC_ACQUIRE) == -1) - SysFutex(&Futex, FUTEX_WAIT_PRIVATE, -1, nullptr, nullptr, 0); - return IsInterrupted(); - } + __atomic_fetch_sub(&Futex, 1, __ATOMIC_SEQ_CST); + while (__atomic_load_n(&Futex, __ATOMIC_ACQUIRE) == -1) + SysFutex(&Futex, FUTEX_WAIT_PRIVATE, -1, nullptr, nullptr, 0); + return IsInterrupted(); + } void Unpark() noexcept { - const int old = __atomic_fetch_add(&Futex, 1, __ATOMIC_SEQ_CST); - if (old == -1) - SysFutex(&Futex, FUTEX_WAKE_PRIVATE, -1, nullptr, nullptr, 0); - } + const int old = __atomic_fetch_add(&Futex, 1, __ATOMIC_SEQ_CST); + if (old == -1) + SysFutex(&Futex, FUTEX_WAKE_PRIVATE, -1, nullptr, nullptr, 0); + } void Interrupt() noexcept { - __atomic_store_n(&Interrupted, true, __ATOMIC_SEQ_CST); - Unpark(); - } + __atomic_store_n(&Interrupted, true, __ATOMIC_SEQ_CST); + Unpark(); + } bool IsInterrupted() const noexcept { - return __atomic_load_n(&Interrupted, __ATOMIC_ACQUIRE); - } - }; + return __atomic_load_n(&Interrupted, __ATOMIC_ACQUIRE); + } + }; #elif defined _win32_ #include <util/generic/bt_exception.h> #include <util/generic/yexception.h> namespace NActors { - class TThreadParkPad::TImpl { + class TThreadParkPad::TImpl { TAtomic Interrupted; - HANDLE EvHandle; - - public: - TImpl() - : Interrupted(false) - { - EvHandle = ::CreateEvent(0, false, false, 0); - if (!EvHandle) - ythrow TWithBackTrace<yexception>() << "::CreateEvent failed"; - } + HANDLE EvHandle; + + public: + TImpl() + : Interrupted(false) + { + EvHandle = ::CreateEvent(0, false, false, 0); + if (!EvHandle) + ythrow TWithBackTrace<yexception>() << "::CreateEvent failed"; + } ~TImpl() { - if (EvHandle) - ::CloseHandle(EvHandle); - } + if (EvHandle) + ::CloseHandle(EvHandle); + } bool Park() noexcept { - ::WaitForSingleObject(EvHandle, INFINITE); + ::WaitForSingleObject(EvHandle, INFINITE); return AtomicGet(Interrupted); - } + } void Unpark() noexcept { - ::SetEvent(EvHandle); - } + ::SetEvent(EvHandle); + } void Interrupt() noexcept { AtomicSet(Interrupted, true); - Unpark(); - } + Unpark(); + } bool IsInterrupted() const noexcept { return AtomicGet(Interrupted); - } - }; + } + }; #else #include <util/system/event.h> namespace NActors { - class TThreadParkPad::TImpl { + class TThreadParkPad::TImpl { TAtomic Interrupted; TSystemEvent Ev; - public: - TImpl() - : Interrupted(false) + public: + TImpl() + : Interrupted(false) , Ev(TSystemEvent::rAuto) - { - } + { + } ~TImpl() { - } + } bool Park() noexcept { - Ev.Wait(); + Ev.Wait(); return AtomicGet(Interrupted); - } - + } + void Unpark() noexcept { - Ev.Signal(); - } - + Ev.Signal(); + } + void Interrupt() noexcept { AtomicSet(Interrupted, true); - Unpark(); - } - + Unpark(); + } + bool IsInterrupted() const noexcept { return AtomicGet(Interrupted); - } - }; -#endif - - TThreadParkPad::TThreadParkPad() - : Impl(new TThreadParkPad::TImpl()) + } + }; +#endif + + TThreadParkPad::TThreadParkPad() + : Impl(new TThreadParkPad::TImpl()) { } - + TThreadParkPad::~TThreadParkPad() { } bool TThreadParkPad::Park() noexcept { - return Impl->Park(); + return Impl->Park(); } void TThreadParkPad::Unpark() noexcept { - Impl->Unpark(); + Impl->Unpark(); } void TThreadParkPad::Interrupt() noexcept { - Impl->Interrupt(); + Impl->Interrupt(); } bool TThreadParkPad::Interrupted() const noexcept { - return Impl->IsInterrupted(); + return Impl->IsInterrupted(); } } diff --git a/library/cpp/actors/util/unordered_cache.h b/library/cpp/actors/util/unordered_cache.h index 76f036c0cf..44ed04c01f 100644 --- a/library/cpp/actors/util/unordered_cache.h +++ b/library/cpp/actors/util/unordered_cache.h @@ -1,7 +1,7 @@ #pragma once #include "defs.h" -#include "queue_chunk.h" +#include "queue_chunk.h" template <typename T, ui32 Size = 512, ui32 ConcurrencyFactor = 1, typename TChunk = TQueueChunk<T, Size>> class TUnorderedCache : TNonCopyable { @@ -30,7 +30,7 @@ private: TReadSlot ReadSlots[Concurrency]; TWriteSlot WriteSlots[Concurrency]; - static_assert(sizeof(TChunk*) == sizeof(TAtomic), "expect sizeof(TChunk*) == sizeof(TAtomic)"); + static_assert(sizeof(TChunk*) == sizeof(TAtomic), "expect sizeof(TChunk*) == sizeof(TAtomic)"); private: struct TLockedWriter { |