From bf3052009ab62ef66a8dee5f6ebd774ab2c6c895 Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Mon, 8 Jul 2024 15:49:22 -0500 Subject: [PATCH] util/cnd_monotonic: Use a void * on Windows This is the same thing that our win32 implementation of c11/threads does and it allows us to avoid using CONDITION_VARIABLE in a header. Part-of: --- src/util/cnd_monotonic.c | 17 ++++++++++++----- src/util/cnd_monotonic.h | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/util/cnd_monotonic.c b/src/util/cnd_monotonic.c index 2900f1be494..c9c508a8e6b 100644 --- a/src/util/cnd_monotonic.c +++ b/src/util/cnd_monotonic.c @@ -32,6 +32,11 @@ #ifdef _WIN32 #include + +static_assert(sizeof(struct u_cnd_monotonic) == sizeof(CONDITION_VARIABLE), + "The size of u_cnd_monotonic must equal to CONDITION_VARIABLE"); +static_assert(sizeof(mtx_t) == sizeof(CRITICAL_SECTION), + "The size of mtx_t must equal to CRITICAL_SECTION"); #endif int @@ -40,7 +45,7 @@ u_cnd_monotonic_init(struct u_cnd_monotonic *cond) assert(cond != NULL); #ifdef _WIN32 - InitializeConditionVariable(&cond->condvar); + InitializeConditionVariable((PCONDITION_VARIABLE)cond); return thrd_success; #else int ret = thrd_error; @@ -76,7 +81,7 @@ u_cnd_monotonic_broadcast(struct u_cnd_monotonic *cond) assert(cond != NULL); #ifdef _WIN32 - WakeAllConditionVariable(&cond->condvar); + WakeAllConditionVariable((PCONDITION_VARIABLE)cond); return thrd_success; #else return (pthread_cond_broadcast(&cond->cond) == 0) ? thrd_success : thrd_error; @@ -89,7 +94,7 @@ u_cnd_monotonic_signal(struct u_cnd_monotonic *cond) assert(cond != NULL); #ifdef _WIN32 - WakeConditionVariable(&cond->condvar); + WakeConditionVariable((PCONDITION_VARIABLE)cond); return thrd_success; #else return (pthread_cond_signal(&cond->cond) == 0) ? thrd_success : thrd_error; @@ -108,7 +113,8 @@ u_cnd_monotonic_timedwait(struct u_cnd_monotonic *cond, mtx_t *mtx, const uint64_t future = (abs_time->tv_sec * 1000) + (abs_time->tv_nsec / 1000000); const uint64_t now = os_time_get_nano() / 1000000; const DWORD timeout = (future > now) ? (DWORD)(future - now) : 0; - if (SleepConditionVariableCS(&cond->condvar, mtx, timeout)) + if (SleepConditionVariableCS((PCONDITION_VARIABLE)cond, + (PCRITICAL_SECTION)mtx, timeout)) return thrd_success; return (GetLastError() == ERROR_TIMEOUT) ? thrd_timedout : thrd_error; #else @@ -126,7 +132,8 @@ u_cnd_monotonic_wait(struct u_cnd_monotonic *cond, mtx_t *mtx) assert(mtx != NULL); #ifdef _WIN32 - SleepConditionVariableCS(&cond->condvar, mtx, INFINITE); + SleepConditionVariableCS((PCONDITION_VARIABLE)cond, + (PCRITICAL_SECTION)mtx, INFINITE); return thrd_success; #else return (pthread_cond_wait(&cond->cond, mtx) == 0) ? thrd_success : thrd_error; diff --git a/src/util/cnd_monotonic.h b/src/util/cnd_monotonic.h index 977a7e902f3..049b5f07364 100644 --- a/src/util/cnd_monotonic.h +++ b/src/util/cnd_monotonic.h @@ -41,7 +41,7 @@ extern "C" { struct u_cnd_monotonic { #ifdef _WIN32 - CONDITION_VARIABLE condvar; + void *Ptr; #else pthread_cond_t cond; #endif