IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
task_handle.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/task_info.hxx>
7
8namespace ice
9{
10
15 struct TaskHandle final
16 {
17 inline TaskHandle() noexcept;
18 inline TaskHandle(TaskHandle&& other) noexcept;
19 inline TaskHandle(TaskHandle const& other) noexcept;
20 inline ~TaskHandle() noexcept;
21
22 inline auto operator=(TaskHandle&& other) noexcept -> TaskHandle&;
23 inline auto operator=(TaskHandle const& other) noexcept -> TaskHandle&;
24
26 auto state() const noexcept
27 {
28 return _info == nullptr ? TaskState::Invalid : _info->state.load(std::memory_order_relaxed);
29 }
30
33 bool is_valid() const noexcept { return state() != TaskState::None; }
34
37 bool is_running() const noexcept { return _info->has_any(TaskState::Running); }
38
42 bool is_suspended() const noexcept { return _info->has_any(TaskState::Suspended); }
43
46 bool was_cancelled() const noexcept { return _info->has_any(TaskState::Canceled); }
47
50 bool has_finished() const noexcept { return _info->has_any(TaskState::Succeeded | TaskState::Failed); }
51
54 bool has_succeded() const noexcept { return _info->has_any(TaskState::Succeeded); }
55
58 bool has_failed() const noexcept { return _info->has_any(TaskState::Failed); }
59
62 inline bool cancel() noexcept;
63
65 };
66
67 namespace detail
68 {
69
70 inline bool try_set_canceled_state(std::atomic<ice::TaskState>& state) noexcept
71 {
72 bool success = false;
73 ice::TaskState expected = state.load(std::memory_order_relaxed);
74 while(ice::has_any(expected, TaskState::Running | TaskState::Created) && success == false)
75 {
76 success = state.compare_exchange_weak(
77 expected,
78 expected | TaskState::Canceled,
79 std::memory_order_relaxed,
80 std::memory_order_relaxed
81 );
82
84 {
85 break;
86 }
87 }
88 return success;
89 }
90
91 } // namespace detail
92
93
94 inline TaskHandle::TaskHandle() noexcept
95 : _info{ nullptr }
96 { }
97
98 inline TaskHandle::TaskHandle(TaskHandle&& other) noexcept
99 : _info{ ice::exchange(other._info, nullptr) }
100 { }
101
102 inline TaskHandle::TaskHandle(TaskHandle const& other) noexcept
103 : _info{ other._info->aquire() }
104 { }
105
106 inline TaskHandle::~TaskHandle() noexcept
107 {
108 if (_info != nullptr) _info->release();
109 }
110
111 inline auto TaskHandle::operator=(TaskHandle&& other) noexcept -> TaskHandle&
112 {
113 if (this != ice::addressof(other))
114 {
115 _info->release();
116 _info = ice::exchange(other._info, nullptr);
117 }
118 return *this;
119 }
120
121 inline auto TaskHandle::operator=(TaskHandle const& other) noexcept -> TaskHandle&
122 {
123 if (this != ice::addressof(other))
124 {
125 _info->release();
126 _info = other._info->aquire();
127 }
128 return *this;
129 }
130
131 inline bool TaskHandle::cancel() noexcept
132 {
133 bool success = false;
134 if (_info != nullptr)
135 {
137 }
138 return success;
139 }
140
141 template<typename Result>
142 struct TaskInfoPromise : public ice::TaskPromise<Result>
143 {
145
146 TaskInfoPromise() noexcept = default;
147
148 template<typename... Args>
149 TaskInfoPromise(ice::TaskHandle& handle, Args const&...) noexcept
150 : _info{ new ice::TaskInfo{} }
151 {
152 if (handle._info)
153 {
154 handle._info->release();
155 }
156 handle._info = _info->aquire();
157 }
158
159 template<typename Class, typename... Args>
160 TaskInfoPromise(Class const&, ice::TaskHandle& handle, Args const&...) noexcept
161 : _info{ new ice::TaskInfo{} }
162 {
163 if (handle._info)
164 {
165 handle._info->release();
166 }
167 handle._info = _info->aquire();
168 }
169
171 {
172 if (_info != nullptr)
173 {
174 if (_info->has_any(ice::TaskState::Canceled))
175 {
176 _info->state.store(ice::TaskState::Canceled | ice::TaskState::Failed, std::memory_order_relaxed);
177 }
178 _info->release();
179 }
180 }
181
183 {
184 template<typename Promise>
186 {
187 ice::TaskInfo* const info = coro.promise()._info;
188 if (info != nullptr)
189 {
190 if (info->has_any(ice::TaskState::Canceled))
191 {
193 }
194 else
195 {
196 info->state.store(ice::TaskState::Succeeded);
197 }
198 }
199
201 }
202 };
203
205 {
207
208 constexpr bool await_ready() const noexcept { return this->_info->has_any(ice::TaskState::Canceled); }
209
210 constexpr void await_suspend(ice::coroutine_handle<>) const noexcept { }
211
212 inline void await_resume() const noexcept
213 {
214 if (this->_info != nullptr)
215 {
216 ICE_ASSERT_CORE(this->_info->has_any(ice::TaskState::Canceled) == false);
217 this->_info->state.store(ice::TaskState::Running);
218 }
219 }
220 };
221
222 inline auto initial_suspend() const noexcept
223 {
224 return InitialAwaitable{ this->_info };
225 }
226
227 inline auto final_suspend() const noexcept
228 {
229 return ExtendedFinalAwaitable{ };
230 }
231 };
232
234 {
235 TaskTokenBase(ice::TaskHandle& handle) noexcept : _handle{ handle } { }
236 ~TaskTokenBase() noexcept = default;
237
238 inline operator ice::TaskHandle&() noexcept { return _handle; }
239
241 };
242
243
244} // namespace ice
245
246// Free function traits
247template<typename Result, typename... Args>
248struct std::coroutine_traits<ice::Task<Result>, ice::TaskHandle&, Args...>
249{
251};
252
253// Member function traits
254template<typename Result, typename Class, typename... Args>
255struct std::coroutine_traits<ice::Task<Result>, Class, ice::TaskHandle&, Args...>
256{
258};
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition task_promise.hxx:13
Definition hashmap_details.hxx:13
bool try_set_canceled_state(std::atomic< ice::TaskState > &state) noexcept
Definition task_handle.hxx:70
Definition info.hxx:8
SPDX-License-Identifier: MIT.
Definition array.hxx:12
TaskState
All states a task can be in.
Definition task_info.hxx:13
@ Canceled
Task was canceled at any point of it's lifetime.
Definition task_info.hxx:32
@ Created
Task exists, but execution did not start.
Definition task_info.hxx:20
@ Invalid
Special state from TaskHandles, handle is not initialized.
Definition task_info.hxx:17
@ Running
Task, or one of it's subtasks are executing.
Definition task_info.hxx:23
@ Succeeded
Task finished execution with a valid result.
Definition task_info.hxx:29
@ None
Special state from TaskHandles, handle is not initialized.
Definition task_info.hxx:15
@ Suspended
Task is suspended and awaits resuming. (Unused).
Definition task_info.hxx:26
@ Failed
Task finished execution but results are invalid.
Definition task_info.hxx:35
constexpr bool has_any(T value, T expected_flags) noexcept
Definition enum_flags.hxx:83
std::coroutine_handle< Type > coroutine_handle
Definition task_types.hxx:42
ice::Expected< ice::ErrorCode > Result
Definition expected.hxx:197
Special handle accessing task information at runtime.
Definition task_handle.hxx:16
TaskHandle() noexcept
Definition task_handle.hxx:94
~TaskHandle() noexcept
Definition task_handle.hxx:106
ice::TaskInfo * _info
Definition task_handle.hxx:64
bool has_finished() const noexcept
Definition task_handle.hxx:50
auto state() const noexcept
Definition task_handle.hxx:26
bool is_suspended() const noexcept
Definition task_handle.hxx:42
bool was_cancelled() const noexcept
Definition task_handle.hxx:46
bool has_succeded() const noexcept
Definition task_handle.hxx:54
bool is_running() const noexcept
Definition task_handle.hxx:37
auto operator=(TaskHandle &&other) noexcept -> TaskHandle &
Definition task_handle.hxx:111
bool cancel() noexcept
Sends a cancel request to the connected task.
Definition task_handle.hxx:131
bool has_failed() const noexcept
Definition task_handle.hxx:58
bool is_valid() const noexcept
Definition task_handle.hxx:33
Definition task_info.hxx:50
bool has_any(ice::TaskState state) const noexcept
Definition task_info.hxx:87
std::atomic< ice::TaskState > state
Definition task_info.hxx:65
auto aquire() noexcept -> ice::TaskInfo *
Definition task_info.hxx:71
Definition task_handle.hxx:183
auto await_suspend(ice::coroutine_handle< Promise > coro) noexcept
Definition task_handle.hxx:185
Definition task_handle.hxx:205
constexpr void await_suspend(ice::coroutine_handle<>) const noexcept
Definition task_handle.hxx:210
void await_resume() const noexcept
Definition task_handle.hxx:212
ice::TaskInfo * _info
Definition task_handle.hxx:206
constexpr bool await_ready() const noexcept
Definition task_handle.hxx:208
Definition task_handle.hxx:143
TaskInfoPromise(Class const &, ice::TaskHandle &handle, Args const &...) noexcept
Definition task_handle.hxx:160
auto final_suspend() const noexcept
Definition task_handle.hxx:227
~TaskInfoPromise() noexcept
Definition task_handle.hxx:170
ice::TaskInfo * _info
Definition task_handle.hxx:144
TaskInfoPromise() noexcept=default
auto initial_suspend() const noexcept
Definition task_handle.hxx:222
Definition task_promise_base.hxx:15
ice::TaskHandle & _handle
Definition task_handle.hxx:240
TaskTokenBase(ice::TaskHandle &handle) noexcept
Definition task_handle.hxx:235
~TaskTokenBase() noexcept=default
ice::TaskInfoPromise< Result > promise_type
Definition task_handle.hxx:257
ice::TaskInfoPromise< Result > promise_type
Definition task_handle.hxx:250