8 months ago |
4 |
1 |
#include "coroutine.h" | ||
2 |
#include <assert.h> | ||||
3 |
#include <setjmp.h> | ||||
4 |
#include <stdbool.h> | ||||
5 |
#include <stddef.h> | ||||
6 |
#include <stdio.h> | ||||
8 months ago |
7 |
#include <stdlib.h> | |||
7 months ago |
8 |
#include "cor_platform.h" | |||
8 months ago |
4 |
9 |
|||
10 |
|||||
8 months ago |
11 |
static void *mustmalloc(size_t size){ | |||
12 |
void *p = malloc(size); | ||||
13 |
assert(p); | ||||
14 |
return p; | ||||
15 |
} | ||||
16 |
|||||
17 |
#define New(type, ...) (type##_ctor((type *)mustmalloc(sizeof(type), ## __VA_ARGS__))) | ||||
18 |
#define Delete(ptr, type) ((ptr) ? (type##_dtor(ptr), free(ptr), (ptr) = NULL) : (void)0) | ||||
8 months ago |
19 |
static void Coroutine_RunNext(); | |||
7 months ago |
20 |
static void _Coroutine_Continue(Coroutine *cor, void *value, bool early); | |||
8 months ago |
21 |
||||
8 months ago |
4 |
22 |
/////////////////////////////////////////////////////////////////////////////// | ||
23 |
// 2-way linked lists... | ||||
24 |
// | ||||
8 months ago |
25 |
// Brought inline here to avoid namespace polution | |||
8 months ago |
4 |
26 |
/////////////////////////////////////////////////////////////////////////////// | ||
27 |
|||||
28 |
typedef struct List_Link List_Link; | ||||
29 |
struct List_Link { | ||||
30 |
List_Link *next; | ||||
31 |
List_Link *prev; | ||||
32 |
}; | ||||
33 |
|||||
34 |
typedef struct List_Head List_Head; | ||||
35 |
struct List_Head { | ||||
36 |
union { | ||||
37 |
struct { | ||||
38 |
List_Link link; | ||||
39 |
List_Link *filler; | ||||
40 |
} fwd; | ||||
41 |
struct { | ||||
42 |
List_Link *filler; | ||||
43 |
List_Link link; | ||||
44 |
} back; | ||||
45 |
}; | ||||
46 |
}; | ||||
47 |
|||||
8 months ago |
48 |
||||
49 |
static inline bool List_IsEmpty( | ||||
50 |
const List_Head *list | ||||
51 |
){ | ||||
8 months ago |
4 |
52 |
return list->fwd.link.next == &list->back.link; | ||
53 |
} | ||||
54 |
|||||
8 months ago |
55 |
||||
56 |
static inline List_Link *List_GetHead( | ||||
57 |
const List_Head *list | ||||
58 |
){ | ||||
8 months ago |
4 |
59 |
return List_IsEmpty(list) ? NULL : list->fwd.link.next; | ||
60 |
} | ||||
8 months ago |
61 |
||||
62 |
|||||
63 |
// static inline List_Link *List_GetTail( | ||||
64 |
// const List_Head *list | ||||
65 |
// ){ | ||||
8 months ago |
66 |
// return List_IsEmpty(list) ? NULL : list->back.link.prev; | |||
67 |
// } | ||||
8 months ago |
68 |
||||
69 |
|||||
8 months ago |
4 |
70 |
#define OFFSETOF(Container, Field) ((char *)&((Container *)4)->Field - (char *)(Container *)4) | ||
71 |
#define List_Link_Container(Container, Link, link) ((Container *)((char *)(link) - OFFSETOF(Container, Link))) | ||||
72 |
|||||
8 months ago |
73 |
||||
74 |
static inline void List_Init( | ||||
75 |
List_Head *list | ||||
76 |
){ | ||||
8 months ago |
4 |
77 |
list->fwd.link.next = &list->back.link; | ||
78 |
list->fwd.link.prev = NULL; | ||||
79 |
list->back.link.prev = &list->fwd.link; | ||||
80 |
} | ||||
81 |
|||||
8 months ago |
82 |
||||
83 |
static inline void List_AddHead( | ||||
84 |
List_Head *list, | ||||
85 |
List_Link *link | ||||
86 |
){ | ||||
8 months ago |
4 |
87 |
List_Link *first = list->fwd.link.next; | ||
88 |
link->next = first; | ||||
89 |
link->prev = &list->fwd.link; | ||||
90 |
first->prev = link; | ||||
91 |
list->fwd.link.next = link; | ||||
92 |
} | ||||
93 |
|||||
8 months ago |
94 |
||||
95 |
static inline void List_AddTail( | ||||
96 |
List_Head *list, | ||||
97 |
List_Link *link | ||||
98 |
){ | ||||
8 months ago |
4 |
99 |
List_Link *last = list->back.link.prev; | ||
100 |
link->prev = last; | ||||
101 |
link->next = &list->back.link; | ||||
102 |
last->next = link; | ||||
103 |
list->back.link.prev = link; | ||||
104 |
} | ||||
105 |
|||||
8 months ago |
106 |
||||
107 |
static inline void List_Remove( | ||||
108 |
List_Link *link | ||||
109 |
){ | ||||
8 months ago |
4 |
110 |
link->prev->next = link->next; | ||
111 |
link->next->prev = link->prev; | ||||
112 |
} | ||||
113 |
|||||
114 |
/////////////////////////////////////////////////////////////////////////////// | ||||
115 |
// ...2-way linked lists | ||||
116 |
/////////////////////////////////////////////////////////////////////////////// | ||||
117 |
|||||
8 months ago |
118 |
typedef struct Coroutines Coroutines; | |||
8 months ago |
4 |
119 |
|||
120 |
enum { | ||||
121 |
Coroutines_Idle, | ||||
122 |
Coroutines_Starting, | ||||
123 |
Coroutines_Started, | ||||
124 |
Coroutines_Active, | ||||
125 |
Coroutines_Stopping | ||||
126 |
}; | ||||
127 |
|||||
128 |
enum { | ||||
129 |
Chunk_Initial, | ||||
130 |
Chunk_Create, | ||||
131 |
Chunk_Enter | ||||
132 |
}; | ||||
133 |
|||||
8 months ago |
134 |
typedef enum Coroutine_State { | |||
8 months ago |
4 |
135 |
Coroutine_Constructing, | ||
136 |
Coroutine_Free, | ||||
137 |
Coroutine_Idle, | ||||
138 |
Coroutine_Running, | ||||
139 |
Coroutine_Waiting, | ||||
140 |
Coroutine_Complete | ||||
8 months ago |
141 |
} Coroutine_State; | |||
8 months ago |
4 |
142 |
|||
143 |
enum { | ||||
144 |
Coroutines_Init, | ||||
145 |
Coroutines_AllocatedChunk, | ||||
146 |
Coroutines_CoroutineComplete, | ||||
147 |
}; | ||||
148 |
|||||
149 |
struct Coroutine { | ||||
8 months ago |
150 |
Coroutines *coroutines; // so can work with it off-thread | |||
151 |
List_Link link; // for whichever list it's on | ||||
152 |
jmp_buf buf; // how to get back to it | ||||
153 |
unsigned char *guard; // where the stack overrun guard is | ||||
154 |
Coroutine_Start start; // entry point | ||||
155 |
void *entry_param; // to pass to start | ||||
156 |
void *value; // yielded/returned | ||||
157 |
Coroutine_State state; | ||||
8 months ago |
4 |
158 |
}; | ||
159 |
|||||
160 |
struct Coroutines { | ||||
7 months ago |
161 |
_Cor_Mutex mutex; | |||
8 months ago |
162 |
jmp_buf controller; // to return from Coroutine_Run | |||
163 |
jmp_buf chunk_allocated;// for chunk allocation | ||||
164 |
unsigned char *guard; // the stack guard for the startup sequence | ||||
8 months ago |
4 |
165 |
|||
166 |
// singletons | ||||
167 |
Coroutine *tip; // top of stack chunk | ||||
168 |
Coroutine *active; // currently running coroutine | ||||
169 |
Coroutine *primary; // Coroutine_Run coroutine | ||||
170 |
|||||
171 |
// lists | ||||
172 |
List_Head free; | ||||
173 |
List_Head inactive; // idle or complete | ||||
174 |
List_Head runable; // running or waiting to run | ||||
175 |
List_Head waiting; // yielded / waiting to run | ||||
7 months ago |
176 |
_Cor_Mutex waiting_mutex; | |||
8 months ago |
4 |
177 |
|||
8 months ago |
178 |
// Summary of the system | |||
179 |
Coroutine_Report report; | ||||
180 |
|||||
8 months ago |
4 |
181 |
// state | ||
182 |
char state; | ||||
183 |
}; | ||||
184 |
|||||
8 months ago |
185 |
_Cor_thread_local Coroutines *g_c; | |||
8 months ago |
4 |
186 |
|||
187 |
static void stack_chunk_chunk(Coroutine *parent); | ||||
8 months ago |
188 |
static void stack_chunk_base(Coroutine *parent, unsigned char *guard); | |||
8 months ago |
4 |
189 |
|||
8 months ago |
190 |
||||
8 months ago |
191 |
// Check whether the guard is intact | |||
192 |
static inline bool Check_Guard( | ||||
193 |
unsigned char *guard | ||||
194 |
){ | ||||
195 |
return guard[0] == 0xde && | ||||
196 |
guard[1] == 0xad && | ||||
197 |
guard[2] == 0xbe && | ||||
198 |
guard[3] == 0xef; | ||||
199 |
} | ||||
200 |
|||||
201 |
|||||
8 months ago |
4 |
202 |
static void Coroutine_PrimeStackChunks() | ||
203 |
{ | ||||
8 months ago |
204 |
unsigned char chunk_of_stack[COROUTINE_STARTUP_STACK_SIZE]; | |||
205 |
for (int i = 0; i < COROUTINE_STARTUP_STACK_SIZE-3; i += 4){ | ||||
206 |
chunk_of_stack[i+0] = 0xde; | ||||
207 |
chunk_of_stack[i+1] = 0xad; | ||||
208 |
chunk_of_stack[i+2] = 0xbe; | ||||
209 |
chunk_of_stack[i+3] = 0xef; | ||||
210 |
} | ||||
211 |
assert(Check_Guard(chunk_of_stack)); | ||||
212 |
|||||
213 |
// Stacks grow down in memory (almost always), so if the caller of this function changes | ||||
214 |
// the guard before entering the coroutine system, it has overrun the startup stack | ||||
215 |
g_c->guard = chunk_of_stack; | ||||
216 |
|||||
217 |
stack_chunk_base(NULL, NULL); | ||||
8 months ago |
4 |
218 |
} | ||
219 |
|||||
8 months ago |
220 |
||||
221 |
static void stack_chunk_chunk( | ||||
222 |
Coroutine *parent | ||||
223 |
){ | ||||
8 months ago |
4 |
224 |
unsigned char chunk_of_stack[COROUTINE_STACK_SIZE]; | ||
8 months ago |
225 |
for (int i = 0; i < COROUTINE_STACK_SIZE-3; i += 4){ | |||
226 |
chunk_of_stack[i+0] = 0xde; | ||||
227 |
chunk_of_stack[i+1] = 0xad; | ||||
228 |
chunk_of_stack[i+2] = 0xbe; | ||||
229 |
chunk_of_stack[i+3] = 0xef; | ||||
8 months ago |
230 |
} | |||
8 months ago |
231 |
stack_chunk_base(parent, chunk_of_stack); | |||
8 months ago |
4 |
232 |
} | ||
233 |
|||||
8 months ago |
234 |
||||
235 |
static void stack_chunk_base( | ||||
8 months ago |
236 |
Coroutine *parent, | |||
237 |
unsigned char *guard | ||||
8 months ago |
238 |
){ | |||
8 months ago |
4 |
239 |
Coroutine here; | ||
240 |
here.state = Coroutine_Constructing; | ||||
241 |
switch (setjmp(here.buf)) { | ||||
242 |
case Chunk_Initial: | ||||
243 |
// got here for the first time | ||||
244 |
// parent now has a chunk_of_stack - add it to the free list | ||||
245 |
if (parent) { | ||||
246 |
assert(parent->state == Coroutine_Constructing); | ||||
8 months ago |
247 |
assert(Check_Guard(guard)); | |||
248 |
parent->guard = guard; | ||||
8 months ago |
4 |
249 |
parent->state = Coroutine_Free; | ||
8 months ago |
250 |
List_AddHead(&g_c->free, &parent->link); | |||
8 months ago |
251 |
g_c->report.coroutines_pool_size += 1; | |||
8 months ago |
4 |
252 |
} | ||
253 |
// note that here is the tip of the chunk-claim stack | ||||
8 months ago |
254 |
here.coroutines = g_c; | |||
255 |
g_c->tip = &here; | ||||
8 months ago |
4 |
256 |
|||
257 |
// return to the coroutine allocator | ||||
8 months ago |
258 |
longjmp(g_c->chunk_allocated, 1); | |||
8 months ago |
4 |
259 |
case Chunk_Create: | ||
260 |
// request to create a new chunk on the stack | ||||
261 |
assert(here.state == Coroutine_Constructing); | ||||
262 |
stack_chunk_chunk(&here); | ||||
263 |
assert(false); | ||||
264 |
case Chunk_Enter: | ||||
265 |
// request to start a coroutine (ie use the chunk for a coroutine) | ||||
8 months ago |
266 |
// arrive here with mutex locked | |||
8 months ago |
4 |
267 |
assert(here.state == Coroutine_Running); | ||
8 months ago |
268 |
g_c->active = &here; | |||
7 months ago |
269 |
_Cor_Mutex_Unlock(&g_c->mutex); | |||
8 months ago |
4 |
270 |
here.value = here.start(here.entry_param); | ||
8 months ago |
271 |
||||
272 |
// check the guard | ||||
273 |
if (!Check_Guard(here.guard)){ | ||||
274 |
printf("Coroutine has overrun its stack - checked after returning from coroutine function\n"); | ||||
275 |
exit(EXIT_FAILURE); | ||||
276 |
} | ||||
277 |
|||||
7 months ago |
278 |
_Cor_Mutex_Lock(&g_c->mutex); | |||
8 months ago |
279 |
g_c->active = NULL; | |||
8 months ago |
4 |
280 |
assert(here.state == Coroutine_Running); | ||
281 |
List_Remove(&here.link); | ||||
282 |
here.state = Coroutine_Complete; | ||||
8 months ago |
283 |
List_AddTail(&g_c->inactive, &here.link); | |||
8 months ago |
4 |
284 |
// coroutine has completed | ||
8 months ago |
285 |
if (g_c->primary == &here) { | |||
8 months ago |
4 |
286 |
// if primary coroutine - return to Coroutine_Run | ||
8 months ago |
287 |
longjmp(g_c->controller, Coroutines_CoroutineComplete); | |||
8 months ago |
4 |
288 |
} | ||
7 months ago |
289 |
_Cor_Mutex_Unlock(&g_c->mutex); | |||
8 months ago |
4 |
290 |
Coroutine_RunNext(); | ||
291 |
assert(false); | ||||
292 |
} | ||||
293 |
} | ||||
294 |
|||||
8 months ago |
295 |
||||
8 months ago |
296 |
static void Coroutine_RunNext() | |||
297 |
{ | ||||
298 |
// arrive here with mutex unlocked | ||||
7 months ago |
299 |
_Cor_Mutex_Lock(&g_c->waiting_mutex); | |||
300 |
_Cor_Mutex_Lock(&g_c->mutex); | ||||
8 months ago |
301 |
Coroutine *next = List_Link_Container(Coroutine, link, List_GetHead(&g_c->runable)); | |||
302 |
assert(next->state == Coroutine_Running); | ||||
303 |
longjmp(next->buf, Chunk_Enter); | ||||
304 |
assert(false); | ||||
305 |
} | ||||
306 |
|||||
307 |
|||||
8 months ago |
4 |
308 |
void Coroutine_StartSystem() | ||
309 |
{ | ||||
8 months ago |
310 |
assert(!g_c); | |||
311 |
g_c = mustmalloc(sizeof(Coroutines)); | ||||
8 months ago |
312 |
||||
8 months ago |
313 |
g_c->state = Coroutines_Starting; | |||
314 |
|||||
7 months ago |
315 |
_Cor_Mutex_ctor(&g_c->mutex); | |||
8 months ago |
316 |
||||
8 months ago |
317 |
g_c->tip = NULL; | |||
318 |
g_c->active = NULL; | ||||
8 months ago |
4 |
319 |
|||
8 months ago |
320 |
List_Init(&g_c->free); | |||
321 |
List_Init(&g_c->inactive); | ||||
322 |
List_Init(&g_c->runable); | ||||
323 |
List_Init(&g_c->waiting); | ||||
7 months ago |
324 |
_Cor_Mutex_ctor(&g_c->waiting_mutex); | |||
325 |
_Cor_Mutex_Lock(&g_c->waiting_mutex); | ||||
8 months ago |
4 |
326 |
|||
8 months ago |
327 |
g_c->report.coroutines_created = 0; | |||
328 |
g_c->report.coroutines_pool_size = 0; | ||||
329 |
g_c->report.lowest_headroom = COROUTINE_STACK_SIZE; | ||||
330 |
|||||
8 months ago |
4 |
331 |
// prime the chunk system | ||
8 months ago |
332 |
if (!setjmp(g_c->chunk_allocated)){ | |||
8 months ago |
4 |
333 |
Coroutine_PrimeStackChunks(); | ||
334 |
assert(false); | ||||
335 |
} | ||||
8 months ago |
336 |
||||
337 |
assert(g_c->state == Coroutines_Starting); | ||||
338 |
g_c->state = Coroutines_Started; | ||||
8 months ago |
4 |
339 |
} | ||
340 |
|||||
8 months ago |
341 |
||||
8 months ago |
342 |
Coroutine_Report Coroutine_StopSystem() | |||
8 months ago |
4 |
343 |
{ | ||
7 months ago |
344 |
_Cor_Mutex_Lock(&g_c->mutex); | |||
8 months ago |
345 |
assert(g_c->state == Coroutines_Started); | |||
346 |
g_c->state = Coroutines_Stopping; | ||||
8 months ago |
4 |
347 |
|||
8 months ago |
348 |
int stackminheadroom = COROUTINE_STACK_SIZE; | |||
349 |
for (List_Link *link = g_c->free.fwd.link.next; link->next; link = link->next){ | ||||
350 |
Coroutine *cor = List_Link_Container(Coroutine, link, link); | ||||
351 |
for (int i = 4; i < COROUTINE_STACK_SIZE-3; i += 4){ | ||||
352 |
if (!Check_Guard(&cor->guard[i])){ | ||||
353 |
stackminheadroom = i < stackminheadroom ? i : stackminheadroom; | ||||
354 |
break; | ||||
355 |
} | ||||
356 |
} | ||||
357 |
} | ||||
358 |
g_c->report.lowest_headroom = stackminheadroom; | ||||
359 |
|||||
8 months ago |
360 |
assert(List_IsEmpty(&g_c->inactive)); | |||
7 months ago |
361 |
_Cor_Mutex_Unlock(&g_c->waiting_mutex); | |||
362 |
_Cor_Mutex_dtor(&g_c->waiting_mutex); | ||||
8 months ago |
4 |
363 |
|||
8 months ago |
364 |
assert(g_c->state == Coroutines_Stopping); | |||
7 months ago |
365 |
_Cor_Mutex_Unlock(&g_c->mutex); | |||
8 months ago |
366 |
g_c->state = Coroutines_Idle; | |||
7 months ago |
367 |
_Cor_Mutex_dtor(&g_c->mutex); | |||
8 months ago |
368 |
||||
369 |
Coroutine_Report res = g_c->report; | ||||
370 |
|||||
8 months ago |
371 |
free(g_c); | |||
372 |
g_c = NULL; | ||||
8 months ago |
373 |
||||
374 |
return res; | ||||
8 months ago |
4 |
375 |
} | ||
376 |
|||||
8 months ago |
377 |
||||
8 months ago |
378 |
void Coroutine_Run_Coroutine( | |||
379 |
Coroutine *cor, | ||||
8 months ago |
380 |
void *value | |||
381 |
){ | ||||
382 |
Coroutines *cors = cor->coroutines; | ||||
383 |
assert(g_c == cors); | ||||
7 months ago |
384 |
_Cor_Mutex_Lock(&cors->mutex); | |||
8 months ago |
385 |
assert(cors->state == Coroutines_Started); | |||
386 |
cors->state = Coroutines_Active; | ||||
387 |
cors->primary = cor; | ||||
8 months ago |
388 |
||||
7 months ago |
389 |
_Coroutine_Continue(cor, value, true); | |||
8 months ago |
4 |
390 |
|||
8 months ago |
391 |
if (!setjmp(cors->controller)){ | |||
7 months ago |
392 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
393 |
||||
394 |
// check the guard | ||||
395 |
if (!Check_Guard(g_c->guard)){ | ||||
396 |
printf("Coroutine startup stack as has overrun - checked on entering the main coroutine\n"); | ||||
397 |
exit(EXIT_FAILURE); | ||||
398 |
} | ||||
399 |
|||||
8 months ago |
4 |
400 |
// start the first coroutine | ||
401 |
Coroutine_RunNext(); | ||||
402 |
} | ||||
8 months ago |
403 |
// arrive here with mutex locked | |||
8 months ago |
404 |
assert(List_IsEmpty(&cors->runable)); | |||
405 |
assert(List_IsEmpty(&cors->waiting)); | ||||
406 |
assert(cors->state == Coroutines_Active); | ||||
407 |
cors->state = Coroutines_Started; | ||||
7 months ago |
408 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
409 |
} | |||
410 |
|||||
411 |
|||||
412 |
void *Coroutine_Run( | ||||
413 |
Coroutine_Start start, | ||||
414 |
void *value | ||||
415 |
){ | ||||
416 |
Coroutine *cor = Coroutine_New(start); | ||||
417 |
Coroutine_Run_Coroutine(cor, value); | ||||
8 months ago |
418 |
void *res = Coroutine_GetValue(cor); | |||
419 |
Coroutine_Delete(cor); | ||||
420 |
return res; | ||||
8 months ago |
4 |
421 |
} | ||
422 |
|||||
423 |
|||||
8 months ago |
424 |
Coroutine *Coroutine_New( | |||
425 |
Coroutine_Start start | ||||
426 |
){ | ||||
8 months ago |
427 |
assert((g_c->state == Coroutines_Started && List_IsEmpty(&g_c->inactive)) || g_c->state == Coroutines_Active); | |||
8 months ago |
428 |
||||
8 months ago |
4 |
429 |
// if none free - add one | ||
8 months ago |
430 |
if (List_IsEmpty(&g_c->free)){ | |||
431 |
if (!setjmp(g_c->chunk_allocated)){ | ||||
432 |
longjmp(g_c->tip->buf, Chunk_Create); | ||||
8 months ago |
4 |
433 |
} | ||
434 |
} | ||||
435 |
|||||
8 months ago |
436 |
Coroutine *cor = List_Link_Container(Coroutine, link, List_GetHead(&g_c->free)); | |||
8 months ago |
4 |
437 |
assert(cor->state == Coroutine_Free); | ||
438 |
cor->state = Coroutine_Idle; | ||||
439 |
cor->start = start; | ||||
440 |
cor->value = NULL; | ||||
441 |
List_Remove(&cor->link); | ||||
8 months ago |
442 |
List_AddHead(&g_c->inactive, &cor->link); | |||
8 months ago |
4 |
443 |
|||
8 months ago |
444 |
g_c->report.coroutines_created += 1; | |||
445 |
|||||
8 months ago |
4 |
446 |
return cor; | ||
447 |
} | ||||
448 |
|||||
8 months ago |
449 |
||||
450 |
void Coroutine_Delete( | ||||
451 |
Coroutine *cor | ||||
452 |
){ | ||||
453 |
Coroutines *cors = cor->coroutines; | ||||
7 months ago |
454 |
_Cor_Mutex_Lock(&cors->mutex); | |||
8 months ago |
4 |
455 |
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Complete); | ||
456 |
cor->state = Coroutine_Free; | ||||
457 |
List_Remove(&cor->link); | ||||
8 months ago |
458 |
List_AddTail(&cors->free, &cor->link); | |||
7 months ago |
459 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
4 |
460 |
} | ||
461 |
|||||
8 months ago |
462 |
||||
7 months ago |
463 |
// Coroutine_Continue, assuming the mutex is claimed | |||
464 |
static void _Coroutine_Continue( | ||||
8 months ago |
465 |
Coroutine *cor, | |||
466 |
void *value, | ||||
467 |
bool early | ||||
468 |
){ | ||||
469 |
Coroutines *cors = cor->coroutines; | ||||
8 months ago |
4 |
470 |
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Waiting); | ||
471 |
cor->entry_param = value; | ||||
472 |
cor->state = Coroutine_Running; | ||||
473 |
List_Remove(&cor->link); | ||||
474 |
if ( early ) { | ||||
8 months ago |
475 |
List_AddHead(&cors->runable, &cor->link); | |||
8 months ago |
4 |
476 |
} else { | ||
8 months ago |
477 |
List_AddTail(&cors->runable, &cor->link); | |||
8 months ago |
4 |
478 |
} | ||
7 months ago |
479 |
_Cor_Mutex_Unlock(&cors->waiting_mutex); | |||
7 months ago |
480 |
} | |||
481 |
|||||
482 |
|||||
483 |
void Coroutine_Continue( | ||||
484 |
Coroutine *cor, | ||||
485 |
void *value, | ||||
486 |
bool early | ||||
487 |
){ | ||||
488 |
Coroutines *cors = cor->coroutines; | ||||
7 months ago |
489 |
_Cor_Mutex_Lock(&cors->mutex); | |||
7 months ago |
490 |
_Coroutine_Continue(cor, value, early); | |||
7 months ago |
491 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
4 |
492 |
} | ||
493 |
|||||
8 months ago |
494 |
||||
495 |
void *Coroutine_Yield( | ||||
496 |
void *value, | ||||
497 |
Coroutine_YieldCallback on_yield, | ||||
8 months ago |
498 |
void *yield_me | |||
8 months ago |
499 |
){ | |||
8 months ago |
500 |
Coroutine *me = g_c->active; | |||
501 |
if (!Check_Guard(me->guard)){ | ||||
502 |
printf("Coroutine has overrun its stack - checked when yielding coroutine\n"); | ||||
503 |
exit(EXIT_FAILURE); | ||||
504 |
} | ||||
505 |
|||||
7 months ago |
506 |
_Cor_Mutex_Lock(&g_c->mutex); | |||
8 months ago |
507 |
Coroutines *cors = me->coroutines; | |||
508 |
assert(me && me->state == Coroutine_Running && cors == g_c); | ||||
8 months ago |
4 |
509 |
me->value = value; | ||
510 |
me->state = Coroutine_Waiting; | ||||
8 months ago |
511 |
||||
8 months ago |
4 |
512 |
List_Remove(&me->link); | ||
7 months ago |
513 |
if (!List_IsEmpty(&cors->runable)){ | |||
7 months ago |
514 |
_Cor_Mutex_Unlock(&cors->waiting_mutex); | |||
7 months ago |
515 |
} | |||
8 months ago |
516 |
List_AddTail(&cors->waiting, &me->link); | |||
8 months ago |
517 |
||||
8 months ago |
518 |
switch (setjmp(me->buf)){ | |||
519 |
case Chunk_Initial: | ||||
7 months ago |
520 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
521 |
on_yield(yield_me); | |||
8 months ago |
4 |
522 |
Coroutine_RunNext(); | ||
8 months ago |
523 |
case Chunk_Create: | |||
524 |
assert(false); | ||||
525 |
case Chunk_Enter: | ||||
526 |
// arrive here with mutex locked | ||||
8 months ago |
527 |
cors->active = me; | |||
8 months ago |
528 |
// when we return here - we are running again | |||
529 |
assert(me->state == Coroutine_Running); | ||||
530 |
void *res = me->entry_param; | ||||
7 months ago |
531 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
532 |
return res; | |||
8 months ago |
4 |
533 |
} | ||
8 months ago |
534 |
return NULL; | |||
8 months ago |
4 |
535 |
} | ||
536 |
|||||
8 months ago |
537 |
||||
538 |
void *Coroutine_GetValue( | ||||
539 |
Coroutine *cor | ||||
540 |
){ | ||||
8 months ago |
4 |
541 |
return cor->value; | ||
542 |
} | ||||
543 |
|||||
8 months ago |
544 |
||||
8 months ago |
4 |
545 |
Coroutine *Coroutine_GetActive() | ||
546 |
{ | ||||
8 months ago |
547 |
return g_c->active; | |||
8 months ago |
4 |
548 |
} | ||
549 |
|||||
8 months ago |
550 |
||||
8 months ago |
551 |
int Coroutine_GetStackHeadroom(){ | |||
552 |
unsigned char tbuf[4]; | ||||
553 |
return tbuf - g_c->active->guard - 4; | ||||
554 |
} | ||||
555 |
|||||
556 |
|||||
557 |
bool Coroutine_HasCoroutinesInFreePool(){ | ||||
558 |
return !List_IsEmpty(&g_c->free); | ||||
559 |
} | ||||
560 |
|||||
561 |
|||||
562 |
void *Coroutine_GetCStackTop(){ | ||||
563 |
return g_c->tip; | ||||
564 |
} | ||||
565 |
|||||
566 |
|||||
567 |
struct Coroutine_ChainParam { | ||||
568 |
Coroutine_Start start; | ||||
569 |
void *value; | ||||
570 |
Coroutine *ret; | ||||
571 |
}; | ||||
572 |
|||||
573 |
|||||
574 |
static void *Coroutine_ChainFn( | ||||
575 |
void *param | ||||
576 |
){ | ||||
577 |
struct Coroutine_ChainParam *params = (struct Coroutine_ChainParam *)param; | ||||
578 |
Coroutine_Continue(params->ret, params->start(params->value), true); | ||||
579 |
return NULL; | ||||
580 |
} | ||||
581 |
|||||
582 |
|||||
583 |
static void Coroutine_ChainYield( | ||||
584 |
void *unused | ||||
585 |
){ | ||||
586 |
(void)unused; | ||||
587 |
} | ||||
588 |
|||||
589 |
|||||
590 |
void *Coroutine_Chain( | ||||
591 |
Coroutine_Start start, | ||||
592 |
void *value | ||||
593 |
){ | ||||
594 |
if (!Check_Guard(Coroutine_GetActive()->guard)){ | ||||
595 |
printf("Coroutine has overrun its stack - checked when chaining\n"); | ||||
596 |
exit(EXIT_FAILURE); | ||||
597 |
} | ||||
598 |
Coroutine *cor = Coroutine_New(Coroutine_ChainFn); | ||||
599 |
struct Coroutine_ChainParam params = { | ||||
600 |
start, | ||||
601 |
value, | ||||
602 |
Coroutine_GetActive() | ||||
603 |
}; | ||||
604 |
Coroutine_Continue(cor, ¶ms, true); | ||||
605 |
void *res = Coroutine_Yield(NULL, Coroutine_ChainYield, NULL); | ||||
606 |
Coroutine_Delete(cor); | ||||
607 |
return res; | ||||
608 |
} | ||||
609 |
|||||
610 |
|||||
8 months ago |
611 |
bool Coroutine_IsRunning( | |||
612 |
Coroutine *cor | ||||
613 |
) | ||||
8 months ago |
4 |
614 |
{ | ||
8 months ago |
615 |
int state = cor->state; | |||
616 |
return state == Coroutine_Running || state == Coroutine_Waiting; | ||||
8 months ago |
4 |
617 |
} | ||
8 months ago |
618 |
||||
619 |
|||||
620 |
bool Coroutine_IsStarted(){ | ||||
621 |
return g_c != NULL; | ||||
622 |
} | ||||
623 |