From aed0b2f64d44cf444a733ca96304a689f003ad30 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Mon, 26 Sep 2022 17:58:53 +0900 Subject: [PATCH] include/sem: Implement sem_trywait() using sem_wait() with 0 timeout Because sem_trywait() does not unlock the waitlock if the counter is greater than 1, the next caller will be left waiting until sem_post() is executed. This commit changes sem_trywait() to call sem_wait() with a timeout of 0 seconds, since this issue does not affect sem_wait(). --- include/sem.sh | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/include/sem.sh b/include/sem.sh index 7189ab7..2fdfbaa 100644 --- a/include/sem.sh +++ b/include/sem.sh @@ -227,27 +227,8 @@ sem_wait() { sem_trywait() { local name="$1" - local waitlock - local countlock - local counter - local err - - waitlock=$(_sem_get_waitlock "$name") - countlock=$(_sem_get_countlock "$name") - counter=$(_sem_get_counter "$name") - err=1 - - if ! wmutex_trylock "$waitlock"; then - return 1 - fi - - if mutex_lock "$countlock"; then - _sem_counter_dec "$counter" - mutex_unlock "$countlock" - err=0 - fi - - return "$err" + sem_wait "$name" 0 + return "$?" } sem_post() { -- 2.47.3