From 15d3ee525aea4e7cea3133039bd23f4c14a978ba Mon Sep 17 00:00:00 2001 From: bumbread Date: Tue, 21 Feb 2023 10:15:02 +1100 Subject: [PATCH] condition variables pt. 1 --- inc/os_win/threads_types.h | 7 +++++-- inc/threads.h | 6 +----- src/os_win/thread.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/inc/os_win/threads_types.h b/inc/os_win/threads_types.h index 1da440f..4ef470f 100644 --- a/inc/os_win/threads_types.h +++ b/inc/os_win/threads_types.h @@ -1,6 +1,8 @@ // 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 @@ -21,8 +23,9 @@ typedef union once_flag { void *ptr; } once_flag; +// This mirrors CONDITION_VARIABLE type (aka _RTL_CONDITION_VARIABLE) typedef struct cnd_t { - int idk_yet; + void *ptr; } cnd_t; typedef struct mtx_t { diff --git a/inc/threads.h b/inc/threads.h index c9b594d..800d810 100644 --- a/inc/threads.h +++ b/inc/threads.h @@ -83,11 +83,7 @@ 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 -); +int cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx, const struct timespec *restrict ts); // Call once void call_once(once_flag *flag, void (*func)(void)); diff --git a/src/os_win/thread.c b/src/os_win/thread.c index d3505d6..3130cd7 100644 --- a/src/os_win/thread.c +++ b/src/os_win/thread.c @@ -200,6 +200,34 @@ void call_once(once_flag *flag, void (*func)(void)) { 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 void mtx_destroy(mtx_t *mtx) {