]> git.corax.cc Git - toolbox/commitdiff
include/{mutex,wmutex}: Wait forever if timeout is 0 or missing
authorMatthias Kruk <matthias.kruk@miraclelinux.com>
Sun, 6 Jun 2021 02:42:13 +0000 (11:42 +0900)
committerMatthias Kruk <matthias.kruk@miraclelinux.com>
Sun, 6 Jun 2021 02:42:13 +0000 (11:42 +0900)
The mutex_lock() and wmutex_lock() functions return immediately if
the timeout is set to 0 or is missing. However, to be compatible with
the previous timeout-less implementations, the functions must wait
forever if no timeout was specified.
This commit changes the two functions so they wait forever if the
timeout is 0 or missing.

include/mutex.sh
include/wmutex.sh

index 0bb71c2808a4475dc2e90b10e4ce51a6033d5406..596a931ec5df95738ab819875076ed2de5aa813e 100644 (file)
@@ -23,8 +23,12 @@ mutex_lock() {
        local lock="$1"
        local -i timeout="$2"
 
+       local -i remaining
+
+       remaining="$timeout"
+
        while ! mutex_trylock "$lock"; do
-               if (( --timeout < 0 )); then
+               if (( timeout != 0 && --remaining < 0 )); then
                        return 1
                fi
 
index d4bcf99be8ee5b4effbfc26b7e3695cbb49605f9..37f508ecf0669b1a9c4620f834017e8f62a18166 100644 (file)
@@ -23,8 +23,12 @@ wmutex_lock() {
        local lock="$1"
        local -i timeout="$2"
 
+       local -i remaining
+
+       remaining="$timeout"
+
        while ! wmutex_trylock "$lock"; do
-               if (( --timeout < 0 )); then
+               if (( timeout != 0 && --remaining < 0 )); then
                        return 1
                fi