2022-06-11 04:49:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-07-31 09:11:28 +00:00
|
|
|
#define thread_local _Thread_local
|
|
|
|
|
2022-06-28 02:08:03 +00:00
|
|
|
#define ONCE_FLAG_INIT 1
|
2022-06-11 04:49:07 +00:00
|
|
|
#define TSS_DTOR_ITERATIONS 32
|
|
|
|
|
2022-08-04 09:47:38 +00:00
|
|
|
#if !defined(_timespec_defined)
|
|
|
|
#define _timespec_defined
|
|
|
|
typedef unsigned long long time_t;
|
|
|
|
struct timespec {
|
|
|
|
time_t tv_sec;
|
|
|
|
long tv_nsec;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2022-06-28 02:08:03 +00:00
|
|
|
#if defined(_WIN32)
|
2022-08-04 09:47:38 +00:00
|
|
|
typedef struct cnd_t {
|
|
|
|
int idk_yet;
|
|
|
|
} cnd_t;
|
|
|
|
|
|
|
|
typedef struct thrd_t {
|
|
|
|
void* handle;
|
|
|
|
} thrd_t;
|
|
|
|
|
|
|
|
typedef struct tss_t {
|
|
|
|
int idk_yet;
|
|
|
|
} tss_t;
|
|
|
|
|
|
|
|
typedef struct mtx_t {
|
|
|
|
int type;
|
|
|
|
// Done to handle recursive mutexes
|
|
|
|
unsigned long recursion;
|
|
|
|
unsigned long owner;
|
2022-08-05 03:00:32 +00:00
|
|
|
_Atomic(int) counter;
|
2022-08-04 09:47:38 +00:00
|
|
|
void* semaphore;
|
|
|
|
} mtx_t;
|
2022-06-14 22:46:14 +00:00
|
|
|
#else
|
2022-08-04 09:47:38 +00:00
|
|
|
#error "Not implemented"
|
2022-06-14 22:46:14 +00:00
|
|
|
#endif
|
2022-06-11 04:49:07 +00:00
|
|
|
|
2022-06-28 02:08:03 +00:00
|
|
|
typedef void(*tss_dtor_t) (void*);
|
|
|
|
typedef int (*thrd_start_t)(void*);
|
|
|
|
typedef int once_flag; // TODO: change this maybe?
|
2022-06-11 04:49:07 +00:00
|
|
|
|
|
|
|
enum {
|
2022-06-28 02:08:03 +00:00
|
|
|
mtx_plain = 0,
|
|
|
|
mtx_recursive = 1,
|
|
|
|
mtx_timed = 2,
|
2022-06-11 04:49:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
thrd_success,
|
|
|
|
thrd_timedout,
|
|
|
|
thrd_busy,
|
|
|
|
thrd_error,
|
|
|
|
thrd_nomem
|
|
|
|
};
|
|
|
|
|
|
|
|
void call_once(once_flag *flag, void (*func)(void));
|
2022-06-14 22:46:14 +00:00
|
|
|
|
2022-06-28 02:08:03 +00:00
|
|
|
int cnd_init (cnd_t *cond);
|
|
|
|
int cnd_broadcast(cnd_t *cond);
|
|
|
|
void cnd_destroy (cnd_t *cond);
|
|
|
|
int cnd_signal (cnd_t *cond);
|
|
|
|
int cnd_wait (cnd_t *cond, mtx_t *mtx);
|
|
|
|
int cnd_timedwait(
|
|
|
|
cnd_t *restrict cond,
|
|
|
|
mtx_t *restrict mtx,
|
|
|
|
const struct timespec *restrict ts
|
|
|
|
);
|
|
|
|
|
|
|
|
void mtx_destroy (mtx_t *mtx);
|
|
|
|
int mtx_init (mtx_t *mtx, int type);
|
|
|
|
int mtx_unlock (mtx_t *mtx);
|
|
|
|
int mtx_trylock (mtx_t *mtx);
|
|
|
|
int mtx_lock (mtx_t *mtx);
|
|
|
|
int mtx_timedlock(
|
|
|
|
mtx_t *restrict mtx,
|
|
|
|
const struct timespec *restrict ts
|
|
|
|
);
|
2022-06-14 22:46:14 +00:00
|
|
|
|
2022-06-11 04:49:07 +00:00
|
|
|
thrd_t thrd_current(void);
|
2022-06-28 02:08:03 +00:00
|
|
|
int thrd_create (thrd_t *thr, thrd_start_t func, void *arg);
|
|
|
|
int thrd_detach (thrd_t thr);
|
|
|
|
int thrd_equal (thrd_t thr0, thrd_t thr1);
|
|
|
|
int thrd_join (thrd_t thr, int *res);
|
|
|
|
void thrd_yield (void);
|
|
|
|
int thrd_sleep(
|
|
|
|
const struct timespec *duration,
|
|
|
|
struct timespec *remaining
|
|
|
|
);
|
2022-08-04 09:47:38 +00:00
|
|
|
_Noreturn void thrd_exit(int res);
|
2022-06-28 02:08:03 +00:00
|
|
|
|
|
|
|
int tss_create(tss_t *key, tss_dtor_t dtor);
|
|
|
|
void tss_delete(tss_t key);
|
|
|
|
void *tss_get (tss_t key);
|
|
|
|
int tss_set (tss_t key, void *val);
|