Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::async::Executor Class Reference

Manages worker threads and executes task graphs using work-stealing scheduling. More...

#include <executor.hpp>

Public Member Functions

 Executor ()=default
 Default constructs an executor with hardware concurrency worker threads.
 Executor (size_t worker_thread_count)
 Constructs an executor with the specified number of worker threads.
 Executor (const Executor &)=delete
 Executor (Executor &&)=delete
 ~Executor ()=default
Executoroperator= (const Executor &)=delete
Executoroperator= (Executor &&)=delete
auto Run (TaskGraph &graph) -> Future< void >
 Runs a task graph once.
auto Run (TaskGraph &&graph) -> Future< void >
 Runs a moved task graph once.
template<std::invocable C>
auto Run (TaskGraph &graph, C &&callable) -> Future< void >
 Runs a task graph once and invokes a callback upon completion.
template<std::invocable C>
auto Run (TaskGraph &&graph, C &&callable) -> Future< void >
 Runs a moved task graph once and invokes a callback upon completion.
auto RunN (TaskGraph &graph, size_t count) -> Future< void >
 Runs a task graph for the specified number of times.
auto RunN (TaskGraph &&graph, size_t count) -> Future< void >
 Runs a moved task graph for the specified number of times.
template<std::invocable C>
auto RunN (TaskGraph &graph, size_t count, C &&callable) -> Future< void >
 Runs a task graph for the specified number of times and invokes a callback.
template<std::invocable C>
auto RunN (TaskGraph &&graph, size_t count, C &&callable) -> Future< void >
 Runs a moved task graph for the specified number of times and invokes a callback.
template<std::predicate Predicate>
auto RunUntil (TaskGraph &graph, Predicate &&predicate) -> Future< void >
 Runs a task graph repeatedly until the predicate returns true.
template<std::predicate Predicate>
auto RunUntil (TaskGraph &&graph, Predicate &&predicate) -> Future< void >
 Runs a moved task graph repeatedly until the predicate returns true.
template<std::predicate Predicate, std::invocable C>
auto RunUntil (TaskGraph &graph, Predicate &&predicate, C &&callable) -> Future< void >
 Runs a task graph repeatedly until the predicate returns true, then invokes a callback.
template<std::predicate Predicate, std::invocable C>
auto RunUntil (TaskGraph &&graph, Predicate &&predicate, C &&callable) -> Future< void >
 Runs a moved task graph repeatedly until the predicate returns true, then invokes a callback.
template<std::invocable C>
auto Async (C &&callable) -> std::future< std::invoke_result_t< C > >
 Creates an asynchronous task that runs the given callable.
template<std::invocable C>
auto Async (std::string name, C &&callable) -> std::future< std::invoke_result_t< C > >
 Creates a named asynchronous task that runs the given callable.
template<std::invocable C>
void SilentAsync (C &&callable)
 Creates an asynchronous task without returning a future.
template<std::invocable C>
void SilentAsync (std::string name, C &&callable)
 Creates a named asynchronous task without returning a future.
template<std::invocable C, std::ranges::range Dependencies>
requires std::same_as<std::ranges::range_value_t<Dependencies>, AsyncTask>
auto DependentAsync (C &&callable, const Dependencies &dependencies) -> std::pair< AsyncTask, std::future< std::invoke_result_t< C > > >
 Creates an asynchronous task that runs after specified dependencies complete.
template<std::invocable C, std::ranges::range Dependencies>
requires std::same_as<std::ranges::range_value_t<Dependencies>, AsyncTask>
AsyncTask SilentDependentAsync (C &&callable, const Dependencies &dependencies)
 Creates an asynchronous task that runs after dependencies complete, without returning a future.
void WaitForAll ()
 Blocks until all submitted tasks complete.
void CoRun (TaskGraph &graph)
 Runs a task graph cooperatively and waits until it completes using the current worker thread.
template<std::predicate Predicate>
void CoRunUntil (Predicate &&predicate)
 Keeps the current worker thread running until the predicate returns true.
bool IsWorkerThread () const
 Checks if the current thread is a worker thread of this executor.
int CurrentWorkerId () const
 Gets the ID of the current worker thread.
size_t WorkerCount () const noexcept
 Gets the total number of worker threads.
size_t IdleWorkerCount () const noexcept
 Gets the number of worker threads currently waiting for work.
