Cogs.Core
|
Manages Task queuing and execution. More...
#include <TaskManager.h>
Public Member Functions | |
TaskManager (Context *context) | |
Constructs a TaskManager. Must be called on the main thread. | |
~TaskManager () | |
Destructs the TaskManager instance. | |
void | updateState (Context *context) |
Update state data, invoked once a frame by context. | |
size_t | getQueueCount () const |
Returns the number of queues managed. | |
TaskQueueId | createQueue (std::string_view name, const size_t numThreads) |
Creates a new task queue with the given name. | |
const std::string & | getQueueName (TaskQueueId queue) const |
Returns the name of a queue. | |
void | getQueueState (QueueState &queueState, std::vector< QueueWorkerState > &workerStates, TaskQueueId queue) const |
Queries the current state of a specific queue. | |
size_t | getQueueConcurrency (TaskQueueId queue) |
Returns the number of worker threads in a task queue. | |
bool | onMainThread () const |
Returns true if called on the main thread. | |
TaskId | createGroup (TaskQueueId queue=GlobalQueue) |
Create a task group and place it in the given queue. | |
TaskId | create (TaskQueueId queue, TaskFunctionRef func) |
Create a task in the given queue. | |
TaskId | createChild (const TaskId &parentTask, TaskFunctionRef func) |
Create a child task and associate it with the given parent task. | |
void | enqueue (const TaskId &taskId) |
Enqueues a previously created task. | |
TaskId | enqueueChild (const TaskId &parentTask, TaskFunctionRef func) |
Enqueue the given task func setting the given parent task. | |
TaskId | enqueue (TaskQueueId queue, TaskFunctionRef func) |
Enqueue the given task func in the given queue. | |
void | destroy (const TaskId &taskId) |
Destroy the task given by taskId. | |
bool | isActive (const TaskId &taskId) |
Poll to check whether or not the task has finished. | |
void | wait (const TaskId &taskId) |
Wait for the task given by taskId. | |
void | waitAll (TaskQueueId queueId) |
Wait for all tasks in the queue given by queueId. | |
void | waitAll () |
Wait for all tasks in all task queues. | |
Static Public Attributes | |
static constexpr TaskQueueId | GlobalQueue = 0U |
Global task queue. | |
static constexpr TaskQueueId | ResourceQueue = 1U |
Resource task queue. | |
Private Member Functions | |
class TaskQueue * | getQueue (TaskQueueId queueId) |
Private Attributes | |
std::unique_ptr< class TaskQueues > | taskQueues |
Task queues manager. | |
Manages Task queuing and execution.
Definition at line 60 of file TaskManager.h.
Cogs::Core::TaskManager::TaskManager | ( | Context * | context | ) |
Constructs a TaskManager. Must be called on the main thread.
Definition at line 889 of file TaskManager.cpp.
References createQueue(), GlobalQueue, ResourceQueue, taskQueues, and Cogs::Core::Context::variables.
Cogs::Core::TaskManager::~TaskManager | ( | ) |
Destructs the TaskManager instance.
Definition at line 918 of file TaskManager.cpp.
Cogs::Core::TaskId Cogs::Core::TaskManager::create | ( | TaskQueueId | queue, |
TaskFunctionRef | func | ||
) |
Create a task in the given queue.
The task is not automatically executed. To enqueue the task for execution, a corresponding call to enqueue() with the returned task id must be made.
queue | Task queue id of the queue to associate the task with. The task is automatically executed on this queue when enqueued. |
func | Function object to execute. |
Definition at line 965 of file TaskManager.cpp.
Cogs::Core::TaskId Cogs::Core::TaskManager::createChild | ( | const TaskId & | parentTask, |
TaskFunctionRef | func | ||
) |
Create a child task and associate it with the given parent task.
The parent task can be either a regular task or a task group.
The parent task is not considered done until all child tasks associated with it are done.
parentTask | Task id of the parent task to associate the task with. |
func | Function object to execute. |
Definition at line 979 of file TaskManager.cpp.
References Cogs::Core::TaskId::queueId.
Cogs::Core::TaskId Cogs::Core::TaskManager::createGroup | ( | TaskQueueId | queue = GlobalQueue | ) |
Create a task group and place it in the given queue.
All tasks created in this task group will be executed by the queue.
Task groups are by default not automatically destroyed, so the user must explicitly call destroy() when the task group can be freed.
queue | Task queue id of the queue to associate the task group with. |
Definition at line 972 of file TaskManager.cpp.
Cogs::Core::TaskQueueId Cogs::Core::TaskManager::createQueue | ( | std::string_view | name, |
const size_t | numThreads | ||
) |
Creates a new task queue with the given name.
name | String name to assign to the queue. |
numThreads | Number of native threads to assign to the queue. |
id | Task queue id to assign to the queue. If not given, a new unique id will be generated automatically. |
Definition at line 1046 of file TaskManager.cpp.
Referenced by TaskManager().
void Cogs::Core::TaskManager::destroy | ( | const TaskId & | taskId | ) |
Destroy the task given by taskId.
Only relevant for tasks that are not automatically destroyed, such as task groups.
The task will be allowed to run until completion before destroying it, equivalent to calling wait() with the same task id before destroy().
taskId | Task id of the task to destroy. |
Definition at line 1007 of file TaskManager.cpp.
References Cogs::Core::TaskId::isValid(), and Cogs::Core::TaskId::queueId.
void Cogs::Core::TaskManager::enqueue | ( | const TaskId & | taskId | ) |
Enqueues a previously created task.
The task will be placed in the execution queue of the task queue it was originally associated with.
taskId | Task id of the task to enqueue. |
Definition at line 986 of file TaskManager.cpp.
References Cogs::Core::TaskId::queueId.
Cogs::Core::TaskId Cogs::Core::TaskManager::enqueue | ( | TaskQueueId | queue, |
TaskFunctionRef | func | ||
) |
Enqueue the given task func in the given queue.
The task will be executed as soon as the task queue has any available workers.
queue | Id of the task queue to add the task to. |
func | Task kernel to execute when running the task. |
Definition at line 993 of file TaskManager.cpp.
Cogs::Core::TaskId Cogs::Core::TaskManager::enqueueChild | ( | const TaskId & | parentTask, |
TaskFunctionRef | func | ||
) |
Enqueue the given task func setting the given parent task.
The task will be executed as soon as the task queue has any available workers.
queue | Id of the task queue to add the task as child of. |
func | Task kernel to execute when running the task. |
Definition at line 1000 of file TaskManager.cpp.
References Cogs::Core::TaskId::queueId.
|
inlineprivate |
Definition at line 960 of file TaskManager.cpp.
size_t Cogs::Core::TaskManager::getQueueConcurrency | ( | TaskQueueId | queue | ) |
Returns the number of worker threads in a task queue.
queue | Task queue id of the queue to query |
Definition at line 949 of file TaskManager.cpp.
size_t Cogs::Core::TaskManager::getQueueCount | ( | ) | const |
Returns the number of queues managed.
Definition at line 932 of file TaskManager.cpp.
const std::string & Cogs::Core::TaskManager::getQueueName | ( | TaskQueueId | queue | ) | const |
Returns the name of a queue.
Definition at line 937 of file TaskManager.cpp.
void Cogs::Core::TaskManager::getQueueState | ( | QueueState & | queueState, |
std::vector< QueueWorkerState > & | workerStates, | ||
TaskQueueId | queue | ||
) | const |
Queries the current state of a specific queue.
Definition at line 943 of file TaskManager.cpp.
bool Cogs::Core::TaskManager::isActive | ( | const TaskId & | taskId | ) |
Poll to check whether or not the task has finished.
Definition at line 1015 of file TaskManager.cpp.
References Cogs::Core::TaskId::queueId.
bool Cogs::Core::TaskManager::onMainThread | ( | ) | const |
Returns true if called on the main thread.
Definition at line 955 of file TaskManager.cpp.
void Cogs::Core::TaskManager::updateState | ( | Context * | context | ) |
Update state data, invoked once a frame by context.
Definition at line 925 of file TaskManager.cpp.
void Cogs::Core::TaskManager::wait | ( | const TaskId & | taskId | ) |
Wait for the task given by taskId.
Will also wait for any child tasks associated with the task.
See notes in GlobalQueue and ResourceQueue.
Definition at line 1020 of file TaskManager.cpp.
References Cogs::Core::TaskId::isValid(), and Cogs::Core::TaskId::queueId.
void Cogs::Core::TaskManager::waitAll | ( | ) |
Wait for all tasks in all task queues.
Definition at line 1038 of file TaskManager.cpp.
void Cogs::Core::TaskManager::waitAll | ( | TaskQueueId | queueId | ) |
Wait for all tasks in the queue given by queueId.
Definition at line 1031 of file TaskManager.cpp.
|
staticconstexpr |
Global task queue.
This queue is for small tasks that can easily finish within a frame and is used by systems for e.g. parallel for. It will be waited on from the main thread, and large tasks here might cause stuttering.
Definition at line 224 of file TaskManager.h.
Referenced by Cogs::Core::BasicOceanSystem::initialize(), Cogs::Core::EchoSounder::UniformGridSystem::preUpdate(), TaskManager(), and Cogs::Core::BasicOceanSystem::update().
|
staticconstexpr |
Resource task queue.
This queue is longer-running tasks and should not be waited on from the main thread. Use onMainThread() to check if code is running on the main thread.
Definition at line 232 of file TaskManager.h.
Referenced by Cogs::Core::AssetManager::handleLoad(), Cogs::Core::FontManager::handleLoad(), Cogs::Core::ModelManager::handleLoad(), Cogs::Core::TextureManager::handleLoad(), Cogs::Core::MeshManager::MeshManager(), Cogs::Core::ModelManager::ModelManager(), TaskManager(), Cogs::Core::EchoSounder::OctProviderSystem::update(), and Cogs::Core::Volumetric::OctDummyProviderSystem::update().
|
private |