From: Matthias Kruk Date: Sun, 6 Jun 2021 02:42:13 +0000 (+0900) Subject: include/{mutex,wmutex}: Wait forever if timeout is 0 or missing X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=fd653075f8dd8ff2676c474fdbc38557962d623c;p=toolbox include/{mutex,wmutex}: Wait forever if timeout is 0 or missing 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. --- diff --git a/include/mutex.sh b/include/mutex.sh index 0bb71c2..596a931 100644 --- a/include/mutex.sh +++ b/include/mutex.sh @@ -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 diff --git a/include/wmutex.sh b/include/wmutex.sh index d4bcf99..37f508e 100644 --- a/include/wmutex.sh +++ b/include/wmutex.sh @@ -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