116 lines
2.4 KiB
C
116 lines
2.4 KiB
C
#include <drang/alloc.h>
|
|
#include <drang/sync.h>
|
|
|
|
#include "internal.h"
|
|
|
|
size_t drang_cond_size(void)
|
|
{
|
|
return sizeof(struct drang_cond);
|
|
}
|
|
|
|
int drang_cond_new(struct drang_cond **cond)
|
|
{
|
|
struct drang_cond *c = NULL;
|
|
DRANG_BEGIN_TRY();
|
|
|
|
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
|
|
|
|
c = DRANG_TRY_ALLOC_T(struct drang_cond);
|
|
|
|
DRANG_TRY(drang_cond_init(c));
|
|
|
|
DRANG_RETURN_IN(cond, c);
|
|
|
|
DRANG_CATCH(_)
|
|
{
|
|
if (c != NULL) {
|
|
drang_free(c);
|
|
}
|
|
}
|
|
DRANG_END_TRY();
|
|
}
|
|
|
|
void drang_cond_free(struct drang_cond *cond)
|
|
{
|
|
if (cond == NULL) {
|
|
return;
|
|
}
|
|
|
|
drang_cond_fini(cond);
|
|
drang_free(cond);
|
|
}
|
|
|
|
int drang_cond_init(struct drang_cond *cond)
|
|
{
|
|
DRANG_BEGIN_TRY();
|
|
|
|
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
|
|
DRANG_CHECK(!cond->initialized, DRANG_EBUSY);
|
|
|
|
InitializeConditionVariable(&cond->cv);
|
|
cond->initialized = true;
|
|
|
|
DRANG_END_TRY_IGNORE();
|
|
}
|
|
|
|
void drang_cond_fini(struct drang_cond *cond)
|
|
{
|
|
if (cond == NULL) {
|
|
return;
|
|
}
|
|
|
|
// Windows CONDITION_VARIABLE doesn't require explicit cleanup
|
|
cond->initialized = false;
|
|
}
|
|
|
|
int drang_cond_signal(struct drang_cond *cond)
|
|
{
|
|
DRANG_BEGIN_TRY();
|
|
|
|
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
|
|
DRANG_CHECK(cond->initialized, DRANG_EINVAL);
|
|
|
|
WakeConditionVariable(&cond->cv);
|
|
|
|
DRANG_END_TRY_IGNORE();
|
|
}
|
|
|
|
int drang_cond_broadcast(struct drang_cond *cond)
|
|
{
|
|
DRANG_BEGIN_TRY();
|
|
|
|
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
|
|
DRANG_CHECK(cond->initialized, DRANG_EINVAL);
|
|
|
|
WakeAllConditionVariable(&cond->cv);
|
|
|
|
DRANG_END_TRY_IGNORE();
|
|
}
|
|
|
|
int drang_cond_wait(struct drang_cond *cond, struct drang_mutex *mutex)
|
|
{
|
|
return drang_cond_timedwait(cond, mutex, 0);
|
|
}
|
|
|
|
int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uint64_t timeout_ms)
|
|
{
|
|
DWORD timeout = (timeout_ms == 0) ? INFINITE : (DWORD) timeout_ms;
|
|
BOOL result;
|
|
|
|
DRANG_BEGIN_TRY();
|
|
|
|
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
|
|
DRANG_CHECK(cond->initialized, DRANG_EINVAL);
|
|
DRANG_CHECK(mutex != NULL, DRANG_EINVAL);
|
|
DRANG_CHECK(mutex->initialized, DRANG_EINVAL);
|
|
|
|
result = SleepConditionVariableCS(&cond->cv, &mutex->cs, timeout);
|
|
if (!result) {
|
|
if (GetLastError() == ERROR_TIMEOUT) {
|
|
DRANG_FAIL(DRANG_ETIMEDOUT);
|
|
}
|
|
DRANG_FAIL(DRANG_EINVAL); // Generic error for other failures
|
|
}
|
|
|
|
DRANG_END_TRY_IGNORE();
|
|
}
|