size_t QueueCount () const noexcept
 Gets the number of task queues in the work-stealing scheduler.
size_t RunningTopologyCount () const
 Gets the number of task graphs currently being executed.

Detailed Description

Manages worker threads and executes task graphs using work-stealing scheduling.

The Executor wraps tf::Executor and provides methods to run TaskGraphs and create asynchronous tasks. It manages a pool of worker threads that efficiently execute tasks using a work-stealing algorithm.

Note
All member functions are thread-safe unless otherwise noted.

Definition at line 32 of file executor.hpp.

Constructor & Destructor Documentation

◆ Executor() [1/4]

helios::async::Executor::Executor ( )
default

Default constructs an executor with hardware concurrency worker threads.

◆ Executor() [2/4]

helios::async::Executor::Executor ( size_t worker_thread_count)
inlineexplicit

Constructs an executor with the specified number of worker threads.

Parameters
worker_thread_countNumber of worker threads to create

Definition at line 44 of file executor.hpp.

◆ Executor() [3/4]

helios::async::Executor::Executor ( const Executor & )
delete

◆ Executor() [4/4]

helios::async::Executor::Executor ( Executor && )
delete

◆ ~Executor()

helios::async::Executor::~Executor ( )
default

Member Function Documentation

◆ Async() [1/2]

template<std::invocable C>
auto helios::async::Executor::Async ( C && callable) -> std::future< std::invoke_result_t< C > >
inline

Creates an asynchronous task that runs the given callable.

The task is scheduled immediately and runs independently.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
callableFunction to execute asynchronously
Returns
Future that will hold the result of the execution

Definition at line 255 of file executor.hpp.

◆ Async() [2/2]

template<std::invocable C>
auto helios::async::Executor::Async ( std::string name,
C && callable ) -> std::future< std::invoke_result_t< C > >
inline

Creates a named asynchronous task that runs the given callable.

The task is scheduled immediately and runs independently.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
nameName for the task (useful for debugging/profiling)
callableFunction to execute asynchronously
Returns
Future that will hold the result of the execution

Definition at line 411 of file executor.hpp.

◆ CoRun()

void helios::async::Executor::CoRun ( TaskGraph & graph)
inline

Runs a task graph cooperatively and waits until it completes using the current worker thread.

Warning
Must be called from within a worker thread of this executor. Triggers assertion if called from a non-worker thread.
Parameters
graphTask graph to execute

Definition at line 466 of file executor.hpp.

◆ CoRunUntil()

template<std::predicate Predicate>
void helios::async::Executor::CoRunUntil ( Predicate && predicate)
inline

Keeps the current worker thread running until the predicate returns true.

Warning
Must be called from within a worker thread of this executor. Triggers assertion if called from a non-worker thread.
Template Parameters
PredicatePredicate type
Parameters
predicateBoolean predicate to determine when to stop

Definition at line 474 of file executor.hpp.

◆ CurrentWorkerId()

int helios::async::Executor::CurrentWorkerId ( ) const
inlinenodiscard

Gets the ID of the current worker thread.

Note
Thread-safe.
Returns
Worker ID (0 to N-1) or -1 if not a worker thread

Definition at line 366 of file executor.hpp.

◆ DependentAsync()

template<std::invocable C, std::ranges::range Dependencies>
requires std::same_as<std::ranges::range_value_t<Dependencies>, AsyncTask>
auto helios::async::Executor::DependentAsync ( C && callable,
const Dependencies & dependencies ) -> std::pair< AsyncTask, std::future< std::invoke_result_t< C > > >
inline

Creates an asynchronous task that runs after specified dependencies complete.

The task will only execute after all dependencies finish.

Note
Thread-safe.
Template Parameters
CCallable type
DependenciesRange type containing AsyncTask dependencies
Parameters
callableFunction to execute asynchronously
dependenciesTasks that must complete before this task runs
Returns
Pair containing AsyncTask handle and Future for the result

Definition at line 427 of file executor.hpp.

◆ IdleWorkerCount()

size_t helios::async::Executor::IdleWorkerCount ( ) const
inlinenodiscardnoexcept

Gets the number of worker threads currently waiting for work.

Note
Thread safe.
Returns
Count of idle worker threads

Definition at line 384 of file executor.hpp.

◆ IsWorkerThread()

bool helios::async::Executor::IsWorkerThread ( ) const
inlinenodiscard

