Cogs.Core
TaskManager.h
1#pragma once
2
3#include "Base.h"
4
5#include <functional>
6#include <memory>
7#include <string_view>
8
9namespace Cogs
10{
11 namespace Core
12 {
14 using TaskQueueId = uint16_t;
15
19 struct TaskId
20 {
22 uint32_t taskHandle = static_cast<uint32_t>(-1);
24 uint16_t generation = 0;
27
29 [[nodiscard]] bool isValid() const { return taskHandle != static_cast<uint32_t>(-1); }
30
31 [[nodiscard]] bool operator ==(const TaskId& rhs) const { return taskHandle == rhs.taskHandle; }
32 };
33
35 static constexpr TaskId NoTask = { static_cast<uint32_t>(-1), 0, 0 };
36
38 using TaskFunction = std::function<void ()>;
39 using TaskFunctionRef = const TaskFunction&;
40
43 {
44 float load = 0.f;
45 float tasksPerSecond = 0.f;
46 float tasksPerFrame = 0.f;
47 };
48
51 {
52 float utilization = 0.f;
53 float tasksPerSecond = 0.f;
54 float tasksPerFrame = 0.f;
55 };
56
60 class COGSCORE_DLL_API TaskManager
61 {
62 public:
64 TaskManager(Context* context);
65
68
70 void updateState(Context* context);
71
73 size_t getQueueCount() const;
74
85#ifdef EMSCRIPTEN
86 // Under emscripten, we want to maintain control over thread spawning, so only the
87 // TaskManager is allowed to create queues (which spwans threads).
88 // Thus, createQueue is made private to avoid anyone accidentally creating a queue.
89 private:
90#endif
91 [[nodiscard]] TaskQueueId createQueue(std::string_view name, const size_t numThreads);
92#ifdef EMSCRIPTEN
93 public:
94#endif
95
97 const std::string& getQueueName(TaskQueueId queue) const;
98
100 void getQueueState(QueueState& queueState, std::vector<QueueWorkerState>& workerStates, TaskQueueId queue) const;
101
108 [[nodiscard]] size_t getQueueConcurrency(TaskQueueId queue);
109
113 [[nodiscard]] bool onMainThread() const;
114
126 [[nodiscard]] TaskId createGroup(TaskQueueId queue = GlobalQueue);
127
138 [[nodiscard]] TaskId create(TaskQueueId queue, TaskFunctionRef func);
139
151 [[nodiscard]] TaskId createChild(const TaskId & parentTask, TaskFunctionRef func);
152
159 void enqueue(const TaskId & taskId);
160
171 TaskId enqueueChild(const TaskId & parentTask, TaskFunctionRef func);
172
183 TaskId enqueue(TaskQueueId queue, TaskFunctionRef func);
184
194 void destroy(const TaskId & taskId);
195
199 [[nodiscard]] bool isActive(const TaskId& taskId);
200
206 void wait(const TaskId & taskId);
207
211 void waitAll(TaskQueueId queueId);
212
216 void waitAll();
217
224 static constexpr TaskQueueId GlobalQueue = 0U;
225
232 static constexpr TaskQueueId ResourceQueue = 1U;
233
234 private:
235 class TaskQueue* getQueue(TaskQueueId queueId);
236
237 private:
239 std::unique_ptr<class TaskQueues> taskQueues;
240 };
241 }
242}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Manages Task queuing and execution.
Definition: TaskManager.h:61
std::unique_ptr< class TaskQueues > taskQueues
Task queues manager.
Definition: TaskManager.h:239
Task queues holds tasks ready for execution by TaskWorkers.
std::function< void()> TaskFunction
Type of task function used by the task manager.
Definition: TaskManager.h:38
uint16_t TaskQueueId
Unique id for a task queue.
Definition: TaskManager.h:14
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Reports current state of a queue.
Definition: TaskManager.h:43
reports current state of a queue worker.
Definition: TaskManager.h:51
Task id struct used to identify unique Task instances.
Definition: TaskManager.h:20
bool isValid() const
Check if the task id is valid.
Definition: TaskManager.h:29
uint32_t taskHandle
Integer handle to the task in a TaskPool.
Definition: TaskManager.h:22
uint16_t generation
Generation counter used to separate generational instances.
Definition: TaskManager.h:24
TaskQueueId queueId
Id of the TaskQueue the Task belongs to.
Definition: TaskManager.h:26