Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
QueuedThreadPool.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 "GenericPlatform/GenericPlatformAffinity.h"
8#include "Templates/Function.h"
9
10class IQueuedWork;
11
12/** Higher priority are picked up first by the task thread pool. */
14{
15 Blocking = 0,
16 Highest = 1,
17 High = 2,
18 Normal = 3,
19 Low = 4,
20 Lowest = 5,
21 Count
22};
23
24inline const TCHAR* LexToString(EQueuedWorkPriority Priority)
25{
26 switch (Priority)
27 {
28 case EQueuedWorkPriority::Blocking:
29 return TEXT("Blocking");
30 case EQueuedWorkPriority::Highest:
31 return TEXT("Highest");
32 case EQueuedWorkPriority::High:
33 return TEXT("High");
34 case EQueuedWorkPriority::Normal:
35 return TEXT("Normal");
36 case EQueuedWorkPriority::Low:
37 return TEXT("Low");
38 case EQueuedWorkPriority::Lowest:
39 return TEXT("Lowest");
40 default:
41 check(false);
42 return TEXT("Unknown");
43 }
44}
45
46/**
47 * Priority Queue tailored for FQueuedThreadPool implementation
48 *
49 * This class is NOT thread-safe and must be properly protected.
50 */
52{
53public:
55
56 /**
57 * Enqueue a work item at specified priority
58 */
60
61 /**
62 * Search and remove a queued work item from the list
63 */
64 bool Retract(IQueuedWork* InQueuedWork);
65
66 /**
67 * Get the next work item in priority order.
68 */
69 IQueuedWork* Dequeue(EQueuedWorkPriority* OutDequeuedWorkPriority = nullptr);
70
71 /**
72 * Get the next work item in priority order without actually dequeuing.
73 */
74 IQueuedWork* Peek(EQueuedWorkPriority* OutDequeuedWorkPriority = nullptr) const;
75
76 /**
77 * Empty the queue.
78 */
79 void Reset();
80
81 /**
82 * Get the total number of queued items.
83 */
84 int32 Num() const { return NumQueuedWork; }
85
86 /**
87 * Sort Priority Bucket given Predicate
88 */
89 void Sort(EQueuedWorkPriority InPriorityBucket, TFunctionRef<bool (const IQueuedWork* A, const IQueuedWork* B)> Predicate);
90private:
91 /** The first queue to extract a work item from to avoid scanning all priorities when unqueuing. */
94 TAtomic<int32> NumQueuedWork;
95};
96
97/**
98 * Interface for queued thread pools.
99 *
100 * This interface is used by all queued thread pools. It used as a callback by
101 * FQueuedThreads and is used to queue asynchronous work for callers.
102 */
104{
105public:
106 /**
107 * Creates the thread pool with the specified number of threads
108 *
109 * @param InNumQueuedThreads Specifies the number of threads to use in the pool
110 * @param StackSize The size of stack the threads in the pool need (32K default)
111 * @param ThreadPriority priority of new pool thread
112 * @param Name optional name for the pool to be used for instrumentation
113 * @return Whether the pool creation was successful or not
114 */
115 virtual bool Create(uint32 InNumQueuedThreads, uint32 StackSize = (32 * 1024), EThreadPriority ThreadPriority = TPri_Normal, const TCHAR* Name = TEXT("UnknownThreadPool")) = 0;
116
117 /** Tells the pool to clean up all background threads */
118 virtual void Destroy() = 0;
119
120 /**
121 * Checks to see if there is a thread available to perform the task. If not,
122 * it queues the work for later. Otherwise it is immediately dispatched.
123 *
124 * @param InQueuedWork The work that needs to be done asynchronously
125 * @param InQueuedWorkPriority The priority at which to process this task
126 * @see RetractQueuedWork
127 */
128 virtual void AddQueuedWork( IQueuedWork* InQueuedWork, EQueuedWorkPriority InQueuedWorkPriority = EQueuedWorkPriority::Normal) = 0;
129
130 /**
131 * Attempts to retract a previously queued task.
132 *
133 * @param InQueuedWork The work to try to retract
134 * @return true if the work was retracted
135 * @see AddQueuedWork
136 */
137 virtual bool RetractQueuedWork(IQueuedWork* InQueuedWork) = 0;
138
139 /**
140 * Get the number of queued threads
141 */
142 virtual int32 GetNumThreads() const = 0;
143
144public:
147
148public:
149
150 /**
151 * Allocates a thread pool
152 *
153 * @return A new thread pool.
154 */
156
157 /**
158 * Stack size for threads created for the thread pool.
159 * Can be overridden by other projects.
160 * If 0 means to use the value passed in the Create method.
161 */
162 static uint32 OverrideStackSize;
163};
164
165/**
166 * Global thread pool for shared async operations
167 */
169
171
173
174#if WITH_EDITOR
175extern FQueuedThreadPool* GLargeThreadPool;
176#endif
#define check(expr)
#define WITH_EDITOR
Definition Build.h:7
EThreadPriority
Definition Enums.h:5969
#define TEXT(x)
Definition Platform.h:1108
EQueuedWorkPriority
FQueuedThreadPool * GBackgroundPriorityThreadPool
FQueuedThreadPool * GThreadPool
const TCHAR * LexToString(EQueuedWorkPriority Priority)
FQueuedThreadPool * GIOThreadPool
virtual bool Create(uint32 InNumQueuedThreads, uint32 StackSize=(32 *1024), EThreadPriority ThreadPriority=TPri_Normal, const TCHAR *Name=TEXT("UnknownThreadPool"))=0
virtual ~FQueuedThreadPool()
static FQueuedThreadPool * Allocate()
virtual int32 GetNumThreads() const =0
virtual bool RetractQueuedWork(IQueuedWork *InQueuedWork)=0
virtual void Destroy()=0
virtual void AddQueuedWork(IQueuedWork *InQueuedWork, EQueuedWorkPriority InQueuedWorkPriority=EQueuedWorkPriority::Normal)=0
static uint32 OverrideStackSize
IQueuedWork * Peek(EQueuedWorkPriority *OutDequeuedWorkPriority=nullptr) const
bool Retract(IQueuedWork *InQueuedWork)
void Sort(EQueuedWorkPriority InPriorityBucket, TFunctionRef< bool(const IQueuedWork *A, const IQueuedWork *B)> Predicate)
TAtomic< int32 > NumQueuedWork
IQueuedWork * Dequeue(EQueuedWorkPriority *OutDequeuedWorkPriority=nullptr)
void Enqueue(IQueuedWork *InQueuedWork, EQueuedWorkPriority InPriority=EQueuedWorkPriority::Normal)