From 03918564cf43cfbd4f873f1b9c6ec3ccd15ede91 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 17 Jun 2016 09:18:40 +0200 Subject: [PATCH] ipc: Simplify mutex_try_lock(). 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 | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/ipc.c b/ipc.c index 478d307..c6f7e6b 100644 --- a/ipc.c +++ b/ipc.c @@ -13,6 +13,7 @@ #include #include #include +#include #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; } -- 2.39.5