From fd653075f8dd8ff2676c474fdbc38557962d623c Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sun, 6 Jun 2021 11:42:13 +0900 Subject: [PATCH] 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. --- include/mutex.sh | 6 +++++- include/wmutex.sh | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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 -- 2.47.3