Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
multi_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 Multi-threaded executor that runs systems in parallel using
17 * work-stealing.
18 * @details Uses an injected `async::Executor` reference to run a
19 * `async::TaskGraph` that respects ordering constraints and access-policy
20 * conflicts. The `async::TaskGraph` and task vector are cached across frames
21 * and cleared/rebuilt each execution.
22 */
23class MultiThreadedExecutor final : public Executor {
24public:
25 /**
26 * @brief Constructs a multi-threaded executor with an external async
27 * executor.
28 * @param executor Reference to the async executor for thread pool management
29 */
30 explicit MultiThreadedExecutor(async::Executor& executor) noexcept
31 : executor_(executor), task_graph_("MultiThreadedExecutor") {}
32
35 ~MultiThreadedExecutor() override = default;
36
37 MultiThreadedExecutor& operator=(const MultiThreadedExecutor&) = delete;
38 MultiThreadedExecutor& operator=(MultiThreadedExecutor&&) noexcept = default;
39
40 /**
41 * @brief Executes the schedule using the multi-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
MultiThreadedExecutor(async::Executor &executor) noexcept
Constructs a multi-threaded executor with an external async executor.
MultiThreadedExecutor(const MultiThreadedExecutor &)=delete
MultiThreadedExecutor(MultiThreadedExecutor &&) noexcept=default
void Wait() override
Waits for the most recent Execute() to complete.
void ExecuteAndWait(Schedule &schedule, World &world) override
Executes all systems in the schedule and blocks until completion.
void Execute(Schedule &schedule, World &world) override
Executes the schedule using the multi-threaded executor.
A collection of systems, sets, and run conditions with ordering constraints.
Definition schedule.hpp:182
The World class manages entities with their components and systems.
Definition world.hpp:39
STL namespace.