aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/singleton.h
blob: f5fa047f5c9aa21598983e273383020e7a490bfb (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once

#include <util/system/atexit.h>
#include <util/system/atomic.h>

#include <new>
#include <utility>

template <class T>
struct TSingletonTraits {
    static constexpr size_t Priority = 65536;
};

namespace NPrivate {
    void FillWithTrash(void* ptr, size_t len);

    void LockRecursive(TAtomic& lock) noexcept;
    void UnlockRecursive(TAtomic& lock) noexcept;

    template <class T>
    void Destroyer(void* ptr) {
        ((T*)ptr)->~T();
        FillWithTrash(ptr, sizeof(T));
    }

    template <class T, size_t P, class... TArgs>
    Y_NO_INLINE T* SingletonBase(T*& ptr, TArgs&&... args) {
        alignas(T) static char buf[sizeof(T)];
        static TAtomic lock;

        LockRecursive(lock);

        auto ret = AtomicGet(ptr);

        try {
            if (!ret) {
                ret = ::new (buf) T(std::forward<TArgs>(args)...);

                try {
                    AtExit(Destroyer<T>, ret, P);
                } catch (...) {
                    Destroyer<T>(ret);

                    throw;
                }

                AtomicSet(ptr, ret);
            }
        } catch (...) {
            UnlockRecursive(lock);

            throw;
        }

        UnlockRecursive(lock);

        return ret;
    }

    template <class T, size_t P, class... TArgs>
    T* SingletonInt(TArgs&&... args) {
        static_assert(sizeof(T) < 32000, "use HugeSingleton instead");

        static T* ptr;
        auto ret = AtomicGet(ptr);

        if (Y_UNLIKELY(!ret)) {
            ret = SingletonBase<T, P>(ptr, std::forward<TArgs>(args)...);
        }

        return ret;
    }

    template <class T>
    class TDefault {
    public:
        template <class... TArgs>
        inline TDefault(TArgs&&... args)
            : T_(std::forward<TArgs>(args)...)
        {
        }

        inline const T* Get() const noexcept {
            return &T_;
        }

    private:
        T T_;
    };

    template <class T>
    struct THeapStore {
        template <class... TArgs>
        inline THeapStore(TArgs&&... args)
            : D(new T(std::forward<TArgs>(args)...))
        {
        }

        inline ~THeapStore() {
            delete D;
        }

        T* D;
    };
}

#define Y_DECLARE_SINGLETON_FRIEND()                \
    template <class T, size_t P, class... TArgs>    \
    friend T* ::NPrivate::SingletonInt(TArgs&&...); \
    template <class T, size_t P, class... TArgs>    \
    friend T* ::NPrivate::SingletonBase(T*&, TArgs&&...);

template <class T, class... TArgs>
T* Singleton(TArgs&&... args) {
    return ::NPrivate::SingletonInt<T, TSingletonTraits<T>::Priority>(std::forward<TArgs>(args)...);
}

template <class T, class... TArgs>
T* HugeSingleton(TArgs&&... args) {
    return Singleton<::NPrivate::THeapStore<T>>(std::forward<TArgs>(args)...)->D;
}

template <class T, size_t P, class... TArgs>
T* SingletonWithPriority(TArgs&&... args) {
    return ::NPrivate::SingletonInt<T, P>(std::forward<TArgs>(args)...);
}

template <class T, size_t P, class... TArgs>
T* HugeSingletonWithPriority(TArgs&&... args) {
    return SingletonWithPriority<::NPrivate::THeapStore<T>, P>(std::forward<TArgs>(args)...)->D;
}

template <class T>
const T& Default() {
    return *(::NPrivate::SingletonInt<typename ::NPrivate::TDefault<T>, TSingletonTraits<T>::Priority>()->Get());
}