]> git.corax.cc Git - toolbox/commitdiff
include/{mutex,wmutex,queue,sem}: Fix timeout behavior of blocking calls
authorMatthias Kruk <matthias.kruk@miraclelinux.com>
Fri, 18 Jun 2021 07:15:24 +0000 (16:15 +0900)
committerMatthias Kruk <matthias.kruk@miraclelinux.com>
Fri, 18 Jun 2021 07:15:24 +0000 (16:15 +0900)
Because of a bug in the timeout handling, the mutex_lock(),
wmutex_lock(), queue_get(), and sem_wait() functions wait forever if a
positive timeout is specified.
This commit changes the behavior so that the functions will wait
forever if the timeout is -1 or omitted, return immediately if the
timeout is zero, or otherwise wait for the specified number of seconds.

include/mutex.sh
include/queue.sh
include/sem.sh
include/wmutex.sh

index eb8de81e4b6546015acddfeb865aacfeff4cc6f4..1d04ce05faa9d2a1630b65c1c58c11d118a0dbbd 100644 (file)
@@ -34,12 +34,12 @@ mutex_lock() {
        local lock="$1"
        local -i timeout="$2"
 
-       local -i remaining
-
-       remaining="$timeout"
+       if (( $# < 2 )); then
+               timeout=-1
+       fi
 
        while ! mutex_trylock "$lock"; do
-               if (( timeout != 0 && --remaining < 0 )); then
+               if (( timeout-- == 0 )); then
                        return 1
                fi
 
index c1c8b9c4f55d960a0551987c200d489e9fb6a1e9..fbcb8ffb77dcdacfea41baa515abd978a702db69 100644 (file)
@@ -387,6 +387,10 @@ queue_get() {
        local item
        local err
 
+       if (( $# < 2 )); then
+               timeout=-1
+       fi
+
        sem=$(_queue_get_sem "$queue")
        mutex=$(_queue_get_mutex "$queue")
        data=$(_queue_get_data "$queue")
index 8446901234f76dc0e679bf1f01e0f95ae1696cf9..7189ab78ba38c1a8ab70b7a39bd4923ec6375956 100644 (file)
@@ -191,6 +191,10 @@ sem_wait() {
        local counter
        local err
 
+       if (( $# < 2 )); then
+               timeout=-1
+       fi
+
        waitlock=$(_sem_get_waitlock "$name")
        countlock=$(_sem_get_countlock "$name")
        counter=$(_sem_get_counter "$name")
index 5b8593f3892b9eec3a457c9e15ca38badbcfe1a4..3ce6f2dfc6da9973419704f2a72fb39eb89e32f0 100644 (file)
@@ -34,12 +34,12 @@ wmutex_lock() {
        local lock="$1"
        local -i timeout="$2"
 
-       local -i remaining
-
-       remaining="$timeout"
+       if (( $# < 2 )); then
+               timeout=-1
+       fi
 
        while ! wmutex_trylock "$lock"; do
-               if (( timeout != 0 && --remaining < 0 )); then
+               if (( timeout-- == 0 )); then
                        return 1
                fi