-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstep2.cpp
153 lines (120 loc) · 3.51 KB
/
step2.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
#include <chrono>
#include <coroutine>
#include "debug.hpp"
struct RepeatAwaiter // awaiter(原始指针) / awaitable(operator->)
{
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 RepeatAwaitable // awaitable(operator->)
{
RepeatAwaiter operator co_await() {
return RepeatAwaiter();
}
};
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 {}
};
struct Promise {
auto initial_suspend() {
return std::suspend_always();
}
auto final_suspend() noexcept {
return PreviousAwaiter(mPrevious);
}
void unhandled_exception() {
throw;
}
auto yield_value(int ret) {
mRetValue = ret;
return std::suspend_always();
}
void return_void() {
mRetValue = 0;
}
std::coroutine_handle<Promise> get_return_object() {
return std::coroutine_handle<Promise>::from_promise(*this);
}
int mRetValue;
std::coroutine_handle<> mPrevious = nullptr;
};
struct Task {
using promise_type = Promise;
Task(std::coroutine_handle<promise_type> coroutine)
: mCoroutine(coroutine) {}
Task(Task &&) = delete;
~Task() {
mCoroutine.destroy();
}
std::coroutine_handle<promise_type> mCoroutine;
};
struct WorldTask {
using promise_type = Promise;
WorldTask(std::coroutine_handle<promise_type> coroutine)
: mCoroutine(coroutine) {}
WorldTask(WorldTask &&) = delete;
~WorldTask() {
mCoroutine.destroy();
}
struct WorldAwaiter {
bool await_ready() const noexcept { return false; }
std::coroutine_handle<> await_suspend(std::coroutine_handle<> coroutine) const noexcept {
mCoroutine.promise().mPrevious = coroutine;
return mCoroutine;
}
void await_resume() const noexcept {}
std::coroutine_handle<promise_type> mCoroutine;
};
auto operator co_await() {
return WorldAwaiter(mCoroutine);
}
std::coroutine_handle<promise_type> mCoroutine;
};
WorldTask world() {
debug(), "world";
co_yield 422;
co_yield 444;
co_return;
}
Task hello() {
debug(), "hello 正在构建worldTask";
WorldTask worldTask = world();
debug(), "hello 构建完了worldTask,开始等待world";
co_await worldTask;
debug(), "hello得到world返回", worldTask.mCoroutine.promise().mRetValue;
co_await worldTask;
debug(), "hello得到world返回", worldTask.mCoroutine.promise().mRetValue;
debug(), "hello 42";
co_yield 42;
debug(), "hello 12";
co_yield 12;
debug(), "hello 6";
co_yield 6;
debug(), "hello 结束";
co_return;
}
int main() {
debug(), "main即将调用hello";
Task t = hello();
debug(), "main调用完了hello"; // 其实只创建了task对象,并没有真正开始执行
while (!t.mCoroutine.done()) {
t.mCoroutine.resume();
debug(), "main得到hello结果为",
t.mCoroutine.promise().mRetValue;
}
return 0;
}