]> git.corax.cc Git - toolbox/commitdiff
include/sem: Implement sem_trywait() using sem_wait() with 0 timeout
authorMatthias Kruk <m@m10k.eu>
Mon, 26 Sep 2022 08:58:53 +0000 (17:58 +0900)
committerMatthias Kruk <m@m10k.eu>
Mon, 26 Sep 2022 08:58:53 +0000 (17:58 +0900)
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

index 7189ab78ba38c1a8ab70b7a39bd4923ec6375956..2fdfbaabbe05c0b321a52956e5fd4239fd41cac6 100644 (file)
@@ -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() {