]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: add _cleanup_ wrappers for pthread_mutex_{lock,unlock}
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 Mar 2020 12:24:44 +0000 (13:24 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 Mar 2020 12:29:39 +0000 (13:29 +0100)
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.

src/basic/meson.build
src/basic/pthread-util.h [new file with mode: 0644]

index ccb22e159505a1cfb4fd5dddf13057055f6cc312..85953dab0c4b87521387905ba329b5d2b98f99e9 100644 (file)
@@ -169,6 +169,7 @@ basic_sources = files('''
         process-util.h
         procfs-util.c
         procfs-util.h
+        pthread-util.h
         quota-util.c
         quota-util.h
         random-util.c
diff --git a/src/basic/pthread-util.h b/src/basic/pthread-util.h
new file mode 100644 (file)
index 0000000..dc3eaba
--- /dev/null
@@ -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);
+}