| 14 | /// </summary> |
| 15 | template<typename T, int32 MaxThreads = (PLATFORM_THREADS_LIMIT >= 16 ? PLATFORM_THREADS_LIMIT : 16)> |
| 16 | class ThreadLocal |
| 17 | { |
| 18 | protected: |
| 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 | |
| 33 | public: |
| 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 | |
| 46 | public: |
| 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 | } |
nothing calls this directly
no test coverage detected