8 months ago |
4 |
1 |
#include "coroutine.h" | ||
2 |
#include <assert.h> | ||||
3 |
#include <setjmp.h> | ||||
4 |
#include <stdbool.h> | ||||
5 |
#include <stddef.h> | ||||
5 months ago |
6 |
#include <stdio.h> | |||
7 months ago |
7 |
#include "cor_platform.h" | |||
8 months ago |
4 |
8 |
|||
6 months ago |
9 |
// see CPython again, this time from ctypes.h | |||
10 |
#if (defined (__SVR4) && defined (__sun)) || defined(COROUTINE_HAVE_ALLOCA_H) | ||||
11 |
# include <alloca.h> | ||||
12 |
#elif defined(MS_WIN32) | ||||
13 |
# include <malloc.h> | ||||
14 |
#endif | ||||
8 months ago |
4 |
15 |
|||
6 months ago |
16 |
/* If the system does not define alloca(), we have to hope for a compiler builtin. */ | |||
17 |
#ifndef alloca | ||||
18 |
# if defined __GNUC__ || (__clang_major__ >= 4) | ||||
19 |
# define alloca __builtin_alloca | ||||
20 |
# else | ||||
21 |
# error "Could not define alloca() on your platform." | ||||
22 |
# endif | ||||
23 |
#endif | ||||
24 |
|||||
3 months ago |
25 |
typedef struct Coroutines Coroutines; | |||
26 |
|||||
7 months ago |
27 |
static void Coroutine_RunNext(void); | |||
3 months ago |
28 |
static bool _Coroutine_Continue(Coroutines *cors, Coroutine *cor, void *value, bool early); | |||
6 months ago |
29 |
static unsigned char *StackTopNow(void); | |||
8 months ago |
30 |
||||
8 months ago |
4 |
31 |
/////////////////////////////////////////////////////////////////////////////// | ||
32 |
// 2-way linked lists... | ||||
33 |
// | ||||
8 months ago |
34 |
// Brought inline here to avoid namespace polution | |||
8 months ago |
4 |
35 |
/////////////////////////////////////////////////////////////////////////////// | ||
36 |
|||||
37 |
typedef struct List_Link List_Link; | ||||
38 |
struct List_Link { | ||||
39 |
List_Link *next; | ||||
40 |
List_Link *prev; | ||||
41 |
}; | ||||
42 |
|||||
43 |
typedef struct List_Head List_Head; | ||||
44 |
struct List_Head { | ||||
45 |
union { | ||||
46 |
struct { | ||||
47 |
List_Link link; | ||||
48 |
List_Link *filler; | ||||
49 |
} fwd; | ||||
50 |
struct { | ||||
51 |
List_Link *filler; | ||||
52 |
List_Link link; | ||||
53 |
} back; | ||||
54 |
}; | ||||
55 |
}; | ||||
56 |
|||||
8 months ago |
57 |
||||
58 |
static inline bool List_IsEmpty( | ||||
59 |
const List_Head *list | ||||
60 |
){ | ||||
8 months ago |
4 |
61 |
return list->fwd.link.next == &list->back.link; | ||
62 |
} | ||||
63 |
|||||
8 months ago |
64 |
||||
65 |
static inline List_Link *List_GetHead( | ||||
66 |
const List_Head *list | ||||
67 |
){ | ||||
8 months ago |
4 |
68 |
return List_IsEmpty(list) ? NULL : list->fwd.link.next; | ||
69 |
} | ||||
8 months ago |
70 |
||||
71 |
|||||
5 months ago |
72 |
static inline List_Link *List_Begin( | |||
73 |
const List_Head *list | ||||
74 |
){ | ||||
75 |
return list->fwd.link.next; | ||||
76 |
} | ||||
77 |
|||||
78 |
|||||
79 |
static inline bool Link_NextIsLink( | ||||
80 |
const List_Link *link | ||||
81 |
){ | ||||
82 |
return link->next != NULL; | ||||
83 |
} | ||||
84 |
|||||
85 |
|||||
86 |
static inline List_Link *Link_Next( | ||||
87 |
List_Link *link | ||||
88 |
){ | ||||
89 |
return link->next; | ||||
90 |
} | ||||
91 |
|||||
92 |
|||||
93 |
static inline bool Link_PrevIsLink( | ||||
94 |
const List_Link *link | ||||
95 |
){ | ||||
96 |
return link->prev != NULL; | ||||
97 |
} | ||||
98 |
|||||
99 |
|||||
100 |
static inline List_Link *Link_Prev( | ||||
101 |
List_Link *link | ||||
102 |
){ | ||||
103 |
return link->prev; | ||||
104 |
} | ||||
105 |
|||||
5 months ago |
106 |
static inline List_Link *List_GetTail( | |||
107 |
const List_Head *list | ||||
108 |
){ | ||||
109 |
return List_IsEmpty(list) ? NULL : list->back.link.prev; | ||||
110 |
} | ||||
8 months ago |
111 |
||||
112 |
|||||
8 months ago |
4 |
113 |
#define OFFSETOF(Container, Field) ((char *)&((Container *)4)->Field - (char *)(Container *)4) | ||
114 |
#define List_Link_Container(Container, Link, link) ((Container *)((char *)(link) - OFFSETOF(Container, Link))) | ||||
115 |
|||||
8 months ago |
116 |
||||
117 |
static inline void List_Init( | ||||
118 |
List_Head *list | ||||
119 |
){ | ||||
8 months ago |
4 |
120 |
list->fwd.link.next = &list->back.link; | ||
121 |
list->fwd.link.prev = NULL; | ||||
122 |
list->back.link.prev = &list->fwd.link; | ||||
123 |
} | ||||
124 |
|||||
8 months ago |
125 |
||||
5 months ago |
126 |
static inline void Link_AddAfter( | |||
127 |
List_Link *link, | ||||
128 |
List_Link *after | ||||
129 |
){ | ||||
130 |
link->next = after->next; | ||||
131 |
link->prev = after; | ||||
132 |
after->next->prev = link; | ||||
133 |
after->next = link; | ||||
134 |
} | ||||
135 |
|||||
136 |
|||||
8 months ago |
137 |
static inline void List_AddHead( | |||
138 |
List_Head *list, | ||||
139 |
List_Link *link | ||||
140 |
){ | ||||
5 months ago |
141 |
Link_AddAfter(link, &list->fwd.link); | |||
8 months ago |
4 |
142 |
} | ||
143 |
|||||
8 months ago |
144 |
||||
5 months ago |
145 |
static inline void Link_AddBefore( | |||
146 |
List_Link *link, | ||||
147 |
List_Link *before | ||||
148 |
){ | ||||
149 |
link->prev = before->prev; | ||||
150 |
link->next = before; | ||||
151 |
before->prev->next = link; | ||||
152 |
before->prev = link; | ||||
153 |
} | ||||
154 |
|||||
155 |
|||||
8 months ago |
156 |
static inline void List_AddTail( | |||
157 |
List_Head *list, | ||||
158 |
List_Link *link | ||||
159 |
){ | ||||
5 months ago |
160 |
Link_AddBefore(link, &list->back.link); | |||
8 months ago |
4 |
161 |
} | ||
162 |
|||||
8 months ago |
163 |
||||
5 months ago |
164 |
static inline void Link_Remove( | |||
8 months ago |
165 |
List_Link *link | |||
166 |
){ | ||||
8 months ago |
4 |
167 |
link->prev->next = link->next; | ||
168 |
link->next->prev = link->prev; | ||||
169 |
} | ||||
170 |
|||||
171 |
/////////////////////////////////////////////////////////////////////////////// | ||||
172 |
// ...2-way linked lists | ||||
173 |
/////////////////////////////////////////////////////////////////////////////// | ||||
174 |
|||||
175 |
enum { | ||||
176 |
Coroutines_Starting, | ||||
177 |
Coroutines_Started, | ||||
178 |
Coroutines_Active, | ||||
179 |
Coroutines_Stopping | ||||
180 |
}; | ||||
181 |
|||||
182 |
enum { | ||||
183 |
Chunk_Initial, | ||||
184 |
Chunk_Create, | ||||
5 months ago |
185 |
Chunk_Split, | |||
8 months ago |
4 |
186 |
Chunk_Enter | ||
187 |
}; | ||||
188 |
|||||
8 months ago |
189 |
typedef enum Coroutine_State { | |||
8 months ago |
4 |
190 |
Coroutine_Free, | ||
191 |
Coroutine_Idle, | ||||
192 |
Coroutine_Running, | ||||
193 |
Coroutine_Waiting, | ||||
194 |
Coroutine_Complete | ||||
8 months ago |
195 |
} Coroutine_State; | |||
8 months ago |
4 |
196 |
|||
197 |
enum { | ||||
198 |
Coroutines_Init, | ||||
199 |
Coroutines_AllocatedChunk, | ||||
200 |
Coroutines_CoroutineComplete, | ||||
201 |
}; | ||||
202 |
|||||
203 |
struct Coroutine { | ||||
6 months ago |
204 |
Coroutines *coroutines; // so can work with it off-thread | |||
205 |
List_Link link; // for whichever list it's on | ||||
5 months ago |
206 |
List_Link all_link; // list of all Coroutines | |||
6 months ago |
207 |
jmp_buf buf; // how to get back to it | |||
5 months ago |
208 |
unsigned char *prev_limit; // the previous Coroutine's stack limit | |||
209 |
unsigned char *base; // where the base (high address) of this Coroutine's stack is | ||||
210 |
unsigned char *limit; // where the limit (low address) of this Coroutine's stack is | ||||
6 months ago |
211 |
unsigned char *guard; // where the stack overrun guard is | |||
5 months ago |
212 |
size_t size; | |||
6 months ago |
213 |
Coroutine_Start start; // entry point | |||
214 |
void *entry_param; // to pass to start | ||||
215 |
void *value; // yielded/returned | ||||
216 |
unsigned char *stack_top; // recorded at yield | ||||
8 months ago |
217 |
Coroutine_State state; | |||
8 months ago |
4 |
218 |
}; | ||
219 |
|||||
220 |
struct Coroutines { | ||||
7 months ago |
221 |
_Cor_Mutex mutex; | |||
8 months ago |
222 |
jmp_buf controller; // to return from Coroutine_Run | |||
223 |
jmp_buf chunk_allocated;// for chunk allocation | ||||
5 months ago |
224 |
size_t gap_before; // bytes between previous's stack_top and next's Coroutine | |||
225 |
size_t gap_after; // bytes between Coroutine and stack_base | ||||
8 months ago |
4 |
226 |
|||
227 |
// singletons | ||||
228 |
Coroutine *tip; // top of stack chunk | ||||
229 |
Coroutine *active; // currently running coroutine | ||||
230 |
Coroutine *primary; // Coroutine_Run coroutine | ||||
6 months ago |
231 |
unsigned char *stack_limit; // when not NULL, where the stack finishes | |||
8 months ago |
4 |
232 |
|||
233 |
// lists | ||||
5 months ago |
234 |
List_Head all; // all Coroutines (in address order) | |||
235 |
List_Head free; // free Coroutines | ||||
8 months ago |
4 |
236 |
List_Head inactive; // idle or complete | ||
237 |
List_Head runable; // running or waiting to run | ||||
238 |
List_Head waiting; // yielded / waiting to run | ||||
7 months ago |
239 |
_Cor_Mutex waiting_mutex; | |||
8 months ago |
4 |
240 |
|||
8 months ago |
241 |
// Summary of the system | |||
242 |
Coroutine_Report report; | ||||
243 |
|||||
8 months ago |
4 |
244 |
// state | ||
245 |
char state; | ||||
246 |
}; | ||||
247 |
|||||
5 months ago |
248 |
_Cor_thread_local Coroutines *g_c; | |||
4 months ago |
249 |
_Cor_thread_local unsigned char *g_stack_limit; | |||
8 months ago |
4 |
250 |
|||
5 months ago |
251 |
static void ReserveStackSpace(Coroutines *cors, Coroutine *parent, size_t chunk_size, unsigned char *childs_limit); | |||
5 months ago |
252 |
static void stack_chunk_base(Coroutines *cors, Coroutine *parent, unsigned char *prev_limit, unsigned char *limit); | |||
8 months ago |
4 |
253 |
|||
8 months ago |
254 |
||||
6 months ago |
255 |
#define GUARD_PATTERN_SIZE (4) | |||
8 months ago |
256 |
// Check whether the guard is intact | |||
257 |
static inline bool Check_Guard( | ||||
258 |
unsigned char *guard | ||||
259 |
){ | ||||
6 months ago |
260 |
return !guard || | |||
261 |
(guard[0] == 0xde && | ||||
262 |
guard[1] == 0xad && | ||||
263 |
guard[2] == 0xbe && | ||||
264 |
guard[3] == 0xef); | ||||
8 months ago |
265 |
} | |||
266 |
|||||
267 |
|||||
6 months ago |
268 |
static inline void Apply_Guard(unsigned char *guard){ | |||
269 |
guard[0] = 0xde; | ||||
270 |
guard[1] = 0xad; | ||||
271 |
guard[2] = 0xbe; | ||||
272 |
guard[3] = 0xef; | ||||
273 |
} | ||||
274 |
|||||
275 |
|||||
3 months ago |
276 |
#ifndef NDEBUG | |||
277 |
static void CheckListIntegrity(List_Head *head, Coroutine_State state1, Coroutine_State state2){ | ||||
278 |
for (List_Link *link = List_Begin(head); Link_NextIsLink(link); link = Link_Next(link)){ | ||||
279 |
Coroutine *candidate = List_Link_Container(Coroutine, link, link); | ||||
280 |
assert(candidate->coroutines == g_c); | ||||
281 |
assert(candidate->state == state1 || candidate->state == state2); | ||||
282 |
bool found = false; | ||||
283 |
for (List_Link *link = List_Begin(&g_c->all); Link_NextIsLink(link); link = Link_Next(link)){ | ||||
284 |
Coroutine *candidate2 = List_Link_Container(Coroutine, all_link, link); | ||||
285 |
if (candidate == candidate2){ | ||||
286 |
found = true; | ||||
287 |
} | ||||
288 |
} | ||||
289 |
assert(found); | ||||
290 |
} | ||||
291 |
} | ||||
292 |
#endif | ||||
293 |
|||||
294 |
|||||
295 |
void Coroutine_CheckIntegrity(void){ | ||||
296 |
#ifndef NDEBUG | ||||
297 |
CheckListIntegrity(&g_c->free, Coroutine_Free, Coroutine_Free); | ||||
298 |
CheckListIntegrity(&g_c->inactive, Coroutine_Idle, Coroutine_Complete); | ||||
299 |
CheckListIntegrity(&g_c->runable, Coroutine_Running, Coroutine_Running); | ||||
300 |
CheckListIntegrity(&g_c->waiting, Coroutine_Waiting, Coroutine_Waiting); | ||||
301 |
#endif | ||||
302 |
} | ||||
303 |
|||||
304 |
|||||
305 |
#ifndef NDEBUG | ||||
5 months ago |
306 |
static bool Coroutine_StackHasOverrun(void){ | |||
6 months ago |
307 |
unsigned char *stack_top = StackTopNow(); | |||
5 months ago |
308 |
unsigned char *stack_limit = g_c ? g_c->stack_limit : NULL; | |||
6 months ago |
309 |
if (stack_limit && stack_top < stack_limit){ | |||
3 months ago |
310 |
// printf("top %p < limit %p\n", stack_top, stack_limit); | |||
6 months ago |
311 |
// current stack top is beyond limit - we are overrunning NOW | |||
5 months ago |
312 |
return true; | |||
6 months ago |
313 |
} | |||
3 months ago |
314 |
if (stack_limit && stack_top < stack_limit+2048){ | |||
315 |
printf("Stack LOW hazard\n"); | ||||
316 |
} | ||||
5 months ago |
317 |
Coroutine *me = g_c ? g_c->active : NULL; | |||
6 months ago |
318 |
if (!me){ | |||
5 months ago |
319 |
return false; | |||
6 months ago |
320 |
} | |||
3 months ago |
321 |
#if COROUTINE_CHECK_INTEGRITY_ON_STACK_CHECK | |||
322 |
// Check all coroutines integrity | ||||
323 |
Coroutine_CheckIntegrity(); | ||||
324 |
#endif | ||||
6 months ago |
325 |
if (me->guard){ | |||
3 months ago |
326 |
bool ret = !Check_Guard(me->guard); | |||
327 |
// if (ret){ | ||||
328 |
// printf("Broken guard me=%p; me->guard=%p me->limit-%p stack top=%p\n", me, me->guard, me->limit, stack_top); | ||||
329 |
// } | ||||
330 |
return ret; | ||||
6 months ago |
331 |
} | |||
3 months ago |
332 |
bool ret = stack_top < me->limit; | |||
333 |
// if (ret){ | ||||
334 |
// printf("stack top %p < me->limit %p\n", stack_top, me->limit); | ||||
335 |
// } | ||||
336 |
return ret; | ||||
6 months ago |
337 |
} | |||
3 months ago |
338 |
#endif | |||
6 months ago |
339 |
||||
340 |
|||||
5 months ago |
341 |
static void ReserveStackSpace( | |||
342 |
Coroutines *cors, | ||||
6 months ago |
343 |
Coroutine *parent, | |||
5 months ago |
344 |
size_t chunk_size, | |||
345 |
unsigned char *childs_limit | ||||
8 months ago |
346 |
){ | |||
6 months ago |
347 |
unsigned char *chunk_of_stack = alloca(chunk_size); | |||
348 |
#if COROUTINE_RECORD_LOWEST_HEADROOM | ||||
349 |
for (size_t i = 0; i <= chunk_size-GUARD_PATTERN_SIZE; i += GUARD_PATTERN_SIZE){ | ||||
350 |
Apply_Guard(&chunk_of_stack[i]); | ||||
351 |
} | ||||
352 |
#else | ||||
6 months ago |
353 |
Apply_Guard(chunk_of_stack); | |||
6 months ago |
354 |
#endif | |||
5 months ago |
355 |
if (parent){ | |||
356 |
parent->guard = chunk_of_stack; | ||||
357 |
parent->limit = chunk_of_stack; | ||||
358 |
parent->base = chunk_of_stack + chunk_size; | ||||
359 |
} | ||||
5 months ago |
360 |
stack_chunk_base(cors, parent, chunk_of_stack, childs_limit); | |||
8 months ago |
4 |
361 |
} | ||
362 |
|||||
8 months ago |
363 |
||||
364 |
static void stack_chunk_base( | ||||
5 months ago |
365 |
Coroutines *cors, | |||
5 months ago |
366 |
Coroutine *parent, | |||
5 months ago |
367 |
unsigned char *prev_limit, | |||
368 |
unsigned char *limit | ||||
8 months ago |
369 |
){ | |||
8 months ago |
4 |
370 |
Coroutine here; | ||
5 months ago |
371 |
here.coroutines = cors; | |||
6 months ago |
372 |
here.state = Coroutine_Free; | |||
5 months ago |
373 |
here.prev_limit = prev_limit; | |||
374 |
here.size = 0; | ||||
375 |
here.base = NULL; | ||||
376 |
here.guard = limit; | ||||
377 |
here.limit = limit; | ||||
378 |
if (limit){ | ||||
379 |
here.base = (unsigned char *)&here - cors->gap_after; | ||||
380 |
here.size = here.base - here.limit; | ||||
381 |
Apply_Guard(limit); | ||||
382 |
} | ||||
383 |
|||||
5 months ago |
384 |
// insert into all list | |||
385 |
if (parent){ | ||||
386 |
Link_AddAfter(&here.all_link, &parent->all_link); | ||||
387 |
} else { | ||||
388 |
List_AddHead(&cors->all, &here.all_link); | ||||
5 months ago |
389 |
} | |||
5 months ago |
390 |
// add to free list | |||
391 |
List_AddTail(&cors->free, &here.link); | ||||
5 months ago |
392 |
||||
393 |
cors->report.coroutines_pool_size += 1; | ||||
394 |
|||||
395 |
if (!cors->tip || &here < cors->tip){ | ||||
396 |
cors->tip = &here; | ||||
397 |
} | ||||
398 |
|||||
6 months ago |
399 |
for(;;){ | |||
400 |
switch (setjmp(here.buf)) { | ||||
401 |
case Chunk_Initial: | ||||
6 months ago |
402 |
if (here.state == Coroutine_Free){ | |||
6 months ago |
403 |
// return to the coroutine allocator | |||
5 months ago |
404 |
longjmp(cors->chunk_allocated, 1); | |||
6 months ago |
405 |
} else { | |||
406 |
assert(here.state == Coroutine_Complete); | ||||
407 |
// we finish here to ensure the setjmp is redone | ||||
5 months ago |
408 |
if (cors->primary == &here) { | |||
6 months ago |
409 |
// if primary coroutine - return to Coroutine_Run | |||
5 months ago |
410 |
longjmp(cors->controller, Coroutines_CoroutineComplete); | |||
6 months ago |
411 |
} | |||
5 months ago |
412 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
6 months ago |
413 |
Coroutine_RunNext(); | |||
414 |
assert(false); | ||||
415 |
} | ||||
416 |
case Chunk_Create: | ||||
6 months ago |
417 |
// Request to create a new chunk on the stack | |||
418 |
// We're here if the coroutine is: | ||||
419 |
// Allocated, but not 'run' (Coroutine_Idle) | ||||
420 |
// Run, but not not entered yet (Coroutine_Running) | ||||
421 |
// Completed (Coroutine_Complete) | ||||
5 months ago |
422 |
// Free, and the coroutines system is starting - we're characterising the system | |||
423 |
assert(here.state == Coroutine_Idle || | ||||
424 |
here.state == Coroutine_Running || | ||||
425 |
here.state == Coroutine_Complete || | ||||
426 |
(here.state == Coroutine_Free && cors->state == Coroutines_Starting)); | ||||
427 |
ReserveStackSpace(here.coroutines, &here, here.size, NULL); | ||||
6 months ago |
428 |
assert(false); | |||
5 months ago |
429 |
case Chunk_Split: | |||
430 |
// Request to split this free block into two | ||||
431 |
// here.size will be set to our shorter size | ||||
432 |
ReserveStackSpace(here.coroutines, &here, here.size, here.limit); | ||||
433 |
assert(false); | ||||
6 months ago |
434 |
case Chunk_Enter: | |||
435 |
// request to start a coroutine (ie use the chunk for a coroutine) | ||||
436 |
// arrive here with mutex locked | ||||
437 |
assert(here.state == Coroutine_Running); | ||||
5 months ago |
438 |
here.coroutines->active = &here; | |||
439 |
_Cor_Mutex_Unlock(&cors->mutex); | ||||
6 months ago |
440 |
here.value = here.start(here.entry_param); | |||
8 months ago |
441 |
||||
6 months ago |
442 |
// check the guard | |||
443 |
assert(Check_Guard(here.guard)); | ||||
8 months ago |
444 |
||||
5 months ago |
445 |
_Cor_Mutex_Lock(&here.coroutines->mutex); | |||
446 |
here.coroutines->active = NULL; | ||||
6 months ago |
447 |
assert(here.state == Coroutine_Running); | |||
5 months ago |
448 |
Link_Remove(&here.link); | |||
6 months ago |
449 |
here.state = Coroutine_Complete; | |||
5 months ago |
450 |
List_AddTail(&here.coroutines->inactive, &here.link); | |||
6 months ago |
451 |
// Coroutine has completed | |||
452 |
// Loop round to redo the setjmp() - if this coroutine yielded, then the setjmp will | ||||
453 |
// need reseting | ||||
8 months ago |
4 |
454 |
} | ||
455 |
} | ||||
456 |
} | ||||
457 |
|||||
8 months ago |
458 |
||||
7 months ago |
459 |
static void Coroutine_RunNext(void) | |||
8 months ago |
460 |
{ | |||
461 |
// arrive here with mutex unlocked | ||||
5 months ago |
462 |
_Cor_Mutex_Lock(&g_c->waiting_mutex); | |||
463 |
_Cor_Mutex_Lock(&g_c->mutex); | ||||
464 |
Coroutine *next = List_Link_Container(Coroutine, link, List_GetHead(&g_c->runable)); | ||||
8 months ago |
465 |
assert(next->state == Coroutine_Running); | |||
466 |
longjmp(next->buf, Chunk_Enter); | ||||
467 |
assert(false); | ||||
468 |
} | ||||
469 |
|||||
470 |
|||||
7 months ago |
471 |
void Coroutine_StartSystem(void) | |||
8 months ago |
4 |
472 |
{ | ||
5 months ago |
473 |
assert(!g_c); | |||
8 months ago |
474 |
||||
5 months ago |
475 |
Coroutines *cors = _Cor_Malloc(sizeof(*g_c)); | |||
476 |
assert(cors); | ||||
8 months ago |
477 |
||||
5 months ago |
478 |
cors->state = Coroutines_Starting; | |||
479 |
_Cor_Mutex_ctor(&cors->mutex); | ||||
8 months ago |
480 |
||||
5 months ago |
481 |
cors->tip = NULL; | |||
482 |
cors->active = NULL; | ||||
5 months ago |
483 |
cors->primary = NULL; | |||
4 months ago |
484 |
cors->stack_limit = g_stack_limit; | |||
8 months ago |
4 |
485 |
|||
5 months ago |
486 |
List_Init(&cors->all); | |||
5 months ago |
487 |
List_Init(&cors->free); | |||
488 |
List_Init(&cors->inactive); | ||||
489 |
List_Init(&cors->runable); | ||||
490 |
List_Init(&cors->waiting); | ||||
491 |
_Cor_Mutex_ctor(&cors->waiting_mutex); | ||||
492 |
_Cor_Mutex_Lock(&cors->waiting_mutex); | ||||
8 months ago |
4 |
493 |
|||
5 months ago |
494 |
cors->report.coroutines_created = 0; | |||
495 |
cors->report.coroutines_pool_size = 0; | ||||
496 |
cors->report.largest_stack = 0; | ||||
497 |
|||||
498 |
// Charactersize the system... | ||||
499 |
if (!setjmp(cors->chunk_allocated)){ | ||||
500 |
ReserveStackSpace(cors, NULL, COROUTINE_STARTUP_STACK_SIZE, NULL); | ||||
8 months ago |
4 |
501 |
} | ||
5 months ago |
502 |
Coroutine *cor = List_Link_Container(Coroutine, link, List_GetHead(&cors->free)); | |||
503 |
cor->size = COROUTINE_STARTUP_STACK_SIZE; | ||||
504 |
if (!setjmp(cors->chunk_allocated)){ | ||||
505 |
longjmp(cor->buf, Chunk_Create); | ||||
506 |
} | ||||
507 |
cors->gap_before = cor->prev_limit - (unsigned char *)cor; | ||||
508 |
cors->gap_after = (unsigned char *)cor - cor->base; | ||||
509 |
// ...charactersize the system | ||||
8 months ago |
510 |
||||
5 months ago |
511 |
// discard what we've just created | |||
5 months ago |
512 |
List_Init(&cors->all); | |||
5 months ago |
513 |
List_Init(&cors->free); | |||
514 |
cors->tip = NULL; | ||||
515 |
|||||
516 |
cors->state = Coroutines_Started; | ||||
517 |
|||||
518 |
g_c = cors; | ||||
8 months ago |
4 |
519 |
} | ||
520 |
|||||
8 months ago |
521 |
||||
6 months ago |
522 |
void Coroutine_SetStackLimit(void *limit){ | |||
4 months ago |
523 |
assert(!limit || !g_c || !(g_c->state == Coroutines_Started || g_c->state == Coroutines_Active) || (unsigned char *)limit < (unsigned char *)g_c->tip || !g_c->tip); | |||
524 |
g_stack_limit = limit; | ||||
525 |
if (g_c){ | ||||
526 |
g_c->stack_limit = limit; | ||||
527 |
} | ||||
6 months ago |
528 |
} | |||
529 |
|||||
530 |
|||||
7 months ago |
531 |
Coroutine_Report Coroutine_StopSystem(void) | |||
8 months ago |
4 |
532 |
{ | ||
5 months ago |
533 |
assert(g_c); | |||
534 |
assert(g_c->state == Coroutines_Started); | ||||
535 |
_Cor_Mutex_Lock(&g_c->mutex); | ||||
536 |
g_c->state = Coroutines_Stopping; | ||||
8 months ago |
4 |
537 |
|||
5 months ago |
538 |
uintptr_t stackminheadroom; | |||
6 months ago |
539 |
#if COROUTINE_RECORD_LOWEST_HEADROOM | |||
5 months ago |
540 |
stackminheadroom = g_c->report.largest_stack; | |||
541 |
for (List_Link *link = g_c->free.fwd.link.next; Link_NextIsLink(link); link = Link_Next(link)){ | ||||
8 months ago |
542 |
Coroutine *cor = List_Link_Container(Coroutine, link, link); | |||
6 months ago |
543 |
if (cor->guard){ | |||
5 months ago |
544 |
for (uintptr_t i = 4; i < cor->size-3; i += 4){ | |||
6 months ago |
545 |
if (!Check_Guard(&cor->guard[i])){ | |||
546 |
stackminheadroom = i < stackminheadroom ? i : stackminheadroom; | ||||
547 |
break; | ||||
548 |
} | ||||
8 months ago |
549 |
} | |||
550 |
} | ||||
551 |
} | ||||
6 months ago |
552 |
#else | |||
553 |
stackminheadroom = 0; | ||||
554 |
#endif | ||||
5 months ago |
555 |
g_c->report.lowest_headroom = stackminheadroom; | |||
8 months ago |
556 |
||||
5 months ago |
557 |
assert(List_IsEmpty(&g_c->inactive)); | |||
558 |
_Cor_Mutex_Unlock(&g_c->waiting_mutex); | ||||
559 |
_Cor_Mutex_dtor(&g_c->waiting_mutex); | ||||
8 months ago |
4 |
560 |
|||
5 months ago |
561 |
assert(g_c->state == Coroutines_Stopping); | |||
562 |
_Cor_Mutex_Unlock(&g_c->mutex); | ||||
563 |
_Cor_Mutex_dtor(&g_c->mutex); | ||||
8 months ago |
564 |
||||
5 months ago |
565 |
Coroutine_Report ret = g_c->report; | |||
566 |
|||||
567 |
_Cor_Free(g_c); | ||||
568 |
g_c = NULL; | ||||
569 |
|||||
570 |
return ret; | ||||
8 months ago |
4 |
571 |
} | ||
572 |
|||||
8 months ago |
573 |
||||
3 months ago |
574 |
#ifndef NDEBUG | |||
575 |
static void Coroutine_ReportNonEmptyList( | ||||
576 |
List_Head const *head, | ||||
577 |
char const *tag | ||||
578 |
){ | ||||
579 |
List_Link *link; | ||||
580 |
for (link = List_Begin(head); Link_NextIsLink(link); link = Link_Next(link)){ | ||||
581 |
Coroutine *cor = List_Link_Container(Coroutine, link, link); | ||||
582 |
printf("%s: %p %p %p\n", tag, cor, cor->start, cor->entry_param); | ||||
583 |
} | ||||
584 |
} | ||||
585 |
#endif | ||||
586 |
|||||
8 months ago |
587 |
void Coroutine_Run_Coroutine( | |||
588 |
Coroutine *cor, | ||||
8 months ago |
589 |
void *value | |||
590 |
){ | ||||
591 |
Coroutines *cors = cor->coroutines; | ||||
5 months ago |
592 |
||||
593 |
// Can't Coroutine_Run_Coroutine() off-thread | ||||
594 |
assert(g_c == cors); | ||||
595 |
|||||
7 months ago |
596 |
_Cor_Mutex_Lock(&cors->mutex); | |||
8 months ago |
597 |
assert(cors->state == Coroutines_Started); | |||
598 |
cors->state = Coroutines_Active; | ||||
599 |
cors->primary = cor; | ||||
8 months ago |
600 |
||||
3 months ago |
601 |
_Coroutine_Continue(cors, cor, value, true); | |||
8 months ago |
4 |
602 |
|||
8 months ago |
603 |
if (!setjmp(cors->controller)){ | |||
7 months ago |
604 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
605 |
||||
8 months ago |
4 |
606 |
// start the first coroutine | ||
607 |
Coroutine_RunNext(); | ||||
608 |
} | ||||
8 months ago |
609 |
// arrive here with mutex locked | |||
3 months ago |
610 |
#ifndef NDEBUG | |||
611 |
if (!List_IsEmpty(&cors->runable) || !List_IsEmpty(&cors->waiting)){ | ||||
612 |
Coroutine_ReportNonEmptyList(&cors->runable, "runable"); | ||||
613 |
Coroutine_ReportNonEmptyList(&cors->waiting, "waiting"); | ||||
614 |
assert(false); | ||||
615 |
} | ||||
616 |
#endif | ||||
8 months ago |
617 |
assert(cors->state == Coroutines_Active); | |||
618 |
cors->state = Coroutines_Started; | ||||
7 months ago |
619 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
620 |
} | |||
621 |
|||||
622 |
|||||
5 months ago |
623 |
bool Coroutine_Run( | |||
5 months ago |
624 |
size_t stack, | |||
8 months ago |
625 |
Coroutine_Start start, | |||
5 months ago |
626 |
void *value, | |||
627 |
void **result | ||||
8 months ago |
628 |
){ | |||
5 months ago |
629 |
if (g_c && g_c->active){ | |||
4 months ago |
630 |
assert(!Coroutine_StackHasOverrun()); | |||
5 months ago |
631 |
void *res = start(value); | |||
632 |
if (result){ | ||||
633 |
*result = res; | ||||
634 |
} | ||||
635 |
// no failures, so... | ||||
636 |
return false; | ||||
6 months ago |
637 |
} | |||
5 months ago |
638 |
assert(!g_c || g_c->state == Coroutines_Started); | |||
639 |
bool need_start = !g_c; | ||||
6 months ago |
640 |
if (need_start){ | |||
641 |
Coroutine_StartSystem(); | ||||
642 |
} | ||||
5 months ago |
643 |
Coroutine *cor = Coroutine_New(stack, start); | |||
5 months ago |
644 |
if (!cor){ | |||
645 |
// that didn't work | ||||
646 |
return true; | ||||
647 |
} | ||||
8 months ago |
648 |
Coroutine_Run_Coroutine(cor, value); | |||
5 months ago |
649 |
if (result){ | |||
650 |
*result = Coroutine_GetValue(cor); | ||||
651 |
} | ||||
8 months ago |
652 |
Coroutine_Delete(cor); | |||
6 months ago |
653 |
if (need_start){ | |||
654 |
Coroutine_StopSystem(); | ||||
655 |
} | ||||
5 months ago |
656 |
// no failures, so... | |||
657 |
return false; | ||||
8 months ago |
4 |
658 |
} | ||
659 |
|||||
660 |
|||||
5 months ago |
661 |
static void Coroutine_FreeToIdle( | |||
662 |
Coroutine *cor, | ||||
8 months ago |
663 |
Coroutine_Start start | |||
664 |
){ | ||||
5 months ago |
665 |
assert(cor->state == Coroutine_Free); | |||
666 |
cor->state = Coroutine_Idle; | ||||
667 |
cor->start = start; | ||||
668 |
cor->value = NULL; | ||||
669 |
Link_Remove(&cor->link); | ||||
670 |
List_AddHead(&g_c->inactive, &cor->link); | ||||
8 months ago |
671 |
||||
5 months ago |
672 |
g_c->report.coroutines_created += 1; | |||
673 |
} | ||||
674 |
|||||
675 |
|||||
676 |
static void Coroutine_FreeToIdleSize( | ||||
677 |
Coroutine *cor, | ||||
678 |
Coroutine_Start start, | ||||
679 |
size_t size | ||||
680 |
){ | ||||
681 |
assert(!cor->guard); | ||||
682 |
cor->size = size; | ||||
683 |
cor->base = (unsigned char *)cor - g_c->gap_after; | ||||
684 |
cor->limit = cor->base - cor->size; | ||||
685 |
Coroutine_FreeToIdle(cor, start); | ||||
686 |
} | ||||
687 |
|||||
688 |
|||||
689 |
static Coroutine *Coroutine_New_Lock_Assumed( | ||||
690 |
size_t size, | ||||
691 |
Coroutine_Start start | ||||
692 |
){ | ||||
693 |
List_Link *link; | ||||
694 |
|||||
695 |
if (!g_c->tip){ | ||||
696 |
// no tip - time to create one | ||||
697 |
|||||
698 |
// we're the non-Coroutine which starts the Coroutine system. | ||||
699 |
// Add a single free block | ||||
700 |
if (!setjmp(g_c->chunk_allocated)){ | ||||
701 |
ReserveStackSpace(g_c, NULL, COROUTINE_STARTUP_STACK_SIZE, NULL); | ||||
5 months ago |
702 |
} | |||
5 months ago |
703 |
} | |||
704 |
|||||
5 months ago |
705 |
Coroutine *cor = NULL; | |||
5 months ago |
706 |
for (link = List_Begin(&g_c->free); Link_NextIsLink(link); link = Link_Next(link)){ | |||
5 months ago |
707 |
Coroutine *candidate = List_Link_Container(Coroutine, link, link); | |||
3 months ago |
708 |
assert(candidate->coroutines == g_c); | |||
5 months ago |
709 |
if (!candidate->guard) { | |||
5 months ago |
710 |
// this must be the tip | |||
5 months ago |
711 |
assert(candidate == g_c->tip); | |||
5 months ago |
712 |
||||
713 |
// If this is the only Coroutine in the system, go ahead and use it regardless of size. | ||||
714 |
// Note: there can only be one free block if there's no other sort of blocks as we merge on free | ||||
715 |
if (List_IsEmpty(&g_c->inactive) && | ||||
716 |
List_IsEmpty(&g_c->runable) && | ||||
717 |
List_IsEmpty(&g_c->waiting) ){ | ||||
718 |
if (g_c->stack_limit){ | ||||
5 months ago |
719 |
size_t available = (unsigned char *)candidate - g_c->stack_limit - g_c->gap_after; | |||
5 months ago |
720 |
size = available < size ? available : size; | |||
721 |
} | ||||
5 months ago |
722 |
Coroutine_FreeToIdleSize(candidate, start, size); | |||
723 |
return candidate; | ||||
6 months ago |
724 |
} | |||
5 months ago |
725 |
||||
726 |
// Not the only coroutine in the system - check size | ||||
727 |
if (g_c->stack_limit){ | ||||
728 |
// there's a limit - see what that space allows.... | ||||
5 months ago |
729 |
size_t available = (unsigned char *)candidate - g_c->stack_limit - g_c->gap_after; | |||
5 months ago |
730 |
||||
731 |
if (available < size){ | ||||
732 |
// not enough space for this coroutine | ||||
3 months ago |
733 |
// printf("Not enough stack space (A) %ld\n", available); | |||
5 months ago |
734 |
return NULL; | |||
735 |
} | ||||
736 |
|||||
4 months ago |
737 |
if (available < size + g_c->gap_before + g_c->gap_after + COROUTINE_MINIMUM_STACK_SIZE) { | |||
5 months ago |
738 |
// not enough space for another coroutine - use all the space for this one | |||
739 |
size = available; | ||||
740 |
} | ||||
6 months ago |
741 |
} | |||
5 months ago |
742 |
Coroutine_FreeToIdleSize(candidate, start, size); | |||
743 |
return candidate; | ||||
8 months ago |
4 |
744 |
} | ||
5 months ago |
745 |
if (candidate->size >= size && candidate > cor){ | |||
746 |
// chunk big enough, and a better choice than cor | ||||
747 |
cor = candidate; | ||||
748 |
} | ||||
749 |
} | ||||
750 |
|||||
751 |
if (cor){ | ||||
752 |
// - work out whether we're splitting or using the whole chunk | ||||
753 |
if (cor->size >= size + g_c->gap_before + g_c->gap_after + COROUTINE_MINIMUM_STACK_SIZE){ | ||||
754 |
// enough space for a second coroutine so split this free block | ||||
755 |
cor->size = size; | ||||
756 |
if (!setjmp(g_c->chunk_allocated)){ | ||||
757 |
longjmp(cor->buf, Chunk_Split); | ||||
5 months ago |
758 |
} | |||
759 |
} | ||||
5 months ago |
760 |
// cor now ready to use | |||
761 |
Coroutine_FreeToIdle(cor, start); | ||||
762 |
return cor; | ||||
8 months ago |
4 |
763 |
} | ||
764 |
|||||
5 months ago |
765 |
// No big-enough free blocks - check if there's space beyond the tip block | |||
766 |
|||||
4 months ago |
767 |
if (g_c->stack_limit) { | |||
768 |
ptrdiff_t available = (unsigned char *)g_c->tip->limit - g_c->gap_before - g_c->gap_after - g_c->stack_limit; | ||||
769 |
if (available < (ptrdiff_t)size){ | ||||
770 |
// no space for a new stack block | ||||
3 months ago |
771 |
// printf("Not enough stack space (B) %p %zu %zu %p %ld\n", g_c->tip->limit, g_c->gap_before, g_c->gap_after, g_c->stack_limit, available); | |||
772 |
// printf("g_c->tip = %p; tip-limit = %ld; tip->size = %zu\n", g_c->tip, (unsigned char *)g_c->tip - g_c->tip->limit, g_c->tip->size); | ||||
4 months ago |
773 |
return NULL; | |||
774 |
} | ||||
5 months ago |
775 |
} | |||
776 |
Coroutine *tip = g_c->tip; | ||||
777 |
Coroutine *me = g_c->active; | ||||
778 |
if (tip == me) { | ||||
779 |
if (!setjmp(g_c->chunk_allocated)){ | ||||
780 |
ReserveStackSpace(g_c, me, StackTopNow() - me->limit, NULL); | ||||
781 |
} | ||||
782 |
} else { | ||||
783 |
if (!setjmp(g_c->chunk_allocated)){ | ||||
784 |
longjmp(tip->buf, Chunk_Create); | ||||
785 |
} | ||||
786 |
} | ||||
787 |
|||||
5 months ago |
788 |
cor = List_Link_Container(Coroutine, link, List_GetTail(&g_c->free)); | |||
8 months ago |
4 |
789 |
assert(cor->state == Coroutine_Free); | ||
5 months ago |
790 |
cor->size = size; | |||
791 |
cor->limit = (unsigned char *)cor - g_c->gap_after - size; | ||||
8 months ago |
4 |
792 |
cor->state = Coroutine_Idle; | ||
793 |
cor->start = start; | ||||
794 |
cor->value = NULL; | ||||
5 months ago |
795 |
Link_Remove(&cor->link); | |||
796 |
List_AddHead(&g_c->inactive, &cor->link); | ||||
8 months ago |
4 |
797 |
|||
5 months ago |
798 |
g_c->report.coroutines_created += 1; | |||
799 |
return cor; | ||||
800 |
} | ||||
8 months ago |
801 |
||||
5 months ago |
802 |
||||
803 |
Coroutine *Coroutine_New( | ||||
804 |
size_t stack, | ||||
805 |
Coroutine_Start start | ||||
806 |
){ | ||||
807 |
assert(g_c); | ||||
808 |
assert((g_c->state == Coroutines_Started && List_IsEmpty(&g_c->inactive)) || g_c->state == Coroutines_Active); | ||||
809 |
assert(!Coroutine_StackHasOverrun()); | ||||
810 |
|||||
811 |
_Cor_Mutex_Lock(&g_c->mutex); | ||||
812 |
|||||
813 |
Coroutine *cor = Coroutine_New_Lock_Assumed(stack, start); | ||||
814 |
|||||
815 |
if (cor && cor->size > g_c->report.largest_stack){ | ||||
816 |
g_c->report.largest_stack = cor->size; | ||||
817 |
} | ||||
818 |
|||||
819 |
_Cor_Mutex_Unlock(&g_c->mutex); | ||||
820 |
|||||
8 months ago |
4 |
821 |
return cor; | ||
822 |
} | ||||
823 |
|||||
8 months ago |
824 |
||||
825 |
void Coroutine_Delete( | ||||
826 |
Coroutine *cor | ||||
827 |
){ | ||||
5 months ago |
828 |
assert(!Coroutine_StackHasOverrun()); | |||
5 months ago |
829 |
if (cor){ | |||
830 |
Coroutines *cors = cor->coroutines; | ||||
831 |
_Cor_Mutex_Lock(&cors->mutex); | ||||
832 |
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Complete); | ||||
833 |
cor->state = Coroutine_Free; | ||||
5 months ago |
834 |
Link_Remove(&cor->link); | |||
835 |
|||||
5 months ago |
836 |
// insert into free list | |||
837 |
List_AddHead(&cors->free, &cor->link); | ||||
5 months ago |
838 |
||||
839 |
// Check for merge with following Coroutine | ||||
5 months ago |
840 |
List_Link *link = Link_Next(&cor->all_link); | |||
5 months ago |
841 |
if (Link_NextIsLink(link)){ | |||
5 months ago |
842 |
Coroutine *listcor = List_Link_Container(Coroutine, all_link, link); | |||
843 |
if (listcor->state == Coroutine_Free){ | ||||
5 months ago |
844 |
// merge | |||
5 months ago |
845 |
cor->size += cor->limit - listcor->limit; | |||
5 months ago |
846 |
cor->limit = listcor->limit; | |||
847 |
cor->guard = listcor->guard; | ||||
5 months ago |
848 |
Link_Remove(&listcor->all_link); | |||
5 months ago |
849 |
Link_Remove(&listcor->link); | |||
5 months ago |
850 |
if (g_c->tip == listcor){ | |||
851 |
g_c->tip = cor; | ||||
852 |
} | ||||
5 months ago |
853 |
} | |||
854 |
} | ||||
855 |
|||||
856 |
// check for merge with prev coroutine | ||||
5 months ago |
857 |
link = Link_Prev(&cor->all_link); | |||
5 months ago |
858 |
if (Link_PrevIsLink(link)){ | |||
5 months ago |
859 |
Coroutine *listcor = List_Link_Container(Coroutine, all_link, link); | |||
860 |
if (listcor->state == Coroutine_Free){ | ||||
5 months ago |
861 |
// merge | |||
5 months ago |
862 |
listcor->size += listcor->limit - cor->limit; | |||
5 months ago |
863 |
listcor->limit = cor->limit; | |||
864 |
listcor->guard = cor->guard; | ||||
5 months ago |
865 |
Link_Remove(&cor->all_link); | |||
5 months ago |
866 |
Link_Remove(&cor->link); | |||
5 months ago |
867 |
if (g_c->tip == cor){ | |||
868 |
g_c->tip = listcor; | ||||
869 |
} | ||||
5 months ago |
870 |
} | |||
871 |
} | ||||
872 |
|||||
5 months ago |
873 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
874 |
} | ||||
8 months ago |
4 |
875 |
} | ||
876 |
|||||
8 months ago |
877 |
||||
7 months ago |
878 |
// Coroutine_Continue, assuming the mutex is claimed | |||
3 months ago |
879 |
// return false for success, true for something went wrong | |||
3 months ago |
880 |
static bool _Coroutine_Continue( | |||
881 |
Coroutines *cors, | ||||
8 months ago |
882 |
Coroutine *cor, | |||
883 |
void *value, | ||||
884 |
bool early | ||||
885 |
){ | ||||
3 months ago |
886 |
if (cor->state == Coroutine_Free || cor->state == Coroutine_Complete){ | |||
887 |
return true; | ||||
888 |
} | ||||
889 |
if (cor->state == Coroutine_Running){ | ||||
890 |
// already running | ||||
891 |
return false; | ||||
892 |
} | ||||
8 months ago |
4 |
893 |
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Waiting); | ||
894 |
cor->entry_param = value; | ||||
895 |
cor->state = Coroutine_Running; | ||||
5 months ago |
896 |
Link_Remove(&cor->link); | |||
8 months ago |
4 |
897 |
if ( early ) { | ||
8 months ago |
898 |
List_AddHead(&cors->runable, &cor->link); | |||
8 months ago |
4 |
899 |
} else { | ||
8 months ago |
900 |
List_AddTail(&cors->runable, &cor->link); | |||
8 months ago |
4 |
901 |
} | ||
7 months ago |
902 |
_Cor_Mutex_Unlock(&cors->waiting_mutex); | |||
3 months ago |
903 |
return false; | |||
7 months ago |
904 |
} | |||
905 |
|||||
906 |
|||||
3 months ago |
907 |
bool Coroutine_Continue( | |||
7 months ago |
908 |
Coroutine *cor, | |||
909 |
void *value, | ||||
910 |
bool early | ||||
911 |
){ | ||||
5 months ago |
912 |
assert(!Coroutine_StackHasOverrun()); | |||
7 months ago |
913 |
Coroutines *cors = cor->coroutines; | |||
7 months ago |
914 |
_Cor_Mutex_Lock(&cors->mutex); | |||
3 months ago |
915 |
bool ret = _Coroutine_Continue(cors, cor, value, early); | |||
7 months ago |
916 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
3 months ago |
917 |
return ret; | |||
8 months ago |
4 |
918 |
} | ||
919 |
|||||
8 months ago |
920 |
||||
921 |
void *Coroutine_Yield( | ||||
922 |
void *value, | ||||
923 |
Coroutine_YieldCallback on_yield, | ||||
8 months ago |
924 |
void *yield_me | |||
8 months ago |
925 |
){ | |||
5 months ago |
926 |
assert(g_c); | |||
927 |
Coroutine *me = g_c->active; | ||||
6 months ago |
928 |
assert(me); | |||
5 months ago |
929 |
assert(!Coroutine_StackHasOverrun()); | |||
8 months ago |
930 |
||||
5 months ago |
931 |
_Cor_Mutex_Lock(&g_c->mutex); | |||
8 months ago |
932 |
Coroutines *cors = me->coroutines; | |||
5 months ago |
933 |
assert(me && me->state == Coroutine_Running && cors == g_c); | |||
6 months ago |
934 |
me->stack_top = StackTopNow(); | |||
8 months ago |
4 |
935 |
me->value = value; | ||
936 |
me->state = Coroutine_Waiting; | ||||
8 months ago |
937 |
||||
5 months ago |
938 |
Link_Remove(&me->link); | |||
7 months ago |
939 |
if (!List_IsEmpty(&cors->runable)){ | |||
7 months ago |
940 |
_Cor_Mutex_Unlock(&cors->waiting_mutex); | |||
7 months ago |
941 |
} | |||
8 months ago |
942 |
List_AddTail(&cors->waiting, &me->link); | |||
8 months ago |
943 |
||||
8 months ago |
944 |
switch (setjmp(me->buf)){ | |||
945 |
case Chunk_Initial: | ||||
7 months ago |
946 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
947 |
on_yield(yield_me); | |||
8 months ago |
4 |
948 |
Coroutine_RunNext(); | ||
6 months ago |
949 |
assert(false); | |||
8 months ago |
950 |
case Chunk_Create: | |||
5 months ago |
951 |
assert(me == g_c->tip); | |||
952 |
ReserveStackSpace(me->coroutines, me, me->stack_top - me->limit, NULL); | ||||
8 months ago |
953 |
assert(false); | |||
954 |
case Chunk_Enter: | ||||
955 |
// arrive here with mutex locked | ||||
8 months ago |
956 |
cors->active = me; | |||
5 months ago |
957 |
assert(!Coroutine_StackHasOverrun()); | |||
8 months ago |
958 |
// when we return here - we are running again | |||
959 |
assert(me->state == Coroutine_Running); | ||||
960 |
void *res = me->entry_param; | ||||
7 months ago |
961 |
_Cor_Mutex_Unlock(&cors->mutex); | |||
8 months ago |
962 |
return res; | |||
8 months ago |
4 |
963 |
} | ||
8 months ago |
964 |
return NULL; | |||
8 months ago |
4 |
965 |
} | ||
966 |
|||||
8 months ago |
967 |
||||
968 |
void *Coroutine_GetValue( | ||||
969 |
Coroutine *cor | ||||
970 |
){ | ||||
8 months ago |
4 |
971 |
return cor->value; | ||
972 |
} | ||||
973 |
|||||
8 months ago |
974 |
||||
7 months ago |
975 |
Coroutine *Coroutine_GetActive(void) | |||
8 months ago |
4 |
976 |
{ | ||
5 months ago |
977 |
return g_c ? g_c->active : NULL; | |||
8 months ago |
4 |
978 |
} | ||
979 |
|||||
8 months ago |
980 |
||||
7 months ago |
981 |
intptr_t Coroutine_GetStackHeadroom(void){ | |||
5 months ago |
982 |
assert(g_c); | |||
983 |
assert(!Coroutine_StackHasOverrun()); | ||||
984 |
Coroutine *me = g_c->active; | ||||
6 months ago |
985 |
if (!me){ | |||
986 |
// no active coroutine | ||||
5 months ago |
987 |
unsigned char *stack_limit = g_c->stack_limit; | |||
6 months ago |
988 |
if (stack_limit){ | |||
989 |
return StackTopNow() - stack_limit; | ||||
990 |
} else { | ||||
991 |
// no information where the stack ends - return something | ||||
5 months ago |
992 |
return COROUTINE_MINIMUM_STACK_SIZE; | |||
6 months ago |
993 |
} | |||
994 |
} | ||||
5 months ago |
995 |
return StackTopNow() - me->limit; | |||
8 months ago |
996 |
} | |||
997 |
|||||
998 |
|||||
6 months ago |
999 |
// This is used to avoid compiler warnings about returning the address of a local | |||
1000 |
static inline void *StopAddressWarnings(void *p) | ||||
1001 |
{ | ||||
1002 |
return p; | ||||
8 months ago |
1003 |
} | |||
1004 |
|||||
1005 |
|||||
6 months ago |
1006 |
void *Coroutine_GetStackHWM(void){ | |||
5 months ago |
1007 |
assert(g_c); | |||
1008 |
assert(g_c->state == Coroutines_Active); | ||||
1009 |
assert(!Coroutine_StackHasOverrun()); | ||||
6 months ago |
1010 |
// Find where the guards end | |||
1011 |
unsigned char *guard; | ||||
5 months ago |
1012 |
for (guard = g_c->active->guard; Check_Guard(guard); guard += 4){ | |||
6 months ago |
1013 |
// do nothing | |||
1014 |
} | ||||
1015 |
return guard; | ||||
1016 |
} | ||||
7 months ago |
1017 |
||||
1018 |
|||||
6 months ago |
1019 |
void Coroutine_ClearStackForHWM(void){ | |||
5 months ago |
1020 |
assert(g_c); | |||
1021 |
assert(g_c->state == Coroutines_Active); | ||||
1022 |
assert(!Coroutine_StackHasOverrun()); | ||||
6 months ago |
1023 |
unsigned char *end = StackTopNow() - GUARD_PATTERN_SIZE; | |||
5 months ago |
1024 |
for (unsigned char *guard = g_c->active->guard+GUARD_PATTERN_SIZE; guard <= end; guard += GUARD_PATTERN_SIZE){ | |||
6 months ago |
1025 |
Apply_Guard(guard); | |||
6 months ago |
1026 |
} | |||
1027 |
} | ||||
1028 |
|||||
1029 |
|||||
5 months ago |
1030 |
static bool Coroutine_CanStartCoroutine_Lock_Assumed( | |||
1031 |
size_t size | ||||
1032 |
){ | ||||
1033 |
if (!g_c->stack_limit){ | ||||
6 months ago |
1034 |
return true; | |||
1035 |
} | ||||
6 months ago |
1036 |
||||
5 months ago |
1037 |
if (!g_c->tip){ | |||
1038 |
return true; | ||||
1039 |
} | ||||
1040 |
|||||
1041 |
if (g_c->tip->state == Coroutine_Free){ | ||||
1042 |
// last block is free | ||||
1043 |
if ((unsigned char *)g_c->tip - g_c->stack_limit >= (ptrdiff_t)(g_c->gap_after + size)){ | ||||
1044 |
// enough room in free block, which is the last block | ||||
1045 |
return true; | ||||
1046 |
} | ||||
1047 |
} else { | ||||
1048 |
// last block is allocated | ||||
1049 |
if (g_c->tip->limit - g_c->stack_limit >= (ptrdiff_t)(g_c->gap_before + g_c->gap_after + size)){ | ||||
1050 |
// enough room after the last block, which is allocated | ||||
1051 |
return true; | ||||
1052 |
} | ||||
1053 |
} | ||||
1054 |
|||||
1055 |
// not enough room between allocated blocks and stack limit, so check free list | ||||
1056 |
List_Link *link; | ||||
1057 |
for (link = List_Begin(&g_c->free); Link_NextIsLink(link); link = Link_Next(link)){ | ||||
1058 |
Coroutine *cor = List_Link_Container(Coroutine, link, link); | ||||
1059 |
if (cor->size >= size){ | ||||
1060 |
return true; | ||||
1061 |
} | ||||
1062 |
} | ||||
1063 |
|||||
1064 |
return false; | ||||
6 months ago |
1065 |
} | |||
1066 |
|||||
1067 |
|||||
5 months ago |
1068 |
bool Coroutine_CanStartCoroutine( | |||
1069 |
size_t size | ||||
1070 |
){ | ||||
3 months ago |
1071 |
assert(g_c); | |||
1072 |
assert(g_c->state == Coroutines_Started || g_c->state == Coroutines_Active); | ||||
1073 |
assert(!Coroutine_StackHasOverrun()); | ||||
1074 |
|||||
5 months ago |
1075 |
_Cor_Mutex_Lock(&g_c->mutex); | |||
1076 |
|||||
1077 |
bool result = Coroutine_CanStartCoroutine_Lock_Assumed(size); | ||||
1078 |
|||||
1079 |
_Cor_Mutex_Unlock(&g_c->mutex); | ||||
1080 |
|||||
1081 |
return result; | ||||
1082 |
} | ||||
1083 |
|||||
7 months ago |
1084 |
void *Coroutine_GetCStackTop(void){ | |||
5 months ago |
1085 |
assert(!Coroutine_StackHasOverrun()); | |||
1086 |
if ((g_c->state == Coroutines_Started || g_c->state == Coroutines_Active) && g_c->tip != g_c->active) { | ||||
1087 |
return g_c->tip->stack_top; | ||||
6 months ago |
1088 |
} else { | |||
1089 |
return StackTopNow(); | ||||
1090 |
} | ||||
8 months ago |
1091 |
} | |||
1092 |
|||||
1093 |
|||||
6 months ago |
1094 |
static unsigned char *StackTopNow(void){ | |||
6 months ago |
1095 |
unsigned char here[4]; | |||
1096 |
return StopAddressWarnings(here); | ||||
1097 |
} | ||||
1098 |
|||||
1099 |
|||||
8 months ago |
1100 |
struct Coroutine_ChainParam { | |||
1101 |
Coroutine_Start start; | ||||
1102 |
void *value; | ||||
1103 |
Coroutine *ret; | ||||
1104 |
}; | ||||
1105 |
|||||
1106 |
|||||
1107 |
static void *Coroutine_ChainFn( | ||||
1108 |
void *param | ||||
1109 |
){ | ||||
1110 |
struct Coroutine_ChainParam *params = (struct Coroutine_ChainParam *)param; | ||||
1111 |
Coroutine_Continue(params->ret, params->start(params->value), true); | ||||
1112 |
return NULL; | ||||
1113 |
} | ||||
1114 |
|||||
1115 |
|||||
1116 |
static void Coroutine_ChainYield( | ||||
1117 |
void *unused | ||||
1118 |
){ | ||||
1119 |
(void)unused; | ||||
1120 |
} | ||||
1121 |
|||||
1122 |
|||||
5 months ago |
1123 |
bool Coroutine_Chain( | |||
5 months ago |
1124 |
size_t size, | |||
8 months ago |
1125 |
Coroutine_Start start, | |||
5 months ago |
1126 |
void *value, | |||
1127 |
void **result | ||||
8 months ago |
1128 |
){ | |||
7 months ago |
1129 |
assert(Check_Guard(Coroutine_GetActive()->guard)); | |||
5 months ago |
1130 |
Coroutine *cor = Coroutine_New(size, Coroutine_ChainFn); | |||
5 months ago |
1131 |
if (!cor){ | |||
1132 |
// failed | ||||
1133 |
return true; | ||||
1134 |
} | ||||
8 months ago |
1135 |
struct Coroutine_ChainParam params = { | |||
1136 |
start, | ||||
1137 |
value, | ||||
1138 |
Coroutine_GetActive() | ||||
1139 |
}; | ||||
1140 |
Coroutine_Continue(cor, ¶ms, true); | ||||
1141 |
void *res = Coroutine_Yield(NULL, Coroutine_ChainYield, NULL); | ||||
1142 |
Coroutine_Delete(cor); | ||||
5 months ago |
1143 |
if (result){ | |||
1144 |
*result = res; | ||||
1145 |
} | ||||
1146 |
// success! | ||||
1147 |
return false; | ||||
8 months ago |
1148 |
} | |||
1149 |
|||||
1150 |
|||||
8 months ago |
1151 |
bool Coroutine_IsRunning( | |||
1152 |
Coroutine *cor | ||||
1153 |
) | ||||
8 months ago |
4 |
1154 |
{ | ||
8 months ago |
1155 |
int state = cor->state; | |||
1156 |
return state == Coroutine_Running || state == Coroutine_Waiting; | ||||
8 months ago |
4 |
1157 |
} | ||
8 months ago |
1158 |
||||
1159 |
|||||
6 months ago |
1160 |
bool Coroutine_IsComplete( | |||
1161 |
Coroutine *cor | ||||
1162 |
) | ||||
1163 |
{ | ||||
1164 |
int state = cor->state; | ||||
1165 |
return state == Coroutine_Complete; | ||||
1166 |
} | ||||
1167 |
|||||
1168 |
|||||
7 months ago |
1169 |
bool Coroutine_IsStarted(void){ | |||
5 months ago |
1170 |
return g_c && (g_c->state == Coroutines_Active || g_c->state == Coroutines_Started); | |||
8 months ago |
1171 |
} | |||
5 months ago |
1172 |
||||
1173 |
void _Coroutine_Dump(void){ | ||||
1174 |
char *state_to_text[] = { | ||||
1175 |
"Free", | ||||
1176 |
"Idle", | ||||
1177 |
"Running", | ||||
1178 |
"Waiting", | ||||
1179 |
"Complete" | ||||
1180 |
}; | ||||
1181 |
unsigned idx = 0; | ||||
1182 |
List_Link *link; | ||||
1183 |
for (link = List_Begin(&g_c->all); Link_NextIsLink(link); link = Link_Next(link)){ | ||||
1184 |
Coroutine *cor = List_Link_Container(Coroutine, all_link, link); | ||||
1185 |
printf("%d) %p (%s) %ld%s\n", idx++, cor, state_to_text[cor->state], cor->size, cor == g_c->tip ? " (TIP)" : ""); | ||||
1186 |
} | ||||
1187 |
} | ||||
1188 |