]> git.tuebingen.mpg.de Git - dss.git/commitdiff
ipc: Simplify mutex_try_lock().
authorAndre Noll <maan@tuebingen.mpg.de>
Fri, 17 Jun 2016 07:18:40 +0000 (09:18 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 21 Aug 2016 19:13:25 +0000 (21:13 +0200)
There is no need to actually obtain the lock. A single semaphore
operation will do just fine. With sem_op equal to zero and IPC_NOWAIT
the semop() call returns immediately, and the return value tells
whether the semaphore value was zero.

Rename the (static) function to mutex_is_locked() to indicate that
it performs only read-only operations on the semaphore set.

ipc.c

diff --git a/ipc.c b/ipc.c
index 478d307e37e4d7f3179596e1b23358cd2528cb77..c6f7e6b00f52c466ea051a40c188c739c7fd060b 100644 (file)
--- a/ipc.c
+++ b/ipc.c
@@ -13,6 +13,7 @@
 #include <stddef.h>
 #include <limits.h>
 #include <sys/param.h>
+#include <stdbool.h>
 
 #include "gcc-compat.h"
 #include "str.h"
@@ -310,25 +311,21 @@ static int mutex_lock(int id)
        return 1;
 }
 
-static int mutex_try_lock(int id)
+static bool mutex_is_locked(int id)
 {
-       struct sembuf sops[2];
+       struct sembuf sops;
        int ret;
 
        DSS_DEBUG_LOG(("trying to lock\n"));
 
-       sops[0].sem_num = 0;
-       sops[0].sem_op = 0;
-       sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
+       sops.sem_num = 0;
+       sops.sem_op = 0;
+       sops.sem_flg = SEM_UNDO | IPC_NOWAIT;
 
-       sops[1].sem_num = 0;
-       sops[1].sem_op = 1;
-       sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT;
-
-       ret = do_semop(id, sops, 2);
+       ret = do_semop(id, &sops, 1);
        if (ret < 0)
-               return -ERRNO_TO_DSS_ERROR(errno);
-       return 1;
+               return true;
+       return false;
 }
 
 int lock_dss(char *config_file)
@@ -356,8 +353,5 @@ int get_dss_pid(char *config_file, pid_t *pid)
                return -E_NOT_RUNNING;
        if (pid)
                *pid = ret;
-       ret = mutex_try_lock(semid);
-       if (ret >= 0)
-               return -E_NOT_RUNNING;
-       return 1;
+       return mutex_is_locked(semid)? 1 : -E_NOT_RUNNING;
 }