Cogs.Core
|
Task queues holds tasks ready for execution by TaskWorkers. More...
Public Member Functions | |
TaskQueue (TaskQueueId id, std::string_view name, size_t numThreads) | |
Construct a TaskQueue instance with the given id and name. | |
TaskQueue (const TaskQueue &)=delete | |
TaskQueue & | operator= (const TaskQueue &)=delete |
~TaskQueue () | |
Destructs a TaskQueue. | |
Task * | createTask (const TaskFunction *func, ElementHandle &handle, const TaskId *parentTaskId=nullptr) |
Creates a new task. | |
Task * | getTask (const TaskId &taskId) |
Get the task with the given id from the pool. | |
void | destroy (const TaskId &taskId) |
Destroy the task with the given id. | |
TaskId | create (TaskFunctionRef func) |
Create a task wrapping the given function. | |
TaskId | createGroup () |
Create a task group. | |
TaskId | create (TaskFunctionRef func, const TaskId &parentTask) |
Create a task wrapping the given function, parented to the given parent task. | |
void | enqueueTask (Task *task) |
Enqueue the given task, executing it whenever a worker thread in the queue is available. | |
void | enqueue (const TaskId &taskId) |
Fetch the task with the given task id and enqueue it. | |
TaskId | enqueue (TaskFunctionRef func) |
Enqueue the given func object in the queue by creating a new Task with the function as work kernel. | |
TaskId | enqueue (TaskFunctionRef func, const TaskId &parentTaskId) |
Enqueue the given func object in the queue by creating a new Task with the function as work kernel. | |
Task * | getNextTask () |
Get the next available Task from the task queue to execute. | |
Task * | getAvailableTask () |
Gets a task from the task queue, or returns nullptr if no available task is found. | |
bool | canExecute (const Task *task) const |
Check if the given task is ready to be executed. | |
void | workOnTask (Task *task) |
Perform work on the given task. | |
void | yield () |
Yield the currently executing thread to perform work on other available tasks or simply give back execution resources to the OS. | |
void | finishTask (Task *task) |
Finish the given task, returning the Task instance to the TaskPool and removing it from the set of active tasks for this queue. | |
void | destroyTask (Task *task) |
Destroy the given task, deallocating it from the task pool and freeing up any held resources. | |
bool | isActive (const TaskId &taskId) |
void | wait (const TaskId &taskId) |
Wait on the task given by taskId. | |
void | waitAll () |
Wait for all enqueued tasks to finish. | |
size_t | getConcurrency () const |
TaskQueueId | getId () const |
const std::string & | getName () const |
void | getQueueState (QueueState &queueState, std::vector< QueueWorkerState > &workerStates) const |
void | updateState (Context *context) |
Private Member Functions | |
void | initializeWorkers () |
Initialize TaskWorker instances. | |
Private Attributes | |
Timer | timer = Timer::startNew() |
uint32_t | lastFrame = 0 |
QueueState | queueState {} |
std::vector< QueueWorkerState > | workerStates |
Aggregated worker state data. | |
std::unique_ptr< std::vector< QueueWorkerStateData > > | workerStateData |
Tracks worker thread kernel launches continuosly. | |
TaskPool | taskPool |
Task pool holding Task instances.s. | |
std::string | name |
Name of the queue used for debugging. | |
Atomic< int > | active {0} |
Number of currently active tasks. | |
bool | done = false |
If the queue is done and should stop executing tasks. | |
Atomic< uint16_t > | generation = 0 |
Generation counter used to track task lifetime. | |
std::queue< Task * > | taskQueue |
Currently queued tasks, ready for execution by a TaskWorker. | |
Mutex | poolMutex |
TaskPool mutex. | |
Mutex | taskMutex |
Task queue mutex. | |
std::condition_variable | taskVariable |
Condition variable used to signal tasks available for execution. | |
TaskQueueId | id {0} |
Id of the task queue. | |
std::vector< Thread > | threads |
Array of threads used to execute TaskWorker instances. | |
Task queues holds tasks ready for execution by TaskWorkers.
Each task queue has a set of TaskWorker instances which will retrieve queued tasks and execute them on their designated threads.
Definition at line 101 of file TaskManager.cpp.
|
inline |
Construct a TaskQueue instance with the given id and name.
The queue name is used to identify threads belonging to the workers of this queue, for example when debugging.
Definition at line 110 of file TaskManager.cpp.
References initializeWorkers().
|
inline |
Destructs a TaskQueue.
Terminates all workers and threads before returning.
Definition at line 121 of file TaskManager.cpp.
References done, taskMutex, taskVariable, and threads.
|
inline |
Check if the given task is ready to be executed.
Only tasks with an active count of 1 are eligible for execution. This means parent tasks with running child tasks are not available for execution until all child tasks finish running.
task | Pointer to the task to check. |
Definition at line 376 of file TaskManager.cpp.
References Cogs::Core::Task::active.
Referenced by workOnTask().
|
inline |
Create a task wrapping the given function.
Definition at line 226 of file TaskManager.cpp.
References createTask(), and Cogs::Core::TaskId::generation.
Create a task wrapping the given function, parented to the given parent task.
Definition at line 250 of file TaskManager.cpp.
References createTask(), and Cogs::Core::TaskId::generation.
|
inline |
Create a task group.
Definition at line 238 of file TaskManager.cpp.
References createTask(), and Cogs::Core::TaskId::generation.
|
inline |
Creates a new task.
If no function is given a task group is created.
Definition at line 139 of file TaskManager.cpp.
References Cogs::Core::Task::active, active, Cogs::Collections::Pool< ElementType >::create(), Cogs::Core::Task::flags, Cogs::Core::Task::generation, generation, Cogs::Collections::Pool< ElementType >::getHandle(), getTask(), Cogs::Core::Task::kernel, Cogs::Core::Task::parent, poolMutex, and taskPool.
Referenced by create(), createGroup(), and enqueue().
|
inline |
Destroy the task with the given id.
The task is allowed to run until completion before being removed from the task pool.
Definition at line 205 of file TaskManager.cpp.
References destroyTask(), Cogs::Core::TaskId::generation, getTask(), and wait().
|
inline |
Destroy the given task, deallocating it from the task pool and freeing up any held resources.
Definition at line 468 of file TaskManager.cpp.
References Cogs::Core::Task::active, active, Cogs::Collections::Pool< ElementType >::destroy(), Cogs::Core::Task::flags, Cogs::Core::Task::generation, generation, poolMutex, and taskPool.
Referenced by destroy(), and finishTask().
|
inline |
Fetch the task with the given task id and enqueue it.
Definition at line 280 of file TaskManager.cpp.
References enqueueTask(), and getTask().
|
inline |
Enqueue the given func object in the queue by creating a new Task with the function as work kernel.
The function will be executed whenever a TaskWorker instance becomes available and previously queued Task instances are removed from the queue.
func | Functor to assign as work kernel for the task. |
Definition at line 294 of file TaskManager.cpp.
References createTask(), enqueueTask(), Cogs::Core::Task::generation, and threads.
Enqueue the given func object in the queue by creating a new Task with the function as work kernel.
The function will be executed whenever a TaskWorker instance becomes available and previously queued Task instances are removed from the queue. The given parent task is associated with the task.
func | Functor to assign as work kernel for the task. |
parentTaskId | Task id of the parent task. |
Definition at line 318 of file TaskManager.cpp.
References createTask(), enqueueTask(), Cogs::Core::Task::generation, and threads.
|
inline |
Enqueue the given task, executing it whenever a worker thread in the queue is available.
Definition at line 262 of file TaskManager.cpp.
References finishTask(), Cogs::Core::Task::kernel, taskMutex, taskQueue, taskVariable, and threads.
Referenced by enqueue().
|
inline |
Finish the given task, returning the Task instance to the TaskPool and removing it from the set of active tasks for this queue.
task | Pointer to a currently active task, which will be deactivated and deallocated. |
Definition at line 440 of file TaskManager.cpp.
References Cogs::Core::Task::active, destroyTask(), finishTask(), Cogs::Core::Task::flags, and Cogs::Core::Task::parent.
Referenced by enqueueTask(), finishTask(), and workOnTask().
|
inline |
Gets a task from the task queue, or returns nullptr if no available task is found.
Definition at line 355 of file TaskManager.cpp.
References taskMutex, and taskQueue.
Referenced by yield().
|
inline |
Definition at line 533 of file TaskManager.cpp.
|
inline |
Definition at line 538 of file TaskManager.cpp.
|
inline |
Definition at line 543 of file TaskManager.cpp.
|
inline |
Get the next available Task from the task queue to execute.
Definition at line 337 of file TaskManager.cpp.
References done, taskMutex, taskQueue, and taskVariable.
Referenced by Cogs::Core::TaskWorker::operator()().
|
inline |
Definition at line 548 of file TaskManager.cpp.
Get the task with the given id from the pool.
Definition at line 194 of file TaskManager.cpp.
References poolMutex, Cogs::Core::TaskId::taskHandle, and taskPool.
Referenced by createTask(), destroy(), enqueue(), and wait().
|
private |
Initialize TaskWorker instances.
Initializes worker threads and TaskWorker instances for the task queue.
Definition at line 673 of file TaskManager.cpp.
References name, threads, workerStateData, and workerStates.
Referenced by TaskQueue().
|
inline |
Definition at line 485 of file TaskManager.cpp.
|
inline |
Definition at line 554 of file TaskManager.cpp.
|
inline |
Wait on the task given by taskId.
If the task has already been completed, returns immediately.
taskId | TaskId of the task to wait for. |
Definition at line 497 of file TaskManager.cpp.
References Cogs::Core::Task::active, Cogs::Core::Task::generation, Cogs::Core::TaskId::generation, getTask(), Cogs::Core::TaskId::queueId, and yield().
Referenced by destroy().
|
inline |
Wait for all enqueued tasks to finish.
If any new tasks are added to the queue during this wait, those will also be waited for.
Definition at line 524 of file TaskManager.cpp.
|
inline |
Perform work on the given task.
If it is not possible to work on the given task, the worker will yield its resources to other tasks in the queue until the task becomes available.
When done working on the task, finishTask() will be called.
task | Pointer to the task to work on. |
Definition at line 391 of file TaskManager.cpp.
References canExecute(), finishTask(), Cogs::Core::Task::kernel, and yield().
Referenced by Cogs::Core::TaskWorker::operator()(), and yield().
|
inline |
Yield the currently executing thread to perform work on other available tasks or simply give back execution resources to the OS.
Definition at line 420 of file TaskManager.cpp.
References getAvailableTask(), and workOnTask().
Referenced by wait(), waitAll(), and workOnTask().
|
private |
Number of currently active tasks.
Definition at line 605 of file TaskManager.cpp.
Referenced by createTask(), destroyTask(), and waitAll().
|
private |
If the queue is done and should stop executing tasks.
Definition at line 608 of file TaskManager.cpp.
Referenced by getNextTask(), and ~TaskQueue().
|
private |
Generation counter used to track task lifetime.
Definition at line 611 of file TaskManager.cpp.
Referenced by createTask(), and destroyTask().
|
private |
Id of the task queue.
Definition at line 626 of file TaskManager.cpp.
|
private |
Definition at line 585 of file TaskManager.cpp.
|
private |
Name of the queue used for debugging.
Definition at line 602 of file TaskManager.cpp.
Referenced by initializeWorkers().
|
private |
TaskPool mutex.
Definition at line 617 of file TaskManager.cpp.
Referenced by createTask(), destroyTask(), and getTask().
|
private |
Definition at line 587 of file TaskManager.cpp.
|
private |
Task queue mutex.
Definition at line 620 of file TaskManager.cpp.
Referenced by enqueueTask(), getAvailableTask(), getNextTask(), and ~TaskQueue().
|
private |
Task pool holding Task instances.s.
Definition at line 599 of file TaskManager.cpp.
Referenced by createTask(), destroyTask(), and getTask().
|
private |
Currently queued tasks, ready for execution by a TaskWorker.
Definition at line 614 of file TaskManager.cpp.
Referenced by enqueueTask(), getAvailableTask(), and getNextTask().
|
private |
Condition variable used to signal tasks available for execution.
Definition at line 623 of file TaskManager.cpp.
Referenced by enqueueTask(), getNextTask(), and ~TaskQueue().
|
private |
Array of threads used to execute TaskWorker instances.
Definition at line 629 of file TaskManager.cpp.
Referenced by enqueue(), enqueueTask(), initializeWorkers(), and ~TaskQueue().
|
private |
Definition at line 584 of file TaskManager.cpp.
|
private |
Tracks worker thread kernel launches continuosly.
Definition at line 593 of file TaskManager.cpp.
Referenced by initializeWorkers().
|
private |
Aggregated worker state data.
Definition at line 590 of file TaskManager.cpp.
Referenced by initializeWorkers().