Radium Engine  1.5.20
Loading...
Searching...
No Matches
Ra::Core::TaskQueue Class Reference

This class allows tasks to be registered and then executed in parallel on separate threads. More...

#include <Core/Tasks/TaskQueue.hpp>

+ Collaboration diagram for Ra::Core::TaskQueue:

Classes

struct  TimerData
 Record of a task's start and end time. More...
 

Public Types

using TaskId = Utils::Index
 Identifier for a task in the task queue.
 

Public Member Functions

 TaskQueue (uint numThreads)
 
 ~TaskQueue ()
 Destructor. Waits for all the threads and safely deletes them.
 
TaskId registerTask (std::unique_ptr< Task > task)
 
void removeTask (TaskId taskId)
 
TaskId getTaskId (const std::string &taskName) const
 
void addDependency (TaskId predecessor, TaskId successor)
 
bool addDependency (const std::string &predecessors, TaskId successor)
 
bool addDependency (TaskId predecessor, const std::string &successors)
 
void addPendingDependency (const std::string &predecessors, TaskId successor)
 
void addPendingDependency (TaskId predecessor, const std::string &successors)
 
void startTasks ()
 
void runTasksInThisThread ()
 
void waitForTasks ()
 Blocks until all tasks and dependencies are finished.
 
const std::vector< TimerData > & getTimerData ()
 Access the data from the last frame execution after processTaskQueue();.
 
void flushTaskQueue ()
 Erases all tasks. Will assert if tasks are unprocessed.
 
void printTaskGraph (std::ostream &output) const
 Prints the current task graph in dot format.
 

Detailed Description

This class allows tasks to be registered and then executed in parallel on separate threads.

It maintains an internal pool of threads. When instructed, it dispatches the tasks to the pooled threads. Task are allowed to have dependencies. A task will be executed only when all its dependencies are satisfied, i.e. all dependant tasks are finished. Note that most functions are not thread safe and must not be called when the task queue is running. Typical usage:

TaskQueue taskQueue( 4 );
auto task = std::make_unique<FunctionTask>( ... );
auto tid = taskQueue.registerTask( std::move( task ) );
// [...]
taskQueue.addDependency( tid, ... );
// [...]
taskQueue.startTasks();
taskQueue.waitForTasks();
taskQueue.flushTaskQueue();
This class allows tasks to be registered and then executed in parallel on separate threads.
Definition TaskQueue.hpp:48
T move(T... args)

Definition at line 47 of file TaskQueue.hpp.

Member Typedef Documentation

◆ TaskId

using Ra::Core::TaskQueue::TaskId = Utils::Index

Identifier for a task in the task queue.

Definition at line 51 of file TaskQueue.hpp.

Constructor & Destructor Documentation

◆ TaskQueue()

Ra::Core::TaskQueue::TaskQueue ( uint numThreads)
explicit

Constructor. Initializes the thread worker pools with numThreads threads. if numThreads == 0, its a runTasksInThisThread only task queue

Definition at line 14 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ ~TaskQueue()

Ra::Core::TaskQueue::~TaskQueue ( )

Destructor. Waits for all the threads and safely deletes them.

Definition at line 22 of file TaskQueue.cpp.

+ Here is the call graph for this function:

Member Function Documentation

◆ addDependency() [1/3]

bool Ra::Core::TaskQueue::addDependency ( const std::string & predecessors,
TaskQueue::TaskId successor )

Add dependency between a task and all task with a given name. Will return false if no dependency has been added.

Definition at line 94 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ addDependency() [2/3]

bool Ra::Core::TaskQueue::addDependency ( TaskQueue::TaskId predecessor,
const std::string & successors )

Definition at line 106 of file TaskQueue.cpp.

◆ addDependency() [3/3]

void Ra::Core::TaskQueue::addDependency ( TaskQueue::TaskId predecessor,
TaskQueue::TaskId successor )

Add dependency between two tasks. The successor task will be executed only when all its predecessor completed.

Definition at line 77 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ addPendingDependency() [1/2]

void Ra::Core::TaskQueue::addPendingDependency ( const std::string & predecessors,
TaskQueue::TaskId successor )

Add a dependency between a task an all tasks with a given name, even if the task is not present yet, the name being resolved when task start.

Definition at line 117 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ addPendingDependency() [2/2]

void Ra::Core::TaskQueue::addPendingDependency ( TaskId predecessor,
const std::string & successors )

Definition at line 123 of file TaskQueue.cpp.

◆ flushTaskQueue()

void Ra::Core::TaskQueue::flushTaskQueue ( )

Erases all tasks. Will assert if tasks are unprocessed.

Definition at line 269 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ getTaskId()

TaskQueue::TaskId Ra::Core::TaskQueue::getTaskId ( const std::string & taskName) const

Definition at line 67 of file TaskQueue.cpp.

◆ getTimerData()

const std::vector< TaskQueue::TimerData > & Ra::Core::TaskQueue::getTimerData ( )

Access the data from the last frame execution after processTaskQueue();.

Definition at line 265 of file TaskQueue.cpp.

◆ printTaskGraph()

void Ra::Core::TaskQueue::printTaskGraph ( std::ostream & output) const

Prints the current task graph in dot format.

Definition at line 338 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ registerTask()

TaskQueue::TaskId Ra::Core::TaskQueue::registerTask ( std::unique_ptr< Task > task)

Registers a task to be executed. Task must have been created with new and be initialized with its parameter. The task queue assumes ownership of the task.

Definition at line 33 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ removeTask()

void Ra::Core::TaskQueue::removeTask ( TaskId taskId)

remove a task, in fact simply replace the task by a dummy empty one. hence do not affect dependencies don't affect other task id's

Definition at line 50 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ runTasksInThisThread()

void Ra::Core::TaskQueue::runTasksInThisThread ( )

Launches the execution of all task in the thread of the caller. Return when all tasks are done. Usefull for instance for opengl related tasks that must run in the context thread. Once tasks are all processed, this method call flushTasksQueue.

Warning
use either this method, either a startTasks/waitForTasks. calling runTasksInThisThead between startTasks/waitForTasks calls may produce unexpected results.

Definition at line 214 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ startTasks()

void Ra::Core::TaskQueue::startTasks ( )

Launches the execution of all the tasks in the task queue. No more tasks should be added at this point.

Definition at line 185 of file TaskQueue.cpp.

+ Here is the call graph for this function:

◆ waitForTasks()

void Ra::Core::TaskQueue::waitForTasks ( )

Blocks until all tasks and dependencies are finished.

Definition at line 259 of file TaskQueue.cpp.

+ Here is the call graph for this function:

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