condition variables pt. 1

This commit is contained in:
bumbread 2023-02-21 10:15:02 +11:00
parent bc49be9e0c
commit 15d3ee525a
3 changed files with 34 additions and 7 deletions

View File

@ -1,6 +1,8 @@
// Note(bumbread): this file is included into threads.h when compiled under // Note(bumbread): this file is included into threads.h when compiled under
// windows. // windows. Here we avoid including windows.h directly because it may be
// undesireable to include a bunch of non-standard names and types into user's
// files just to get a few of them.
#pragma once #pragma once
@ -21,8 +23,9 @@ typedef union once_flag {
void *ptr; void *ptr;
} once_flag; } once_flag;
// This mirrors CONDITION_VARIABLE type (aka _RTL_CONDITION_VARIABLE)
typedef struct cnd_t { typedef struct cnd_t {
int idk_yet; void *ptr;
} cnd_t; } cnd_t;
typedef struct mtx_t { typedef struct mtx_t {

View File

@ -83,11 +83,7 @@ int cnd_broadcast(cnd_t *cond);
void cnd_destroy (cnd_t *cond); void cnd_destroy (cnd_t *cond);
int cnd_signal (cnd_t *cond); int cnd_signal (cnd_t *cond);
int cnd_wait (cnd_t *cond, mtx_t *mtx); int cnd_wait (cnd_t *cond, mtx_t *mtx);
int cnd_timedwait( int cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx, const struct timespec *restrict ts);
cnd_t *restrict cond,
mtx_t *restrict mtx,
const struct timespec *restrict ts
);
// Call once // Call once
void call_once(once_flag *flag, void (*func)(void)); void call_once(once_flag *flag, void (*func)(void));

View File

@ -200,6 +200,34 @@ void call_once(once_flag *flag, void (*func)(void)) {
InitOnceExecuteOnce((void *)flag, _call_once_trampoline, funcp, NULL); InitOnceExecuteOnce((void *)flag, _call_once_trampoline, funcp, NULL);
} }
// Condition variables
int cnd_init(cnd_t *cond) {
InitializeConditionVariable((void *)cond);
}
int cnd_broadcast(cnd_t *cond) {
WakeAllConditionVariable((void *)cond);
return thrd_success;
}
void cnd_destroy(cnd_t *cond) {
return; // Does nothing
}
int cnd_signal(cnd_t *cond) {
WakeConditionVariable((void *)cond);
return thrd_success;
}
int cnd_wait(cnd_t *cond, mtx_t *mtx) {
return thrd_error; // TODO after mutexes
}
int cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx, const struct timespec *restrict ts) {
return thrd_error; // TODO after mutexes
}
// Mutex functions // Mutex functions
void mtx_destroy(mtx_t *mtx) { void mtx_destroy(mtx_t *mtx) {