From 19c2fe9b5e2d3a9206621579a934d5cf478f99d1 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sun, 9 Aug 2020 14:44:00 +1000 Subject: [PATCH] Revert "[common] linux: improve event mechanics" The logic here is wrong, this should be done externally as multiple waiters will cause issues --- VERSION | 2 +- common/src/platform/linux/event.c | 73 ++++++++++++++++--------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/VERSION b/VERSION index 7cc964ca..dc7ca5ee 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B2-rc2-26-g0f2ecdf5f1+1 \ No newline at end of file +B2-rc2-25-g3511fb8d59+1 \ No newline at end of file diff --git a/common/src/platform/linux/event.c b/common/src/platform/linux/event.c index 7dc3699a..4495bf4a 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; - atomic_int signaled; + uint32_t flag; bool autoReset; }; @@ -76,13 +76,6 @@ 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"); @@ -90,7 +83,7 @@ bool lgWaitEventAbs(LGEvent * handle, struct timespec * ts) } bool ret = true; - while(ret && !atomic_load(&handle->signaled)) + while(ret && !atomic_load(&handle->flag)) { if (!ts) { @@ -99,17 +92,12 @@ 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: @@ -124,6 +112,9 @@ 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"); @@ -135,13 +126,6 @@ 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); @@ -161,13 +145,6 @@ 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); } @@ -175,13 +152,24 @@ bool lgSignalEvent(LGEvent * handle) { assert(handle); - if (atomic_fetch_add(&handle->signaled, 1) == 0) + if (pthread_mutex_lock(&handle->mutex) != 0) { - if (pthread_cond_broadcast(&handle->cond) != 0) - { - DEBUG_ERROR("Failed to signal the condition"); - return false; - } + 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; } return true; @@ -190,7 +178,20 @@ bool lgSignalEvent(LGEvent * handle) bool lgResetEvent(LGEvent * handle) { assert(handle); - if (atomic_load(&handle->signaled)) - atomic_fetch_sub(&handle->signaled, 1); + + 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; + } + return true; }