forked from pcsx-redux/nugget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoroutine.hh
211 lines (188 loc) · 7.93 KB
/
coroutine.hh
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
/*
MIT License
Copyright (c) 2022 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <coroutine>
#include <type_traits>
#include "common/syscalls/syscalls.h"
namespace psyqo {
/**
* @brief A suitable type to hold and return a C++20 coroutine.
*
* @details C++20 introduced the concept of coroutines in the language. This
* type can be used to properly hold a coroutine and yield and resume it
* within psyqo. An important caveat of using coroutines is that the language
* insist on calling `new` and `delete` silently within the coroutine object.
* This may be a problem for users who don't want to use the heap.
*
* @tparam T The type the coroutine returns. `void` by default.
*/
template <typename T = void>
struct Coroutine {
struct Empty {};
typedef typename std::conditional<std::is_void<T>::value, Empty, T>::type SafeT;
Coroutine() = default;
Coroutine(Coroutine &&other) = default;
Coroutine &operator=(Coroutine &&other) = default;
Coroutine(Coroutine const &) = delete;
Coroutine &operator=(Coroutine const &) = delete;
/**
* @brief The awaiter type.
*
* @details The awaiter type is the type that is used to suspend the coroutine
* after scheduling an asychronous operation. The keyword `co_await` can be used
* on an instance of the object to suspend the current coroutine. Creating an
* instance of this object is done by calling `coroutine.awaiter()`.
*/
struct Awaiter {
Awaiter(Awaiter &&other) = default;
Awaiter &operator=(Awaiter &&other) = default;
Awaiter(Awaiter const &) = default;
Awaiter &operator=(Awaiter const &) = default;
constexpr bool await_ready() const noexcept {
bool ret = m_coroutine->m_earlyResume;
m_coroutine->m_earlyResume = false;
return ret;
}
constexpr void await_suspend(std::coroutine_handle<> h) { m_coroutine->m_suspended = true; }
constexpr void await_resume() const noexcept {}
private:
Awaiter(Coroutine *coroutine) : m_coroutine(coroutine) {}
Coroutine *m_coroutine;
friend struct Coroutine;
};
/**
* @brief Creates an `Awaiter` object.
*
* @details This method is used to create an instance of the `Awaiter` object.
* It's used to suspend the coroutine after scheduling an asynchronous operation.
*/
Awaiter awaiter() { return Awaiter(this); }
/**
* @brief Resumes the coroutine.
*
* @details This method resumes the coroutine. It's used to resume the coroutine
* after an asynchronous operation has completed. It is safe to call it from
* within the coroutine itself, meaning it is safe to call it from a callback
* which may execute in the same callstack as the coroutine. In this case, the
* next `co_yield` on the `Awaiter` object will be a no-op.
*/
void resume() {
if (!m_handle) return;
if (!m_suspended) {
m_earlyResume = true;
return;
}
m_suspended = false;
m_handle.resume();
}
/**
* @brief Returns the status of the coroutine.
*
* @details This method returns the status of the coroutine. It will return
* `true` if the coroutine is done executing, `false` otherwise. The typical
* usage of this method is to poll it from the scene loop. The first time it
* returns `true`, the coroutine will be destroyed. The next times, it will
* return `true` without doing anything, making the polling loop faster.
*/
bool done() {
if (!m_handle) return true;
bool isDone = m_handle.done();
if (isDone) {
if constexpr (!std::is_void<T>::value) {
m_value = eastl::move(m_handle.promise().m_value);
}
m_handle.destroy();
m_handle = nullptr;
}
return isDone;
}
/**
* @brief Returns the value returned by the coroutine.
*
* @details This method returns the value returned by the coroutine. It is
* only valid to call it after the coroutine has finished executing. The
* typical usage of this method is to call it after the `done` method
* returns `true`. The coroutine sets its return value using the `co_return`
* keyword. Since it is possible for the return type to be `void`, the
* return type of this method is `T` if `T` is not `void`, and `Empty` if
* `T` is `void`.
*/
const SafeT &value() const { return m_value; }
private:
struct PromiseVoid {
Coroutine<> get_return_object() {
return Coroutine<>{eastl::move(std::coroutine_handle<Promise>::from_promise(*this))};
}
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void unhandled_exception() {}
void return_void() {
auto awaitingCoroutine = m_awaitingCoroutine;
if (awaitingCoroutine) {
// This doesn't feel right, but I don't know how to do it otherwise,
// since the coroutine is a template and I can't forward the type.
__builtin_coro_resume(awaitingCoroutine);
}
}
[[no_unique_address]] Empty m_value;
void *m_awaitingCoroutine = nullptr;
};
struct PromiseValue {
Coroutine<T> get_return_object() {
return Coroutine{eastl::move(std::coroutine_handle<Promise>::from_promise(*this))};
}
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void unhandled_exception() {}
void return_value(T &&value) {
m_value = eastl::move(value);
auto awaitingCoroutine = m_awaitingCoroutine;
if (awaitingCoroutine) {
// This doesn't feel right, but I don't know how to do it otherwise,
// since the coroutine is a template and I can't forward the type.
__builtin_coro_resume(awaitingCoroutine);
}
}
T m_value;
void *m_awaitingCoroutine = nullptr;
};
typedef typename std::conditional<std::is_void<T>::value, PromiseVoid, PromiseValue>::type Promise;
Coroutine(std::coroutine_handle<Promise> &&handle) : m_handle(eastl::move(handle)) {}
std::coroutine_handle<Promise> m_handle;
[[no_unique_address]] SafeT m_value;
void *m_awaitingCoroutine = nullptr;
bool m_suspended = true;
bool m_earlyResume = false;
public:
using promise_type = Promise;
constexpr bool await_ready() { return m_handle.done(); }
template <typename U>
constexpr void await_suspend(std::coroutine_handle<U> h) {
auto &promise = m_handle.promise();
promise.m_awaitingCoroutine = h.address();
resume();
}
constexpr SafeT await_resume() {
SafeT value = eastl::move(m_handle.promise().m_value);
m_handle.destroy();
return value;
}
};
} // namespace psyqo