diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/system/atexit.cpp | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/system/atexit.cpp')
-rw-r--r-- | util/system/atexit.cpp | 188 |
1 files changed, 94 insertions, 94 deletions
diff --git a/util/system/atexit.cpp b/util/system/atexit.cpp index 4b72d53204..74fb10b6b1 100644 --- a/util/system/atexit.cpp +++ b/util/system/atexit.cpp @@ -1,109 +1,109 @@ -#include "atexit.h" +#include "atexit.h" #include "atomic.h" -#include "yassert.h" -#include "spinlock.h" -#include "thread.h" - +#include "yassert.h" +#include "spinlock.h" +#include "thread.h" + #include <util/generic/ylimits.h> -#include <util/generic/utility.h> -#include <util/generic/deque.h> -#include <util/generic/queue.h> - -#include <tuple> - -#include <cstdlib> - -namespace { - class TAtExit { - struct TFunc { - TAtExitFunc Func; - void* Ctx; - size_t Priority; - size_t Number; - }; - - struct TCmp { - inline bool operator()(const TFunc* l, const TFunc* r) const noexcept { - return std::tie(l->Priority, l->Number) < std::tie(r->Priority, r->Number); - } - }; - - public: - inline TAtExit() noexcept - : FinishStarted_(0) - { - } - +#include <util/generic/utility.h> +#include <util/generic/deque.h> +#include <util/generic/queue.h> + +#include <tuple> + +#include <cstdlib> + +namespace { + class TAtExit { + struct TFunc { + TAtExitFunc Func; + void* Ctx; + size_t Priority; + size_t Number; + }; + + struct TCmp { + inline bool operator()(const TFunc* l, const TFunc* r) const noexcept { + return std::tie(l->Priority, l->Number) < std::tie(r->Priority, r->Number); + } + }; + + public: + inline TAtExit() noexcept + : FinishStarted_(0) + { + } + inline void Finish() noexcept { AtomicSet(FinishStarted_, 1); - - auto guard = Guard(Lock_); - - while (Items_) { - auto c = Items_.top(); - + + auto guard = Guard(Lock_); + + while (Items_) { + auto c = Items_.top(); + Y_ASSERT(c); - - Items_.pop(); - { - auto unguard = Unguard(guard); + Items_.pop(); + + { + auto unguard = Unguard(guard); - try { + try { c->Func(c->Ctx); - } catch (...) { - // ¯\_(ツ)_/¯ + } catch (...) { + // ¯\_(ツ)_/¯ } - } - } - } - - inline void Register(TAtExitFunc func, void* ctx, size_t priority) { - with_lock (Lock_) { + } + } + } + + inline void Register(TAtExitFunc func, void* ctx, size_t priority) { + with_lock (Lock_) { Store_.push_back({func, ctx, priority, Store_.size()}); - Items_.push(&Store_.back()); - } - } - + Items_.push(&Store_.back()); + } + } + inline bool FinishStarted() const { return AtomicGet(FinishStarted_); } - - private: - TAdaptiveLock Lock_; + + private: + TAdaptiveLock Lock_; TAtomic FinishStarted_; TDeque<TFunc> Store_; TPriorityQueue<TFunc*, TVector<TFunc*>, TCmp> Items_; - }; - - static TAtomic atExitLock = 0; + }; + + static TAtomic atExitLock = 0; static TAtExit* volatile atExitPtr = nullptr; - alignas(TAtExit) static char atExitMem[sizeof(TAtExit)]; - - static void OnExit() { + alignas(TAtExit) static char atExitMem[sizeof(TAtExit)]; + + static void OnExit() { if (TAtExit* const atExit = AtomicGet(atExitPtr)) { - atExit->Finish(); - atExit->~TAtExit(); + atExit->Finish(); + atExit->~TAtExit(); AtomicSet(atExitPtr, nullptr); - } - } - - static inline TAtExit* Instance() { + } + } + + static inline TAtExit* Instance() { if (TAtExit* const atExit = AtomicGet(atExitPtr)) { return atExit; } - with_lock (atExitLock) { + with_lock (atExitLock) { if (TAtExit* const atExit = AtomicGet(atExitPtr)) { return atExit; - } + } atexit(OnExit); TAtExit* const atExit = new (atExitMem) TAtExit; AtomicSet(atExitPtr, atExit); return atExit; - } - } -} - + } + } +} + void ManualRunAtExitFinalizers() { OnExit(); } @@ -115,22 +115,22 @@ bool ExitStarted() { return false; } -void AtExit(TAtExitFunc func, void* ctx, size_t priority) { - Instance()->Register(func, ctx, priority); -} - -void AtExit(TAtExitFunc func, void* ctx) { - AtExit(func, ctx, Max<size_t>()); -} - -static void TraditionalCloser(void* ctx) { +void AtExit(TAtExitFunc func, void* ctx, size_t priority) { + Instance()->Register(func, ctx, priority); +} + +void AtExit(TAtExitFunc func, void* ctx) { + AtExit(func, ctx, Max<size_t>()); +} + +static void TraditionalCloser(void* ctx) { reinterpret_cast<TTraditionalAtExitFunc>(ctx)(); -} - -void AtExit(TTraditionalAtExitFunc func) { +} + +void AtExit(TTraditionalAtExitFunc func) { AtExit(TraditionalCloser, reinterpret_cast<void*>(func)); -} - -void AtExit(TTraditionalAtExitFunc func, size_t priority) { +} + +void AtExit(TTraditionalAtExitFunc func, size_t priority) { AtExit(TraditionalCloser, reinterpret_cast<void*>(func), priority); -} +} |