| #include <stdio.h> |
| #include <stddef.h> |
| #include "coroutine.h" |
| #include "generator.h" |
| #include "asleep.h" |
| #include "task.h" |
|
| #include <dirent.h> |
| #include <string.h> |
| #include <stdlib.h> |
| #include <time.h> |
|
| #define DEMO_STACK_SIZE (8192*sizeof(void *)) |
|
| void *chaindeeper(void *param){ |
| // enough headroom for printf on an Intel Mac - your system may be different |
| if (Coroutine_GetStackHeadroom() < 2000){ |
| void *result; |
| bool fail = Coroutine_Chain(DEMO_STACK_SIZE, 0, chaindeeper, param, &result); |
| return fail ? NULL : result; |
| } |
| printf("%ld %ld\n", (long)param, Coroutine_GetStackHeadroom()); |
| bool domore = Generator_Yield(param); |
| if (domore){ |
| long depth = (long)param; |
| if (depth > 1000){ |
| return NULL; |
| } |
| return chaindeeper((void *)(depth + 1)); |
| } |
| return NULL; |
| } |
|
|
| void *chaintest( |
| void *param |
| ){ |
| (void)param; |
|
| // Need to run two coroutines so that at least one of their stacks is limited, and so |
| // needs to chain |
| Generator gen1; |
| Generator_ctor(&gen1, DEMO_STACK_SIZE, 0, chaindeeper, 0); |
| Generator gen2; |
| Generator_ctor(&gen2, DEMO_STACK_SIZE, 0, chaindeeper, 0); |
| void *param1; |
| void *param2; |
| while(Generator_Next(&gen1, ¶m1) && Generator_Next(&gen2, ¶m2)){ |
| } |
| Generator_dtor(&gen2); |
| Generator_dtor(&gen1); |
|
| Coroutine_Report report = Coroutine_GetReport(); |
| printf("%d routines using a pool of %d, min headroom %zu\n", report.coroutines_created, report.coroutines_pool_size, report.lowest_headroom); |
|
| return param; |
| } |
|
|
| int main(int argc, char *argv[]) { |
| (void)argc; |
| (void)argv; |
|
| Coroutine_Run(DEMO_STACK_SIZE, 0, chaintest, NULL, NULL); |
|
| return 0; |
| } |
|