From 87a714861c5746d990b44b6e0839f082d7068f81 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 11 Apr 2023 18:05:25 -0400 Subject: lock: Add wrappers for POSIX synchronization primitives --- Makefile | 3 ++- src/lock.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 1 + 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/lock.h diff --git a/Makefile b/Makefile index aa8617e..20b4d07 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,8 @@ LOCAL_CPPFLAGS := \ -D_LARGEFILE64_SOURCE \ -D_FILE_OFFSET_BITS=64 \ -D_TIME_BITS=64 \ - -DBFS_VERSION=\"$(VERSION)\" + -DBFS_VERSION=\"$(VERSION)\" \ + -pthread LOCAL_CFLAGS := -std=c17 LOCAL_LDFLAGS := 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 +// SPDX-License-Identifier: 0BSD + +/** + * Wrappers for POSIX synchronization primitives. + */ + +#ifndef BFS_LOCK_H +#define BFS_LOCK_H + +#include "diag.h" +#include +#include + +#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 diff --git a/src/main.c b/src/main.c index c354553..95be48f 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,7 @@ * - dstring.[ch] (a dynamic string library) * - fsade.[ch] (a facade over non-standard filesystem features) * - list.h (linked list macros) + * - lock.h (mutexes, condition variables, etc.) * - mtab.[ch] (parses the system's mount table) * - pwcache.[ch] (a cache for the user/group tables) * - sanity.h (sanitizer interfaces) -- cgit v1.2.3