Checks if the current thread is a worker thread of this executor.

Note
Thread safe.
Returns
True if current thread is a worker, false otherwise

Definition at line 359 of file executor.hpp.

◆ operator=() [1/2]

Executor & helios::async::Executor::operator= ( const Executor & )
delete

◆ operator=() [2/2]

Executor & helios::async::Executor::operator= ( Executor && )
delete

◆ QueueCount()

size_t helios::async::Executor::QueueCount ( ) const
inlinenodiscardnoexcept

Gets the number of task queues in the work-stealing scheduler.

Note
Thread safe.
Returns
Count of task queues

Definition at line 393 of file executor.hpp.

◆ Run() [1/4]

auto helios::async::Executor::Run ( TaskGraph && graph) -> Future< void >
inline

Runs a moved task graph once.

The executor takes ownership of the moved graph.

Note
Thread-safe.
Parameters
graphTask graph to execute (moved)
Returns
Future that completes when execution finishes

Definition at line 72 of file executor.hpp.

◆ Run() [2/4]

template<std::invocable C>
auto helios::async::Executor::Run ( TaskGraph && graph,
C && callable ) -> Future< void >
inline

Runs a moved task graph once and invokes a callback upon completion.

The executor takes ownership of the moved graph.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
graphTask graph to execute (moved)
callableCallback to invoke after execution completes
Returns
Future that completes when execution finishes

Definition at line 103 of file executor.hpp.

◆ Run() [3/4]

auto helios::async::Executor::Run ( TaskGraph & graph) -> Future< void >
inline

Runs a task graph once.

The executor does not own the graph - ensure it remains alive during execution.

Note
Thread-safe.
Parameters
graphTask graph to execute
Returns
Future that completes when execution finishes

Definition at line 61 of file executor.hpp.

◆ Run() [4/4]

template<std::invocable C>
auto helios::async::Executor::Run ( TaskGraph & graph,
C && callable ) -> Future< void >
inline

Runs a task graph once and invokes a callback upon completion.

The executor does not own the graph - ensure it remains alive during execution.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
graphTask graph to execute
callableCallback to invoke after execution completes
Returns
Future that completes when execution finishes

Definition at line 88 of file executor.hpp.

◆ RunN() [1/4]

auto helios::async::Executor::RunN ( TaskGraph && graph,
size_t count ) -> Future< void >
inline

Runs a moved task graph for the specified number of times.

The executor takes ownership of the moved graph.

Note
Thread-safe.
Parameters
graphTask graph to execute (moved)
countNumber of times to run the graph
Returns
Future that completes when all executions finish

Definition at line 130 of file executor.hpp.

◆ RunN() [2/4]

template<std::invocable C>
auto helios::async::Executor::RunN ( TaskGraph && graph,
size_t count,
C && callable ) -> Future< void >
inline

Runs a moved task graph for the specified number of times and invokes a callback.

The executor takes ownership of the moved graph.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
graphTask graph to execute (moved)
countNumber of times to run the graph
callableCallback to invoke after all executions complete
Returns
Future that completes when all executions finish

Definition at line 165 of file executor.hpp.

◆ RunN() [3/4]

auto helios::async::Executor::RunN ( TaskGraph & graph,
size_t count ) -> Future< void >
inline

Runs a task graph for the specified number of times.

The executor does not own the graph - ensure it remains alive during execution.

Note
Thread-safe.
Parameters
graphTask graph to execute
countNumber of times to run the graph
Returns
Future that completes when all executions finish

Definition at line 118 of file executor.hpp.

◆ RunN() [4/4]

template<std::invocable C>
auto helios::async::Executor::RunN ( TaskGraph & graph,
size_t count,
C && callable ) -> Future< void >
inline

Runs a task graph for the specified number of times and invokes a callback.

The executor does not own the graph - ensure it remains alive during execution.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
graphTask graph to execute
countNumber of times to run the graph
callableCallback to invoke after all executions complete
Returns
Future that completes when all executions finish

Definition at line 148 of file executor.hpp.

◆ RunningTopologyCount()

size_t helios::async::Executor::RunningTopologyCount ( ) const
inlinenodiscard

Gets the number of task graphs currently being executed.

Note
Thread safe.
Returns
Count of running task graphs

Definition at line 402 of file executor.hpp.

◆ RunUntil() [1/4]

