0 branches 0 tags
81 86
91
93
WIP re-engineer for more robust operation, less chaining, and biggest-stack-possible chains
on 4:02 PM Jun 5 2026
async_demo.c
90
91
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>
#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();
void *res = NULL;
bool canceled = Task_Run(DEMO_STACK_SIZE, asynctest, NULL, &res);
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
#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, 0, asynctestpart, &task1param);
printf("task1 going\n");
asynctestpartparam task2param = {
"Second",
0.75f,
8
};
Task *task2 = Task_New(DEMO_STACK_SIZE, 0, 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();
void *res = NULL;
bool canceled = Task_Run(DEMO_STACK_SIZE, 0, asynctest, NULL, &res);
ASleep_StopSystem();
printf("Async result %d:%p\n", canceled, res);
}
chaining_demo.c
90
91
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);
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, 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
#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());
long depth = (long)param;
if (depth > 10000){
return NULL;
}
return chaindeeper((void *)(depth + 1));
}
void *chaintest(
void *param
){
(void)param;
chaindeeper((void *)0);
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;
}
generator_demo.c
90
91
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;
}
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, 0, 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, 0, gentest, "..", NULL);
return 0;
}
low_stack.c
90
91
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
#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
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run(DEMO_STACK_SIZE, stacktest, &testresult, NULL);
printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
// 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_SetStackLimit(stack_now - stacksize);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run(DEMO_STACK_SIZE, stacktest, &testresult, NULL);
printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
}
}
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
#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, 0, 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
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run(DEMO_STACK_SIZE, 0, stacktest, &testresult, NULL);
printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
// 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_SetStackLimit(stack_now - stacksize);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run(DEMO_STACK_SIZE, 0, stacktest, &testresult, NULL);
printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
}
}
stack_size_variety.c
90
91
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#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 *dotests(
void *param
){
(void)param;
Coroutine_Dump_();
Coroutine *active = Coroutine_GetActive();
Coroutine *a = Coroutine_New(DEMO_STACK_SIZE, dotests);
Coroutine *b = Coroutine_New(DEMO_STACK_SIZE, dotests);
Coroutine *c = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbcc]\n");
Coroutine_Dump_();
printf("%ld %ld %ld (a, b, c: a < b < c)\n", (unsigned char *)active - (unsigned char *)a, (unsigned char *)active - (unsigned char *)b, (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
Coroutine_Dump_();
b = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbcc]\n");
Coroutine_Dump_();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
Coroutine_Dump_();
Coroutine *d = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aaffccdddd]\n");
printf("%ld (d: c < d)\n", (unsigned char *)active - (unsigned char *)d);
Coroutine_Dump_();
printf("[testing merge before]\n");
Coroutine_Delete(c);
printf("[stack=aaffffdddd]\n");
Coroutine_Dump_();
b = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aabbbbdddd]\n");
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
Coroutine_Dump_();
printf("[testing merge after]\n");
Coroutine_Delete(b);
printf("[stack=aaffffdddd]\n");
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
a = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aaaaffdddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
Coroutine_Dump_();
printf("[testing merge both ways]\n");
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
a = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aaffffdddd]\n");
Coroutine_Dump_();
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
b = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbffdddd]\n");
Coroutine_Dump_();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
c = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbccdddd]\n");
printf("%ld (c again)\n", (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(a);
printf("[stack=ffbbccdddd]\n");
Coroutine_Delete(c);
printf("[stack=ffbbffdddd]\n");
Coroutine_Delete(b);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
a = Coroutine_New(DEMO_STACK_SIZE*3, dotests);
printf("[stack=aaaaaadddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
Coroutine_Delete(d);
printf("[stack=fffffffffff]\n");
Coroutine_Dump_();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=affffffffff]\n");
Coroutine_Dump_();
b = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=abfffffffff]\n");
Coroutine_Dump_();
c = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=abcffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=fbcffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(b);
printf("[stack=ffcffffffff]\n");
Coroutine_Dump_();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=afcffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(c);
printf("[stack=affffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=fffffffffff]\n");
Coroutine_Dump_();
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
printf("No StartSystem\n");
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
printf("\n\n\n\nStartSystem\n");
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
printf("\n\n\n\nSartSystem with stack limit\n");
Coroutine_SetStackLimit((unsigned char *)&argc - 1024*1024);
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
}
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#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 *dotests(
void *param
){
(void)param;
Coroutine_Dump_();
Coroutine *active = Coroutine_GetActive();
Coroutine *a = Coroutine_New(DEMO_STACK_SIZE, 0, dotests);
Coroutine *b = Coroutine_New(DEMO_STACK_SIZE, 0, dotests);
Coroutine *c = Coroutine_New(DEMO_STACK_SIZE, 0, dotests);
printf("[stack=aabbcc]\n");
Coroutine_Dump_();
printf("%ld %ld %ld (a, b, c: a < b < c)\n", (unsigned char *)active - (unsigned char *)a, (unsigned char *)active - (unsigned char *)b, (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
Coroutine_Dump_();
b = Coroutine_New(DEMO_STACK_SIZE, 0, dotests);
printf("[stack=aabbcc]\n");
Coroutine_Dump_();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
Coroutine_Dump_();
Coroutine *d = Coroutine_New(DEMO_STACK_SIZE*2, 0, dotests);
printf("[stack=aaffccdddd]\n");
printf("%ld (d: c < d)\n", (unsigned char *)active - (unsigned char *)d);
Coroutine_Dump_();
printf("[testing merge before]\n");
Coroutine_Delete(c);
printf("[stack=aaffffdddd]\n");
Coroutine_Dump_();
b = Coroutine_New(DEMO_STACK_SIZE*2, 0, dotests);
printf("[stack=aabbbbdddd]\n");
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
Coroutine_Dump_();
printf("[testing merge after]\n");
Coroutine_Delete(b);
printf("[stack=aaffffdddd]\n");
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
a = Coroutine_New(DEMO_STACK_SIZE*2, 0, dotests);
printf("[stack=aaaaffdddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
Coroutine_Dump_();
printf("[testing merge both ways]\n");
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
a = Coroutine_New(DEMO_STACK_SIZE, 0, dotests);
printf("[stack=aaffffdddd]\n");
Coroutine_Dump_();
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
b = Coroutine_New(DEMO_STACK_SIZE, 0, dotests);
printf("[stack=aabbffdddd]\n");
Coroutine_Dump_();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
c = Coroutine_New(DEMO_STACK_SIZE, 0, dotests);
printf("[stack=aabbccdddd]\n");
printf("%ld (c again)\n", (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(a);
printf("[stack=ffbbccdddd]\n");
Coroutine_Delete(c);
printf("[stack=ffbbffdddd]\n");
Coroutine_Delete(b);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
a = Coroutine_New(DEMO_STACK_SIZE*3, 0, dotests);
printf("[stack=aaaaaadddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
Coroutine_Dump_();
Coroutine_Delete(d);
printf("[stack=fffffffffff]\n");
Coroutine_Dump_();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, 0, dotests);
printf("[stack=affffffffff]\n");
Coroutine_Dump_();
b = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, 0, dotests);
printf("[stack=abfffffffff]\n");
Coroutine_Dump_();
c = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, 0, dotests);
printf("[stack=abcffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=fbcffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(b);
printf("[stack=ffcffffffff]\n");
Coroutine_Dump_();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, 0, dotests);
printf("[stack=afcffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(c);
printf("[stack=affffffffff]\n");
Coroutine_Dump_();
Coroutine_Delete(a);
printf("[stack=fffffffffff]\n");
Coroutine_Dump_();
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
printf("No StartSystem\n");
Coroutine_Run(DEMO_STACK_SIZE, 0, dotests, NULL, NULL);
printf("\n\n\n\nStartSystem\n");
Coroutine_Run(DEMO_STACK_SIZE, 0, dotests, NULL, NULL);
printf("\n\n\n\nSartSystem with stack limit\n");
Coroutine_SetStackLimit((unsigned char *)&argc - 1024*1024);
Coroutine_Run(DEMO_STACK_SIZE, 0, dotests, NULL, NULL);
}