43 45
81
Remove need for malloc by Coroutine, and add Coroutine_Err
on 12:28 AM Feb 20 2026
80
81
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
#include "cor_platform.h"
#include <assert.h>
#include "timespec_utils.h"
#include <stdio.h>
void _Cor_Mutex_ctor(_Cor_Mutex *mut){
int r;
r = pthread_mutex_init(&mut->mut, NULL);
assert(r == 0);
}
void _Cor_Mutex_dtor(_Cor_Mutex *mut){
int r;
r = pthread_mutex_destroy(&mut->mut);
assert(r == 0);
}
void _Cor_Mutex_Lock(_Cor_Mutex *mut){
int r;
r = pthread_mutex_lock(&mut->mut);
assert(r == 0);
}
void _Cor_Mutex_Unlock(_Cor_Mutex *mut){
int r;
r = pthread_mutex_unlock(&mut->mut);
assert(r == 0);
}
int64_t _Cor_Realtime_Now(){
int r;
struct timespec now;
r = clock_gettime(CLOCK_REALTIME, &now);
assert(r == 0);
return int64_ns_from_timespec(now);
}
void _Cor_Semaphore_ctor(_Cor_Semaphore *sem){
int r;
r = pthread_mutex_init(&sem->mut, NULL);
assert(r == 0);
r = pthread_cond_init(&sem->cond, NULL);
assert(r == 0);
sem->count = 0;
}
void _Cor_Sempahore_dtor(_Cor_Semaphore *sem){
int r;
r = pthread_cond_destroy(&sem->cond);
assert(r == 0);
r = pthread_mutex_destroy(&sem->mut);
assert(r == 0);
}
// timeout_when < 0 means 'wait forever'
// Returns true for success, false for timeout
bool _Cor_Semaphore_Wait(_Cor_Semaphore *sem, int64_t timeout_when){
int r;
r = pthread_mutex_lock(&sem->mut);
assert(r == 0);
if (sem->count == 0) {
if (timeout_when >= 0) {
struct timespec ts = timespec_from_int64_ns(timeout_when);
r = pthread_cond_timedwait(&sem->cond, &sem->mut, &ts);
}
else {
r = pthread_cond_wait(&sem->cond, &sem->mut);
}
}
assert(r == 0 || r == ETIMEDOUT);
bool res;
if (sem->count > 0) {
sem->count--;
res = true;
} else {
res = false;
}
r = pthread_mutex_unlock(&sem->mut);
assert(r == 0);
return res;
}
void _Cor_Semaphore_Signal(_Cor_Semaphore *sem){
int r;
r = pthread_mutex_lock(&sem->mut);
assert(r == 0);
sem->count++;
r = pthread_cond_signal(&sem->cond);
assert(r == 0);
r = pthread_mutex_unlock(&sem->mut);
assert(r == 0);
}
void _Cor_Thread_ctor(_Cor_Thread *th, void *(*fn)(void *), void *param){
int r;
th->joined = false;
r = pthread_create(&th->th, NULL, fn, param);
assert(r == 0);
}
void _Cor_Thread_dtor(_Cor_Thread *th){
int r;
if (!th->joined){
r = pthread_detach(th->th);
assert(r == 0);
}
}
void *_Cor_Thread_Join(_Cor_Thread *th){
int r;
void *res;
r = pthread_join(th->th, &res);
assert(r == 0);
th->joined = true;
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
#include "cor_platform.h"
#include <assert.h>
#include "timespec_utils.h"
#include <stdio.h>
int _Cor_Mutex_ctor(_Cor_Mutex *mut){
return pthread_mutex_init(&mut->mut, NULL);
}
int _Cor_Mutex_dtor(_Cor_Mutex *mut){
return pthread_mutex_destroy(&mut->mut);
}
int _Cor_Mutex_Lock(_Cor_Mutex *mut){
return pthread_mutex_lock(&mut->mut);
}
int _Cor_Mutex_Unlock(_Cor_Mutex *mut){
return pthread_mutex_unlock(&mut->mut);
}
int64_t _Cor_Realtime_Now(){
int r;
struct timespec now;
r = clock_gettime(CLOCK_REALTIME, &now);
assert(r == 0);
return int64_ns_from_timespec(now);
}
void _Cor_Semaphore_ctor(_Cor_Semaphore *sem){
int r;
r = pthread_mutex_init(&sem->mut, NULL);
assert(r == 0);
r = pthread_cond_init(&sem->cond, NULL);
assert(r == 0);
sem->count = 0;
}
void _Cor_Sempahore_dtor(_Cor_Semaphore *sem){
int r;
r = pthread_cond_destroy(&sem->cond);
assert(r == 0);
r = pthread_mutex_destroy(&sem->mut);
assert(r == 0);
}
// timeout_when < 0 means 'wait forever'
// Returns true for success, false for timeout
bool _Cor_Semaphore_Wait(_Cor_Semaphore *sem, int64_t timeout_when){
int r;
r = pthread_mutex_lock(&sem->mut);
assert(r == 0);
if (sem->count == 0) {
if (timeout_when >= 0) {
struct timespec ts = timespec_from_int64_ns(timeout_when);
r = pthread_cond_timedwait(&sem->cond, &sem->mut, &ts);
}
else {
r = pthread_cond_wait(&sem->cond, &sem->mut);
}
}
assert(r == 0 || r == ETIMEDOUT);
bool res;
if (sem->count > 0) {
sem->count--;
res = true;
} else {
res = false;
}
r = pthread_mutex_unlock(&sem->mut);
assert(r == 0);
return res;
}
void _Cor_Semaphore_Signal(_Cor_Semaphore *sem){
int r;
r = pthread_mutex_lock(&sem->mut);
assert(r == 0);
sem->count++;
r = pthread_cond_signal(&sem->cond);
assert(r == 0);
r = pthread_mutex_unlock(&sem->mut);
assert(r == 0);
}
void _Cor_Thread_ctor(_Cor_Thread *th, void *(*fn)(void *), void *param){
int r;
th->joined = false;
r = pthread_create(&th->th, NULL, fn, param);
assert(r == 0);
}
void _Cor_Thread_dtor(_Cor_Thread *th){
int r;
if (!th->joined){
r = pthread_detach(th->th);
assert(r == 0);
}
}
void *_Cor_Thread_Join(_Cor_Thread *th){
int r;
void *res;
r = pthread_join(th->th, &res);
assert(r == 0);
th->joined = true;
return res;
}