-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstep4.cpp
178 lines (140 loc) · 4.01 KB
/
step4.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
#include <chrono>
#include <coroutine>
#include "debug.hpp"
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>
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);
}
std::coroutine_handle<promise_type> mCoroutine;
};
Task<std::string> baby() {
debug(), "baby";
co_return "aaa\n";
}
Task<double> world() {
debug(), "world";
co_return 3.14;
}
Task<int> hello() {
auto ret = co_await baby();
debug(), ret;
int i = (int)co_await world();
debug(), "hello得到world结果为", i;
co_return i + 1;
}
int main() {
debug(), "main即将调用hello";
auto t = hello();
debug(), "main调用完了hello"; // 其实只创建了task对象,并没有真正开始执行
while (!t.mCoroutine.done()) {
t.mCoroutine.resume();
debug(), "main得到hello结果为",
t.mCoroutine.promise().result();
}
return 0;
}