0 branches 0 tags
40 41
42
43 44
Remove unnecessary include
on 9:24 AM Oct 20 2025
trunk/coroutine/future.c
41
42
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "future.h"
#include "coroutine.h"
#include "task.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include "cor_thread_local.h"
#include <math.h>
#include <string.h>
#include "timespec_utils.h"
Future_vfptrs_t Future_vfptrs = {
&Future_dtor,
&_Future_Await,
&_Future_SetResult
};
typedef struct Future_WatcherSpec {
Future_Watcher watcher;
void *me;
} Future_WatcherSpec;
void Future_ctor(Future *fut){
fut->vfptrs = &Future_vfptrs;
int r = pthread_mutex_init(&fut->mutex, NULL);
assert(r == 0);
fut->state = Future_State_Waiting;
fut->value = NULL;
fut->canceled = false;
fut->watchers = NULL;
fut->nwatchers = 0;
fut->maxwatchers = 0;
}
Future *Future_New(){
Future *fut = malloc(sizeof(Future));
Future_ctor(fut);
return fut;
}
void Future_dtor(
Future *fut
){
int r;
r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
assert(fut->nwatchers == 0);
free(fut->watchers);
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
r = pthread_mutex_destroy(&fut->mutex);
assert(r == 0);
}
void Future_Delete(
Future *fut
){
fut->vfptrs->dtor(fut);
free(fut);
}
void Future_AddWatcher(
Future *fut,
Future_Watcher watcher,
void *watcher_me
){
int r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
if (fut->state == Future_State_Done) {
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
watcher(watcher_me, fut);
} else {
if (fut->nwatchers >= fut->maxwatchers) {
fut->maxwatchers = (fut->maxwatchers == 0) ? 4 : fut->maxwatchers * 2;
fut->watchers = realloc(fut->watchers, fut->maxwatchers * sizeof(Future_WatcherSpec));
assert(fut->watchers);
}
fut->watchers[fut->nwatchers].watcher = watcher;
fut->watchers[fut->nwatchers].me = watcher_me;
fut->nwatchers++;
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
}
}
void Future_RemoveWatcher(
Future *fut,
Future_Watcher watcher,
void *watcher_me
){
int r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
for (int i = 0; i < fut->nwatchers; i++) {
if (fut->watchers[i].watcher == watcher && fut->watchers[i].me == watcher_me) {
fut->nwatchers--;
if (i < fut->nwatchers) {
fut->watchers[i] = fut->watchers[fut->nwatchers];
}
break;
}
}
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
}
static void _Future_Ready(
Future *fut
){
int r;
fut->state = Future_State_Done;
// Take note of watchers list, and reset it
Future_WatcherSpec *watchers = fut->watchers;
int nwatchers = fut->nwatchers;
fut->watchers = NULL;
fut->nwatchers = 0;
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
// notify those watchers
for (int i = 0; i < nwatchers; i++) {
watchers[i].watcher(watchers[i].me, fut);
}
free(watchers);
}
void Future_SetResult(
Future *fut,
bool canceled,
void *value
){
fut->vfptrs->set_result(fut, canceled, value);
}
bool Future_GetResult(
Future *fut,
void **res
){
assert(fut->state == Future_State_Done);
if (res){
*res = fut->value;
}
return fut->canceled;
}
typedef struct future_bits {
Future *fut;
Coroutine *cor;
} future_bits;
static void future_complete(
void *me,
Future *fut
){
(void)fut;
Coroutine *cor = (Coroutine *)me;
Coroutine_Continue(cor, NULL, false);
}
static void on_yield_for_future(
void *me
){
future_bits *bits = (future_bits *)me;
Future_AddWatcher(bits->fut, future_complete, bits->cor);
}
void _Future_Await(
Future *fut
){
future_bits bits;
bits.fut = fut;
bits.cor = Coroutine_GetActive();
Task *my_task = current_task;
current_task = NULL;
my_task->awaiting_future = fut;
if (my_task->canceled){
Future_SetResult(fut, true, my_task->cancel_value);
}
Coroutine_Yield(NULL, on_yield_for_future, &bits);
my_task->awaiting_future = NULL;
current_task = my_task;
}
void _Future_SetResult(
Future *fut,
bool canceled,
void *res
){
int r;
r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
if(fut->state == Future_State_Waiting){
fut->canceled = canceled;
fut->value = res;
_Future_Ready(fut);
} else {
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
}
}
bool Future_Await(
Future *fut,
void **res
){
fut->vfptrs->await(fut);
if (res){
*res = fut->value;
}
return fut->canceled;
}
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "future.h"
#include "coroutine.h"
#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"
Future_vfptrs_t Future_vfptrs = {
&Future_dtor,
&_Future_Await,
&_Future_SetResult
};
typedef struct Future_WatcherSpec {
Future_Watcher watcher;
void *me;
} Future_WatcherSpec;
void Future_ctor(Future *fut){
fut->vfptrs = &Future_vfptrs;
int r = pthread_mutex_init(&fut->mutex, NULL);
assert(r == 0);
fut->state = Future_State_Waiting;
fut->value = NULL;
fut->canceled = false;
fut->watchers = NULL;
fut->nwatchers = 0;
fut->maxwatchers = 0;
}
Future *Future_New(){
Future *fut = malloc(sizeof(Future));
Future_ctor(fut);
return fut;
}
void Future_dtor(
Future *fut
){
int r;
r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
assert(fut->nwatchers == 0);
free(fut->watchers);
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
r = pthread_mutex_destroy(&fut->mutex);
assert(r == 0);
}
void Future_Delete(
Future *fut
){
fut->vfptrs->dtor(fut);
free(fut);
}
void Future_AddWatcher(
Future *fut,
Future_Watcher watcher,
void *watcher_me
){
int r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
if (fut->state == Future_State_Done) {
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
watcher(watcher_me, fut);
} else {
if (fut->nwatchers >= fut->maxwatchers) {
fut->maxwatchers = (fut->maxwatchers == 0) ? 4 : fut->maxwatchers * 2;
fut->watchers = realloc(fut->watchers, fut->maxwatchers * sizeof(Future_WatcherSpec));
assert(fut->watchers);
}
fut->watchers[fut->nwatchers].watcher = watcher;
fut->watchers[fut->nwatchers].me = watcher_me;
fut->nwatchers++;
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
}
}
void Future_RemoveWatcher(
Future *fut,
Future_Watcher watcher,
void *watcher_me
){
int r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
for (int i = 0; i < fut->nwatchers; i++) {
if (fut->watchers[i].watcher == watcher && fut->watchers[i].me == watcher_me) {
fut->nwatchers--;
if (i < fut->nwatchers) {
fut->watchers[i] = fut->watchers[fut->nwatchers];
}
break;
}
}
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
}
static void _Future_Ready(
Future *fut
){
int r;
fut->state = Future_State_Done;
// Take note of watchers list, and reset it
Future_WatcherSpec *watchers = fut->watchers;
int nwatchers = fut->nwatchers;
fut->watchers = NULL;
fut->nwatchers = 0;
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
// notify those watchers
for (int i = 0; i < nwatchers; i++) {
watchers[i].watcher(watchers[i].me, fut);
}
free(watchers);
}
void Future_SetResult(
Future *fut,
bool canceled,
void *value
){
fut->vfptrs->set_result(fut, canceled, value);
}
bool Future_GetResult(
Future *fut,
void **res
){
assert(fut->state == Future_State_Done);
if (res){
*res = fut->value;
}
return fut->canceled;
}
typedef struct future_bits {
Future *fut;
Coroutine *cor;
} future_bits;
static void future_complete(
void *me,
Future *fut
){
(void)fut;
Coroutine *cor = (Coroutine *)me;
Coroutine_Continue(cor, NULL, false);
}
static void on_yield_for_future(
void *me
){
future_bits *bits = (future_bits *)me;
Future_AddWatcher(bits->fut, future_complete, bits->cor);
}
void _Future_Await(
Future *fut
){
future_bits bits;
bits.fut = fut;
bits.cor = Coroutine_GetActive();
Task *my_task = current_task;
current_task = NULL;
my_task->awaiting_future = fut;
if (my_task->canceled){
Future_SetResult(fut, true, my_task->cancel_value);
}
Coroutine_Yield(NULL, on_yield_for_future, &bits);
my_task->awaiting_future = NULL;
current_task = my_task;
}
void _Future_SetResult(
Future *fut,
bool canceled,
void *res
){
int r;
r = pthread_mutex_lock(&fut->mutex);
assert(r == 0);
if(fut->state == Future_State_Waiting){
fut->canceled = canceled;
fut->value = res;
_Future_Ready(fut);
} else {
r = pthread_mutex_unlock(&fut->mutex);
assert(r == 0);
}
}
bool Future_Await(
Future *fut,
void **res
){
fut->vfptrs->await(fut);
if (res){
*res = fut->value;
}
return fut->canceled;
}
trunk/include/asleep.h
41
42
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef ASLEEP_H
#define ASLEEP_H
#include <stdbool.h>
#include "task.h"
#include "cor_thread_local.h"
void ASleep_StartSystem();
bool ASleep(float delay, void **value);
void ASleep_StopSystem();
#endif
1
2
3
4
5
6
7
8
9
10
11
#ifndef ASLEEP_H
#define ASLEEP_H
#include <stdbool.h>
#include "task.h"
void ASleep_StartSystem();
bool ASleep(float delay, void **value);
void ASleep_StopSystem();
#endif