-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_loop.cppm
More file actions
333 lines (259 loc) · 8.85 KB
/
Copy pathrun_loop.cppm
File metadata and controls
333 lines (259 loc) · 8.85 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
export module mcpplibs.cmp:run_loop;
import std;
import :task;
namespace mcpplibs::cmp::detail {
struct RootCompletionBase {
bool completed_ { false };
std::exception_ptr exception_ {};
};
template<typename T>
struct RootCompletion final : RootCompletionBase {
std::optional<T> result_ {};
};
template<>
struct RootCompletion<void> final : RootCompletionBase {};
class RunLoopState final {
private:
std::mutex mutex_ {};
std::condition_variable condition_ {};
std::deque<std::coroutine_handle<>> ready_ {};
bool running_ { false };
public:
void begin_run() {
const std::lock_guard lock { mutex_ };
if (running_) {
throw std::logic_error { "run loop is already running" };
}
if (!ready_.empty()) {
throw std::logic_error { "run loop contains abandoned work" };
}
running_ = true;
}
void enqueue(std::coroutine_handle<> coroutine) {
if (!coroutine) {
throw std::invalid_argument { "cannot schedule an empty coroutine" };
}
{
const std::lock_guard lock { mutex_ };
if (!running_) {
throw std::logic_error { "scheduler has no active run" };
}
ready_.push_back(coroutine);
}
condition_.notify_one();
}
void complete(RootCompletionBase& completion) noexcept {
// 通过互斥量发布完成状态,以及此前写入的结果或异常
{
const std::lock_guard lock { mutex_ };
completion.completed_ = true;
}
condition_.notify_one();
}
void drive(RootCompletionBase& completion) {
while (true) {
std::coroutine_handle<> coroutine {};
{
std::unique_lock lock { mutex_ };
condition_.wait(lock, [&] {
return completion.completed_ || !ready_.empty();
});
if (completion.completed_) {
if (!ready_.empty()) {
throw std::logic_error {
"root task completed with outstanding work"
};
}
running_ = false;
return;
}
coroutine = ready_.front();
ready_.pop_front();
}
if (!coroutine || coroutine.done()) {
throw std::logic_error { "run loop contains a non-runnable coroutine" };
}
// 锁外恢复,协程再次入队时不会与 RunLoop 自锁
coroutine.resume();
}
}
void abort_run() noexcept {
const std::lock_guard lock { mutex_ };
ready_.clear();
running_ = false;
}
};
class RunGuard final {
private:
std::shared_ptr<RunLoopState> state_ {};
bool active_ { true };
public:
explicit RunGuard(std::shared_ptr<RunLoopState> state)
: state_ { std::move(state) } {
state_->begin_run();
}
RunGuard(const RunGuard&) = delete;
RunGuard& operator=(const RunGuard&) = delete;
RunGuard(RunGuard&&) = delete;
RunGuard& operator=(RunGuard&&) = delete;
~RunGuard() {
if (active_) {
state_->abort_run();
}
}
void release() noexcept {
active_ = false;
}
};
class RootOperation final {
public:
struct promise_type {
RunLoopState* state_ {};
RootCompletionBase* completion_ {};
[[nodiscard]] RootOperation 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;
}
void await_suspend(
std::coroutine_handle<promise_type> coroutine) const noexcept {
auto* const state = coroutine.promise().state_;
auto* const completion = coroutine.promise().completion_;
state->complete(*completion);
}
constexpr void await_resume() const noexcept {}
};
[[nodiscard]] constexpr FinalAwaiter final_suspend() const noexcept {
return {};
}
constexpr void return_void() const noexcept {}
void unhandled_exception() noexcept {
completion_->exception_ = std::current_exception();
}
};
private:
using Handle = std::coroutine_handle<promise_type>;
Handle coroutine_ {};
explicit RootOperation(Handle coroutine) noexcept
: coroutine_ { coroutine } {}
public:
RootOperation(const RootOperation&) = delete;
RootOperation& operator=(const RootOperation&) = delete;
RootOperation(RootOperation&& other) noexcept
: coroutine_ { std::exchange(other.coroutine_, {}) } {}
RootOperation& operator=(RootOperation&&) = delete;
~RootOperation() {
if (coroutine_) {
coroutine_.destroy();
}
}
void bind(RunLoopState& state, RootCompletionBase& completion) noexcept {
coroutine_.promise().state_ = &state;
coroutine_.promise().completion_ = &completion;
}
[[nodiscard]] std::coroutine_handle<> handle() const noexcept {
return coroutine_;
}
};
inline RootOperation RootOperation::promise_type::get_return_object() noexcept {
return RootOperation {
std::coroutine_handle<promise_type>::from_promise(*this)
};
}
template<typename T>
RootOperation make_root_operation(Task<T> task, RootCompletion<T>& completion) {
if constexpr (std::same_as<T, void>) {
co_await std::move(task);
} else {
completion.result_.emplace(co_await std::move(task));
}
}
} // namespace mcpplibs::cmp::detail
export namespace mcpplibs::cmp {
class RunLoop final {
public:
class Scheduler final {
private:
class ScheduleAwaiter final {
private:
std::weak_ptr<detail::RunLoopState> state_ {};
public:
explicit ScheduleAwaiter(
std::weak_ptr<detail::RunLoopState> state) noexcept
: state_ { std::move(state) } {}
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
void await_suspend(std::coroutine_handle<> continuation) {
// 入队后协程可能立即恢复,因此先把状态保存到当前线程的栈上。
const auto state = state_.lock();
if (!state) {
throw std::logic_error { "scheduler's run loop no longer exists" };
}
state->enqueue(continuation);
}
constexpr void await_resume() const noexcept {}
};
std::weak_ptr<detail::RunLoopState> state_ {};
explicit Scheduler(
const std::shared_ptr<detail::RunLoopState>& state) noexcept
: state_ { state } {}
friend class RunLoop;
public:
Scheduler() = delete;
Scheduler(const Scheduler&) = default;
Scheduler& operator=(const Scheduler&) = default;
Scheduler(Scheduler&&) noexcept = default;
Scheduler& operator=(Scheduler&&) noexcept = default;
~Scheduler() = default;
[[nodiscard]] auto schedule() const noexcept {
return ScheduleAwaiter { state_ };
}
friend bool operator==(
const Scheduler& left,
const Scheduler& right) noexcept {
return !left.state_.owner_before(right.state_) &&
!right.state_.owner_before(left.state_);
}
};
private:
std::shared_ptr<detail::RunLoopState> state_ {
std::make_shared<detail::RunLoopState>()
};
public:
RunLoop() = default;
RunLoop(const RunLoop&) = delete;
RunLoop& operator=(const RunLoop&) = delete;
RunLoop(RunLoop&&) = delete;
RunLoop& operator=(RunLoop&&) = delete;
~RunLoop() = default;
[[nodiscard]] Scheduler get_scheduler() const noexcept {
return Scheduler { state_ };
}
template<typename T>
T run(Task<T> task) {
detail::RootCompletion<T> completion {};
detail::RunGuard guard { state_ };
auto operation = detail::make_root_operation(
std::move(task),
completion);
operation.bind(*state_, completion);
state_->enqueue(operation.handle());
state_->drive(completion);
guard.release();
if (completion.exception_) {
std::rethrow_exception(completion.exception_);
}
if constexpr (!std::same_as<T, void>) {
if (!completion.result_) {
throw std::logic_error { "root task completed without a result" };
}
return std::move(*completion.result_);
}
}
};
} // namespace mcpplibs::cmp