-
Notifications
You must be signed in to change notification settings - Fork 0
/
coro.cc
200 lines (153 loc) · 3.33 KB
/
coro.cc
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
#include <cstdio>
#include <cstdlib>
#include <experimental/coroutine>
#include <variant>
namespace stdcoro = std::experimental;
template <class T>
struct async_task;
template <class T>
struct async_promise__base_returning {
void return_value(T&&) {}
};
template <>
struct async_promise__base_returning<void> {
void return_void() {}
};
template <class T>
struct async_promise : async_promise__base_returning<T> {
async_task<T> get_return_object();
constexpr stdcoro::suspend_always initial_suspend()
{
return {};
}
constexpr stdcoro::suspend_never final_suspend()
{
return {};
}
void unhandled_exception()
{
abort();
}
};
template <class T>
struct async_task {
using promise_type = async_promise<T>;
friend struct async_promise<T>;
private:
using handle_type = stdcoro::coroutine_handle<promise_type>;
handle_type h;
explicit async_task(handle_type h_)
: h(h_)
{}
public:
friend void start(async_task task)
{
task.h.resume();
}
};
template <class T>
async_task<T> async_promise<T>::get_return_object()
{
return async_task<T>(
stdcoro::coroutine_handle<async_promise>
::from_promise(*this));
}
template <class T>
struct async_value;
template <class T>
struct async_value_producer {
private:
struct empty_t {};
using state_t = std::variant<
empty_t,
T,
stdcoro::coroutine_handle<>>;
enum {
empty,
resolved,
awaiting
};
state_t state;
friend struct async_value<T>;
public:
async_value_producer() = default;
async_value_producer(async_value_producer const&) = delete;
async_value_producer& operator=(async_value_producer const&) = delete;
async_value_producer(async_value_producer&&) = delete;
async_value_producer& operator=(async_value_producer&&) = delete;
async_value<T> value();
template <class... Arg>
void emplace_value(Arg&&... arg)
{
stdcoro::coroutine_handle<> coro;
switch (state.index()) {
case awaiting:
coro = std::move(std::get<awaiting>(state));
// fallthrough
case empty:
state.template emplace<resolved>((Arg&&)arg...);
break;
case resolved:
return;
}
if (coro)
coro.resume();
}
};
template <class T>
struct async_value {
private:
using P = async_value_producer<T>;
friend P;
typename P::state_t* p_state;
explicit async_value(typename P::state_t& st)
: p_state(&st)
{}
public:
friend auto operator co_await(async_value av)
{
struct waiter {
typename P::state_t* p_state;
bool await_ready()
{
return p_state->index() == P::resolved;
}
bool await_suspend(stdcoro::coroutine_handle<> h)
{
switch (p_state->index()) {
case P::empty:
p_state->template emplace<P::awaiting>(h);
return true;
case P::resolved:
return false;
default:
abort();
}
}
T await_resume()
{
return (T&&)std::get<P::resolved>(*p_state);
}
};
return waiter { av.p_state };
}
};
template <class T>
async_value<T> async_value_producer<T>::value()
{
return async_value<T>(state);
}
async_task<void>
f(async_value<int> val)
{
auto x = co_await val;
printf("%d\n", x);
}
int main()
{
async_value_producer<int> p;
start(f(p.value()));
puts("started");
p.emplace_value(42);
puts("sent");
}