Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
single_threaded.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <functional>
10#include <optional>
11#include <vector>
12
13namespace helios::ecs {
14
15/**
16 * @brief Single-threaded executor that runs systems sequentially on a single
17 * thread.
18 * @details Uses an `async::Executor` with a `async::TaskGraph` where all tasks
19 * are linearized (each depends on the previous), guaranteeing sequential
20 * execution while still using the thread pool infrastructure.
21 */
22class SingleThreadedExecutor final : public Executor {
23public:
24 /**
25 * @brief Constructs a single-threaded executor with an external async
26 * executor.
27 * @param executor Reference to the async executor for task execution
28 */
29 explicit SingleThreadedExecutor(async::Executor& executor) noexcept
30 : executor_(executor), task_graph_("SingleThreadedExecutor") {}
31
34 ~SingleThreadedExecutor() override = default;
35
38 default;
39
40 /**
41 * @brief Executes the schedule using the single-threaded executor.
42 * @param schedule The schedule to execute
43 * @param world The world context for system execution
44 */
45 void Execute(Schedule& schedule, World& world) override;
46
47 void ExecuteAndWait(Schedule& schedule, World& world) override;
48
49 /**
50 * @brief Waits for the most recent Execute() to complete.
51 * @details Blocks until the submitted task graph finishes.
52 */
53 void Wait() override;
54
55private:
56 void BuildGraph(Schedule& schedule, World& world);
57
58 std::reference_wrapper<async::Executor> executor_;
59 async::TaskGraph task_graph_;
60 std::vector<async::Task> tasks_;
61 std::optional<async::Future<void>> future_;
62};
63
65 if (!future_.has_value()) [[unlikely]] {
66 return;
67 }
68 future_->Wait();
69 future_.reset();
70}
71
72} // namespace helios::ecs
Manages worker threads and executes task graphs using work-stealing scheduling.
Definition executor.hpp:32
Abstract interface for schedule executors.
Definition executor.hpp:27
A collection of systems, sets, and run conditions with ordering constraints.
Definition schedule.hpp:182
void ExecuteAndWait(Schedule &schedule, World &world) override
Executes all systems in the schedule and blocks until completion.
SingleThreadedExecutor(const SingleThreadedExecutor &)=delete
void Wait() override
Waits for the most recent Execute() to complete.
SingleThreadedExecutor(SingleThreadedExecutor &&) noexcept=default
void Execute(Schedule &schedule, World &world) override
Executes the schedule using the single-threaded executor.
SingleThreadedExecutor(async::Executor &executor) noexcept
Constructs a single-threaded executor with an external async executor.
The World class manages entities with their components and systems.
Definition world.hpp:39
STL namespace.