-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_group.cppm
More file actions
278 lines (212 loc) · 7.22 KB
/
Copy pathtask_group.cppm
File metadata and controls
278 lines (212 loc) · 7.22 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
export module mcpplibs.cmp:task_group;
import std;
import :task;
import :when_all;
export namespace mcpplibs::cmp {
class TaskGroup final {
private:
enum class State {
unused,
open,
joining,
joined
};
class ChildTask final {
public:
struct promise_type {
TaskGroup* group_ {};
std::exception_ptr exception_ {};
[[nodiscard]] ChildTask get_return_object() noexcept;
[[nodiscard]] constexpr std::suspend_always initial_suspend() const noexcept {
return {};
}
class FinalAwaiter final {
public:
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
[[nodiscard]] std::coroutine_handle<> await_suspend(
std::coroutine_handle<promise_type> coroutine) const noexcept;
constexpr void await_resume() const noexcept {}
};
[[nodiscard]] constexpr FinalAwaiter final_suspend() const noexcept {
return {};
}
constexpr void return_void() const noexcept {}
void unhandled_exception() noexcept {
exception_ = std::current_exception();
}
};
private:
using Handle = std::coroutine_handle<promise_type>;
Handle coroutine_ {};
explicit ChildTask(Handle coroutine) noexcept
: coroutine_ { coroutine } {}
public:
ChildTask(const ChildTask&) = delete;
ChildTask& operator=(const ChildTask&) = delete;
ChildTask(ChildTask&& other) noexcept
: coroutine_ { std::exchange(other.coroutine_, {}) } {}
ChildTask& operator=(ChildTask&&) = delete;
~ChildTask() {
if (coroutine_) {
coroutine_.destroy();
}
}
[[nodiscard]] std::coroutine_handle<> bind(TaskGroup& group) noexcept {
coroutine_.promise().group_ = &group;
return coroutine_;
}
[[nodiscard]] std::exception_ptr exception() const noexcept {
return coroutine_.promise().exception_;
}
};
class JoinAwaiter final {
private:
TaskGroup* group_ {};
public:
explicit JoinAwaiter(TaskGroup& group) noexcept
: group_ { &group } {}
JoinAwaiter(const JoinAwaiter&) = delete;
JoinAwaiter& operator=(const JoinAwaiter&) = delete;
JoinAwaiter(JoinAwaiter&&) = delete;
JoinAwaiter& operator=(JoinAwaiter&&) = delete;
[[nodiscard]] bool await_ready() {
return group_->begin_join_();
}
[[nodiscard]] bool await_suspend(
std::coroutine_handle<> continuation) {
// 发布后最后一个子任务可能立即恢复并销毁当前 awaiter
auto* const group = group_;
return group->suspend_join_(continuation);
}
void await_resume() {
group_->rethrow_first_exception_();
}
};
std::mutex mutex_ {};
std::stop_source stopSource_ {};
std::vector<ChildTask> tasks_ {};
State state_ { State::unused };
std::size_t active_ {};
std::coroutine_handle<> joinContinuation_ {};
[[nodiscard]] static ChildTask make_child_(Task<void> task);
[[nodiscard]] bool begin_join_();
[[nodiscard]] bool suspend_join_(std::coroutine_handle<> continuation);
[[nodiscard]] std::coroutine_handle<> notify_child_completed_() noexcept;
void rethrow_first_exception_();
public:
TaskGroup() = default;
TaskGroup(const TaskGroup&) = delete;
TaskGroup& operator=(const TaskGroup&) = delete;
TaskGroup(TaskGroup&&) = delete;
TaskGroup& operator=(TaskGroup&&) = delete;
~TaskGroup();
void spawn(Task<void> task);
[[nodiscard]] Task<void> join();
[[nodiscard]] Task<void> cancel_and_join();
[[nodiscard]] std::stop_token get_stop_token() const noexcept {
return stopSource_.get_token();
}
bool request_stop() noexcept {
return stopSource_.request_stop();
}
};
inline TaskGroup::ChildTask
TaskGroup::ChildTask::promise_type::get_return_object() noexcept {
return ChildTask {
std::coroutine_handle<promise_type>::from_promise(*this)
};
}
inline std::coroutine_handle<>
TaskGroup::ChildTask::promise_type::FinalAwaiter::await_suspend(
std::coroutine_handle<promise_type> coroutine) const noexcept {
// 用对称转移恢复 join,避免在当前帧之外再嵌套一层 resume 调用
auto* const group = coroutine.promise().group_;
return group->notify_child_completed_();
}
inline TaskGroup::ChildTask TaskGroup::make_child_(Task<void> task) {
co_await std::move(task);
}
inline TaskGroup::~TaskGroup() {
const std::lock_guard lock { mutex_ };
if (state_ != State::unused && state_ != State::joined) {
std::terminate();
}
}
inline void TaskGroup::spawn(Task<void> task) {
auto child = make_child_(std::move(task));
const auto coroutine = child.bind(*this);
{
const std::lock_guard lock { mutex_ };
if (state_ == State::joined) {
throw std::logic_error { "task group is closed" };
}
tasks_.push_back(std::move(child));
++active_;
if (state_ == State::unused) {
state_ = State::open;
}
}
// 锁外启动,立即完成的子任务会回调当前 group
detail::JoinStartQueue::Scope startScope {};
coroutine.resume();
}
inline bool TaskGroup::begin_join_() {
const std::lock_guard lock { mutex_ };
if (state_ == State::joining || state_ == State::joined) {
throw std::logic_error { "task group can only be joined once" };
}
if (active_ == 0) {
state_ = State::joined;
return true;
}
state_ = State::joining;
return false;
}
inline bool TaskGroup::suspend_join_(
std::coroutine_handle<> continuation) {
const std::lock_guard lock { mutex_ };
if (state_ == State::joined) {
return false;
}
joinContinuation_ = continuation;
return true;
}
inline std::coroutine_handle<> TaskGroup::notify_child_completed_() noexcept {
std::coroutine_handle<> continuation {};
{
const std::lock_guard lock { mutex_ };
if (active_ == 0) {
std::terminate();
}
--active_;
if (active_ == 0 && state_ == State::joining) {
state_ = State::joined;
continuation = std::exchange(joinContinuation_, {});
}
}
return continuation ? continuation : std::noop_coroutine();
}
inline void TaskGroup::rethrow_first_exception_() {
std::exception_ptr exception {};
for (const auto& task : tasks_) {
if (const auto childError = task.exception()) {
exception = childError;
break;
}
}
// 静止点之后没有接纳,保留首个异常即可释放全部终态包装帧
std::vector<ChildTask> {}.swap(tasks_);
if (exception) {
std::rethrow_exception(exception);
}
}
inline Task<void> TaskGroup::join() {
co_await JoinAwaiter { *this };
}
inline Task<void> TaskGroup::cancel_and_join() {
request_stop();
co_await join();
}
} // namespace mcpplibs::cmp