From 468f5376adfc9103526df1805969852c8adfeeab Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 9 Dec 2014 04:27:45 +0100 Subject: improve with_mutex and with_mutex_if also MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/macros.h | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/libmdsserver') diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 40d68d0..6c8f944 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -90,10 +90,18 @@ * @param mutex:pthread_mutex_t The mutex * @param instructions The instructions to run while the mutex is locked */ -#define with_mutex(mutex, instructions) \ - errno = pthread_mutex_lock(&(mutex)); \ - instructions \ - errno = pthread_mutex_unlock(&(mutex)) +#define with_mutex(mutex, instructions) \ + do \ + { \ + errno = pthread_mutex_lock(&(mutex)); \ + do \ + { \ + instructions ; \ + } \ + while (0); \ + errno = pthread_mutex_unlock(&(mutex)); \ + } \ + while (0) /** * Wrapper for `pthread_mutex_lock` and `pthread_mutex_unlock` with an embedded if-statement @@ -103,12 +111,18 @@ * @param instructions The instructions to run while the mutex is locked */ #define with_mutex_if(mutex, condition, instructions) \ - errno = pthread_mutex_lock(&(mutex)); \ - if (condition) \ + do \ { \ - instructions \ + errno = pthread_mutex_lock(&(mutex)); \ + if (condition) \ + do \ + { \ + instructions ; \ + } \ + while (0); \ + errno = pthread_mutex_unlock(&(mutex)); \ } \ - errno = pthread_mutex_unlock(&(mutex)) + while (0) /** -- cgit v1.2.3-70-g09d2