-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstep5.cpp
257 lines (203 loc) · 6.21 KB
/
step5.cpp
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
#include <chrono>
#include <coroutine>
#include <deque>
#include <queue>
#include <thread>
#include "debug.hpp"
using namespace std::chrono_literals;
struct RepeatAwaiter {
bool await_ready() const noexcept { return false; }
std::coroutine_handle<> await_suspend(std::coroutine_handle<> coroutine) const noexcept {
if (coroutine.done())
return std::noop_coroutine();
else
return coroutine;
}
void await_resume() const noexcept {}
};
struct PreviousAwaiter {
std::coroutine_handle<> mPrevious;
bool await_ready() const noexcept { return false; }
std::coroutine_handle<> await_suspend(std::coroutine_handle<> coroutine) const noexcept {
if (mPrevious)
return mPrevious;
else
return std::noop_coroutine();
}
void await_resume() const noexcept {}
};
template <class T>
struct Promise {
auto initial_suspend() noexcept {
return std::suspend_always();
}
auto final_suspend() noexcept {
return PreviousAwaiter(mPrevious);
}
void unhandled_exception() noexcept {
mException = std::current_exception();
}
auto yield_value(T ret) noexcept {
new (&mResult) T(std::move(ret));
return std::suspend_always();
}
void return_value(T ret) noexcept {
new (&mResult) T(std::move(ret));
}
T result() {
if (mException) [[unlikely]] {
std::rethrow_exception(mException);
}
T ret = std::move(mResult);
mResult.~T();
return ret;
}
std::coroutine_handle<Promise> get_return_object() {
return std::coroutine_handle<Promise>::from_promise(*this);
}
std::coroutine_handle<> mPrevious{};
std::exception_ptr mException{};
union {
T mResult;
};
Promise() noexcept {}
Promise(Promise &&) = delete;
~Promise() {}
};
template <>
struct Promise<void> {
auto initial_suspend() noexcept {
return std::suspend_always();
}
auto final_suspend() noexcept {
return PreviousAwaiter(mPrevious);
}
void unhandled_exception() noexcept {
mException = std::current_exception();
}
void return_void() noexcept {
}
void result() {
if (mException) [[unlikely]] {
std::rethrow_exception(mException);
}
}
std::coroutine_handle<Promise> get_return_object() {
return std::coroutine_handle<Promise>::from_promise(*this);
}
std::coroutine_handle<> mPrevious{};
std::exception_ptr mException{};
Promise() = default;
Promise(Promise &&) = delete;
~Promise() = default;
};
template <class T = void>
struct Task {
using promise_type = Promise<T>;
Task(std::coroutine_handle<promise_type> coroutine) noexcept
: mCoroutine(coroutine) {}
Task(Task &&) = delete;
~Task() {
mCoroutine.destroy();
}
struct Awaiter {
bool await_ready() const noexcept { return false; }
std::coroutine_handle<promise_type> await_suspend(std::coroutine_handle<> coroutine) const noexcept {
mCoroutine.promise().mPrevious = coroutine;
return mCoroutine;
}
T await_resume() const {
return mCoroutine.promise().result();
}
std::coroutine_handle<promise_type> mCoroutine;
};
auto operator co_await() const noexcept {
return Awaiter(mCoroutine);
}
operator std::coroutine_handle<>() const noexcept {
return mCoroutine;
}
std::coroutine_handle<promise_type> mCoroutine;
};
struct Loop {
std::deque<std::coroutine_handle<>> mReadyQueue;
struct TimerEntry {
std::chrono::system_clock::time_point expireTime;
std::coroutine_handle<> coroutine;
bool operator<(TimerEntry const &that) const noexcept {
return expireTime > that.expireTime;
}
};
std::priority_queue<TimerEntry> mTimerHeap;
void addTask(std::coroutine_handle<> coroutine) {
mReadyQueue.push_front(coroutine);
}
void addTimer(std::chrono::system_clock::time_point expireTime, std::coroutine_handle<> coroutine) {
mTimerHeap.push({expireTime, coroutine});
}
void runAll() {
while (!mTimerHeap.empty() || !mReadyQueue.empty()) {
while (!mReadyQueue.empty()) {
auto coroutine = mReadyQueue.front();
mReadyQueue.pop_front();
coroutine.resume();
}
if (!mTimerHeap.empty()) {
auto nowTime = std::chrono::system_clock::now();
auto timer = std::move(mTimerHeap.top());
if (timer.expireTime < nowTime) {
mTimerHeap.pop();
timer.coroutine.resume();
} else {
std::this_thread::sleep_until(timer.expireTime);
}
}
}
}
Loop &operator=(Loop &&) = delete;
};
Loop &getLoop() {
static Loop loop;
return loop;
}
struct SleepAwaiter {
bool await_ready() const noexcept {
return false;
}
void await_suspend(std::coroutine_handle<> coroutine) const {
getLoop().addTimer(mExpireTime, coroutine);
}
void await_resume() const noexcept {
}
std::chrono::system_clock::time_point mExpireTime;
};
Task<void> sleep_until(std::chrono::system_clock::time_point expireTime) {
co_await SleepAwaiter(expireTime);
co_return;
}
Task<void> sleep_for(std::chrono::system_clock::duration duration) {
co_await SleepAwaiter(std::chrono::system_clock::now() + duration);
co_return;
}
Task<int> hello1() {
debug(), "hello1开始睡1秒";
co_await sleep_for(1s); // 1s 等价于 std::chrono::seconds(1)
debug(), "hello1睡醒了";
co_return 1;
}
Task<int> hello2() {
debug(), "hello2开始睡2秒";
co_await sleep_for(2s); // 2s 等价于 std::chrono::seconds(2)
debug(), "hello2睡醒了";
co_return 2;
}
int main() {
auto t1 = hello1();
auto t2 = hello2();
getLoop().addTask(t1);
getLoop().addTask(t2);
getLoop().runAll();
debug(), "主函数中得到hello1结果:", t1.mCoroutine.promise().result();
debug(), "主函数中得到hello2结果:", t2.mCoroutine.promise().result();
return 0;
}