Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::app::details::ScheduleExecutor Class Reference

Manages system scheduling and execution for a single schedule. More...

#include <scheduler.hpp>

Public Member Functions

 ScheduleExecutor ()=default
 
 ScheduleExecutor (ScheduleId schedule_id)
 
 ScheduleExecutor (const ScheduleExecutor &)=delete
 
 ScheduleExecutor (ScheduleExecutor &&) noexcept=default
 
 ~ScheduleExecutor ()=default
 
ScheduleExecutoroperator= (const ScheduleExecutor &)=delete
 
ScheduleExecutoroperator= (ScheduleExecutor &&) noexcept=default
 
void Clear ()
 Clears all systems and resets the schedule.
 
void AddSystem (size_t system_storage_index)
 Adds a system to this schedule.
 
void RegisterOrdering (ecs::SystemTypeId system_id, SystemOrdering ordering)
 Registers ordering constraints for a system.
 
void BuildExecutionGraph (ecs::World &world, std::span< SystemStorage > system_storage, const std::unordered_map< SystemSetId, SystemSetInfo > &system_sets)
 Builds the execution graph based on system access policies.
 
void Execute (ecs::World &world, async::Executor &executor, std::span< SystemStorage > system_storage)
 Executes all systems in this schedule.
 
auto FindSystemIndexByType (ecs::SystemTypeId system_id, std::span< const SystemStorage > system_storage) const noexcept -> std::optional< size_t >
 Finds the storage index of a system by type ID within this schedule.
 
bool IsMainStage () const noexcept
 Checks if this schedule is the Main stage.
 
bool Contains (size_t system_storage_index) const noexcept
 Checks if a system is in this schedule by storage index.
 
bool ContainsSystemOfType (ecs::SystemTypeId system_id, std::span< const SystemStorage > system_storage) const noexcept
 Checks if a system of given type is in this schedule.
 
bool Empty () const noexcept
 Checks if this schedule has any systems.
 
ScheduleId GetScheduleId () const noexcept
 Gets the schedule ID.
 
size_t SystemCount () const noexcept
 Gets the number of systems in this schedule.
 
const std::vector< size_t > & SystemIndices () const noexcept
 Gets const reference to system indices in this schedule.
 

Detailed Description

Manages system scheduling and execution for a single schedule.

Builds a dependency graph based on AccessPolicy conflicts and executes systems concurrently when possible using TaskGraph.

Note
Not thread-safe.

Definition at line 71 of file scheduler.hpp.

Constructor & Destructor Documentation

◆ ScheduleExecutor() [1/4]

helios::app::details::ScheduleExecutor::ScheduleExecutor ( )
default

◆ ScheduleExecutor() [2/4]

helios::app::details::ScheduleExecutor::ScheduleExecutor ( ScheduleId  schedule_id)
inlineexplicit

Definition at line 74 of file scheduler.hpp.

74: schedule_id_(schedule_id) {}

◆ ScheduleExecutor() [3/4]

helios::app::details::ScheduleExecutor::ScheduleExecutor ( const ScheduleExecutor )
delete

◆ ScheduleExecutor() [4/4]

helios::app::details::ScheduleExecutor::ScheduleExecutor ( ScheduleExecutor &&  )
defaultnoexcept

◆ ~ScheduleExecutor()

helios::app::details::ScheduleExecutor::~ScheduleExecutor ( )
default

Member Function Documentation

◆ AddSystem()

void helios::app::details::ScheduleExecutor::AddSystem ( size_t  system_storage_index)
inline

Adds a system to this schedule.

Parameters
system_storage_indexIndex in the global system storage

Definition at line 234 of file scheduler.hpp.

234 {
235 system_indices_.push_back(system_storage_index);
236 graph_built_ = false;
237}

◆ BuildExecutionGraph()

void helios::app::details::ScheduleExecutor::BuildExecutionGraph ( ecs::World world,
std::span< SystemStorage system_storage,
const std::unordered_map< SystemSetId, SystemSetInfo > &  system_sets 
)

Builds the execution graph based on system access policies.

Analyzes conflicts and creates dependency chains.

Parameters
worldWorld to execute systems on
system_storageReference to global system storage

Definition at line 162 of file scheduler.cpp.

163 {
164 if (system_indices_.empty()) {
165 graph_built_ = true;
166 return;
167 }
168
169 execution_graph_.Clear();
170
171 auto [tasks, system_id_to_task_index] = CreateSystemTasks(system_storage, world);
172 ApplyExplicitOrdering(tasks, system_id_to_task_index, system_storage);
173 ApplySetOrdering(tasks, system_id_to_task_index, system_sets);
174 ApplyAccessPolicyOrdering(tasks, system_storage);
175
176 graph_built_ = true;
177}

◆ Clear()

void helios::app::details::ScheduleExecutor::Clear ( )
inline

Clears all systems and resets the schedule.

Definition at line 227 of file scheduler.hpp.

227 {
228 system_indices_.clear();
229 system_orderings_.clear();
230 execution_graph_.Clear();
231 graph_built_ = false;
232}

◆ Contains()

bool helios::app::details::ScheduleExecutor::Contains ( size_t  system_storage_index) const
inlinenoexcept

Checks if a system is in this schedule by storage index.

Parameters
system_storage_indexIndex in the global system storage
Returns
True if system is present, false otherwise

Definition at line 141 of file scheduler.hpp.

141 {
142 return std::ranges::find(system_indices_, system_storage_index) != system_indices_.end();
143 }

◆ ContainsSystemOfType()

