-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.hpp
405 lines (327 loc) · 10.2 KB
/
minio.hpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#pragma once
#include <coroutine>
#include <utility>
#include <exception>
#include <vector>
#include <cstdint>
#include <cassert>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/epoll.h>
using std::coroutine_handle;
using std::suspend_never;
using std::suspend_always;
using std::noop_coroutine;
using std::uint32_t;
using std::vector;
#define MINIO_NS_BEG namespace minio {
#define MINIO_NS_END }
MINIO_NS_BEG
namespace utils {
int dup_fd(int fd) noexcept {
return dup(fd);
// fd flags are not derived
}
int set_non_blocking(int fd) noexcept {
int old_opt = fcntl(fd, F_GETFL);
int new_opt = old_opt | O_NONBLOCK;
fcntl(fd, F_SETFL, new_opt);
return old_opt;
}
int set_reuse_addr(int fd) noexcept {
int opt = 1;
return setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)
);
}
}
namespace coro {
namespace detail {
namespace promise {
enum class State {
Pending = 0,
Ready,
Fail,
Detach,
};
template<typename T>
struct PromiseBase {
T value;
State state = State::Pending;
// co_return value
void return_value(T&& value) && noexcept {
this->value = std::move(value);
this->state = State::Ready;
}
void return_value(const T& value) & noexcept {
this->value = value;
this->state = State::Ready;
}
// must within the coroutine's lifetime
// the coroutine should be suspended at final_suspend
T& get_result() & noexcept {
return value;
}
T&& get_result() && noexcept {
return std::move(value);
}
};
template<>
struct PromiseBase<void> {
State state = State::Pending;
void return_void() noexcept {
this->state = State::Ready;
}
void get_result() noexcept {}
};
template<typename Promise>
struct FinalAwaiterBase {
// alwyas suspend
// manually delete coroutine frame if detached
constexpr bool await_ready() const noexcept { return false; }
// nothing todo, the promise is fulfilled
void await_resume() const noexcept {};
coroutine_handle<>
await_suspend(coroutine_handle<Promise> handle) noexcept {
auto& promise = handle.promise();
auto super_handle = promise.super_handle;
auto state = promise.state;
// go back to outer coroutine
if (state != State::Detach) {
return super_handle ? super_handle : noop_coroutine();
}
// detached
handle.destroy();
return noop_coroutine();
}
};
}
namespace task_awaiter {
template<typename Promise>
struct Base {
coroutine_handle<Promise> this_handle;
constexpr bool await_ready() const noexcept { return false; }
// save outer coroutine handler
// and execute inner coroutine
coroutine_handle<Promise>
await_suspend(coroutine_handle<> super_handle) noexcept {
this_handle.promise().super_handle = super_handle;
return this_handle;
}
};
// return value of co_return
template<typename T, typename Promise>
struct AwaiterBase: Base<Promise> {
T&& await_resume() noexcept {
return std::move(
this->this_handle.promise().get_result()
);
}
};
template<typename Promise>
struct AwaiterBase<void, Promise>: Base<Promise> {
void await_resume() noexcept {}
};
}
}
// affect initial_suspend
enum class Mode {
LAZY = 0,
EAGER = 1
};
template<typename T, Mode mode = Mode::LAZY>
struct Task {
struct Promise;
using promise_type = Promise;
coroutine_handle<promise_type> this_handle;
struct Promise: detail::promise::PromiseBase<T> {
using State = detail::promise::State;
using FinalAwaiter = detail::promise::FinalAwaiterBase<promise_type>;
coroutine_handle<> super_handle;
// from PromiseBase<T>
// State state
// T value
Task get_return_object() noexcept {
return { coroutine_handle<promise_type>::from_promise(*this) };
}
void unhandled_exception() noexcept {
this->state = State::Fail;
// R.I.P
std::terminate();
}
// this is a lazy coroutine by default!!
constexpr std::conditional_t<
mode==Mode::LAZY,
suspend_always,
suspend_never>
initial_suspend() const noexcept { return {}; }
FinalAwaiter final_suspend() const noexcept { return {}; }
};
// make the task itself awaitable, aka nested task
using Awaiter = detail::task_awaiter::AwaiterBase<T, promise_type>;
Awaiter operator co_await() noexcept {
return { this_handle };
}
// may cause memory leak if the coroutine is not resumed
void detach() noexcept {
using State = detail::promise::State;
if (this_handle == nullptr) return;
this_handle.promise().state = State::Detach;
this_handle = nullptr;
}
template<typename U>
friend void spawn(Task<U>&& task) noexcept ;
Task(coroutine_handle<promise_type> h = nullptr)noexcept: this_handle(h) {};
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task& operator=(Task&& rhs) = delete;
Task(Task&& rhs) noexcept: this_handle(rhs.this_handle) {
rhs.this_handle = nullptr;
}
~Task() noexcept {
// detached or moved
if (!this_handle) return;
// release coroutine frame
this_handle.destroy();
}
};
template<typename T>
void spawn(Task<T>&& task) noexcept {
auto handle = task.this_handle;
task.detach();
handle.resume();
}
auto this_coro() noexcept {
struct Awaiter {
coroutine_handle<> this_handle;
constexpr bool await_ready() { return false; }
constexpr bool await_suspend(coroutine_handle<> h) {
this_handle = h;
// resume immediately
return false;
}
coroutine_handle<> await_resume() {
return this_handle;
}
};
return Awaiter{};
}
}
namespace epoll {
struct Poller {
const int epfd;
int count = 0;
Poller() noexcept: epfd(epoll_create1(0)) {}
Poller(const Poller&) = delete;
Poller& operator=(const Poller&) = delete;
Poller(Poller&&) = delete;
Poller& operator=(Poller&&) = delete;
~Poller() noexcept {
close(epfd);
}
void add(int fd, uint32_t ev, void *ptr) noexcept {
epoll_event ex {.events = ev, .data{.ptr = ptr}};
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ex) == 0) {
++count;
}
}
void remove(int fd) noexcept {
if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, nullptr) == 0) {
--count;
}
}
void start_loop() noexcept {
vector<epoll_event> events{};
events.reserve(1024);
while(count > 0) {
events.resize(count);
auto evdata = events.data();
int nfds = epoll_wait(epfd, evdata, count, -1);
for(int i = 0; i < nfds; ++i) {
auto ev = evdata[i];
auto handle = coroutine_handle<>::from_address(ev.data.ptr);
handle.resume();
}
}
}
static Poller& instance() {
static Poller instance{};
return instance;
}
};
auto ready(int fd, uint32_t ev) noexcept {
struct Awaiter {
int fd;
uint32_t ev;
constexpr bool await_ready() noexcept { return false; }
// register event
void await_suspend(coroutine_handle<> h) noexcept {
auto& poller = Poller::instance();
poller.add(fd, ev, h.address());
}
// remove event
void await_resume() noexcept {
auto& poller = Poller::instance();
poller.remove(fd);
if (ev & EPOLLOUT) {
close(fd);
}
}
};
// to avoid conflict
if (ev & EPOLLIN) {
return Awaiter{fd, ev};
} else {
int fd2 = utils::dup_fd(fd);
return Awaiter{fd2, ev};
}
}
enum class Mode {
LT = 0,
ET = 1
};
template<Mode mode = Mode::LT>
auto readable(int fd) noexcept {
if constexpr (mode == Mode::ET) {
return ready(fd, EPOLLIN|EPOLLET);
} else {
return ready(fd, EPOLLIN);
}
}
template<Mode mode = Mode::LT>
auto writable(int fd) noexcept {
if constexpr (mode == Mode::ET) {
return ready(fd, EPOLLOUT|EPOLLET);
} else {
return ready(fd, EPOLLOUT);
}
}
using coro::Task;
template<typename T>
T block_on(Task<T>&& task) noexcept {
// take ownership
Task<T> xtask = std::move(task);
auto handle = xtask.this_handle;
auto& poller = Poller::instance();
handle.resume();
poller.start_loop();
return handle.promise().get_result();
// dropped
}
template<>
void block_on(Task<void>&& task) noexcept {
Task<void> xtask = std::move(task);
auto handle = xtask.this_handle;
auto& poller = Poller::instance();
handle.resume();
poller.start_loop();
}
}
using coro::Task;
using coro::spawn;
using coro::this_coro;
using epoll::readable;
using epoll::writable;
using epoll::block_on;
MINIO_NS_END