diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/generic/singleton.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/generic/singleton.cpp')
-rw-r--r-- | util/generic/singleton.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/util/generic/singleton.cpp b/util/generic/singleton.cpp new file mode 100644 index 00000000000..eb5a0662f8d --- /dev/null +++ b/util/generic/singleton.cpp @@ -0,0 +1,61 @@ +#include "singleton.h" + +#include <util/system/spinlock.h> +#include <util/system/thread.h> +#include <util/system/sanitizers.h> + +#include <cstring> + +namespace { + static inline bool MyAtomicTryLock(TAtomic& a, TAtomicBase v) noexcept { + return AtomicCas(&a, v, 0); + } + + static inline bool MyAtomicTryAndTryLock(TAtomic& a, TAtomicBase v) noexcept { + 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) + Y_UNUSED(ptr); + Y_UNUSED(len); +#else + if constexpr (NSan::TSanIsOn()) { + Y_UNUSED(ptr); + Y_UNUSED(len); + } else { + memset(ptr, 0xBA, len); + } +#endif +} + +void NPrivate::LockRecursive(TAtomic& lock) noexcept { + 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)); + } +} + +void NPrivate::UnlockRecursive(TAtomic& lock) noexcept { + Y_VERIFY(AtomicGet(lock) == MyThreadId(), "unlock from another thread?!?!"); + AtomicUnlock(&lock); +} |