template<std::predicate Predicate>
auto helios::async::Executor::RunUntil ( TaskGraph && graph,
Predicate && predicate ) -> Future< void >
inline

Runs a moved task graph repeatedly until the predicate returns true.

The executor takes ownership of the moved graph.

Note
Thread-safe.
Template Parameters
PredicatePredicate type
Parameters
graphTask graph to execute (moved)
predicateBoolean predicate to determine when to stop
Returns
Future that completes when predicate returns true

Definition at line 197 of file executor.hpp.

◆ RunUntil() [2/4]

template<std::predicate Predicate, std::invocable C>
auto helios::async::Executor::RunUntil ( TaskGraph && graph,
Predicate && predicate,
C && callable ) -> Future< void >
inline

Runs a moved task graph repeatedly until the predicate returns true, then invokes a callback.

The executor takes ownership of the moved graph.

Note
Thread-safe.
Template Parameters
PredicatePredicate type
CCallable type
Parameters
graphTask graph to execute (moved)
predicateBoolean predicate to determine when to stop
callableCallback to invoke after execution completes
Returns
Future that completes when predicate returns true and callback finishes

Definition at line 239 of file executor.hpp.

◆ RunUntil() [3/4]

template<std::predicate Predicate>
auto helios::async::Executor::RunUntil ( TaskGraph & graph,
Predicate && predicate ) -> Future< void >
inline

Runs a task graph repeatedly until the predicate returns true.

The executor does not own the graph - ensure it remains alive during execution.

Note
Thread-safe.
Template Parameters
PredicatePredicate type
Parameters
graphTask graph to execute
predicateBoolean predicate to determine when to stop
Returns
Future that completes when predicate returns true

Definition at line 182 of file executor.hpp.

◆ RunUntil() [4/4]

template<std::predicate Predicate, std::invocable C>
auto helios::async::Executor::RunUntil ( TaskGraph & graph,
Predicate && predicate,
C && callable ) -> Future< void >
inline

Runs a task graph repeatedly until the predicate returns true, then invokes a callback.

The executor does not own the graph - ensure it remains alive during execution.

Note
Thread-safe.
Template Parameters
PredicatePredicate type
CCallable type
Parameters
graphTask graph to execute
predicateBoolean predicate to determine when to stop
callableCallback to invoke after execution completes
Returns
Future that completes when predicate returns true and callback finishes

Definition at line 218 of file executor.hpp.

◆ SilentAsync() [1/2]

template<std::invocable C>
void helios::async::Executor::SilentAsync ( C && callable)
inline

Creates an asynchronous task without returning a future.

More efficient than Async when you don't need the result.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
callableFunction to execute asynchronously

Definition at line 280 of file executor.hpp.

◆ SilentAsync() [2/2]

template<std::invocable C>
void helios::async::Executor::SilentAsync ( std::string name,
C && callable )
inline

Creates a named asynchronous task without returning a future.

More efficient than Async when you don't need the result.

Note
Thread-safe.
Template Parameters
CCallable type
Parameters
nameName for the task (useful for debugging/profiling)
callableFunction to execute asynchronously

Definition at line 419 of file executor.hpp.

◆ SilentDependentAsync()

template<std::invocable C, std::ranges::range Dependencies>
requires std::same_as<std::ranges::range_value_t<Dependencies>, AsyncTask>
AsyncTask helios::async::Executor::SilentDependentAsync ( C && callable,
const Dependencies & dependencies )
inline

Creates an asynchronous task that runs after dependencies complete, without returning a future.

More efficient than DependentAsync when you don't need the result.

Note
Thread-safe.
Template Parameters
CCallable type
DependenciesRange type containing AsyncTask dependencies
Parameters
callableFunction to execute asynchronously
dependenciesTasks that must complete before this task runs
Returns
AsyncTask handle

Definition at line 446 of file executor.hpp.

◆ WaitForAll()

void helios::async::Executor::WaitForAll ( )
inline

Blocks until all submitted tasks complete.

Waits for all taskflows and async tasks to finish.

Note
Thread-safe.

Definition at line 461 of file executor.hpp.

◆ WorkerCount()

size_t helios::async::Executor::WorkerCount ( ) const
inlinenodiscardnoexcept

Gets the total number of worker threads.

Note
Thread safe.
Returns
Count of worker threads

Definition at line 375 of file executor.hpp.