forked from NVIDIA/stdexec
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathon_coro_disposition.hpp
More file actions
257 lines (217 loc) · 7.84 KB
/
on_coro_disposition.hpp
File metadata and controls
257 lines (217 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Copyright (c) 2023 Maikel Nadolski
* Copyright (c) 2021-2024 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// The original idea is taken from libunifex and adapted to STDEXEC.
#include "../stdexec/coroutine.hpp"
#include "../stdexec/execution.hpp"
#include "any_sender_of.hpp"
#include "inline_scheduler.hpp" // IWYU pragma: keep
#include "task.hpp"
#include <exception>
namespace experimental::execution
{
namespace __on_coro_disp
{
using namespace STDEXEC;
using __any_scheduler =
any_receiver_ref<completion_signatures<set_error_t(std::exception_ptr),
set_stopped_t()>>::any_sender<>::any_scheduler<>;
template <class _Promise>
concept __promise_with_disposition = __at_coro_exit::__has_continuation<_Promise>
&& requires(_Promise& __promise) {
{
__promise.disposition()
} -> __std::convertible_to<task_disposition>;
};
struct __get_disposition
{
task_disposition __disposition_;
static constexpr auto await_ready() noexcept -> bool
{
return false;
}
template <class _Promise>
auto await_suspend(__std::coroutine_handle<_Promise> __h) noexcept -> bool
{
auto& __promise = __h.promise();
__disposition_ = __promise.__get_disposition_callback_(__promise.__parent_.address());
return false;
}
[[nodiscard]]
auto await_resume() const noexcept -> task_disposition
{
return __disposition_;
}
};
template <class... _Ts>
class [[nodiscard]] __task
{
struct __promise;
public:
using promise_type = __promise;
explicit __task(__std::coroutine_handle<__promise> __coro) noexcept
: __coro_(__coro)
{}
__task(__task&& __that) noexcept
: __coro_(std::exchange(__that.__coro_, {}))
{}
[[nodiscard]]
auto await_ready() const noexcept -> bool
{
return false;
}
template <__promise_with_disposition _Promise>
auto await_suspend(__std::coroutine_handle<_Promise> __parent) noexcept -> bool
{
__coro_.promise().__parent_ = __parent;
__coro_.promise().__get_disposition_callback_ = [](void* __parent) noexcept
{
_Promise& __promise = __std::coroutine_handle<_Promise>::from_address(__parent).promise();
return __promise.disposition();
};
__coro_.promise().__scheduler_ = get_scheduler(get_env(__parent.promise()));
__coro_.promise().set_continuation(__parent.promise().continuation());
__parent.promise().set_continuation(__coro_);
return false;
}
auto await_resume() noexcept -> std::tuple<_Ts&...>
{
return std::exchange(__coro_, {}).promise().__args_;
}
private:
struct __final_awaitable
{
static constexpr auto await_ready() noexcept -> bool
{
return false;
}
static auto await_suspend(__std::coroutine_handle<__promise> __h) noexcept //
-> __std::coroutine_handle<>
{
auto __cont = __h.promise().continuation();
auto __coro = __h.promise().__is_stopped_ ? __cont.unhandled_stopped() : __cont.handle();
return STDEXEC_CORO_DESTROY_AND_CONTINUE(__h, __coro);
}
void await_resume() const noexcept {}
};
struct __env
{
__promise const & __promise_;
[[nodiscard]]
auto query(get_scheduler_t) const noexcept -> __any_scheduler
{
return __promise_.__scheduler_;
}
};
struct __promise : with_awaitable_senders<__promise>
{
#if STDEXEC_EDG()
template <class _Action>
__promise(_Action&&, _Ts&&... __ts) noexcept
: __args_{__ts...}
{}
#else
template <class _Action>
explicit __promise(_Action&&, _Ts&... __ts) noexcept
: __args_{__ts...}
{}
#endif
auto initial_suspend() noexcept -> __std::suspend_always
{
return {};
}
auto final_suspend() noexcept -> __final_awaitable
{
return {};
}
void return_void() noexcept {}
[[noreturn]]
void unhandled_exception() noexcept
{
std::terminate();
}
auto unhandled_stopped() noexcept -> __std::coroutine_handle<__promise>
{
__is_stopped_ = true;
return __std::coroutine_handle<__promise>::from_promise(*this);
}
auto get_return_object() noexcept -> __task
{
return __task(__std::coroutine_handle<__promise>::from_promise(*this));
}
auto await_transform(__get_disposition __awaitable) noexcept -> __get_disposition
{
return __awaitable;
}
template <class _Awaitable>
auto await_transform(_Awaitable&& __awaitable) noexcept -> decltype(auto)
{
return as_awaitable(__at_coro_exit::__die_on_stop(static_cast<_Awaitable&&>(__awaitable)),
*this);
}
auto get_env() const noexcept -> __env
{
return {*this};
}
bool __is_stopped_{false};
std::tuple<_Ts&...> __args_{};
using __get_disposition_callback_t = task_disposition (*)(void*) noexcept;
__std::coroutine_handle<> __parent_{};
__get_disposition_callback_t __get_disposition_callback_{nullptr};
__any_scheduler __scheduler_{STDEXEC::inline_scheduler{}};
};
__std::coroutine_handle<__promise> __coro_;
};
template <task_disposition _OnCompletion>
class __on_disp
{
private:
template <class _Action, class... _Ts>
static auto __impl(_Action __action, _Ts... __ts) -> __task<_Ts...>
{
#if STDEXEC_EDG()
// This works around an EDG bug where the compiler misinterprets __get_disposition:
// operand to this co_await expression resolves to non-class "<unnamed>"
using __get_disposition =
std::enable_if_t<sizeof(_Action) != 0, __on_coro_disp::__get_disposition>;
#endif
task_disposition __d = co_await __get_disposition();
if (__d == _OnCompletion)
{
co_await static_cast<_Action&&>(__action)(static_cast<_Ts&&>(__ts)...);
}
}
public:
template <class _Action, class... _Ts>
requires __callable<__decay_t<_Action>, __decay_t<_Ts>...>
auto operator()(_Action&& __action, _Ts&&... __ts) const -> __task<_Ts...>
{
return __impl(static_cast<_Action&&>(__action), static_cast<_Ts&&>(__ts)...);
}
};
struct __succeeded_t : __on_disp<task_disposition::succeeded>
{};
struct __stopped_t : __on_disp<task_disposition::stopped>
{};
struct __failed_t : __on_disp<task_disposition::failed>
{};
} // namespace __on_coro_disp
inline constexpr __on_coro_disp::__succeeded_t on_coroutine_succeeded{};
inline constexpr __on_coro_disp::__stopped_t on_coroutine_stopped{};
inline constexpr __on_coro_disp::__failed_t on_coroutine_failed{};
} // namespace experimental::execution
namespace exec = experimental::execution;