| 1 | #include <stdio.h> |
| 2 | #include <stddef.h> |
| 3 | #include "coroutine.h" |
| 4 | #include "generator.h" |
| 5 | #include "asleep.h" |
| 6 | #include "task.h" |
| 7 | |
| 8 | #include <dirent.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | #define DEMO_STACK_SIZE (8192*sizeof(void *)) |
| 14 | |
| 15 | void *chaindeeper(void *param){ |
| 16 | // enough headroom for printf on an Intel Mac - your system may be different |
| 17 | if (Coroutine_GetStackHeadroom() < 2000){ |
| 18 | void *result; |
| 19 | bool fail = Coroutine_Chain(DEMO_STACK_SIZE, chaindeeper, param, &result); |
| 20 | return fail ? NULL : result; |
| 21 | } |
| 22 | printf("%ld %ld\n", (long)param, Coroutine_GetStackHeadroom()); |
| 23 | long depth = (long)param; |
| 24 | if (depth > 10000){ |
| 25 | return NULL; |
| 26 | } |
| 27 | return chaindeeper((void *)(depth + 1)); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | void *chaintest( |
| 32 | void *param |
| 33 | ){ |
| 34 | (void)param; |
| 35 | |
| 36 | chaindeeper((void *)0); |
| 37 | |
| 38 | return param; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | int main(int argc, char *argv[]) { |
| 43 | (void)argc; |
| 44 | (void)argv; |
| 45 | |
| 46 | Coroutine_StartSystem(); |
| 47 | Coroutine_Run(DEMO_STACK_SIZE, chaintest, NULL, NULL); |
| 48 | Coroutine_Report report = Coroutine_StopSystem(); |
| 49 | |
| 50 | printf("%d routines using a pool of %d, min headroom %zu\n", report.coroutines_created, report.coroutines_pool_size, report.lowest_headroom); |
| 51 | } |
| 52 | |