Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
async_task.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
4
6
7#include <taskflow/core/async_task.hpp>
8
9#include <cstddef>
10#include <utility>
11
12namespace helios::async {
13
14/**
15 * @brief Handle to an asynchronous task managed by the Executor.
16 * @details Wraps a tf::AsyncTask, providing methods to query task state and manage its lifecycle.
17 * Instances are typically returned by Executor's asynchronous methods.
18 * @note Thread-safe.
19 */
20class AsyncTask {
21public:
22 AsyncTask() = default;
23 AsyncTask(const AsyncTask&) = default;
24 AsyncTask(AsyncTask&&) noexcept = default;
25 ~AsyncTask() = default;
26
27 AsyncTask& operator=(const AsyncTask&) = default;
28 AsyncTask& operator=(AsyncTask&&) noexcept = default;
29
30 /**
31 * @brief Resets the underlying asynchronous task handle.
32 */
33 void Reset() { task_.reset(); }
34
35 bool operator==(const AsyncTask& other) const noexcept { return task_.hash_value() == other.task_.hash_value(); }
36 bool operator!=(const AsyncTask& other) const noexcept { return !(*this == other); }
37
38 /**
39 * @brief Checks if the asynchronous task has completed.
40 * @return True if the task is done, false otherwise.
41 * @details Returns false if task is empty.
42 */
43 [[nodiscard]] bool Done() const;
44
45 /**
46 * @brief Checks if the task handle is empty (not associated with any task).
47 * @return True if empty, false otherwise.
48 */
49 [[nodiscard]] bool Empty() const { return task_.empty(); }
50
51 /**
52 * @brief Returns a hash value for the task.
53 * @return Hash value.
54 * @details Returns '0' if task is empty.
55 */
56 [[nodiscard]] size_t Hash() const;
57
58 /**
59 * @brief Returns the number of references to the underlying task.
60 * @return Reference count.
61 * @details Returns '0' if task is empty.
62 */
63 [[nodiscard]] size_t UseCount() const { return task_.use_count(); }
64
65 /**
66 * @brief Gets the type of the task.
67 * @return TaskType::Async
68 */
69 [[nodiscard]] static constexpr TaskType GetTaskType() noexcept { return TaskType::Async; }
70
71private:
72 explicit AsyncTask(tf::AsyncTask task) : task_(std::move(task)) {}
73
74 [[nodiscard]] tf::AsyncTask& UnderlyingTask() noexcept { return task_; }
75 [[nodiscard]] const tf::AsyncTask& UnderlyingTask() const noexcept { return task_; }
76
77 tf::AsyncTask task_;
78
79 friend class Executor;
80 friend class SubTaskGraph;
81};
82
83inline bool AsyncTask::Done() const {
84 if (Empty()) [[unlikely]] {
85 return false;
86 }
87
88 return task_.is_done();
89}
90
91inline size_t AsyncTask::Hash() const {
92 if (Empty()) [[unlikely]] {
93 return 0;
94 }
95
96 return task_.hash_value();
97}
98
99} // namespace helios::async
Handle to an asynchronous task managed by the Executor.
AsyncTask(AsyncTask &&) noexcept=default
size_t UseCount() const
Returns the number of references to the underlying task.
void Reset()
Resets the underlying asynchronous task handle.
size_t Hash() const
Returns a hash value for the task.
bool Done() const
Checks if the asynchronous task has completed.
bool Empty() const
Checks if the task handle is empty (not associated with any task).
bool operator==(const AsyncTask &other) const noexcept
AsyncTask(const AsyncTask &)=default
bool operator!=(const AsyncTask &other) const noexcept
static constexpr TaskType GetTaskType() noexcept
Gets the type of the task.
Manages worker threads and executes task graphs using work-stealing scheduling.
Definition executor.hpp:33
Dynamic task graph that can be created within the execution of a task.
TaskType
Types of tasks in the async system.
Definition common.hpp:20
@ Async
Asynchronous task executed independently.
STL namespace.