UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Scheduler.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
10#include "Async/Mutex.h"
11#include "Async/UniqueLock.h"
12#include "Containers/Array.h"
14#include "Containers/List.h"
16#include "HAL/Event.h"
18#include "HAL/PlatformMutex.h"
19#include "HAL/PlatformProcess.h"
20#include "HAL/Thread.h"
21#include "LocalQueue.h"
23#include "Templates/Function.h"
25#include "Templates/UniquePtr.h"
28
29#include <atomic>
30
31namespace LowLevelTasks
32{
39
41 {
42 protected:
43 class FImpl;
44
47
48 enum class EWorkerType
49 {
50 None,
53 };
54
55 struct FTlsValues : public TIntrusiveLinkedList<FTlsValues>
56 {
60 std::atomic<bool> bPendingWakeUp = false;
61 bool bIsStandbyWorker = false;
62
63 inline bool IsBackgroundWorker()
64 {
66 }
67
68 inline bool IsStandbyWorker()
69 {
70 return bIsStandbyWorker;
71 }
72
77
78 static void* operator new(size_t Size);
79 static void operator delete(void* Ptr);
80 };
81
89
90 private:
91 static thread_local FTlsValuesHolder TlsValuesHolder;
92
93 public:
94 CORE_API bool IsWorkerThread() const;
95
96 protected:
97 CORE_API bool HasPendingWakeUp() const;
98
100 };
101
102 class FScheduler final : public FSchedulerTls
103 {
105 static constexpr uint32 WorkerSpinCycles = 53;
106
107 static CORE_API FScheduler Singleton;
108
109 // using 16 bytes here because it fits the vtable and one additional pointer
110 using FConditional = TTaskDelegate<bool(), 16>;
111
112 public: // Public Interface of the Scheduler
113 inline static FScheduler& Get();
114
115 //start number of workers where 0 is the system default
117 CORE_API void StopWorkers(bool DrainGlobalQueue = true);
119
120 //try to launch the task, the return value will specify if the task was in the ready state and has been launched
122
123 //number of instantiated workers
124 inline uint32 GetNumWorkers() const;
125
126 //maximum number of workers, including standby workers (Oversubscription)
127 inline uint32 GetMaxNumWorkers() const;
128
129 //get the worker priority set when workers were started
130 inline EThreadPriority GetWorkerPriority() const { return WorkerPriority; }
131
132 //get the background priority set when workers were started
133 inline EThreadPriority GetBackgroundPriority() const { return BackgroundPriority; }
134
135 //determine if we're currently out of workers for a given task priority
137
138 //event that will fire when the scheduler has reached its oversubscription limit (all threads are waiting).
139 //note: This event can be broadcasted from any thread so the receiver needs to be thread-safe
140 // For optimal performance, avoid binding UObjects to this event and use AddRaw/AddLambda instead.
141 // Also, what's happening inside that callback should be as brief and simple as possible (i.e. raising an event)
143 public:
144 FScheduler() = default;
145 ~FScheduler();
146
147 private:
148 [[nodiscard]] FTask* ExecuteTask(FTask* InTask);
154 inline bool WakeUpWorker(bool bBackgroundWorker);
155 CORE_API void IncrementOversubscription();
156 CORE_API void DecrementOversubscription();
157 template<typename QueueType, FTask* (QueueType::*DequeueFunction)(bool), bool bIsStandbyWorker>
158 bool TryExecuteTaskFrom(Private::FWaitEvent* WaitEvent, QueueType* Queue, Private::FOutOfWork& OutOfWork, bool bPermitBackgroundWork);
159
161 private:
162 Private::FWaitingQueue WaitingQueue[2] = { { WorkerEvents, OversubscriptionLimitReachedEvent }, { WorkerEvents, OversubscriptionLimitReachedEvent } };
163 FSchedulerTls::FQueueRegistry QueueRegistry;
164 UE::FPlatformRecursiveMutex WorkerThreadsCS;
165 TUniquePtr<std::atomic<FThread*>[]> WorkerThreads;
169 std::atomic_uint ActiveWorkers { 0 };
170 std::atomic_uint NextWorkerId { 0 };
171 std::atomic<int32> ForegroundCreationIndex{ 0 };
172 std::atomic<int32> BackgroundCreationIndex{ 0 };
173 uint64 WorkerAffinity = 0;
174 uint64 BackgroundAffinity = 0;
177 std::atomic_bool TemporaryShutdown{ false };
178 FOversubscriptionLimitReached OversubscriptionLimitReachedEvent;
179 };
180
185
186 /******************
187 * IMPLEMENTATION *
188 ******************/
190 {
191 if(Task.TryPrepareLaunch())
192 {
193 LaunchInternal(Task, QueuePreference, bWakeUpWorker);
194 return true;
195 }
196 return false;
197 }
198
200 {
201 return ActiveWorkers.load(std::memory_order_relaxed);
202 }
203
204 // Return the maximum number of worker threads, including Standby Workers
206 {
207 return WorkerLocalQueues.Num();
208 }
209
210 inline bool FScheduler::WakeUpWorker(bool bBackgroundWorker)
211 {
212 return WaitingQueue[bBackgroundWorker].Notify() != 0;
213 }
214
216 {
217 return Singleton;
218 }
219
221 {
222 StopWorkers();
223 }
224}
225
226#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_6
227#include "HAL/CriticalSection.h"
228#endif
#define UE_NONCOPYABLE(TypeName)
Definition CoreMiscDefines.h:457
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
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_BelowNormal
Definition GenericPlatformAffinity.h:29
@ TPri_Normal
Definition GenericPlatformAffinity.h:27
const bool
Definition NetworkReplayStreaming.h:178
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
EForkable
Definition Thread.h:27
@ NonForkable
Definition Thread.h:27
Definition Scheduler.cpp:61
Definition Scheduler.h:41
Private::TLocalQueueRegistry<> FQueueRegistry
Definition Scheduler.h:45
EWorkerType
Definition Scheduler.h:49
CORE_API bool HasPendingWakeUp() const
Definition Scheduler.cpp:485
static FTlsValues & GetTlsValuesRef()
Definition Scheduler.cpp:171
CORE_API bool IsWorkerThread() const
Definition Scheduler.cpp:662
Definition Scheduler.h:103
EThreadPriority GetBackgroundPriority() const
Definition Scheduler.h:133
CORE_API void RestartWorkers(uint32 NumForegroundWorkers=0, uint32 NumBackgroundWorkers=0, FThread::EForkable IsForkable=FThread::NonForkable, EThreadPriority WorkerPriority=EThreadPriority::TPri_Normal, EThreadPriority BackgroundPriority=EThreadPriority::TPri_BelowNormal, uint64 InWorkerAffinity=0, uint64 InBackgroundAffinity=0)
Definition Scheduler.cpp:511
friend class FOversubscriptionScope
Definition Scheduler.h:160
CORE_API void StopWorkers(bool DrainGlobalQueue=true)
Definition Scheduler.cpp:433
bool TryLaunch(FTask &Task, EQueuePreference QueuePreference=EQueuePreference::DefaultPreference, bool bWakeUpWorker=true)
Definition Scheduler.h:189
CORE_API void StartWorkers(uint32 NumForegroundWorkers=0, uint32 NumBackgroundWorkers=0, FThread::EForkable IsForkable=FThread::NonForkable, EThreadPriority InWorkerPriority=EThreadPriority::TPri_Normal, EThreadPriority InBackgroundPriority=EThreadPriority::TPri_BelowNormal, uint64 InWorkerAffinity=0, uint64 InBackgroundAffinity=0)
Definition Scheduler.cpp:225
CORE_API bool IsOversubscriptionLimitReached(ETaskPriority TaskPriority) const
Definition Scheduler.cpp:365
uint32 GetNumWorkers() const
Definition Scheduler.h:199
uint32 GetMaxNumWorkers() const
Definition Scheduler.h:205
CORE_API FOversubscriptionLimitReached & GetOversubscriptionLimitReachedEvent()
Definition Scheduler.cpp:383
static FScheduler & Get()
Definition Scheduler.h:215
~FScheduler()
Definition Scheduler.h:220
EThreadPriority GetWorkerPriority() const
Definition Scheduler.h:130
Definition Task.h:310
Definition TaskShared.h:20
Definition WaitingQueue.h:43
int32 Notify(int32 Count=1)
Definition WaitingQueue.h:94
Definition TaskDelegate.h:40
Definition List.h:349
Definition UniquePtr.h:107
Definition Scheduler.cpp:25
EQueuePreference
Definition Scheduler.h:34
ETaskPriority
Definition Task.h:18
UE_FORCEINLINE_HINT bool TryLaunch(FTask &Task, EQueuePreference QueuePreference=EQueuePreference::DefaultPreference, bool bWakeUpWorker=true)
Definition Scheduler.h:181
FPThreadsRecursiveMutex FPlatformRecursiveMutex
Definition AndroidPlatformMutex.h:12
CORE_API FTlsValuesHolder()
Definition Scheduler.cpp:94
FTlsValues * TlsValues
Definition Scheduler.h:87
CORE_API ~FTlsValuesHolder()
Definition Scheduler.cpp:114
bool bIsStandbyWorker
Definition Scheduler.h:61
EWorkerType WorkerType
Definition Scheduler.h:59
std::atomic< bool > bPendingWakeUp
Definition Scheduler.h:60
void SetStandbyWorker(bool bInIsStandbyWorker)
Definition Scheduler.h:73
bool IsBackgroundWorker()
Definition Scheduler.h:63
FLocalQueueType * LocalQueue
Definition Scheduler.h:58
bool IsStandbyWorker()
Definition Scheduler.h:68
FSchedulerTls * ActiveScheduler
Definition Scheduler.h:57
Definition WaitingQueue.h:34