-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcoroutine.c
289 lines (251 loc) · 7.79 KB
/
coroutine.c
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <poll.h>
#include <unistd.h>
#include <sys/mman.h>
#include "coroutine.h"
// TODO: make the STACK_CAPACITY customizable by the user
//#define STACK_CAPACITY (4*1024)
#define STACK_CAPACITY (1024*getpagesize())
// Initial capacity of a dynamic array
#ifndef DA_INIT_CAP
#define DA_INIT_CAP 256
#endif
// Append an item to a dynamic array
#define da_append(da, item) \
do { \
if ((da)->count >= (da)->capacity) { \
(da)->capacity = (da)->capacity == 0 ? DA_INIT_CAP : (da)->capacity*2; \
(da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \
assert((da)->items != NULL && "Buy more RAM lol"); \
} \
\
(da)->items[(da)->count++] = (item); \
} while (0)
#define da_remove_unordered(da, i) \
do { \
size_t j = (i); \
assert(j < (da)->count); \
(da)->items[j] = (da)->items[--(da)->count]; \
} while(0)
#define UNUSED(x) (void)(x)
#define TODO(message) do { fprintf(stderr, "%s:%d: TODO: %s\n", __FILE__, __LINE__, message); abort(); } while(0)
#define UNREACHABLE(message) do { fprintf(stderr, "%s:%d: UNREACHABLE: %s\n", __FILE__, __LINE__, message); abort(); } while(0)
typedef struct {
void *rsp;
void *stack_base;
} Context;
typedef struct {
Context *items;
size_t count;
size_t capacity;
} Contexts;
typedef struct {
size_t *items;
size_t count;
size_t capacity;
} Indices;
typedef struct {
struct pollfd *items;
size_t count;
size_t capacity;
} Polls;
// TODO: coroutines library probably does not work well in multithreaded environment
static size_t current = 0;
static Indices active = {0};
static Indices dead = {0};
static Contexts contexts = {0};
static Indices asleep = {0};
static Polls polls = {0};
// TODO: ARM support
// Requires modifications in all the @arch places
typedef enum {
SM_NONE = 0,
SM_READ,
SM_WRITE,
} Sleep_Mode;
// Linux x86_64 call convention
// %rdi, %rsi, %rdx, %rcx, %r8, and %r9
void __attribute__((naked)) coroutine_yield(void)
{
// @arch
asm(
" pushq %rdi\n"
" pushq %rbp\n"
" pushq %rbx\n"
" pushq %r12\n"
" pushq %r13\n"
" pushq %r14\n"
" pushq %r15\n"
" movq %rsp, %rdi\n" // rsp
" movq $0, %rsi\n" // sm = SM_NONE
" jmp coroutine_switch_context\n");
}
void __attribute__((naked)) coroutine_sleep_read(int fd)
{
(void) fd;
// @arch
asm(
" pushq %rdi\n"
" pushq %rbp\n"
" pushq %rbx\n"
" pushq %r12\n"
" pushq %r13\n"
" pushq %r14\n"
" pushq %r15\n"
" movq %rdi, %rdx\n" // fd
" movq %rsp, %rdi\n" // rsp
" movq $1, %rsi\n" // sm = SM_READ
" jmp coroutine_switch_context\n");
}
void __attribute__((naked)) coroutine_sleep_write(int fd)
{
(void) fd;
// @arch
asm(
" pushq %rdi\n"
" pushq %rbp\n"
" pushq %rbx\n"
" pushq %r12\n"
" pushq %r13\n"
" pushq %r14\n"
" pushq %r15\n"
" movq %rdi, %rdx\n" // fd
" movq %rsp, %rdi\n" // rsp
" movq $2, %rsi\n" // sm = SM_WRITE
" jmp coroutine_switch_context\n");
}
void __attribute__((naked)) coroutine_restore_context(void *rsp)
{
// @arch
(void)rsp;
asm(
" movq %rdi, %rsp\n"
" popq %r15\n"
" popq %r14\n"
" popq %r13\n"
" popq %r12\n"
" popq %rbx\n"
" popq %rbp\n"
" popq %rdi\n"
" ret\n");
}
void coroutine_switch_context(void *rsp, Sleep_Mode sm, int fd)
{
contexts.items[active.items[current]].rsp = rsp;
switch (sm) {
case SM_NONE: current += 1; break;
case SM_READ: {
da_append(&asleep, active.items[current]);
struct pollfd pfd = {.fd = fd, .events = POLLRDNORM,};
da_append(&polls, pfd);
da_remove_unordered(&active, current);
} break;
case SM_WRITE: {
da_append(&asleep, active.items[current]);
struct pollfd pfd = {.fd = fd, .events = POLLWRNORM,};
da_append(&polls, pfd);
da_remove_unordered(&active, current);
} break;
default: UNREACHABLE("coroutine_switch_context");
}
if (polls.count > 0) {
int timeout = active.count == 0 ? -1 : 0;
int result = poll(polls.items, polls.count, timeout);
if (result < 0) TODO("poll");
for (size_t i = 0; i < polls.count;) {
if (polls.items[i].revents) {
size_t id = asleep.items[i];
da_remove_unordered(&polls, i);
da_remove_unordered(&asleep, i);
da_append(&active, id);
} else {
++i;
}
}
}
assert(active.count > 0);
current %= active.count;
coroutine_restore_context(contexts.items[active.items[current]].rsp);
}
// TODO: think how to get rid of coroutine_init() call at all
void coroutine_init(void)
{
if (contexts.count != 0) return;
da_append(&contexts, (Context){0});
da_append(&active, 0);
}
void coroutine__finish_current(void)
{
if (active.items[current] == 0) {
UNREACHABLE("Main Coroutine with id == 0 should never reach this place");
}
da_append(&dead, active.items[current]);
da_remove_unordered(&active, current);
if (polls.count > 0) {
int timeout = active.count == 0 ? -1 : 0;
int result = poll(polls.items, polls.count, timeout);
if (result < 0) TODO("poll");
for (size_t i = 0; i < polls.count;) {
if (polls.items[i].revents) {
size_t id = asleep.items[i];
da_remove_unordered(&polls, i);
da_remove_unordered(&asleep, i);
da_append(&active, id);
} else {
++i;
}
}
}
assert(active.count > 0);
current %= active.count;
coroutine_restore_context(contexts.items[active.items[current]].rsp);
}
void coroutine_go(void (*f)(void*), void *arg)
{
size_t id;
if (dead.count > 0) {
id = dead.items[--dead.count];
} else {
da_append(&contexts, ((Context){0}));
id = contexts.count-1;
contexts.items[id].stack_base = mmap(NULL, STACK_CAPACITY, PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_STACK|MAP_ANONYMOUS|MAP_GROWSDOWN, -1, 0);
assert(contexts.items[id].stack_base != MAP_FAILED);
}
void **rsp = (void**)((char*)contexts.items[id].stack_base + STACK_CAPACITY);
// @arch
*(--rsp) = coroutine__finish_current;
*(--rsp) = f;
*(--rsp) = arg; // push rdi
*(--rsp) = 0; // push rbx
*(--rsp) = 0; // push rbp
*(--rsp) = 0; // push r12
*(--rsp) = 0; // push r13
*(--rsp) = 0; // push r14
*(--rsp) = 0; // push r15
contexts.items[id].rsp = rsp;
da_append(&active, id);
}
size_t coroutine_id(void)
{
return active.items[current];
}
size_t coroutine_alive(void)
{
return active.count;
}
void coroutine_wake_up(size_t id)
{
// @speed coroutine_wake_up is linear
for (size_t i = 0; i < asleep.count; ++i) {
if (asleep.items[i] == id) {
da_remove_unordered(&asleep, id);
da_remove_unordered(&polls, id);
da_append(&active, id);
return;
}
}
}