aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/leaky_ref_counted_singleton-inl.h
blob: a68ec5ed6aedb4b01cf2a2be0f9e47f3d5e85ffb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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