]> git.corax.cc Git - toolbox/commitdiff
include/sem: Add sem_peek() function
authorMatthias Kruk <m@m10k.eu>
Thu, 15 Apr 2021 13:35:00 +0000 (22:35 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 15 Apr 2021 13:35:00 +0000 (22:35 +0900)
This commit adds the sem_peek() function, which allows the caller to
determine the value of a semaphore without modifying it.

include/sem.sh

index 4a041343219ab1440c0afe407059136963d782d9..b59ac13d47b3769f5315fa317bc22e18f52b8adb 100644 (file)
@@ -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
+}