1 contributor
63 lines1.7 KB
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
15void *stack_inner(void *param){
16 printf("%td) Inner headroom (big stack now) = %td\n", (ptrdiff_t)param, Coroutine_GetStackHeadroom());
17 // Stack may be trimmed here...
18 Generator_Yield(0);
19 printf("%td) Inner headroom (stack may be trimmed) = %td\n", (ptrdiff_t)param, Coroutine_GetStackHeadroom());
20 return NULL;
21}
22
23void *stack_outer(void *param){
24 printf("%td) Outer headroom (start) = %td\n", (ptrdiff_t)param, Coroutine_GetStackHeadroom());
25 Generator_Yield(0);
26 printf("%td) Outer headroom (may be trimmed) = %td\n", (ptrdiff_t)param, Coroutine_GetStackHeadroom());
27 Coroutine_CallWithMaxStack(stack_inner, param, NULL);
28 printf("%td) Outer headroom (return from call) = %td\n", (ptrdiff_t)param, Coroutine_GetStackHeadroom());
29 return NULL;
30}
31
32
33void *chaintest(
34 void *param
35){
36 (void)param;
37
38 // Need to run two coroutines so that at least one of their stacks is limited, and so
39 // needs to chain
40 Generator gen1;
41 Generator_ctor(&gen1, DEMO_STACK_SIZE, 0, stack_outer, (void *)0);
42 Generator gen2;
43 Generator_ctor(&gen2, DEMO_STACK_SIZE, 0, stack_outer, (void *)1);
44 void *param1;
45 void *param2;
46 while(Generator_Next(&gen1, &param1) && Generator_Next(&gen2, &param2)){
47 }
48 Generator_dtor(&gen2);
49 Generator_dtor(&gen1);
50
51 return param;
52}
53
54
55int main(int argc, char *argv[]) {
56 (void)argc;
57 (void)argv;
58
59 Coroutine_Run(DEMO_STACK_SIZE, 0, chaintest, NULL, NULL);
60
61 return 0;
62}
63