-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaskruntime4.2.h
267 lines (196 loc) · 8.53 KB
/
taskruntime4.2.h
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
#include <experimental/coroutine>
#include "./../src/silk_pool.h"
#include <sys/types.h>
#include <sys/event.h>
#include <unistd.h>
#include <sys/socket.h>
#include <fcntl.h>
namespace silk {
namespace demo_runtime_4_2 {
template<typename T> struct task_promise;
template<typename T> struct task;
struct independed_task;
enum task_state { unspawned, spawned, awaitable, completed, destroyed };
struct task_promise_base {
std::atomic<task_state> state = task_state::unspawned;
std::experimental::coroutine_handle<> continuation;
};
struct frame : public silk::task {
std::experimental::coroutine_handle<> coro;
frame(std::experimental::coroutine_handle<> c) : coro(c) {}
};
void spawn(std::experimental::coroutine_handle<> coro) {
spawn(current_worker_id, (silk::task*) new frame(coro));
}
struct final_awaitable {
bool await_ready() const noexcept { return false; }
template<typename T> void await_suspend(std::experimental::coroutine_handle<T> coro) {
task_promise_base& p = coro.promise();
if (p.state.exchange(task_state::completed, std::memory_order_release) == task_state::awaitable) {
spawn(p.continuation);
}
}
void await_resume() noexcept {}
};
template<typename T = void> struct task_awaitable {
task<T>& awaitable;
bool await_ready() noexcept { return false; }
void await_suspend(std::experimental::coroutine_handle<> coro) noexcept {
task_promise_base& p = awaitable.coro.promise();
p.continuation = coro;
task_state s = p.state.exchange(task_state::awaitable, std::memory_order_release);
if (s == task_state::unspawned) {
spawn(awaitable.coro);
} else if (s == task_state::completed) {
spawn(coro);
}
}
auto await_resume() noexcept {
awaitable.coro.promise().state.store(task_state::destroyed, std::memory_order_release);
return awaitable.result();
}
};
template<typename T> struct task_promise : public task_promise_base {
std::exception_ptr e_;
T v_;
task<T> get_return_object() noexcept;
auto initial_suspend() { return std::experimental::suspend_always{}; }
auto final_suspend() { return final_awaitable{}; }
void unhandled_exception() { e_ = std::current_exception(); }
void return_value(const T v) { v_ = v; }
T result() {
if (e_) {
std::rethrow_exception(e_);
}
return v_;
}
};
template<> struct task_promise<void> : public task_promise_base {
task_promise() noexcept = default;
task<void> get_return_object() noexcept;
auto initial_suspend() { return std::experimental::suspend_always{}; }
auto final_suspend() { return final_awaitable{}; }
void return_void() noexcept {}
std::exception_ptr e_;
void unhandled_exception() { e_ = std::current_exception(); }
void result() {
if (e_) {
std::rethrow_exception(e_);
}
}
};
template<typename T = void> struct task {
using promise_type = task_promise<T>;
std::experimental::coroutine_handle<task_promise<T>> coro;
task(std::experimental::coroutine_handle<task_promise<T>> c) : coro(c) { }
~task() {
if (coro && coro.done() && coro.promise().state.load(std::memory_order_acquire) == task_state::destroyed) {
coro.destroy();
}
}
const T result() { return coro.promise().result(); }
task_awaitable<T> operator co_await() { return task_awaitable<T> {*this}; }
};
template<typename T> task<T> task_promise<T>::get_return_object() noexcept {
return task<T> { std::experimental::coroutine_handle<task_promise>::from_promise(*this) };
}
inline task<void> task_promise<void>::get_return_object() noexcept {
return task<void> { std::experimental::coroutine_handle<task_promise>::from_promise(*this) };
}
struct independed_task_promise {
independed_task get_return_object() noexcept;
auto initial_suspend() { return std::experimental::suspend_always{}; }
auto final_suspend() { return std::experimental::suspend_never{}; }
void return_void() noexcept {}
std::exception_ptr e_;
void unhandled_exception() { e_ = std::current_exception(); }
void result() {
if (e_) {
std::rethrow_exception(e_);
}
}
};
struct independed_task {
using promise_type = independed_task_promise;
std::experimental::coroutine_handle<> coro;
independed_task(std::experimental::coroutine_handle<> c) : coro(c) { }
};
inline independed_task independed_task_promise::get_return_object() noexcept {
return independed_task{ std::experimental::coroutine_handle<independed_task_promise>::from_promise(*this) };
}
template<typename T = void> task<T> spawn(task<T> c) {
task_promise_base& p = c.coro.promise();
p.state.store(task_state::spawned, std::memory_order_release);
spawn(c.coro);
return c;
}
void spawn(independed_task c) {
spawn(c.coro);
}
void schedule(silk::task* t) {
frame* c = (frame*)t;
c->coro.resume();
delete c;
}
struct yield_awaitable {
bool await_ready() const noexcept { return false; }
template<typename T> void await_suspend(std::experimental::coroutine_handle<T> c) {
spawn(c);
}
void await_resume() noexcept {}
};
auto yield() {
return yield_awaitable{};
}
int kq;
struct io_read_awaitable {
char* buf;
int nbytes;
int socket;
int n;
std::experimental::coroutine_handle<> coro;
constexpr bool await_ready() const noexcept { return false; }
void await_suspend(std::experimental::coroutine_handle<> c) {
coro = c;
struct kevent evSet;
EV_SET(&evSet, socket, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, this);
assert(-1 != kevent(kq, & evSet, 1, NULL, 0, NULL));
}
auto await_resume() { return n; }
};
struct io_accept_awaitable {
int listening_socket;
struct sockaddr_storage addr;
socklen_t socklen = sizeof(addr);
bool success;
int err;
int s;
bool await_ready() noexcept {
s = accept(listening_socket, (struct sockaddr *)&addr, &socklen);
success = !(s == -1 && errno == EAGAIN);
err = errno;
return success;
}
void await_suspend(std::experimental::coroutine_handle<> coro) {
struct kevent evSet;
EV_SET(&evSet, listening_socket, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, new frame(coro));
assert(-1 != kevent(kq, & evSet, 1, NULL, 0, NULL));
}
auto await_resume() {
if ( success ) {
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
return std::make_tuple(s, addr, err);
}
s = accept(listening_socket, (struct sockaddr *)&addr, &socklen);
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
return std::make_tuple(s, addr, errno);
}
};
auto read_async(const int socket, char* buf, const int nbytes) {
return io_read_awaitable {buf, nbytes, socket};
}
auto accept_async( const int listening_socket ) {
return io_accept_awaitable { listening_socket };
}
}
}