diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-03-28 13:24:44 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-03-28 13:29:39 +0100 |
commit | 29d4392ca05a31db53176f552041bb6183351d63 (patch) | |
tree | a38127f9f4c26252d653d91338fabd75fa08a7c4 /src/basic/pthread-util.h | |
parent | nss-systemd: add missing jump to unlock mutex (diff) | |
download | systemd-29d4392ca05a31db53176f552041bb6183351d63.tar.xz systemd-29d4392ca05a31db53176f552041bb6183351d63.zip |
basic: add _cleanup_ wrappers for pthread_mutex_{lock,unlock}
I put the helper functions in a separate header file, because they don't fit
anywhere else. pthread_mutex_{lock,unlock} is used in two places: nss-systemd
and hashmap. I don't indent to convert hashmap to use the helpers, because
there it'd make the code more complicated. Is it worth to create a new header
file even if the only use is in nss-systemd.c? I think yes, because it feels
clean and also I think it's likely that pthread_mutex_{lock,unlock} will be
used in other places later.
Diffstat (limited to 'src/basic/pthread-util.h')
-rw-r--r-- | src/basic/pthread-util.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/basic/pthread-util.h b/src/basic/pthread-util.h new file mode 100644 index 0000000000..dc3eaba436 --- /dev/null +++ b/src/basic/pthread-util.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include <pthread.h> + +#include "macro.h" + +static inline pthread_mutex_t* pthread_mutex_lock_assert(pthread_mutex_t *mutex) { + assert_se(pthread_mutex_lock(mutex) == 0); + return mutex; +} + +static inline void pthread_mutex_unlock_assertp(pthread_mutex_t **mutexp) { + if (*mutexp) + assert_se(pthread_mutex_unlock(*mutexp) == 0); +} |