summaryrefslogtreecommitdiffstats
path: root/src/lock.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-04-11 18:05:25 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-06-12 12:22:17 -0400
commit87a714861c5746d990b44b6e0839f082d7068f81 (patch)
tree8f35a7793f4c9529c6901e67b19997f226553a07 /src/lock.h
parentd001ab384ae9b694a833a5d6140106970f31ceb1 (diff)
downloadbfs-87a714861c5746d990b44b6e0839f082d7068f81.tar.xz
lock: Add wrappers for POSIX synchronization primitives
Diffstat (limited to 'src/lock.h')
-rw-r--r--src/lock.h84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/lock.h b/src/lock.h
new file mode 100644
index 0000000..2ca7876
--- /dev/null
+++ b/src/lock.h
@@ -0,0 +1,84 @@
+// Copyright © Tavian Barnes <tavianator@tavianator.com>
+// SPDX-License-Identifier: 0BSD
+
+/**
+ * Wrappers for POSIX synchronization primitives.
+ */
+
+#ifndef BFS_LOCK_H
+#define BFS_LOCK_H
+
+#include "diag.h"
+#include <errno.h>
+#include <pthread.h>
+
+#define lock_verify(expr, cond) \
+ bfs_verify((errno = (expr), (cond)), "%s: %s", #expr, strerror(errno))
+
+/**
+ * Wrapper for pthread_mutex_init().
+ *
+ * @return
+ * 0 on success, -1 on error.
+ */
+#define mutex_init(mutex, attr) \
+ ((errno = pthread_mutex_init(mutex, attr)) ? -1 : 0)
+
+/**
+ * Wrapper for pthread_mutex_lock().
+ */
+#define mutex_lock(mutex) \
+ lock_verify(pthread_mutex_lock(mutex), errno == 0)
+
+/**
+ * Wrapper for pthread_mutex_trylock().
+ *
+ * @return
+ * Whether the mutex was locked.
+ */
+#define mutex_trylock(mutex) \
+ (lock_verify(pthread_mutex_trylock(mutex), errno == 0 || errno == EBUSY), errno == 0)
+
+/**
+ * Wrapper for pthread_mutex_unlock().
+ */
+#define mutex_unlock(mutex) \
+ lock_verify(pthread_mutex_unlock(mutex), errno == 0)
+
+/**
+ * Wrapper for pthread_mutex_destroy().
+ */
+#define mutex_destroy(mutex) \
+ lock_verify(pthread_mutex_destroy(mutex), errno == 0)
+
+/**
+ * Wrapper for pthread_cond_init().
+ */
+#define cond_init(cond, attr) \
+ ((errno = pthread_cond_init(cond, attr)) ? -1 : 0)
+
+/**
+ * Wrapper for pthread_cond_wait().
+ */
+#define cond_wait(cond, mutex) \
+ lock_verify(pthread_cond_wait(cond, mutex), errno == 0)
+
+/**
+ * Wrapper for pthread_cond_signal().
+ */
+#define cond_signal(cond) \
+ lock_verify(pthread_cond_signal(cond), errno == 0)
+
+/**
+ * Wrapper for pthread_cond_broadcast().
+ */
+#define cond_broadcast(cond) \
+ lock_verify(pthread_cond_broadcast(cond), errno == 0)
+
+/**
+ * Wrapper for pthread_cond_destroy().
+ */
+#define cond_destroy(cond) \
+ lock_verify(pthread_cond_destroy(cond), errno == 0)
+
+#endif // BFS_LOCK_H