diff options
| author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 | 
|---|---|---|
| committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 | 
| commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
| tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/yt/memory/leaky_ref_counted_singleton-inl.h | |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/yt/memory/leaky_ref_counted_singleton-inl.h')
| -rw-r--r-- | library/cpp/yt/memory/leaky_ref_counted_singleton-inl.h | 43 | 
1 files changed, 43 insertions, 0 deletions
diff --git a/library/cpp/yt/memory/leaky_ref_counted_singleton-inl.h b/library/cpp/yt/memory/leaky_ref_counted_singleton-inl.h new file mode 100644 index 00000000000..a68ec5ed6ae --- /dev/null +++ b/library/cpp/yt/memory/leaky_ref_counted_singleton-inl.h @@ -0,0 +1,43 @@ +#ifndef LEAKY_REF_COUNTED_SINGLETON_INL_H_ +#error "Direct inclusion of this file is not allowed, include leaky_ref_counted_singleton.h" +// For the sake of sane code completion. +#include "leaky_ref_counted_singleton.h" +#endif + +#include "new.h" + +#include <atomic> +#include <mutex> + +#include <util/system/compiler.h> +#include <util/system/sanitizers.h> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +template <class T> +TIntrusivePtr<T> LeakyRefCountedSingleton() +{ +    static std::atomic<T*> Ptr; +    auto* ptr = Ptr.load(std::memory_order_acquire); +    if (Y_LIKELY(ptr)) { +        return ptr; +    } + +    static std::once_flag Initialized; +    std::call_once(Initialized, [] { +        auto ptr = New<T>(); +        Ref(ptr.Get()); +        Ptr.store(ptr.Get()); +#if defined(_asan_enabled_) +        NSan::MarkAsIntentionallyLeaked(ptr.Get()); +#endif +    }); + +    return Ptr.load(); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT  | 
