-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaskruntime4.1.h
171 lines (123 loc) · 5.29 KB
/
taskruntime4.1.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
#include <experimental/coroutine>
#include "./../src/silk_pool.h"
#include <sys/types.h>
#include <sys/event.h>
namespace silk {
namespace demo_runtime_4_1 {
template<typename T> struct task_promise;
template<typename T> struct task;
struct independed_task;
struct task_promise_base {
std::experimental::coroutine_handle<> continuation;
};
struct final_awaitable {
bool await_ready() const noexcept { return false; }
template<typename T> void await_suspend(std::experimental::coroutine_handle<T> coro) {
coro.promise().continuation.resume();
}
void await_resume() noexcept {}
};
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) {
silk::spawn(silk::current_worker_id, (silk::task*) new frame(coro));
}
template<typename T = void> struct task_awaitable {
task<T>& awaitable;
bool await_ready() noexcept { return awaitable.coro.done(); }
void await_suspend(std::experimental::coroutine_handle<> coro) noexcept {
awaitable.coro.promise().continuation = coro;
awaitable.coro.resume();
}
auto await_resume() noexcept { 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.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) };
}
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{};
}
}
}