Plasma Engine  2.0
Loading...
Searching...
No Matches
TaskSystemDeclarations.h
1#pragma once
2
3#include <Foundation/Containers/HybridArray.h>
4#include <Foundation/Threading/ConditionVariable.h>
5#include <Foundation/Time/Time.h>
6#include <Foundation/Types/Delegate.h>
7#include <Foundation/Types/SharedPtr.h>
8#include <Foundation/Types/UniquePtr.h>
9
10class plTask;
11class plTaskGroup;
15class plDGMLGraph;
16class plAllocator;
17
74
77{
78 enum Enum : plUInt8
79 {
80 WaitTillFinished,
81 ReturnWithoutBlocking
82 };
83};
84
87{
88 enum Enum : plUInt8
89 {
92 ShortTasks,
93 LongTasks,
94 FileAccess,
95 ENUM_COUNT
96 };
97
98 static const char* GetThreadTypeName(plWorkerThreadType::Enum threadType);
99};
100
102class PL_FOUNDATION_DLL plTaskGroupID
103{
104public:
105 PL_ALWAYS_INLINE plTaskGroupID() = default;
106 PL_ALWAYS_INLINE ~plTaskGroupID() = default;
107
109 PL_ALWAYS_INLINE bool IsValid() const { return m_pTaskGroup != nullptr; }
110
112 PL_ALWAYS_INLINE void Invalidate() { m_pTaskGroup = nullptr; }
113
114 PL_ALWAYS_INLINE bool operator==(const plTaskGroupID& other) const
115 {
116 return m_pTaskGroup == other.m_pTaskGroup && m_uiGroupCounter == other.m_uiGroupCounter;
117 }
118 PL_ALWAYS_INLINE bool operator!=(const plTaskGroupID& other) const
119 {
120 return m_pTaskGroup != other.m_pTaskGroup || m_uiGroupCounter != other.m_uiGroupCounter;
121 }
122 PL_ALWAYS_INLINE bool operator<(const plTaskGroupID& other) const
123 {
124 return m_pTaskGroup < other.m_pTaskGroup || (m_pTaskGroup == other.m_pTaskGroup && m_uiGroupCounter < other.m_uiGroupCounter);
125 }
126
127private:
128 friend class plTaskSystem;
129 friend class plTaskGroup;
130
131 // the counter is used to determine whether this group id references the 'same' group, as m_pTaskGroup.
132 // if m_pTaskGroup->m_uiGroupCounter is different to this->m_uiGroupCounter, then the group ID is not valid anymore.
133 plUInt32 m_uiGroupCounter = 0;
134
135 // points to the actual task group object
136 plTaskGroup* m_pTaskGroup = nullptr;
137};
138
141
144
146{
147 PL_DECLARE_POD_TYPE();
148
149 plTaskGroupID m_TaskGroup;
150 plTaskGroupID m_DependsOn;
151};
152
159enum class plTaskNesting
160{
161 Maybe,
162 Never,
163};
164
166struct PL_FOUNDATION_DLL plParallelForParams
167{
168 plParallelForParams() = default; // do not remove, needed for Clang
169
173 plUInt32 m_uiBinSize = 1;
174
181 plUInt32 m_uiMaxTasksPerThread = 2;
182
183 plTaskNesting m_NestingMode = plTaskNesting::Never;
184
186 plAllocator* m_pTaskAllocator = nullptr;
187
188 void DetermineThreading(plUInt64 uiNumItemsToExecute, plUInt32& out_uiNumTasksToRun, plUInt64& out_uiNumItemsPerTask) const;
189};
190
191using plParallelForIndexedFunction32 = plDelegate<void(plUInt32, plUInt32), 48>;
192using plParallelForIndexedFunction64 = plDelegate<void(plUInt64, plUInt64), 48>;
193
194template <typename ElemType>
195using plParallelForFunction = plDelegate<void(plUInt32, plArrayPtr<ElemType>), 48>;
196
197enum class plTaskWorkerState
198{
199 Active = 0,
200 Idle = 1,
201 Blocked = 2,
202};
Base class for all memory allocators.
Definition Allocator.h:23
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
This class encapsulates building a DGML compatible graph.
Definition DGMLWriter.h:10
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
Definition TaskGroup.h:11
Given out by plTaskSystem::CreateTaskGroup to identify a task group.
Definition TaskSystemDeclarations.h:103
PL_ALWAYS_INLINE bool IsValid() const
Returns false, if the GroupID does not reference a valid plTaskGroup.
Definition TaskSystemDeclarations.h:109
PL_ALWAYS_INLINE void Invalidate()
Resets the GroupID into an invalid state.
Definition TaskSystemDeclarations.h:112
Base class for custom tasks.
Definition Task.h:10
This system allows to automatically distribute tasks onto a number of worker threads.
Definition TaskSystem.h:25
Definition TaskSystemState.h:22
Definition TaskSystemState.h:6
Definition TaskWorkerThread.h:10
Enum that describes what to do when waiting for or canceling tasks, that have already started executi...
Definition TaskSystemDeclarations.h:77
Settings for plTaskSystem::ParallelFor invocations.
Definition TaskSystemDeclarations.h:167
Definition TaskSystemDeclarations.h:146
Describes the priority with which to execute a task.
Definition TaskSystemDeclarations.h:38
Enum
Definition TaskSystemDeclarations.h:41
@ LongRunningHighPriority
Definition TaskSystemDeclarations.h:58
@ LateThisFrame
Low priority, guaranteed to get finished in this frame.
Definition TaskSystemDeclarations.h:44
@ In6Frames
A short task that should be finished no later than in 6 frames.
Definition TaskSystemDeclarations.h:53
@ ThisFrameMainThread
Definition TaskSystemDeclarations.h:65
@ In3Frames
A short task that should be finished no later than in 3 frames.
Definition TaskSystemDeclarations.h:50
@ ThisFrame
Medium priority, guaranteed to get finished in this frame.
Definition TaskSystemDeclarations.h:43
@ In7Frames
A short task that should be finished no later than in 7 frames.
Definition TaskSystemDeclarations.h:54
@ SomeFrameMainThread
Definition TaskSystemDeclarations.h:67
@ LongRunning
Use this priority for tasks that might run for a while.
Definition TaskSystemDeclarations.h:60
@ In5Frames
A short task that should be finished no later than in 5 frames.
Definition TaskSystemDeclarations.h:52
@ In8Frames
A short task that should be finished no later than in 8 frames.
Definition TaskSystemDeclarations.h:55
@ EarlyNextFrame
Highest priority in next frame, guaranteed to get finished this frame or the next.
Definition TaskSystemDeclarations.h:45
@ In4Frames
A short task that should be finished no later than in 4 frames.
Definition TaskSystemDeclarations.h:51
@ In2Frames
A short task that should be finished no later than in 2 frames.
Definition TaskSystemDeclarations.h:49
@ EarlyThisFrame
Highest priority, guaranteed to get finished in this frame.
Definition TaskSystemDeclarations.h:42
@ FileAccess
Definition TaskSystemDeclarations.h:63
@ LateNextFrame
Low priority in next frame, guaranteed to get finished this frame or the next.
Definition TaskSystemDeclarations.h:47
@ FileAccessHighPriority
Definition TaskSystemDeclarations.h:61
@ In9Frames
A short task that should be finished no later than in 9 frames.
Definition TaskSystemDeclarations.h:56
@ NextFrame
Medium priority in next frame, guaranteed to get finished this frame or the next.
Definition TaskSystemDeclarations.h:46
Definition TaskSystemDeclarations.h:87
Enum
Definition TaskSystemDeclarations.h:89
@ Unknown
Default for all non-plTaskSystem-worker threads. Will only execute short tasks.
Definition TaskSystemDeclarations.h:90
@ MainThread
May only be used by the main thread (automatically used by the plTaskSystem)
Definition TaskSystemDeclarations.h:91