UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WaitingQueue.h
Go to the documentation of this file.
1// Copyright (C) 2016 Dmitry Vyukov <dvyukov@google.com>
2//
3// This Source Code Form is subject to the terms of the Mozilla
4// Public License v. 2.0. If a copy of the MPL was not distributed
5// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7// This implementation is based on EventCount.h
8// included in the Eigen library but almost everything has been
9// rewritten.
10
11#pragma once
12
14#include "Containers/Array.h"
16#include "HAL/Event.h"
17
18#include <atomic>
19
21{
22 enum class EWaitState
23 {
24 NotSignaled = 0,
25 Waiting,
27 };
28
29 /*
30 * the struct is naturally 64 bytes aligned, the extra alignment just
31 * re-enforces this assumption and will error if it changes in the future
32 */
33 struct alignas(64) FWaitEvent
34 {
35 std::atomic<uint64> Next{ 0 };
37 std::atomic<EWaitState> State{ EWaitState::NotSignaled };
39 bool bIsStandby{ false };
40 };
41
43 {
44 uint32 ThreadCount{ 0 }; // Normal amount of threads when there is no oversubscription.
45 uint32 MaxThreadCount{ 0 }; // Max limit that can be reached during oversubscription period.
46 TFunction<void()> CreateThread;
47 std::atomic<uint32> Oversubscription{ 0 };
48 std::atomic<uint64> State;
49 std::atomic<uint64> StandbyState;
50 TAlignedArray<FWaitEvent>& NodesArray;
51 std::atomic<bool> bIsShuttingDown{ false };
52 FOversubscriptionLimitReached& OversubscriptionLimitReachedEvent;
53 public:
59
63
64 // First step to execute when no more work is found in the queues.
66 // Second step to execute when no more work is found in the queues.
68
69 // Immediately goes to sleep if oversubscription period is finished and we're over the allowed thread count.
71
72 // First step run by normal workers when no more work is found in the queues.
74 // Second step run by normal workers when no more work is found in the queues.
76
77 // Step to run by normal workers if they detect new work after they called prepare wait.
78 // Returns true if we need to wake up a new worker.
79 CORE_API bool CancelWait(FWaitEvent* Node);
80
81 // Increment oversubscription and notify a thread if we're under the allowed thread count.
82 // If dynamic thread creation is allowed, this could spawn a new thread if needed.
84
85 // Decrement oversubscription only, any active threads will finish their current task and will
86 // go to sleep if conditional standby determines we're now over the active thread count.
88
89 // Is the current waiting queue out of workers
91
92 // Try to wake up the amount of workers passed in the parameters.
93 // Return the number that were woken up.
95 {
96 return NotifyInternal(Count);
97 }
98
99 private:
100 CORE_API bool TryStartNewThread();
101 CORE_API int32 NotifyInternal(int32 Count);
102 CORE_API void Park(FWaitEvent* Node, FOutOfWork& OutOfWork, int32 SpinCycles, int32 WaitCycles);
104 CORE_API void CheckState(uint64 State, bool bIsWaiter = false);
105 CORE_API void CheckStandbyState(uint64 State);
106 };
107
108} // namespace LowLevelTasks::Private
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
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
void Init()
Definition LockFreeList.h:4
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Event.h:135
Definition TaskShared.h:20
Definition WaitingQueue.h:43
CORE_API void DecrementOversubscription()
Definition WaitingQueue.cpp:427
FWaitingQueue(TAlignedArray< FWaitEvent > &InNodesArray, FOversubscriptionLimitReached &InOversubscriptionLimitReachedEvent)
Definition WaitingQueue.h:54
CORE_API void StartShutdown()
Definition WaitingQueue.cpp:303
CORE_API void PrepareWait(FWaitEvent *Node)
Definition WaitingQueue.cpp:155
CORE_API void PrepareStandby(FWaitEvent *Node)
Definition WaitingQueue.cpp:324
CORE_API void FinishShutdown()
Definition WaitingQueue.cpp:147
int32 Notify(int32 Count=1)
Definition WaitingQueue.h:94
CORE_API bool CommitWait(FWaitEvent *Node, FOutOfWork &OutOfWork, int32 SpinCycles, int32 WaitCycles)
Definition WaitingQueue.cpp:199
CORE_API bool CancelWait(FWaitEvent *Node)
Definition WaitingQueue.cpp:255
CORE_API void IncrementOversubscription()
Definition WaitingQueue.cpp:408
CORE_API bool CommitStandby(FWaitEvent *Node, FOutOfWork &OutOfWork)
Definition WaitingQueue.cpp:372
CORE_API bool IsOversubscriptionLimitReached() const
Definition WaitingQueue.cpp:164
CORE_API void ConditionalStandby(FWaitEvent *Node)
Definition WaitingQueue.cpp:334
Definition Array.h:670
Definition AndroidPlatformMisc.h:14
Definition WaitingQueue.cpp:76
EWaitState
Definition WaitingQueue.h:23
Definition WaitingQueue.h:34
FEventRef Event
Definition WaitingQueue.h:38
uint64 Epoch
Definition WaitingQueue.h:36
std::atomic< uint64 > Next
Definition WaitingQueue.h:35
bool bIsStandby
Definition WaitingQueue.h:39
std::atomic< EWaitState > State
Definition WaitingQueue.h:37