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