0 branches 0 tags
37 42
43
44 45
Collect platform specific bits into cor_platform
on 2:19 PM Oct 20 2025
cor_platform.h
future.def.h
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <pthread.h>
typedef struct Future_WatcherSpec Future_WatcherSpec;
typedef enum Future_State {
Future_State_Waiting,
Future_State_Done
} Future_State;
struct Future {
Future_vfptrs_t *vfptrs;
Future_State state;
void *value;
bool canceled;
pthread_mutex_t mutex;
Future_WatcherSpec *watchers;
int nwatchers;
int maxwatchers;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "cor_platform.h"
typedef struct Future_WatcherSpec Future_WatcherSpec;
typedef enum Future_State {
Future_State_Waiting,
Future_State_Done
} Future_State;
struct Future {
Future_vfptrs_t *vfptrs;
Future_State state;
void *value;
bool canceled;
_Cor_Mutex mutex;
Future_WatcherSpec *watchers;
int nwatchers;
int maxwatchers;
};
task.def.h
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
#include <pthread.h>
#include "coroutine.h"
struct Task {
Future base;
Coroutine *cor;
Task_Entry entry;
void *param;
Future *awaiting_future;
bool canceled;
void *cancel_value;
};
static inline bool Task_IsCanceled(
Task *tsk
){
return tsk->canceled;
}
static inline bool Task_Await(
Task *tsk,
void **res
){
return Future_Await(&tsk->base, res);
}
static inline Future *Task_GetAwaitedFuture(
Task *tsk
){
return tsk->awaiting_future;
}
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
#include "coroutine.h"
struct Task {
Future base;
Coroutine *cor;
Task_Entry entry;
void *param;
Future *awaiting_future;
bool canceled;
void *cancel_value;
};
static inline bool Task_IsCanceled(
Task *tsk
){
return tsk->canceled;
}
static inline bool Task_Await(
Task *tsk,
void **res
){
return Future_Await(&tsk->base, res);
}
static inline Future *Task_GetAwaitedFuture(
Task *tsk
){
return tsk->awaiting_future;
}
task.h
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
#ifndef TASK_H
#define TASK_H
#include <stdbool.h>
#include "future.h"
#include "cor_thread_local.h"
typedef struct Task Task;
extern _Cor_thread_local Task *current_task;
typedef bool (*Task_Entry)(void *param, void **res);
void Task_ctor(Task *tsk, Task_Entry entry, void *param);
Task *Task_New(Task_Entry entry, void *param);
void Task_dtor(Task *tsk);
void Task_Delete(Task *tsk);
static inline bool Task_Await(Task *tsk, void **res);
void Task_Cancel(Task *tsk, void *cancel_value);
static inline bool Task_IsCanceled(Task *tsk);
static inline Future *Task_GetAwaitedFuture(Task *tsk);
bool Task_Run(Task_Entry start, void *value, void **res);
#include "task.def.h"
#endif
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
#ifndef TASK_H
#define TASK_H
#include <stdbool.h>
#include "future.h"
#include "cor_platform.h"
typedef struct Task Task;
extern _Cor_thread_local Task *current_task;
typedef bool (*Task_Entry)(void *param, void **res);
void Task_ctor(Task *tsk, Task_Entry entry, void *param);
Task *Task_New(Task_Entry entry, void *param);
void Task_dtor(Task *tsk);
void Task_Delete(Task *tsk);
static inline bool Task_Await(Task *tsk, void **res);
void Task_Cancel(Task *tsk, void *cancel_value);
static inline bool Task_IsCanceled(Task *tsk);
static inline Future *Task_GetAwaitedFuture(Task *tsk);
bool Task_Run(Task_Entry start, void *value, void **res);
#include "task.def.h"
#endif
timespec_utils.def.h
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
#include <time.h>
static inline bool timespec_lt(struct timespec a, struct timespec b){
return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec);
}
static inline bool timespec_lte(struct timespec a, struct timespec b){
return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec);
}
static inline bool timespec_eq(struct timespec a, struct timespec b){
return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
}
static inline bool timespec_gt(struct timespec a, struct timespec b){
return timespec_lt(b, a);
}
static inline struct timespec timespec_sub(struct timespec a, struct timespec b){
struct timespec r;
if (a.tv_nsec < b.tv_nsec){
r.tv_nsec = 1000000000 + a.tv_nsec - b.tv_nsec;
r.tv_sec = a.tv_sec - b.tv_sec - 1;
} else {
r.tv_nsec = a.tv_nsec - b.tv_nsec;
r.tv_sec = a.tv_sec - b.tv_sec;
}
return r;
}
static inline bool timespec_gte(struct timespec a, struct timespec b){
return timespec_lte(b, a);
}
static inline bool timespec_ne(struct timespec a, struct timespec b){
return !timespec_eq(a, b);
}
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
#include <time.h>
static inline struct timespec timespec_from_int64_ns(int64_t time_ns){
struct timespec r;
if (time_ns < 0){
// Round negative times towards -infinity
time_ns = -time_ns;
r.tv_nsec = 999999999 - ((time_ns-1) % 1000000000);
r.tv_sec = -((time_ns+999999999) / 1000000000);
} else {
r.tv_nsec = time_ns % 1000000000;
r.tv_sec = time_ns / 1000000000;
}
return r;
}
static inline int64_t int64_ns_from_timespec(struct timespec time){
return (int64_t)time.tv_sec * 1000000000 + time.tv_nsec;
}
static inline bool timespec_lt(struct timespec a, struct timespec b){
return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec);
}
static inline bool timespec_lte(struct timespec a, struct timespec b){
return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec);
}
static inline bool timespec_eq(struct timespec a, struct timespec b){
return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
}
static inline bool timespec_gt(struct timespec a, struct timespec b){
return timespec_lt(b, a);
}
static inline struct timespec timespec_sub(struct timespec a, struct timespec b){
struct timespec r;
if (a.tv_nsec < b.tv_nsec){
r.tv_nsec = 1000000000 + a.tv_nsec - b.tv_nsec;
r.tv_sec = a.tv_sec - b.tv_sec - 1;
} else {
r.tv_nsec = a.tv_nsec - b.tv_nsec;
r.tv_sec = a.tv_sec - b.tv_sec;
}
return r;
}
static inline bool timespec_gte(struct timespec a, struct timespec b){
return timespec_lte(b, a);
}
static inline bool timespec_ne(struct timespec a, struct timespec b){
return !timespec_eq(a, b);
}
timespec_utils.h
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _TIMESPEC_UTILS_H
#define _TIMESPEC_UTILS_H
struct timespec;
static inline bool timespec_eq(struct timespec a, struct timespec b);
static inline bool timespec_ne(struct timespec a, struct timespec b);
static inline bool timespec_lt(struct timespec a, struct timespec b);
static inline bool timespec_lte(struct timespec a, struct timespec b);
static inline bool timespec_gt(struct timespec a, struct timespec b);
static inline bool timespec_gte(struct timespec a, struct timespec b);
static inline struct timespec timespec_sub(struct timespec a, struct timespec b);
#include "timespec_utils.def.h"
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _TIMESPEC_UTILS_H
#define _TIMESPEC_UTILS_H
#include <stdint.h>
struct timespec;
static inline struct timespec timespec_from_int64_ns(int64_t time);
static inline int64_t int64_ns_from_timespec(struct timespec time);
static inline bool timespec_eq(struct timespec a, struct timespec b);
static inline bool timespec_ne(struct timespec a, struct timespec b);
static inline bool timespec_lt(struct timespec a, struct timespec b);
static inline bool timespec_lte(struct timespec a, struct timespec b);
static inline bool timespec_gt(struct timespec a, struct timespec b);
static inline bool timespec_gte(struct timespec a, struct timespec b);
static inline struct timespec timespec_sub(struct timespec a, struct timespec b);
#include "timespec_utils.def.h"
#endif