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 /util/generic/singleton.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/generic/singleton.cpp')
-rw-r--r-- | util/generic/singleton.cpp | 90 |
1 files changed, 45 insertions, 45 deletions
diff --git a/util/generic/singleton.cpp b/util/generic/singleton.cpp index eb5a0662f8..b193a589ab 100644 --- a/util/generic/singleton.cpp +++ b/util/generic/singleton.cpp @@ -1,61 +1,61 @@ -#include "singleton.h" - -#include <util/system/spinlock.h> -#include <util/system/thread.h> +#include "singleton.h" + +#include <util/system/spinlock.h> +#include <util/system/thread.h> #include <util/system/sanitizers.h> - -#include <cstring> - -namespace { + +#include <cstring> + +namespace { static inline bool MyAtomicTryLock(TAtomic& a, TAtomicBase v) noexcept { - return AtomicCas(&a, v, 0); - } - + return AtomicCas(&a, v, 0); + } + static inline bool MyAtomicTryAndTryLock(TAtomic& a, TAtomicBase v) noexcept { - return (AtomicGet(a) == 0) && MyAtomicTryLock(a, v); - } - + return (AtomicGet(a) == 0) && MyAtomicTryLock(a, v); + } + static inline TAtomicBase MyThreadId() noexcept { - const TAtomicBase ret = TThread::CurrentThreadId(); - - if (ret) { - return ret; - } - - //clash almost impossible, ONLY if we have threads with ids 0 and 1! - return 1; - } -} - -void NPrivate::FillWithTrash(void* ptr, size_t len) { -#if defined(NDEBUG) + const TAtomicBase ret = TThread::CurrentThreadId(); + + if (ret) { + return ret; + } + + //clash almost impossible, ONLY if we have threads with ids 0 and 1! + return 1; + } +} + +void NPrivate::FillWithTrash(void* ptr, size_t len) { +#if defined(NDEBUG) Y_UNUSED(ptr); Y_UNUSED(len); -#else +#else if constexpr (NSan::TSanIsOn()) { Y_UNUSED(ptr); Y_UNUSED(len); } else { memset(ptr, 0xBA, len); } -#endif -} - +#endif +} + void NPrivate::LockRecursive(TAtomic& lock) noexcept { - const TAtomicBase id = MyThreadId(); - + const TAtomicBase id = MyThreadId(); + Y_VERIFY(AtomicGet(lock) != id, "recursive singleton initialization"); - - if (!MyAtomicTryLock(lock, id)) { - TSpinWait sw; - - do { - sw.Sleep(); - } while (!MyAtomicTryAndTryLock(lock, id)); - } -} - + + if (!MyAtomicTryLock(lock, id)) { + TSpinWait sw; + + do { + sw.Sleep(); + } while (!MyAtomicTryAndTryLock(lock, id)); + } +} + void NPrivate::UnlockRecursive(TAtomic& lock) noexcept { Y_VERIFY(AtomicGet(lock) == MyThreadId(), "unlock from another thread?!?!"); - AtomicUnlock(&lock); -} + AtomicUnlock(&lock); +} |