37
43
45
Collect platform specific bits into cor_platform
on 2:19 PM Oct 20 2025
42
43
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "asleep.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"
typedef struct Loop_Timer {
struct timespec when;
Future *who;
} Loop_Timer;
typedef struct _ASleep {
pthread_mutex_t mutex;
unsigned users;
Loop_Timer *sleepers;
int lo;
int hi;
int max;
pthread_cond_t cond;
bool sleeploop_cancel;
pthread_t sleeploop;
bool started;
} _ASleep;
static _ASleep asleep;
static void *ASleep_Sleeper(void *param);
static void ASleep_ctor(
_ASleep *me
){
int r;
me->sleepers = NULL;
me->lo = 0;
me->hi = 0;
me->max = 0;
r = pthread_mutex_init(&me->mutex, NULL);
assert(r == 0);
r = pthread_cond_init(&me->cond, NULL);
assert(r == 0);
me->sleeploop_cancel = false;
r = pthread_create(&me->sleeploop, NULL, ASleep_Sleeper, me);
assert(r == 0);
me->started = true;
}
static void ASleep_dtor(
_ASleep *me
){
int r;
me->started = false;
me->sleeploop_cancel = true;
r = pthread_cond_signal(&me->cond);
assert(r == 0);
r = pthread_join(me->sleeploop, NULL);
assert(r == 0);
r = pthread_cond_destroy(&me->cond);
assert(r == 0);
pthread_mutex_destroy(&me->mutex);
assert(r == 0);
free(me->sleepers);
}
void ASleep_StartSystem(){
ASleep_ctor(&asleep);
}
void ASleep_StopSystem(){
ASleep_dtor(&asleep);
}
static void ASleep_AddSleeper(
_ASleep *me,
struct timespec when,
Future *who
){
int r = pthread_mutex_lock(&me->mutex);
assert(r == 0);
int nsleepers;
if (me->lo <= me->hi){
int nsleepers = me->hi - me->lo;
// ensure there's always an empty slot so that lo == hi always means no sleepers
if (nsleepers+1 >= me->max) {
me->max = (me->max == 0) ? 4 : me->max * 2;
me->sleepers = realloc(me->sleepers, me->max * sizeof(Loop_Timer));
assert(me->sleepers);
}
} else {
nsleepers = me->hi + me->max - me->lo;
// ensure there's always an empty slot so that lo == hi always means no sleepers
if (nsleepers+1 >= me->max) {
int oldmax = me->max;
me->max = (me->max == 0) ? 4 : me->max * 2;
me->sleepers = realloc(me->sleepers, me->max * sizeof(Loop_Timer));
assert(me->sleepers);
memmove(&me->sleepers[me->lo], &me->lo + me->max - oldmax, sizeof(*me->sleepers) * oldmax - me->lo);
me->lo += me->max - oldmax;
}
}
// insertion sort - we're assuming there's not going to be many sleepers active at once
int i;
int nexti;
for (i = me->hi; i != me->lo; i = nexti){
nexti = i-1;
if (i < 0){
nexti += me->max;
}
if (timespec_lte(me->sleepers[nexti].when, when)){
break;
}
me->sleepers[i] = me->sleepers[nexti];
}
me->sleepers[i].when = when;
me->sleepers[i].who = who;
me->hi += 1;
if (me->hi >= me->max){
me->hi -= me->max;
}
r = pthread_cond_signal(&me->cond);
assert(r == 0);
r = pthread_mutex_unlock(&me->mutex);
assert(r == 0);
}
static void ASleep_RemoveSleeper(
_ASleep *me,
struct timespec when,
Future *who
){
int r = pthread_mutex_lock(&me->mutex);
assert(r == 0);
int i;
for (i = me->lo; i != me->hi; ){
if (timespec_eq(me->sleepers[i].when, when) && me->sleepers[i].who == who){
int previ = i;
i += 1;
if (i >= me->max){
i -= me->max;
}
for (; i != me->hi; ){
me->sleepers[previ] = me->sleepers[i];
previ = i;
i += 1;
if (i >= me->max){
i -= me->max;
}
}
me->hi = previ;
break;
}
i += 1;
if (i >= me->max){
i -= me->max;
}
}
r = pthread_cond_signal(&me->cond);
assert(r == 0);
r = pthread_mutex_unlock(&me->mutex);
assert(r == 0);
}
static void *ASleep_Sleeper(
void *param
){
_ASleep *me = (_ASleep *)param;
int r;
while (!me->sleeploop_cancel){
r = pthread_mutex_lock(&me->mutex);
assert(r == 0);
if (me->lo != me->hi){
struct timespec when = me->sleepers[0].when;
r = pthread_mutex_unlock(&me->mutex);
assert(r == 0);
r = pthread_cond_timedwait(&me->cond, &me->mutex, &when);
assert(r == 0 || r == ETIMEDOUT);
} else {
r = pthread_mutex_unlock(&me->mutex);
assert(r == 0);
r = pthread_cond_wait(&me->cond, &me->mutex);
assert(r == 0);
}
// issue any due timers
struct timespec now;
r = clock_gettime(CLOCK_REALTIME, &now);
assert(r == 0);
while(me->lo != me->hi && timespec_lte(me->sleepers[me->lo].when, now)) {
Future *fut = me->sleepers[me->lo].who;
me->lo += 1;
if (me->lo >= me->max){
me->lo -= me->max;
}
r = pthread_mutex_unlock(&me->mutex);
assert(r == 0);
Future_SetResult(fut, false, NULL);
r = pthread_mutex_lock(&me->mutex);
assert(r == 0);
r = clock_gettime(CLOCK_REALTIME, &now);
assert(r == 0);
}
r = pthread_mutex_unlock(&me->mutex);
assert(r == 0);
}
return NULL;
}
// value is a pointer to a float delay in seconds
bool ASleep(
float delay,
void **value
){
assert(current_task && asleep.started);
if (delay < 0){
delay = 0;
}
float secs = floorf(delay);
float nsecs = (delay - secs) * 1e9;
struct timespec endtime;
clock_gettime(CLOCK_REALTIME, &endtime);
endtime.tv_nsec += (int)nsecs;
if (endtime.tv_nsec > 1000000000){
endtime.tv_sec += 1;
endtime.tv_nsec -= 1000000000;
}
endtime.tv_sec += (int)secs;
Future fut;
Future_ctor(&fut);
ASleep_AddSleeper(&asleep, endtime, &fut);
bool res = Future_Await(&fut, value);
ASleep_RemoveSleeper(&asleep, endtime, &fut);
Future_dtor(&fut);
return 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
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
#include "asleep.h"
#include "task.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#include "cor_platform.h"
typedef struct Loop_Timer {
int64_t when;
Future *who;
} Loop_Timer;
typedef struct _ASleep {
_Cor_Mutex mutex;
unsigned users;
Loop_Timer *sleepers;
int lo;
int hi;
int max;
_Cor_Semaphore sem;
bool sleeploop_cancel;
pthread_t sleeploop;
bool started;
} _ASleep;
static _ASleep asleep;
static void *ASleep_Sleeper(void *param);
static void ASleep_ctor(
_ASleep *me
){
int r;
me->sleepers = NULL;
me->lo = 0;
me->hi = 0;
me->max = 0;
_Cor_Mutex_ctor(&me->mutex);
_Cor_Semaphore_ctor(&me->sem);
me->sleeploop_cancel = false;
r = pthread_create(&me->sleeploop, NULL, ASleep_Sleeper, me);
assert(r == 0);
me->started = true;
}
static void ASleep_dtor(
_ASleep *me
){
int r;
me->started = false;
me->sleeploop_cancel = true;
_Cor_Semaphore_Signal(&me->sem);
r = pthread_join(me->sleeploop, NULL);
assert(r == 0);
_Cor_Sempahore_dtor(&me->sem);
_Cor_Mutex_dtor(&me->mutex);
free(me->sleepers);
}
void ASleep_StartSystem(){
ASleep_ctor(&asleep);
}
void ASleep_StopSystem(){
ASleep_dtor(&asleep);
}
static void ASleep_AddSleeper(
_ASleep *me,
int64_t when,
Future *who
){
_Cor_Mutex_Lock(&me->mutex);
int nsleepers;
if (me->lo <= me->hi){
int nsleepers = me->hi - me->lo;
// ensure there's always an empty slot so that lo == hi always means no sleepers
if (nsleepers+1 >= me->max) {
me->max = (me->max == 0) ? 4 : me->max * 2;
me->sleepers = realloc(me->sleepers, me->max * sizeof(Loop_Timer));
assert(me->sleepers);
}
} else {
nsleepers = me->hi + me->max - me->lo;
// ensure there's always an empty slot so that lo == hi always means no sleepers
if (nsleepers+1 >= me->max) {
int oldmax = me->max;
me->max = (me->max == 0) ? 4 : me->max * 2;
me->sleepers = realloc(me->sleepers, me->max * sizeof(Loop_Timer));
assert(me->sleepers);
memmove(&me->sleepers[me->lo], &me->lo + me->max - oldmax, sizeof(*me->sleepers) * oldmax - me->lo);
me->lo += me->max - oldmax;
}
}
// insertion sort - we're assuming there's not going to be many sleepers active at once
int i;
int nexti;
for (i = me->hi; i != me->lo; i = nexti){
nexti = i-1;
if (i < 0){
nexti += me->max;
}
if (me->sleepers[nexti].when <= when){
break;
}
me->sleepers[i] = me->sleepers[nexti];
}
me->sleepers[i].when = when;
me->sleepers[i].who = who;
me->hi += 1;
if (me->hi >= me->max){
me->hi -= me->max;
}
_Cor_Mutex_Unlock(&me->mutex);
_Cor_Semaphore_Signal(&me->sem);
}
static void ASleep_RemoveSleeper(
_ASleep *me,
int64_t when,
Future *who
){
_Cor_Mutex_Lock(&me->mutex);
int i;
for (i = me->lo; i != me->hi; ){
if (me->sleepers[i].when == when && me->sleepers[i].who == who){
int previ = i;
i += 1;
if (i >= me->max){
i -= me->max;
}
for (; i != me->hi; ){
me->sleepers[previ] = me->sleepers[i];
previ = i;
i += 1;
if (i >= me->max){
i -= me->max;
}
}
me->hi = previ;
break;
}
i += 1;
if (i >= me->max){
i -= me->max;
}
}
_Cor_Mutex_Unlock(&me->mutex);
_Cor_Semaphore_Signal(&me->sem);
}
static void *ASleep_Sleeper(
void *param
){
_ASleep *me = (_ASleep *)param;
while (!me->sleeploop_cancel){
_Cor_Mutex_Lock(&me->mutex);
bool got_one;
if (me->lo != me->hi){
int64_t when = me->sleepers[0].when;
_Cor_Mutex_Unlock(&me->mutex);
got_one = _Cor_Semaphore_Wait(&me->sem, when);
} else {
_Cor_Mutex_Unlock(&me->mutex);
got_one = _Cor_Semaphore_Wait(&me->sem, -1);
}
if (!got_one){
// timed out, so...
// issue any due timers
int64_t now = _Cor_Realtime_Now();
while(me->lo != me->hi && me->sleepers[me->lo].when <= now) {
Future *fut = me->sleepers[me->lo].who;
me->lo += 1;
if (me->lo >= me->max){
me->lo -= me->max;
}
_Cor_Mutex_Unlock(&me->mutex);
Future_SetResult(fut, false, NULL);
_Cor_Mutex_Lock(&me->mutex);
now = _Cor_Realtime_Now();
}
_Cor_Mutex_Unlock(&me->mutex);
}
}
return NULL;
}
// value is a pointer to a float delay in seconds
bool ASleep(
float delay,
void **value
){
assert(current_task && asleep.started);
if (delay < 0){
delay = 0;
}
int64_t endtime = _Cor_Realtime_Now() + (int64_t)(delay * 1000000000);
Future fut;
Future_ctor(&fut);
ASleep_AddSleeper(&asleep, endtime, &fut);
bool res = Future_Await(&fut, value);
ASleep_RemoveSleeper(&asleep, endtime, &fut);
Future_dtor(&fut);
return res;
}