0 branches 0 tags
66 67
68
69 73
Variable stack Coroutines WIP
on 3:07 PM Dec 23 2025
async_demo.c
67
68
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
#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>
typedef struct asynctestpartparam {
char *name;
float delay;
int count;
} asynctestpartparam;
bool asynctestpart(void *param, void **res){
(void)res;
asynctestpartparam *spec = (asynctestpartparam *)param;
printf("%s started\n", spec->name);
for (int i=0; i < spec->count; ++i){
ASleep(spec->delay, NULL);
printf("%s %d\n", spec->name, i);
}
return false;
}
bool asynctest(void *param, void **res){
(void)param;
(void)res;
printf("async test started\n");
asynctestpartparam task1param = {
"First",
0.5f,
5
};
Task task1;
Task_ctor(&task1, asynctestpart, &task1param);
printf("task1 going\n");
asynctestpartparam task2param = {
"Second",
0.75f,
8
};
Task *task2 = Task_New(asynctestpart, &task2param);
printf("task2 going\n");
bool canceled1 = Task_Await(&task1, NULL);
bool canceled2 = Task_Await(task2, NULL);
Task_Delete(task2);
Task_dtor(&task1);
printf("Tasks complete %d %d\n", canceled1, canceled2);
return canceled1 | canceled2;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
ASleep_StartSystem();
Coroutine_StartSystem();
void *res = NULL;
bool canceled = Task_Run(asynctest, NULL, &res);
Coroutine_StopSystem();
ASleep_StopSystem();
printf("Async result %d:%p\n", canceled, res);
}
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
#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 *))
typedef struct asynctestpartparam {
char *name;
float delay;
int count;
} asynctestpartparam;
bool asynctestpart(void *param, void **res){
(void)res;
asynctestpartparam *spec = (asynctestpartparam *)param;
printf("%s started\n", spec->name);
for (int i=0; i < spec->count; ++i){
ASleep(spec->delay, NULL);
printf("%s %d\n", spec->name, i);
}
return false;
}
bool asynctest(void *param, void **res){
(void)param;
(void)res;
printf("async test started\n");
asynctestpartparam task1param = {
"First",
0.5f,
5
};
Task task1;
Task_ctor(&task1, DEMO_STACK_SIZE, asynctestpart, &task1param);
printf("task1 going\n");
asynctestpartparam task2param = {
"Second",
0.75f,
8
};
Task *task2 = Task_New(DEMO_STACK_SIZE, asynctestpart, &task2param);
printf("task2 going\n");
bool canceled1 = Task_Await(&task1, NULL);
bool canceled2 = Task_Await(task2, NULL);
Task_Delete(task2);
Task_dtor(&task1);
printf("Tasks complete %d %d\n", canceled1, canceled2);
return canceled1 | canceled2;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
ASleep_StartSystem();
Coroutine_StartSystem();
void *res = NULL;
bool canceled = Task_Run(DEMO_STACK_SIZE, asynctest, NULL, &res);
Coroutine_StopSystem();
ASleep_StopSystem();
printf("Async result %d:%p\n", canceled, res);
}
chaining_demo.c
67
68
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
#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>
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(chaindeeper, param, &result);
return fail ? NULL : result;
}
printf("%ld %ld\n", (long)param, Coroutine_GetStackHeadroom());
long depth = (long)param;
if (depth > 10000){
return NULL;
}
return chaindeeper((void *)(depth + 1));
}
void *chaintest(
void *param
){
(void)param;
chaindeeper((void *)0);
return param;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Coroutine_StartSystem();
Coroutine_Run(chaintest, NULL, NULL);
Coroutine_Report report = Coroutine_StopSystem();
printf("%d routines using a pool of %d, min headroom %d\n", report.coroutines_created, report.coroutines_pool_size, report.lowest_headroom);
}
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
#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, chaindeeper, param, &result);
return fail ? NULL : result;
}
printf("%ld %ld\n", (long)param, Coroutine_GetStackHeadroom());
long depth = (long)param;
if (depth > 10000){
return NULL;
}
return chaindeeper((void *)(depth + 1));
}
void *chaintest(
void *param
){
(void)param;
chaindeeper((void *)0);
return param;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Coroutine_StartSystem();
Coroutine_Run(DEMO_STACK_SIZE, chaintest, NULL, NULL);
Coroutine_Report report = Coroutine_StopSystem();
printf("%d routines using a pool of %d, min headroom %zu\n", report.coroutines_created, report.coroutines_pool_size, report.lowest_headroom);
}
generator_demo.c
67
68
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
#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>
void *yield_files(void *param){
bool domore = true;
char *path = param;
DIR *d;
struct dirent *dir;
int pathlen = strlen(path);
d = opendir(path);
if (d) {
while (domore && (dir = readdir(d)) != NULL) {
int blklen = pathlen + 1 + strlen(dir->d_name) + 1;
char *r = malloc(blklen);
snprintf(r, blklen, "%s/%s", path, dir->d_name);
domore = Generator_Yield(r);
if (domore && dir->d_type == DT_DIR) {
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) {
r = malloc(blklen);
snprintf(r, blklen, "%s/%s", path, dir->d_name);
domore = yield_files(r);
free(r);
}
}
}
closedir(d);
}
return (void *)domore;
}
void *gentest(void *param){
Generator gen;
Generator_ctor(&gen, yield_files, (char *)param);
int count = 0;
while(Generator_Next(&gen, &param)){
printf("%d) %s\n", count, (char *)param);
free(param);
if (++count>16000) break;
}
Generator_dtor(&gen);
return param;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Coroutine_Run(gentest, "..", 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
#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 *yield_files(void *param){
bool domore = true;
char *path = param;
DIR *d;
struct dirent *dir;
int pathlen = strlen(path);
d = opendir(path);
if (d) {
while (domore && (dir = readdir(d)) != NULL) {
int blklen = pathlen + 1 + strlen(dir->d_name) + 1;
char *r = malloc(blklen);
snprintf(r, blklen, "%s/%s", path, dir->d_name);
domore = Generator_Yield(r);
if (domore && dir->d_type == DT_DIR) {
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) {
r = malloc(blklen);
snprintf(r, blklen, "%s/%s", path, dir->d_name);
domore = yield_files(r);
free(r);
}
}
}
closedir(d);
}
return (void *)domore;
}
void *gentest(void *param){
Generator gen;
Generator_ctor(&gen, DEMO_STACK_SIZE, yield_files, (char *)param);
int count = 0;
while(Generator_Next(&gen, &param)){
printf("%d) %s\n", count, (char *)param);
free(param);
if (++count>16000) break;
}
Generator_dtor(&gen);
return param;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Coroutine_Run(DEMO_STACK_SIZE, gentest, "..", NULL);
return 0;
}
low_stack.c
67
68
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>
Coroutine *cor_main;
typedef struct {
intptr_t headroom;
bool canstartcoroutine;
} TestResult;
void *stacktest(
void *param
){
TestResult *testresult = (TestResult *)param;
if (Coroutine_CanStartCoroutine()){
Coroutine *cor = Coroutine_New(stacktest);
Coroutine_Delete(cor);
}
testresult->headroom = Coroutine_GetStackHeadroom();
testresult->canstartcoroutine = Coroutine_CanStartCoroutine();
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
unsigned char *stack_now = (unsigned char *)&argc;
printf("Various stack headrooms:\n");
intptr_t limitnocoroutine;
TestResult testresult;
// what stack do we get with no stack limit set
Coroutine_StartSystem();
cor_main = Coroutine_New(stacktest);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run_Coroutine(cor_main, &testresult);
Coroutine_Delete(cor_main);
printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
Coroutine_StopSystem();
// what stack do we get with an undersize stack limit
for (intptr_t stacksize = COROUTINE_STACK_SIZE/2; stacksize < COROUTINE_STACK_SIZE*4; stacksize += COROUTINE_STACK_SIZE/2){
Coroutine_StartSystem();
Coroutine_SetStackLimit(stack_now - stacksize);
cor_main = Coroutine_New(stacktest);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run_Coroutine(cor_main, &testresult);
Coroutine_Delete(cor_main);
printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
Coroutine_StopSystem();
}
}
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
#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 *))
Coroutine *cor_main;
typedef struct {
intptr_t headroom;
bool canstartcoroutine;
} TestResult;
void *stacktest(
void *param
){
TestResult *testresult = (TestResult *)param;
if (Coroutine_CanStartCoroutine(DEMO_STACK_SIZE)){
Coroutine *cor = Coroutine_New(DEMO_STACK_SIZE, stacktest);
Coroutine_Delete(cor);
}
testresult->headroom = Coroutine_GetStackHeadroom();
testresult->canstartcoroutine = Coroutine_CanStartCoroutine(DEMO_STACK_SIZE);
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
unsigned char *stack_now = (unsigned char *)&argc;
printf("Various stack headrooms:\n");
intptr_t limitnocoroutine;
TestResult testresult;
// what stack do we get with no stack limit set
Coroutine_StartSystem();
cor_main = Coroutine_New(DEMO_STACK_SIZE, stacktest);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run_Coroutine(cor_main, &testresult);
Coroutine_Delete(cor_main);
printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
Coroutine_StopSystem();
// what stack do we get with an undersize stack limit
for (size_t stacksize = DEMO_STACK_SIZE/2; stacksize < DEMO_STACK_SIZE*4; stacksize += DEMO_STACK_SIZE/2){
Coroutine_StartSystem();
Coroutine_SetStackLimit(stack_now - stacksize);
cor_main = Coroutine_New(DEMO_STACK_SIZE, stacktest);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run_Coroutine(cor_main, &testresult);
Coroutine_Delete(cor_main);
printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
Coroutine_StopSystem();
}
}