Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
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"
7#include "Containers/UnrealString.h"
8#include "HAL/PlatformTLS.h"
9#include "HAL/PlatformAffinity.h"
10
11class FRunnable;
12class FTlsAutoCleanup;
13
14/**
15 * Interface for runnable threads.
16 *
17 * This interface specifies the methods used to manage a thread's life cycle.
18 */
20{
22 friend class FTlsAutoCleanup;
23 friend class FThreadManager;
24 friend class FForkableThread;
25 friend class FForkProcessHelper;
26
27 /** Index of TLS slot for FRunnableThread pointer. */
28 static uint32 RunnableTlsSlot;
29
30public:
31
32 /** Gets a new Tls slot for storing the runnable thread pointer. */
33 static uint32 GetTlsSlot();
34
35 /**
36 * Factory method to create a thread with the specified stack size and thread priority.
37 *
38 * @param InRunnable The runnable object to execute
39 * @param ThreadName Name of the thread
40 * @param InStackSize The size of the stack to create. 0 means use the current thread's stack size
41 * @param InThreadPri Tells the thread whether it needs to adjust its priority or not. Defaults to normal priority
42 * @return The newly created thread or nullptr if it failed
43 */
45 class FRunnable* InRunnable,
46 const TCHAR* ThreadName,
47 uint32 InStackSize = 0,
48 EThreadPriority InThreadPri = TPri_Normal,
49 uint64 InThreadAffinityMask = FPlatformAffinity::GetNoAffinityMask(),
51
52 /**
53 * Changes the thread priority of the currently running thread
54 *
55 * @param NewPriority The thread priority to change to
56 */
57 virtual void SetThreadPriority( EThreadPriority NewPriority ) = 0;
58
59 /**
60 * Changes the thread affinity of the currently running thread
61 *
62 * @param ThreadAffinityMask The thread affinity to change to (can be 0 to keep previously set affinity mask)
63 * @param ProcessorGroup The thread group to change to
64 * @return returns true if the affinity changed, false if affinity did not change
65 */
66 virtual bool SetThreadAffinity( const FThreadAffinity& Affinity ) { return false; };
67
68 /**
69 * Tells the thread to either pause execution or resume depending on the
70 * passed in value.
71 *
72 * @param bShouldPause Whether to pause the thread (true) or resume (false)
73 */
74 virtual void Suspend( bool bShouldPause = true ) = 0;
75
76 /**
77 * Tells the thread to exit. If the caller needs to know when the thread has exited, it should use the bShouldWait value.
78 * It's highly recommended not to kill the thread without waiting for it.
79 * Having a thread forcibly destroyed can cause leaks and deadlocks.
80 *
81 * The kill method is calling Stop() on the runnable to kill the thread gracefully.
82 *
83 * @param bShouldWait If true, the call will wait infinitely for the thread to exit.
84 * @return Always true
85 */
86 virtual bool Kill( bool bShouldWait = true ) = 0;
87
88 /** Halts the caller until this thread is has completed its work. */
89 virtual void WaitForCompletion() = 0;
90
91 /** List of unique thread types we can create */
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
102 /** Returns the type of thread this is */
104 {
105 return ThreadType::Real;
106 }
107
108 /**
109 * Thread ID for this thread
110 *
111 * @return ID that was set by CreateThread
112 * @see GetThreadName
113 */
114 const uint32 GetThreadID() const
115 {
116 return ThreadID;
117 }
118
119 /**
120 * Retrieves the given name of the thread
121 *
122 * @return Name that was set by CreateThread
123 * @see GetThreadID
124 */
125 const FString& GetThreadName() const
126 {
127 return ThreadName;
128 }
129
130 /** Returns the runnable's thread priority */
132 {
133 return ThreadPriority;
134 }
135
136 /** Default constructor. */
138
139 /** Virtual destructor */
140 virtual ~FRunnableThread();
141
142 /**
143 * @return a runnable thread that is executing this runnable, if return value is nullptr, it means the running thread can be game thread or a thread created outside the runnable interface
144 */
146 {
148 return RunnableThread;
149 }
150
151protected:
152
153 /**
154 * Creates the thread with the specified stack size and thread priority.
155 *
156 * @param InRunnable The runnable object to execute
157 * @param ThreadName Name of the thread
158 * @param InStackSize The size of the stack to create. 0 means use the current thread's stack size
159 * @param InThreadPri Tells the thread whether it needs to adjust its priority or not. Defaults to normal priority
160 * @return True if the thread and all of its initialization was successful, false otherwise
161 */
162 virtual bool CreateInternal( FRunnable* InRunnable, const TCHAR* InThreadName,
163 uint32 InStackSize = 0,
164 EThreadPriority InThreadPri = TPri_Normal, uint64 InThreadAffinityMask = 0,
165 EThreadCreateFlags InCreateFlags = EThreadCreateFlags::None) = 0;
166
167 /** Stores this instance in the runnable thread TLS slot. */
168 void SetTls();
169
170 /** Deletes all FTlsAutoCleanup objects created for this thread. */
171 void FreeTls();
172
173 /** Holds the name of the thread. */
175
176 /** The runnable object to execute on this thread. */
178
179 /** Sync event to make sure that Init() has been completed before allowing the main thread to continue. */
181
182 /** The Affinity to run the thread with. */
184
185 /** The priority to run the thread at. */
187
188 /** ID set during thread creation. */
189 uint32 ThreadID;
190
191private:
192
193 /** Called to setup a newly created RunnableThread */
194 static void SetupCreatedThread(FRunnableThread*& NewThread, class FRunnable* InRunnable, const TCHAR* ThreadName, uint32 InStackSize, EThreadPriority InThreadPri, uint64 InThreadAffinityMask, EThreadCreateFlags InCreateFlags);
195
196 /** Used by the thread manager to tick threads in single-threaded mode */
197 virtual void Tick() {}
198
199 /**
200 * Called on the forked process when the forkable thread can create a real thread
201 */
202 virtual void OnPostFork()
203 {
204 checkf(false, TEXT("Only forkable threads should receive OnPostFork."));
205 }
206
207 /**
208 * Called after the internal thread is created so it can register debug information
209 */
210 void PostCreate(EThreadPriority ThreadPriority);
211};
#define checkf(expr, format,...)
EThreadCreateFlags
Definition Enums.h:11288
EThreadPriority
Definition Enums.h:5969
FGenericPlatformAffinity FPlatformAffinity
FWindowsPlatformTLS FPlatformTLS
Definition Event.h:21
static const uint64 GetNoAffinityMask()
static void SetupCreatedThread(FRunnableThread *&NewThread, class FRunnable *InRunnable, const TCHAR *ThreadName, uint32 InStackSize, EThreadPriority InThreadPri, uint64 InThreadAffinityMask, EThreadCreateFlags InCreateFlags)
static uint32 GetTlsSlot()
void PostCreate(EThreadPriority ThreadPriority)
virtual void WaitForCompletion()=0
static FRunnableThread * Create(class FRunnable *InRunnable, const TCHAR *ThreadName, uint32 InStackSize=0, EThreadPriority InThreadPri=TPri_Normal, uint64 InThreadAffinityMask=FPlatformAffinity::GetNoAffinityMask(), EThreadCreateFlags InCreateFlags=EThreadCreateFlags::None)
EThreadPriority ThreadPriority
EThreadPriority GetThreadPriority() const
FEvent * ThreadInitSyncEvent
virtual FRunnableThread::ThreadType GetThreadType() const
virtual void Tick()
const uint32 GetThreadID() const
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
virtual bool SetThreadAffinity(const FThreadAffinity &Affinity)
virtual void Suspend(bool bShouldPause=true)=0
static FRunnableThread * GetRunnableThread()
FRunnable * Runnable
virtual void OnPostFork()
static uint32 RunnableTlsSlot
virtual ~FRunnableThread()
virtual bool Kill(bool bShouldWait=true)=0
const FString & GetThreadName() const
static FORCEINLINE void * GetTlsValue(uint32 SlotIndex)