aboutsummaryrefslogtreecommitdiffstats
path: root/util/thread/singleton.h
blob: fac938d2c59911674347416e4d74a75e537b50cb (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
#pragma once

#include <util/system/tls.h>
#include <util/generic/singleton.h>
#include <util/generic/ptr.h>

namespace NPrivate {
    template <class T, size_t Priority>
    struct TFastThreadSingletonHelper {
        static inline T* GetSlow() {
            return SingletonWithPriority<NTls::TValue<T>, Priority>()->GetPtr();
        }

        static inline T* Get() {
#if defined(Y_HAVE_FAST_POD_TLS)
            Y_POD_STATIC_THREAD(T*) fast(nullptr);

            if (Y_UNLIKELY(!fast)) {
                fast = GetSlow();
            }

            return fast;
#else
            return GetSlow();
#endif
        }
    };
} // namespace NPrivate

template <class T, size_t Priority>
Y_RETURNS_NONNULL static inline T* FastTlsSingletonWithPriority() {
    return ::NPrivate::TFastThreadSingletonHelper<T, Priority>::Get();
}

// NB: the singleton is the same for all modules that use
// FastTlsSingleton with the same type parameter. If unique singleton
// required, use unique types.
template <class T>
Y_RETURNS_NONNULL static inline T* FastTlsSingleton() {
    return FastTlsSingletonWithPriority<T, TSingletonTraits<T>::Priority>();
}