Cogs.Core
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
Cogs::Core::TaskManager Class Reference

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 TaskQueuegetQueue (TaskQueueId queueId)
 

Private Attributes

std::unique_ptr< class TaskQueuestaskQueues
 Task queues manager.
 

Detailed Description

Manages Task queuing and execution.

Definition at line 60 of file TaskManager.h.

Constructor & Destructor Documentation

◆ TaskManager()

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.

◆ ~TaskManager()

Cogs::Core::TaskManager::~TaskManager ( )

Destructs the TaskManager instance.

Definition at line 918 of file TaskManager.cpp.

Member Function Documentation

◆ create()

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.

Parameters
queueTask queue id of the queue to associate the task with. The task is automatically executed on this queue when enqueued.
funcFunction object to execute.
Returns
Task id of the newly created task.

Definition at line 965 of file TaskManager.cpp.

◆ createChild()

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.

Parameters
parentTaskTask id of the parent task to associate the task with.
funcFunction object to execute.
Returns
Task id of the newly created task.

Definition at line 979 of file TaskManager.cpp.

References Cogs::Core::TaskId::queueId.

◆ createGroup()

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.

Parameters
queueTask queue id of the queue to associate the task group with.
Returns
Task id of the newly created task group.

Definition at line 972 of file TaskManager.cpp.

◆ createQueue()

Cogs::Core::TaskQueueId Cogs::Core::TaskManager::createQueue ( std::string_view  name,
const size_t  numThreads 
)

Creates a new task queue with the given name.

Parameters
nameString name to assign to the queue.
numThreadsNumber of native threads to assign to the queue.
idTask queue id to assign to the queue. If not given, a new unique id will be generated automatically.
Returns
A task queue id used to identify the queue.

Definition at line 1046 of file TaskManager.cpp.

Referenced by TaskManager().

◆ destroy()

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().

Parameters
taskIdTask id of the task to destroy.

Definition at line 1007 of file TaskManager.cpp.

References Cogs::Core::TaskId::isValid(), and Cogs::Core::TaskId::queueId.

◆ enqueue() [1/2]

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.

Parameters
taskIdTask id of the task to enqueue.

Definition at line 986 of file TaskManager.cpp.

References Cogs::Core::TaskId::queueId.

◆ enqueue() [2/2]

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.

Parameters
queueId of the task queue to add the task to.
funcTask kernel to execute when running the task.
Returns
Task id of the newly created task.

Definition at line 993 of file TaskManager.cpp.

◆ enqueueChild()

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.

Parameters
queueId of the task queue to add the task as child of.
funcTask kernel to execute when running the task.
Returns
Task id of the newly created task.

Definition at line 1000 of file TaskManager.cpp.

References Cogs::Core::TaskId::queueId.

◆ getQueue()

Cogs::Core::TaskQueue * Cogs::Core::TaskManager::getQueue ( TaskQueueId  queueId)
inlineprivate

Definition at line 960 of file TaskManager.cpp.

◆ getQueueConcurrency()

size_t Cogs::Core::TaskManager::getQueueConcurrency ( TaskQueueId  queue)

Returns the number of worker threads in a task queue.

Parameters
queueTask queue id of the queue to query
Returns
Number of worker threads serving a task queue. Can be zero.

Definition at line 949 of file TaskManager.cpp.

◆ getQueueCount()

size_t Cogs::Core::TaskManager::getQueueCount ( ) const

Returns the number of queues managed.

Definition at line 932 of file TaskManager.cpp.

◆ getQueueName()

const std::string & Cogs::Core::TaskManager::getQueueName ( TaskQueueId  queue) const

Returns the name of a queue.

Definition at line 937 of file TaskManager.cpp.

◆ getQueueState()

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.

◆ isActive()

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.

◆ onMainThread()

bool Cogs::Core::TaskManager::onMainThread ( ) const

Returns true if called on the main thread.

Definition at line 955 of file TaskManager.cpp.

◆ updateState()

void Cogs::Core::TaskManager::updateState ( Context context)

Update state data, invoked once a frame by context.

Definition at line 925 of file TaskManager.cpp.

◆ wait()

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.

◆ waitAll() [1/2]

void Cogs::Core::TaskManager::waitAll ( )

Wait for all tasks in all task queues.

Definition at line 1038 of file TaskManager.cpp.

◆ waitAll() [2/2]

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.

Member Data Documentation

◆ GlobalQueue

constexpr TaskQueueId Cogs::Core::TaskManager::GlobalQueue = 0U
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().

◆ ResourceQueue

constexpr TaskQueueId Cogs::Core::TaskManager::ResourceQueue = 1U
staticconstexpr

◆ taskQueues

std::unique_ptr<class TaskQueues> Cogs::Core::TaskManager::taskQueues
private

Task queues manager.

Definition at line 239 of file TaskManager.h.

Referenced by TaskManager().


The documentation for this class was generated from the following files: