From 31fb248fc6916f3988d99c10220ba42cf432611b Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Thu, 15 Apr 2021 22:35:00 +0900 Subject: [PATCH] include/sem: Add sem_peek() function This commit adds the sem_peek() function, which allows the caller to determine the value of a semaphore without modifying it. --- include/sem.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/sem.sh b/include/sem.sh index 4a04134..b59ac13 100644 --- a/include/sem.sh +++ b/include/sem.sh @@ -236,3 +236,31 @@ sem_post() { return "$err" } + +sem_peek() { + local name="$1" + + local mutex + local sem + local value + local err + + mutex=$(_sem_mutexpath "$name") + sem=$(_sem_sempath "$name") + err=false + + mutex_lock "$mutex" + + if ! value=$(<"$sem"); then + err=true + fi + + mutex_unlock "$mutex" + + if "$err"; then + return 1 + fi + + echo "$value" + return 0 +} -- 2.47.3