UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RunnableThread.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"
6#include "Containers/Array.h"
8#include "HAL/PlatformTLS.h"
10
11class FRunnable;
12class FTlsAutoCleanup;
13
20{
22 friend class FTlsAutoCleanup;
23 friend class FThreadManager;
24 friend class FForkableThread;
25 friend class FForkProcessHelper;
26
28 static CORE_API uint32 RunnableTlsSlot;
29
30public:
31
33 static CORE_API uint32 GetTlsSlot();
34
45 class FRunnable* InRunnable,
46 const TCHAR* ThreadName,
51
58
66 virtual bool SetThreadAffinity( const FThreadAffinity& Affinity ) { return false; };
67
74 virtual void Suspend( bool bShouldPause = true ) = 0;
75
86 virtual bool Kill( bool bShouldWait = true ) = 0;
87
89 virtual void WaitForCompletion() = 0;
90
92 enum class ThreadType
93 {
94 // Regular thread that executes the runnable object in it's own context
95 Real,
96 // Fake threads are created for a single threaded environment and are always executed from the main tick
97 Fake,
98 // Forkable threads will behave like fake threads for the master process, but will become real threads on forked processes
100 };
101
104 {
105 return ThreadType::Real;
106 }
107
114 const uint32 GetThreadID() const
115 {
116 return ThreadID;
117 }
118
125 const FString& GetThreadName() const
126 {
127 return ThreadName;
128 }
129
132 {
133 return ThreadPriority;
134 }
135
138
140 CORE_API virtual ~FRunnableThread();
141
146 {
147 FRunnableThread* RunnableThread = (FRunnableThread*)FPlatformTLS::GetTlsValue(RunnableTlsSlot);
148 return RunnableThread;
149 }
150
151protected:
152
166
168 CORE_API void SetTls();
169
171 CORE_API void FreeTls();
172
174 FString ThreadName;
175
178
181
184
187
190
191private:
192
195
197 virtual void Tick() {}
198
202 virtual void OnPreFork()
203 {
204 checkf(false, TEXT("Only forkable threads should receive OnPreFork."));
205 }
206
210 virtual void OnPostFork()
211 {
212 checkf(false, TEXT("Only forkable threads should receive OnPostFork."));
213 }
214
218 CORE_API void PostCreate(EThreadPriority ThreadPriority);
219};
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EThreadPriority
Definition GenericPlatformAffinity.h:26
@ TPri_Normal
Definition GenericPlatformAffinity.h:27
EThreadCreateFlags
Definition GenericPlatformAffinity.h:38
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Event.h:21
Definition Fork.h:41
Definition ThreadingBase.cpp:1561
static const uint64 GetNoAffinityMask()
Definition GenericPlatformAffinity.h:88
Definition RunnableThread.h:20
virtual void WaitForCompletion()=0
CORE_API void SetTls()
Definition ThreadingBase.cpp:920
EThreadPriority ThreadPriority
Definition RunnableThread.h:186
EThreadPriority GetThreadPriority() const
Definition RunnableThread.h:131
FEvent * ThreadInitSyncEvent
Definition RunnableThread.h:180
virtual FRunnableThread::ThreadType GetThreadType() const
Definition RunnableThread.h:103
const uint32 GetThreadID() const
Definition RunnableThread.h:114
virtual bool CreateInternal(FRunnable *InRunnable, const TCHAR *InThreadName, uint32 InStackSize=0, EThreadPriority InThreadPri=TPri_Normal, uint64 InThreadAffinityMask=0, EThreadCreateFlags InCreateFlags=EThreadCreateFlags::None)=0
virtual void SetThreadPriority(EThreadPriority NewPriority)=0
FString ThreadName
Definition RunnableThread.h:174
virtual bool SetThreadAffinity(const FThreadAffinity &Affinity)
Definition RunnableThread.h:66
virtual void Suspend(bool bShouldPause=true)=0
static FRunnableThread * GetRunnableThread()
Definition RunnableThread.h:145
FRunnable * Runnable
Definition RunnableThread.h:177
uint64 ThreadAffinityMask
Definition RunnableThread.h:183
virtual bool Kill(bool bShouldWait=true)=0
CORE_API void FreeTls()
Definition ThreadingBase.cpp:929
ThreadType
Definition RunnableThread.h:93
virtual CORE_API ~FRunnableThread()
Definition ThreadingBase.cpp:854
static CORE_API uint32 GetTlsSlot()
Definition ThreadingBase.cpp:837
uint32 ThreadID
Definition RunnableThread.h:189
CORE_API FRunnableThread()
Definition ThreadingBase.cpp:845
const FString & GetThreadName() const
Definition RunnableThread.h:125
Definition Runnable.h:20
Definition ThreadManager.h:20
Definition ThreadSingleton.h:16
Definition TlsAutoCleanup.h:12
static UE_FORCEINLINE_HINT void * GetTlsValue(uint32 SlotIndex)
Definition AndroidPlatformTLS.h:57
Definition PlatformAffinity.h:10