aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/tls.h
blob: 3c4f56dbeb7f033c49c9d4a3be2d9bfd54879e2e (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#pragma once

#include "defaults.h"

#include <util/generic/ptr.h>
#include <util/generic/noncopyable.h>

#include <new>

#if defined(_darwin_)
    #define Y_DISABLE_THRKEY_OPTIMIZATION
#endif

#if defined(_arm_) && defined(_linux_)
    #define Y_DISABLE_THRKEY_OPTIMIZATION
#endif

#if defined(__GNUC__) && defined(__ANDROID__) && defined(__i686__) // https://st.yandex-team.ru/DEVTOOLS-3352
    #define Y_DISABLE_THRKEY_OPTIMIZATION
#endif

/**
    @def Y_THREAD(TType)

    A thread-local wrapper for a given class. Suitable for POD and classes with a constructor with a single argument.

    The wrapper can be treated as the original class in many cases, as it has the same signature for the constructor and an implicit cast to the origianl class.

    Has methods :
        - implicit caster to TType
        - TType& Get()
        - TType* GetPtr()

    Time complexity: getting a variable takes O(number of threads where the variable has been constructed)

    Memory usage: O(number of threads where the variable has been constructed)

    Best practices:
        - storing singletons won't result in heavy memory overheads
        - storing pointers allows complex constructors as well as lazy constructions
        - storing static variables won't result in heavy memory overheads

    Possibly bad practices:
        - field in a class with numerous instances and numerous threads will result in slow working and memory overheads

    Example:
        @code
        //the field declaration in header
        Y_THREAD(TBuffer) TmpBuffer;
        //...later somewhere in cpp...
        TmpBuffer.Clear();
        for (size_t i = 0; i < sz && TrieCursor[i].second.IsFork(); ++i) {
            TmpBuffer.Append(TrieCursor[i].second.Char);
        }
        @endcode

    Example:
        @code
        //the field decalrataion in header
        Y_THREAD(TMyWriter*) ThreadLocalWriter;
        //...later somewhere in cpp...
        TMyWriter*& writerRef = ThreadLocalWriter.Get();
        if (writerRef == nullptr) {
            THolder<TMyWriter> threadLocalWriter( new TMyWriter(
                *Session,
                MinLogError,
                MaxRps,
                LogFraction,
                WriteCounters,
                Log));
            writerRef = threadLocalWriter.Get();
        }
        @endcode

    Example:
        @code
        //in header
        namespace TMorph {
            Y_THREAD(ELanguage) ThreadLocalMainLanguage;
        }
        //in cpp
        Y_THREAD(ELanguage) TMorph::ThreadLocalMainLanguage(LANG_RUS);
        @endcode

    Example:
        @code
        Y_THREAD(TScoreCalcer*) ScoreCalcerPtr;
        static TScoreCalcer* GetScoreCalcer(yint maxElemCount) {
            if (ScoreCalcerPtr == 0) {
                ScoreCalcerPtr = new TScoreCalcer();
                ScoreCalcerPtr->Alloc(maxElemCount);
            }
            return ScoreCalcerPtr;
        }
        @endcode

    @param TType POD or a class with a constructor taking 1 argument
**/

/**
    @def Y_STATIC_THREAD(TType)

    Equivalent to "static Y_THREAD(TType)"

    @see Y_THREAD(TType)
**/

/**
    @def Y_POD_THREAD(TType)

    Same interface as Y_THREAD(TType), but TType must be a POD.
    Implemented (based on the compiler) as Y_THREAD(TType) or as native tls.

    @see Y_THREAD(TType)
**/

/**
    @def STATIC_POD_THREAD(TType)

    Equivalent to "static Y_POD_THREAD(TType)"

    @see Y_POD_THREAD(TType)
**/

#define Y_THREAD(T) ::NTls::TValue<T>
#define Y_STATIC_THREAD(T) static Y_THREAD(T)

// gcc and msvc support automatic tls for POD types
#if defined(Y_DISABLE_THRKEY_OPTIMIZATION)
// nothing to do
#elif defined(__clang__)
    #define Y_POD_THREAD(T) thread_local T
    #define Y_POD_STATIC_THREAD(T) static thread_local T
#elif defined(__GNUC__) && !defined(_cygwin_) && !defined(_arm_) && !defined(__IOS_SIMULATOR__)
    #define Y_POD_THREAD(T) __thread T
    #define Y_POD_STATIC_THREAD(T) static __thread T
// msvc doesn't support __declspec(thread) in dlls, loaded manually (via LoadLibrary)
#elif (defined(_MSC_VER) && !defined(_WINDLL)) || defined(_arm_)
    #define Y_POD_THREAD(T) __declspec(thread) T
    #define Y_POD_STATIC_THREAD(T) __declspec(thread) static T
#endif

#if !defined(Y_POD_THREAD) || !defined(Y_POD_STATIC_THREAD)
    #define Y_POD_THREAD(T) Y_THREAD(T)
    #define Y_POD_STATIC_THREAD(T) Y_STATIC_THREAD(T)
#else
    #define Y_HAVE_FAST_POD_TLS
#endif

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

namespace NTls {
    using TDtor = void (*)(void*);

    class TKey {
    public:
        TKey(TDtor dtor);
        TKey(TKey&&) noexcept;
        ~TKey();

        void* Get() const;
        void Set(void* ptr) const;

        static void Cleanup() noexcept;

    private:
        class TImpl;
        THolder<TImpl> Impl_;
    };

    struct TCleaner {
        inline ~TCleaner() {
            TKey::Cleanup();
        }
    };

    template <class T>
    class TValue: public TMoveOnly {
        class TConstructor {
        public:
            TConstructor() noexcept = default;

            virtual ~TConstructor() = default;

            virtual T* Construct(void* ptr) const = 0;
        };

        class TDefaultConstructor: public TConstructor {
        public:
            ~TDefaultConstructor() override = default;

            T* Construct(void* ptr) const override {
                //memset(ptr, 0, sizeof(T));
                return ::new (ptr) T();
            }
        };

        template <class T1>
        class TCopyConstructor: public TConstructor {
        public:
            inline TCopyConstructor(const T1& value)
                : Value(value)
            {
            }

            ~TCopyConstructor() override = default;

            T* Construct(void* ptr) const override {
                return ::new (ptr) T(Value);
            }

        private:
            T1 Value;
        };

    public:
        inline TValue()
            : Constructor_(new TDefaultConstructor())
            , Key_(Dtor)
        {
        }

        template <class T1>
        inline TValue(const T1& value)
            : Constructor_(new TCopyConstructor<T1>(value))
            , Key_(Dtor)
        {
        }

        template <class T1>
        inline T& operator=(const T1& val) {
            return Get() = val;
        }

        inline operator const T&() const {
            return Get();
        }

        inline operator T&() {
            return Get();
        }

        inline const T& operator->() const {
            return Get();
        }

        inline T& operator->() {
            return Get();
        }

        inline const T* operator&() const {
            return GetPtr();
        }

        inline T* operator&() {
            return GetPtr();
        }

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

        inline T* GetPtr() const {
            T* val = static_cast<T*>(Key_.Get());

            if (!val) {
                THolder<void> mem(::operator new(sizeof(T)));
                THolder<T> newval(Constructor_->Construct(mem.Get()));

                Y_UNUSED(mem.Release());
                Key_.Set((void*)newval.Get());
                val = newval.Release();
            }

            return val;
        }

    private:
        static void Dtor(void* ptr) {
            THolder<void> mem(ptr);

            ((T*)ptr)->~T();
            ::NPrivate::FillWithTrash(ptr, sizeof(T));
        }

    private:
        THolder<TConstructor> Constructor_;
        TKey Key_;
    };
}

template <class T>
static inline T& TlsRef(NTls::TValue<T>& v) noexcept {
    return v;
}

template <class T>
static inline const T& TlsRef(const NTls::TValue<T>& v) noexcept {
    return v;
}

template <class T>
static inline T& TlsRef(T& v) noexcept {
    return v;
}