diff --git a/libs/libc/semaphore/sem_post.c b/libs/libc/semaphore/sem_post.c index 874954a8a2..d34b6fea56 100644 --- a/libs/libc/semaphore/sem_post.c +++ b/libs/libc/semaphore/sem_post.c @@ -151,24 +151,26 @@ int nxsem_post(FAR sem_t *sem) } # endif - if (fastpath) + while (fastpath) { - int32_t old; - int32_t new; FAR atomic_t *val = mutex ? NXSEM_MHOLDER(sem) : NXSEM_COUNT(sem); + int32_t old = atomic_read(val); + int32_t new; if (mutex) { - old = _SCHED_GETTID(); + if (NXSEM_MBLOCKING(old)) + { + break; + } + new = NXSEM_NO_MHOLDER; } else { - old = atomic_read(val); - if (old < 0) { - goto out; + break; } new = old + 1; @@ -179,9 +181,6 @@ int nxsem_post(FAR sem_t *sem) return OK; } } - -out: - #else UNUSED(mutex); UNUSED(fastpath); diff --git a/libs/libc/semaphore/sem_trywait.c b/libs/libc/semaphore/sem_trywait.c index 39112aa219..8f1686ea02 100644 --- a/libs/libc/semaphore/sem_trywait.c +++ b/libs/libc/semaphore/sem_trywait.c @@ -146,43 +146,35 @@ int nxsem_trywait(FAR sem_t *sem) } # endif - if (fastpath) + while (fastpath) { - bool ret = false; - int32_t old; - int32_t new; FAR atomic_t *val = mutex ? NXSEM_MHOLDER(sem) : NXSEM_COUNT(sem); + int32_t old = atomic_read(val); + int32_t new; if (mutex) { - old = NXSEM_NO_MHOLDER; + if (old != NXSEM_NO_MHOLDER) + { + return -EAGAIN; + } + + new = _SCHED_GETTID(); } else { - old = atomic_read(val); + if (old < 1) + { + return -EAGAIN; + } + + new = old - 1; } - do + if (atomic_try_cmpxchg_acquire(val, &old, new)) { - if (!mutex) - { - if (old < 1) - { - break; - } - - new = old - 1; - } - else - { - new = _SCHED_GETTID(); - } - - ret = atomic_try_cmpxchg_acquire(NXSEM_MHOLDER(sem), &old, new); + return OK; } - while (!mutex && !ret); - - return ret ? OK : -EAGAIN; } #else diff --git a/libs/libc/semaphore/sem_wait.c b/libs/libc/semaphore/sem_wait.c index 6a187e23ac..dca20eb4bd 100644 --- a/libs/libc/semaphore/sem_wait.c +++ b/libs/libc/semaphore/sem_wait.c @@ -174,24 +174,26 @@ int nxsem_wait(FAR sem_t *sem) } # endif - if (fastpath) + while (fastpath) { - int32_t old; - int32_t new; FAR atomic_t *val = mutex ? NXSEM_MHOLDER(sem) : NXSEM_COUNT(sem); + int32_t old = atomic_read(val); + int32_t new; if (mutex) { - old = NXSEM_NO_MHOLDER; + if (old != NXSEM_NO_MHOLDER) + { + break; + } + new = _SCHED_GETTID(); } else { - old = atomic_read(val); - if (old < 1) { - goto out; + break; } new = old - 1; @@ -202,9 +204,6 @@ int nxsem_wait(FAR sem_t *sem) return OK; } } - -out: - #else UNUSED(mutex); UNUSED(fastpath);