diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-12-09 04:27:45 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-12-09 04:27:52 +0100 |
commit | 468f5376adfc9103526df1805969852c8adfeeab (patch) | |
tree | ed31bb52ccf74cd61403f94a2a3f7ec861c21370 | |
parent | make some macros easiler on the syntax (diff) | |
download | mds-468f5376adfc9103526df1805969852c8adfeeab.tar.gz mds-468f5376adfc9103526df1805969852c8adfeeab.tar.bz2 mds-468f5376adfc9103526df1805969852c8adfeeab.tar.xz |
improve with_mutex and with_mutex_if also
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/libmdsserver/macros.h | 30 | ||||
-rw-r--r-- | src/mds-server/slavery.c | 4 |
2 files changed, 23 insertions, 11 deletions
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) /** diff --git a/src/mds-server/slavery.c b/src/mds-server/slavery.c index 283251a..df6d552 100644 --- a/src/mds-server/slavery.c +++ b/src/mds-server/slavery.c @@ -137,9 +137,7 @@ client_t* initialise_client(int client_fd) pthread_mutex_unlock(&slave_mutex); free(information); if (entry != LINKED_LIST_UNUSED) - { - with_mutex (slave_mutex, linked_list_remove(&client_list, entry);); - } + with_mutex (slave_mutex, linked_list_remove(&client_list, entry);); errno = errno_; return NULL; } |