0 branches 0 tags
93 104
105
Coroutine chaining is hidden frmo the caller
on 2:38 PM Jul 24 2026
chaining_demo.c
104
105
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#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, &param1) && Generator_Next(&gen2, &param2)){
}
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#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>
#include <assert.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, &param1) && Generator_Next(&gen2, &param2)){
}
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;
}
void on_yield(void *param){
(void)param;
}
void *chaintest2_inner(
void *param
){
Coroutine *root = param;
Coroutine_Continue(root, NULL, true);
Coroutine_Yield((void *)456, on_yield, NULL);
assert(Coroutine_GetValue(root) == (void *)3);
return NULL;
}
void *chaintest2_outer(
void *param
){
Coroutine *root = param;
assert(Coroutine_GetValue(root) == (void *)1);
Coroutine_Continue(root, NULL, true);
Coroutine_Yield((void *)123, on_yield, NULL);
assert(Coroutine_GetValue(root) == (void *)2);
Coroutine_Chain(DEMO_STACK_SIZE, 0, chaintest2_inner, root, NULL);
Coroutine_Continue(root, NULL, true);
return (void *)789;
}
// This test checks the function of chaining:
// * Yielding inside a chained coroutine stores the value with
// the outermost/root coroutine of the chain.
// * Continuing the root coroutine of a chain continues the chain
// tip coroutine (ie the one which had yielded last)
void *chaintest2(
void *param
){
(void)param;
Coroutine *me = Coroutine_GetActive();
Coroutine *cor = Coroutine_New(DEMO_STACK_SIZE, 0, chaintest2_outer);
Coroutine_Continue(cor, me, true);
Coroutine_Yield((void *)1, on_yield, NULL);
assert(Coroutine_IsRunning(cor));
assert(Coroutine_GetValue(cor) == (void *)123);
Coroutine_Continue(cor, me, true);
Coroutine_Yield((void *)2, on_yield, NULL);
assert(Coroutine_IsRunning(cor));
assert(Coroutine_GetValue(cor) == (void *)456);
Coroutine_Continue(cor, me, true);
Coroutine_Yield((void *)3, on_yield, NULL);
assert(!Coroutine_IsRunning(cor));
assert(Coroutine_GetValue(cor) == (void *)789);
Coroutine_Delete(cor);
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Coroutine_Run(DEMO_STACK_SIZE, 0, chaintest, NULL, NULL);
Coroutine_Run(DEMO_STACK_SIZE, 0, chaintest2, NULL, NULL);
return 0;
}