55 68
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
#ifndef COR_PLATFORM_H
#define COR_PLATFORM_H
// platform specific parts collected together
#include <stdbool.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
// inspired by CPython to achieve platform indenpendence for thread local variables
#ifdef thread_local
#define _Cor_thread_local thread_local
#elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
#define _Cor_thread_local _Thread_local
#elif defined(_MSC_VER) /* AKA NT_THREADS */
#define _Cor_thread_local __declspec(thread)
#elif defined(__GNUC__) /* includes clang */
#define _Cor_thread_local __thread
#else
#define _Cor_thread_local
#endif
// malloc & free...
static inline void *
_Cor_Malloc(size_t size){
return malloc(size);
}
static inline void
_Cor_Free(void *ptr){
free(ptr);
}
// ...malloc & free
// see CPython again, this time in ctypes.h
#define COROUTINE_HAVE_ALLOCA_H 1
// Non-reentrant Mutexes
typedef struct _Cor_Mutex {
pthread_mutex_t mut;
} _Cor_Mutex;
extern void _Cor_Mutex_ctor(_Cor_Mutex *);
extern void _Cor_Mutex_dtor(_Cor_Mutex *);
extern void _Cor_Mutex_Lock(_Cor_Mutex *);
extern void _Cor_Mutex_Unlock(_Cor_Mutex *);
// The 'now' to use for _Cor_Semaphore_Wait, in ns.
extern int64_t _Cor_Realtime_Now();
typedef struct _Cor_Semaphore {
pthread_mutex_t mut;
pthread_cond_t cond;
unsigned count;
} _Cor_Semaphore;
extern void _Cor_Semaphore_ctor(_Cor_Semaphore *sem);
extern void _Cor_Sempahore_dtor(_Cor_Semaphore *sem);
// timeout_when < 0 means 'wait forever'
// Returns true for success, false for timeout
extern bool _Cor_Semaphore_Wait(_Cor_Semaphore *sem, int64_t timeout_when);
extern void _Cor_Semaphore_Signal(_Cor_Semaphore *sem);
typedef struct _Cor_Thread {
pthread_t th;
bool joined;
} _Cor_Thread;
extern void _Cor_Thread_ctor(_Cor_Thread *, void *(*)(void *), void *);
extern void _Cor_Thread_dtor(_Cor_Thread *);
extern void *_Cor_Thread_Join(_Cor_Thread *);
#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
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
#ifndef COR_PLATFORM_H
#define COR_PLATFORM_H
// platform specific parts collected together
#include <stdbool.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
// inspired by CPython to achieve platform indenpendence for thread local variables
#ifdef thread_local
#define _Cor_thread_local thread_local
#elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
#define _Cor_thread_local _Thread_local
#elif defined(_MSC_VER) /* AKA NT_THREADS */
#define _Cor_thread_local __declspec(thread)
#elif defined(__GNUC__) /* includes clang */
#define _Cor_thread_local __thread
#else
#define _Cor_thread_local
#endif
// see CPython again, this time in ctypes.h
#define COROUTINE_HAVE_ALLOCA_H 1
// Non-reentrant Mutexes
typedef struct _Cor_Mutex {
pthread_mutex_t mut;
} _Cor_Mutex;
extern int _Cor_Mutex_ctor(_Cor_Mutex *);
extern int _Cor_Mutex_dtor(_Cor_Mutex *);
extern int _Cor_Mutex_Lock(_Cor_Mutex *);
extern int _Cor_Mutex_Unlock(_Cor_Mutex *);
// The 'now' to use for _Cor_Semaphore_Wait, in ns.
extern int64_t _Cor_Realtime_Now();
typedef struct _Cor_Semaphore {
pthread_mutex_t mut;
pthread_cond_t cond;
unsigned count;
} _Cor_Semaphore;
extern void _Cor_Semaphore_ctor(_Cor_Semaphore *sem);
extern void _Cor_Sempahore_dtor(_Cor_Semaphore *sem);
// timeout_when < 0 means 'wait forever'
// Returns true for success, false for timeout
extern bool _Cor_Semaphore_Wait(_Cor_Semaphore *sem, int64_t timeout_when);
extern void _Cor_Semaphore_Signal(_Cor_Semaphore *sem);
typedef struct _Cor_Thread {
pthread_t th;
bool joined;
} _Cor_Thread;
extern void _Cor_Thread_ctor(_Cor_Thread *, void *(*)(void *), void *);
extern void _Cor_Thread_dtor(_Cor_Thread *);
extern void *_Cor_Thread_Join(_Cor_Thread *);
#endif