UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WindowsPlatformTLS.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
8
9#if WITH_EDITOR
10
12
13// Custom cross-platform dynamic TLS implementation because we hit Windows TLS slot limit (1088). compile-time defined limits for the maximum
14// number of TLS slots and the maximum number of threads that use TLS. No dynamic memory allocation. Lock-free.
15// Getting and setting a slot value is fast but has an additional indirection compared with OS TLS.
16// Memory footprint is (roughly): MaxSlots * 4B + MaxThreads * 4B + MaxSlots * MaxThreads * 8B
17
19{
20 inline constexpr uint32 MaxSlots = WINDOWS_MAX_NUM_TLS_SLOTS; // how many slots are available
21 inline constexpr uint32 MaxThreads = WINDOWS_MAX_NUM_THREADS_WITH_TLS_SLOTS; // how many threads can use TLS
22
23 // a single OS TLS slot that is used to store the thread storage
25
27}
28
29#endif
30
35 : public FGenericPlatformTLS
36{
46
52#if WITH_EDITOR
54#else
59
60#endif
61
67#if WITH_EDITOR
68 CORE_API static void FreeTlsSlot(uint32 SlotIndex);
69#else
70 UE_FORCEINLINE_HINT static void FreeTlsSlot(uint32 SlotIndex)
71 {
72 Windows::TlsFree(SlotIndex);
73 }
74#endif
75
82 static inline void SetTlsValue(uint32 SlotIndex, void* Value)
83 {
84#if WITH_EDITOR
85 using namespace WindowsPlatformTLS_Private;
86
87 checkf(SlotIndex < MaxSlots, TEXT("Invalid slot index %u"), SlotIndex);
88
90 if (ThreadStorage == 0)
91 {
93 }
94 ThreadStorage[SlotIndex] = Value;
95#else
96 Windows::TlsSetValue(SlotIndex, Value);
97#endif
98 }
99
106 static inline void* GetTlsValue(uint32 SlotIndex)
107 {
108#if WITH_EDITOR
109 using namespace WindowsPlatformTLS_Private;
110
111 checkf(SlotIndex < MaxSlots, TEXT("Invalid slot index %u"), SlotIndex);
112
114 return ThreadStorage ? ThreadStorage[SlotIndex] : nullptr;
115#else
116 return Windows::TlsGetValue(SlotIndex);
117#endif
118 }
119};
120
121
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FWindowsPlatformTLS FPlatformTLS
Definition WindowsPlatformTLS.h:122
uint32_t uint32
Definition binka_ue_file_header.h:6
MINIMAL_WINDOWS_API BOOL WINAPI TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue)
MINIMAL_WINDOWS_API DWORD WINAPI GetCurrentThreadId()
MINIMAL_WINDOWS_API LPVOID WINAPI TlsGetValue(DWORD dwTlsIndex)
MINIMAL_WINDOWS_API BOOL WINAPI TlsFree(DWORD dwTlsIndex)
MINIMAL_WINDOWS_API LPVOID WINAPI FlsGetValue(DWORD dwFlsIndex)
MINIMAL_WINDOWS_API DWORD WINAPI TlsAlloc()
Definition GenericPlatformTLS.h:12
Definition WindowsPlatformTLS.h:36
static void SetTlsValue(uint32 SlotIndex, void *Value)
Definition WindowsPlatformTLS.h:82
static UE_FORCEINLINE_HINT void FreeTlsSlot(uint32 SlotIndex)
Definition WindowsPlatformTLS.h:70
static UE_FORCEINLINE_HINT uint32 AllocTlsSlot()
Definition WindowsPlatformTLS.h:55
static void * GetTlsValue(uint32 SlotIndex)
Definition WindowsPlatformTLS.h:106
static UE_FORCEINLINE_HINT uint32 GetCurrentThreadId(void)
Definition WindowsPlatformTLS.h:42