From 88d25ee98c5eeed842c2cfe67cce3d83d68f7423 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sun, 9 Aug 2020 13:26:55 +1000 Subject: [PATCH] [common] linux: improve event mechanics --- VERSION | 2 +- common/src/platform/linux/event.c | 73 +++++++++++++++---------------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/VERSION b/VERSION index dc7ca5ee..7cc964ca 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B2-rc2-25-g3511fb8d59+1 \ No newline at end of file +B2-rc2-26-g0f2ecdf5f1+1 \ No newline at end of file diff --git a/common/src/platform/linux/event.c b/common/src/platform/linux/event.c index 4495bf4a..7dc3699a 100644 --- a/common/src/platform/linux/event.c +++ b/common/src/platform/linux/event.c @@ -32,7 +32,7 @@ struct LGEvent { pthread_mutex_t mutex; pthread_cond_t cond; - uint32_t flag; + atomic_int signaled; bool autoReset; }; @@ -76,6 +76,13 @@ bool lgWaitEventAbs(LGEvent * handle, struct timespec * ts) { assert(handle); + if (atomic_load(&handle->signaled)) + { + if (handle->autoReset) + atomic_fetch_sub(&handle->signaled, 1); + return true; + } + if (pthread_mutex_lock(&handle->mutex) != 0) { DEBUG_ERROR("Failed to lock the mutex"); @@ -83,7 +90,7 @@ bool lgWaitEventAbs(LGEvent * handle, struct timespec * ts) } bool ret = true; - while(ret && !atomic_load(&handle->flag)) + while(ret && !atomic_load(&handle->signaled)) { if (!ts) { @@ -92,12 +99,17 @@ bool lgWaitEventAbs(LGEvent * handle, struct timespec * ts) DEBUG_ERROR("Wait to wait on the condition"); ret = false; } + + if (handle->autoReset) + atomic_fetch_sub(&handle->signaled, 1); } else { switch(pthread_cond_timedwait(&handle->cond, &handle->mutex, ts)) { case 0: + if (handle->autoReset) + atomic_fetch_sub(&handle->signaled, 1); break; case ETIMEDOUT: @@ -112,9 +124,6 @@ bool lgWaitEventAbs(LGEvent * handle, struct timespec * ts) } } - if (handle->autoReset) - atomic_store(&handle->flag, 0); - if (pthread_mutex_unlock(&handle->mutex) != 0) { DEBUG_ERROR("Failed to unlock the mutex"); @@ -126,6 +135,13 @@ bool lgWaitEventAbs(LGEvent * handle, struct timespec * ts) bool lgWaitEventNS(LGEvent * handle, unsigned int timeout) { + if (atomic_load(&handle->signaled)) + { + if (handle->autoReset) + atomic_fetch_sub(&handle->signaled, 1); + return true; + } + if (timeout == TIMEOUT_INFINITE) return lgWaitEventAbs(handle, NULL); @@ -145,6 +161,13 @@ bool lgWaitEventNS(LGEvent * handle, unsigned int timeout) bool lgWaitEvent(LGEvent * handle, unsigned int timeout) { + if (atomic_load(&handle->signaled)) + { + if (handle->autoReset) + atomic_fetch_sub(&handle->signaled, 1); + return true; + } + return lgWaitEventNS(handle, timeout * 1000000); } @@ -152,24 +175,13 @@ bool lgSignalEvent(LGEvent * handle) { assert(handle); - if (pthread_mutex_lock(&handle->mutex) != 0) + if (atomic_fetch_add(&handle->signaled, 1) == 0) { - DEBUG_ERROR("Failed to lock the mutex"); - return false; - } - - atomic_store(&handle->flag, 1); - - if (pthread_mutex_unlock(&handle->mutex) != 0) - { - DEBUG_ERROR("Failed to unlock the mutex"); - return false; - } - - if (pthread_cond_broadcast(&handle->cond) != 0) - { - DEBUG_ERROR("Failed to signal the condition"); - return false; + if (pthread_cond_broadcast(&handle->cond) != 0) + { + DEBUG_ERROR("Failed to signal the condition"); + return false; + } } return true; @@ -178,20 +190,7 @@ bool lgSignalEvent(LGEvent * handle) bool lgResetEvent(LGEvent * handle) { assert(handle); - - if (pthread_mutex_lock(&handle->mutex) != 0) - { - DEBUG_ERROR("Failed to lock the mutex"); - return false; - } - - atomic_store(&handle->flag, 0); - - if (pthread_mutex_unlock(&handle->mutex) != 0) - { - DEBUG_ERROR("Failed to unlock the mutex"); - return false; - } - + if (atomic_load(&handle->signaled)) + atomic_fetch_sub(&handle->signaled, 1); return true; }