81 86
91
WIP re-engineer for more robust operation, less chaining, and biggest-stack-possible chains
on 4:02 PM Jun 5 2026
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
#include "task.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#include "timespec_utils.h"
_Cor_thread_local Task *current_task;
static void _Task_SetResult(
Future *fut,
bool canceled,
void *res
){
(void)fut;
(void)canceled;
(void)res;
// only the task can set it's future's result
assert(false);
}
Future_vfptrs_t Task_vfptrs = {
(void(*)(Future *fut))&Task_dtor,
&_Future_Await,
&_Task_SetResult,
};
static void *Task_entry(void *me){
Task *tsk = (Task *)me;
void *res = NULL;
assert(current_task == NULL);
current_task = tsk;
bool canceled = tsk->entry(tsk->param, &res);
current_task = NULL;
_Future_SetResult(&tsk->base, canceled, res);
return res;
}
void Task_ctor(
Task *tsk,
size_t stack_size,
Task_Entry entry,
void *param
){
assert(Coroutine_NS(IsStarted)());
Future_ctor(&tsk->base);
tsk->base.vfptrs = &Task_vfptrs;
tsk->entry = entry;
tsk->param = param;
tsk->cor = Coroutine_NS(New)(stack_size, Task_entry);
tsk->awaiting_future = NULL;
tsk->canceled = false;
tsk->cancel_value = NULL;
if (current_task){
Coroutine_NS(Continue)(tsk->cor, tsk, false);
}
}
void Task_dtor(
Task *tsk
){
Coroutine_NS(Delete)(tsk->cor);
Future_dtor(&tsk->base);
}
Task *Task_New(
size_t stack_size,
Task_Entry entry,
void *param
){
Task *tsk = malloc(sizeof(Task));
Task_ctor(tsk, stack_size, entry, param);
return tsk;
}
void Task_Delete(
Task *tsk
){
Future_Delete(&tsk->base);
}
void Task_Cancel(
Task *tsk,
void *cancel_value
){
if (!tsk->canceled){
tsk->canceled = true;
tsk->cancel_value = cancel_value;
Future *awaiting_future = tsk->awaiting_future;
if (awaiting_future){
Future_SetResult(awaiting_future, true, NULL);
}
}
}
struct Task_Run_Params {
size_t stack_size;
Task_Entry start;
void *value;
void **res;
};
static Coroutine_Err Task_Runner(void *_params)
{
struct Task_Run_Params *params = (struct Task_Run_Params *)_params;
Task tsk;
Task_ctor(&tsk, params->stack_size, params->start, params->value);
Coroutine_Err err = Coroutine_NS(Run_Coroutine)(tsk.cor, &tsk);
if (!err){
err = Future_GetResult(&tsk.base, params->res);
}
Task_dtor(&tsk);
return err;
}
bool Task_Run(size_t stack_size, Task_Entry start, void *value, void **res){
if (current_task){
return Coroutine_Err_SystemRunning;
}
struct Task_Run_Params params = {stack_size, start, value, res};
return Coroutine_NS(RunSystem)(Task_Runner, &params);
}
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "task.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#include "timespec_utils.h"
_Cor_thread_local Task *current_task;
static void
_Task_SetResult(
Future *fut,
bool canceled,
void *res
){
(void)fut;
(void)canceled;
(void)res;
// only the task can set it's future's result
assert(false);
}
Future_vfptrs_t Task_vfptrs = {
(void(*)(Future *fut))&Task_dtor,
&_Future_Await,
&_Task_SetResult,
};
static void *
Task_entry(void *me){
Task *tsk = (Task *)me;
void *res = NULL;
assert(current_task == NULL);
current_task = tsk;
bool canceled = tsk->entry(tsk->param, &res);
current_task = NULL;
_Future_SetResult(&tsk->base, canceled, res);
return res;
}
static void
Task_ctor_(
Task *tsk,
Coroutine *cor
){
assert(Coroutine_NS(IsStarted)());
Future_ctor(&tsk->base);
tsk->base.vfptrs = &Task_vfptrs;
tsk->cor = cor;
tsk->cor_owned = false;
tsk->awaiting_future = NULL;
tsk->canceled = false;
tsk->cancel_value = NULL;
if (current_task){
Coroutine_NS(Continue)(tsk->cor, tsk, false);
}
}
void
Task_ctor(
Task *tsk,
size_t min_stack,
size_t min_stack_headroom,
Task_Entry entry,
void *param
){
assert(Coroutine_NS(IsStarted)());
Task_ctor_(tsk, Coroutine_NS(New)(min_stack, min_stack_headroom, Task_entry));
tsk->cor_owned = true;
tsk->entry = entry;
tsk->param = param;
if (current_task){
Coroutine_NS(Continue)(tsk->cor, tsk, false);
}
}
void
Task_dtor(
Task *tsk
){
if (tsk->cor_owned){
Coroutine_NS(Delete)(tsk->cor);
}
Future_dtor(&tsk->base);
}
Task *
Task_New(
size_t min_stack,
size_t min_stack_headroom,
Task_Entry entry,
void *param
){
Task *tsk = malloc(sizeof(Task));
Task_ctor(tsk, min_stack, min_stack_headroom, entry, param);
return tsk;
}
void
Task_Delete(
Task *tsk
){
Future_Delete(&tsk->base);
}
void
Task_Cancel(
Task *tsk,
void *cancel_value
){
if (!tsk->canceled){
tsk->canceled = true;
tsk->cancel_value = cancel_value;
Future *awaiting_future = tsk->awaiting_future;
if (awaiting_future){
Future_SetResult(awaiting_future, true, NULL);
}
}
}
struct Task_Run_Params {
size_t min_stack;
size_t min_stack_headroom;
Task_Entry start;
void *value;
void **res;
};
static Coroutine_Err
Task_Runner(
void *_params,
Coroutine *root
){
struct Task_Run_Params *params = (struct Task_Run_Params *)_params;
Task roottsk;
Task_ctor_(&roottsk, root);
current_task = &roottsk;
Task childtask;
Task_ctor(&childtask, params->min_stack, params->min_stack_headroom, params->start, params->value);
Coroutine_Err err = Task_Await(&childtask, params->res);
Task_dtor(&childtask);
current_task = NULL;
Task_dtor(&roottsk);
return err;
}
bool
Task_Run(
size_t min_stack,
size_t min_stack_headroom,
Task_Entry start,
void *value,
void **res
){
if (current_task){
return Coroutine_Err_SystemRunning;
}
struct Task_Run_Params params = {min_stack, min_stack_headroom, start, value, res};
return Coroutine_NS(RunSystem)(0, COROUTINE_STARTUP_STACK_SIZE, Task_Runner, &params);
}