bool helios::app::details::ScheduleExecutor::ContainsSystemOfType ( ecs::SystemTypeId  system_id,
std::span< const SystemStorage system_storage 
) const
inlinenoexcept

Checks if a system of given type is in this schedule.

Parameters
system_idSystem type ID to check
system_storageReference to global system storage
Returns
True if system of this type is present, false otherwise

Definition at line 151 of file scheduler.hpp.

152 {
153 return std::ranges::any_of(system_indices_, [system_storage, system_id](size_t index) {
154 return index < system_storage.size() && system_storage[index].info.type_id == system_id;
155 });
156 }

◆ Empty()

bool helios::app::details::ScheduleExecutor::Empty ( ) const
inlinenoexcept

Checks if this schedule has any systems.

Returns
True if empty, false otherwise

Definition at line 162 of file scheduler.hpp.

162{ return system_indices_.empty(); }

◆ Execute()

void helios::app::details::ScheduleExecutor::Execute ( ecs::World world,
async::Executor executor,
std::span< SystemStorage system_storage 
)

Executes all systems in this schedule.

Warning
Triggers assertion if execution graph has not been built.
Parameters
worldWorld to execute systems on
executorAsync executor for parallel execution (ignored if use_async_ is false)
system_storageReference to global system storage

Definition at line 27 of file scheduler.cpp.

27 {
28 HELIOS_ASSERT(graph_built_, "Failed to execute: Execution graph must be built before executing schedule!");
29
30 if (IsMainStage()) {
31 // Main stage: Execute systems sequentially on the main thread
32 // Systems run in dependency order for immediate visibility
33 for (const size_t index : system_indices_) {
34 auto& storage = system_storage[index];
35 app::SystemContext ctx(world, storage.info, executor, storage.local_storage);
36 storage.system->Update(ctx);
37 ++storage.info.execution_count;
38
39 // Merge events after each system in main schedule for immediate visibility
40 if (!storage.local_storage.GetEventQueue().Empty()) {
41 world.MergeEventQueue(storage.local_storage.GetEventQueue());
42 storage.local_storage.GetEventQueue().Clear();
43 }
44 }
45 } else {
46 // All other stages: Execute the graph asynchronously via executor
47 executor.Run(execution_graph_).Wait();
48
49 // After async execution, merge all local event queues into world's main queue
50 for (const size_t index : system_indices_) {
51 auto& storage = system_storage[index];
52 if (!storage.local_storage.GetEventQueue().Empty()) {
53 world.MergeEventQueue(storage.local_storage.GetEventQueue());
54 storage.local_storage.GetEventQueue().Clear();
55 }
56 }
57 }
58}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140
bool IsMainStage() const noexcept
Checks if this schedule is the Main stage.

◆ FindSystemIndexByType()

auto helios::app::details::ScheduleExecutor::FindSystemIndexByType ( ecs::SystemTypeId  system_id,
std::span< const SystemStorage system_storage 
) const -> std::optional<size_t>
inlinenoexcept

Finds the storage index of a system by type ID within this schedule.

Parameters
system_idSystem type ID to find
system_storageReference to global system storage
Returns
Index in system storage if found, std::nullopt otherwise

Definition at line 244 of file scheduler.hpp.

246 {
247 std::optional<size_t> result;
248 const auto it = std::ranges::find_if(system_indices_, [system_storage, system_id](size_t index) {
249 return index < system_storage.size() && system_storage[index].info.type_id == system_id;
250 });
251
252 if (it != system_indices_.end()) {
253 result = *it;
254 }
255 return result;
256}

◆ GetScheduleId()

ScheduleId helios::app::details::ScheduleExecutor::GetScheduleId ( ) const
inlinenoexcept

Gets the schedule ID.

Returns
Schedule ID

Definition at line 168 of file scheduler.hpp.

168{ return schedule_id_; }

◆ IsMainStage()

bool helios::app::details::ScheduleExecutor::IsMainStage ( ) const
inlinenoexcept

Checks if this schedule is the Main stage.

Main stage executes synchronously on the main thread. All other stages execute asynchronously via the executor.

Returns
True if this is the Main stage, false otherwise

Definition at line 134 of file scheduler.hpp.

134{ return schedule_id_ == ScheduleIdOf<Main>(); }

◆ operator=() [1/2]

ScheduleExecutor & helios::app::details::ScheduleExecutor::operator= ( const ScheduleExecutor )
delete

◆ operator=() [2/2]

ScheduleExecutor & helios::app::details::ScheduleExecutor::operator= ( ScheduleExecutor &&  )
defaultnoexcept

◆ RegisterOrdering()

void helios::app::details::ScheduleExecutor::RegisterOrdering ( ecs::SystemTypeId  system_id,
SystemOrdering  ordering 
)
inline

Registers ordering constraints for a system.

Parameters
system_idSystem type ID
orderingOrdering constraints (before/after)

Definition at line 239 of file scheduler.hpp.

239 {
240 system_orderings_[system_id] = std::move(ordering);
241 graph_built_ = false;
242}

◆ SystemCount()

size_t helios::app::details::ScheduleExecutor::SystemCount ( ) const
inlinenoexcept

Gets the number of systems in this schedule.

Returns
Number of systems

Definition at line 174 of file scheduler.hpp.

174{ return system_indices_.size(); }

◆ SystemIndices()

const std::vector< size_t > & helios::app::details::ScheduleExecutor::SystemIndices ( ) const
inlinenoexcept

Gets const reference to system indices in this schedule.

Returns
Const reference to vector of system storage indices

Definition at line 180 of file scheduler.hpp.

180{ return system_indices_; }