IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
task_cancelation_token.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/task_handle.hxx>
6
7namespace ice
8{
9
11 {
13
14 inline bool was_cancelled() const noexcept { _handle.was_cancelled(); }
15
16 inline auto checkpoint() const noexcept;
17
18 template<typename Fn>
19 inline auto checkpoint(Fn&& on_cancel_cb) const noexcept;
20 };
21
22 inline auto TaskCancelationToken::checkpoint() const noexcept
23 {
24 struct Awaitable
25 {
26 ice::TaskState state;
27
28 // Skip checkpoint if the task was not canceled.
29 inline bool await_ready() const noexcept { return ice::has_none(state, TaskState::Canceled); }
30
31 // If we where canceled destroy the coroutine to clean everything up and make the Task's coroutine handle object invalid.
32 inline auto await_suspend(std::coroutine_handle<> coro) const noexcept -> ice::coroutine_handle<>
33 {
34 // Get the base promise type for all of our coroutines. (We assume the TaskPromiseBase is always the base type)
35 auto const promise_coro = ice::coroutine_handle<ice::TaskPromiseBase>::from_address(coro.address());
36
37 // If there is a continuation, resume into it
38 if (ice::coroutine_handle<> const continuation = promise_coro.promise().continuation(); continuation)
39 {
40 return continuation;
41 }
42 return std::noop_coroutine();
43 }
44
45 constexpr void await_resume() const noexcept
46 {
47 }
48
49 } await{ _handle.state() };
50 return await;
51 }
52
53 template<typename Fn>
54 inline auto TaskCancelationToken::checkpoint(Fn&& on_cancel_cb) const noexcept
55 {
56 struct Awaitable
57 {
58 ice::TaskState state;
59 Fn _fn;
60
61 // Skip checkpoint if the task was not canceled.
62 inline bool await_ready() const noexcept { return ice::has_none(state, TaskState::Canceled); }
63
64 // If we where canceled destroy the coroutine to clean everything up and make the Task's coroutine handle object invalid.
65 inline auto await_suspend(std::coroutine_handle<> coro) const noexcept -> ice::coroutine_handle<>
66 {
67 _fn(); // We already know we are cancelled, can be used to cleanup
68
69 // Get the base promise type for all of our coroutines. (We assume the TaskPromiseBase is always the base type)
70 auto const promise_coro = ice::coroutine_handle<ice::TaskPromiseBase>::from_address(coro.address());
71
72 // If there is a continuation, resume into it
73 if (ice::coroutine_handle<> const continuation = promise_coro.promise().continuation(); continuation)
74 {
75 return continuation;
76 }
77 return std::noop_coroutine();
78 }
79
80 constexpr void await_resume() const noexcept
81 {
82 }
83
84 } await{ _handle.state(), ice::forward<Fn>(on_cancel_cb) };
85 return await;
86 }
87
88} // namespace ice
89
90// Free function traits
91template<typename Result, typename... Args>
92struct std::coroutine_traits<ice::Task<Result>, ice::TaskCancelationToken, Args...>
93{
95};
96
97// Member function traits
98template<typename Result, typename Class, typename... Args>
99struct std::coroutine_traits<ice::Task<Result>, Class, ice::TaskCancelationToken, Args...>
100{
102};
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
std::coroutine_handle< Type > coroutine_handle
Definition task_types.hxx:42
constexpr bool has_none(T value, T expected_flags) noexcept
Definition enum_flags.hxx:89
ice::Expected< ice::ErrorCode > Result
Definition expected.hxx:197
Definition task_cancelation_token.hxx:11
auto checkpoint() const noexcept
Definition task_cancelation_token.hxx:22
bool was_cancelled() const noexcept
Definition task_cancelation_token.hxx:14
TaskTokenBase(ice::TaskHandle &handle) noexcept
Definition task_handle.hxx:235
Definition task_handle.hxx:143
ice::TaskHandle & _handle
Definition task_handle.hxx:240
TaskTokenBase(ice::TaskHandle &handle) noexcept
Definition task_handle.hxx:235
ice::TaskInfoPromise< Result > promise_type
Definition task_cancelation_token.hxx:101
ice::TaskInfoPromise< Result > promise_type
Definition task_cancelation_token.hxx:94