MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / ThreadLocal

Class ThreadLocal

Source/Engine/Threading/ThreadLocal.h:16–158  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14/// </summary>
15template<typename T, int32 MaxThreads = (PLATFORM_THREADS_LIMIT >= 16 ? PLATFORM_THREADS_LIMIT : 16)>
16class ThreadLocal
17{
18protected:
19 static_assert(TIsPODType<T>::Value, "Only POD types are supported");
20
21 struct Bucket
22 {
23 volatile int64 ThreadID;
24 T Value;
25 };
26
27 Bucket _staticBuckets[MaxThreads];
28#if THREAD_LOCAL_USE_DYNAMIC_BUCKETS
29 Bucket* _dynamicBuckets = nullptr;
30 constexpr static int32 DynamicMaxThreads = 1024;
31#endif
32
33public:
34 ThreadLocal()
35 {
36 Platform::MemoryClear(_staticBuckets, sizeof(_staticBuckets));
37 }
38
39#if THREAD_LOCAL_USE_DYNAMIC_BUCKETS
40 ~ThreadLocal()
41 {
42 Platform::Free(_dynamicBuckets);
43 }
44#endif
45
46public:
47 FORCE_INLINE T& Get()
48 {
49 return GetBucket().Value;
50 }
51
52 FORCE_INLINE void Set(const T& value)
53 {
54 GetBucket().Value = value;
55 }
56
57 int32 Count() const
58 {
59 int32 result = 0;
60 for (int32 i = 0; i < MaxThreads; i++)
61 {
62 if (Platform::AtomicRead((int64 volatile*)&_staticBuckets[i].ThreadID) != 0)
63 result++;
64 }
65#if THREAD_LOCAL_USE_DYNAMIC_BUCKETS
66 if (auto dynamicBuckets = (Bucket*)Platform::AtomicRead((intptr volatile*)&_dynamicBuckets))
67 {
68 for (int32 i = 0; i < DynamicMaxThreads; i++)
69 {
70 if (Platform::AtomicRead((int64 volatile*)&dynamicBuckets[i].ThreadID) != 0)
71 result++;
72 }
73 }

Callers

nothing calls this directly

Calls 5

MemoryClearFunction · 0.85
GetCurrentThreadIDFunction · 0.50
AtomicReadFunction · 0.50
FreeFunction · 0.50

Tested by

no test coverage detected