diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-06-26 15:04:13 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-06-26 15:04:13 -0400 |
commit | 1313875b02c690ca5a40e585d24fdec240bb419d (patch) | |
tree | 6cd7733b8e3ae07129955fcb627f313032456f10 /src | |
parent | abd29143d805fa16c65489d5b1d79428943d0187 (diff) | |
download | bfs-1313875b02c690ca5a40e585d24fdec240bb419d.tar.xz |
thread: Wrap more pthread APIs
Diffstat (limited to 'src')
-rw-r--r-- | src/ioq.c | 9 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/thread.h (renamed from src/lock.h) | 46 | ||||
-rw-r--r-- | src/xregex.c | 5 |
4 files changed, 38 insertions, 24 deletions
@@ -9,7 +9,7 @@ #include "config.h" #include "diag.h" #include "dir.h" -#include "lock.h" +#include "thread.h" #include "sanity.h" #include <assert.h> #include <errno.h> @@ -335,8 +335,7 @@ struct ioq *ioq_create(size_t depth, size_t nthreads) { } for (size_t i = 0; i < nthreads; ++i) { - errno = pthread_create(&ioq->threads[i], NULL, ioq_work, ioq); - if (errno != 0) { + if (thread_create(&ioq->threads[i], NULL, ioq_work, ioq) != 0) { goto fail; } ++ioq->nthreads; @@ -417,9 +416,7 @@ void ioq_destroy(struct ioq *ioq) { ioq_cancel(ioq); for (size_t i = 0; i < ioq->nthreads; ++i) { - if (pthread_join(ioq->threads[i], NULL) != 0) { - abort(); - } + thread_join(ioq->threads[i], NULL); } ioqq_destroy(ioq->ready); @@ -34,11 +34,11 @@ * - fsade.[ch] (a facade over non-standard filesystem features) * - ioq.[ch] (an async I/O queue) * - 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) * - stat.[ch] (wraps stat(), or statx() on Linux) + * - thread.h (multi-threading) * - trie.[ch] (a trie set/map implementation) * - typo.[ch] (fuzzy matching for typos) * - xregex.[ch] (regular expression support) diff --git a/src/lock.h b/src/thread.h index 2b5f951..b2edf17 100644 --- a/src/lock.h +++ b/src/thread.h @@ -2,26 +2,38 @@ // SPDX-License-Identifier: 0BSD /** - * Wrappers for POSIX synchronization primitives. + * Wrappers for POSIX threading APIs. */ -#ifndef BFS_LOCK_H -#define BFS_LOCK_H +#ifndef BFS_THREAD_H +#define BFS_THREAD_H #include "diag.h" #include <errno.h> #include <pthread.h> #include <string.h> -#define lock_verify(expr, cond) \ +#define thread_verify(expr, cond) \ bfs_verify((errno = (expr), (cond)), "%s: %s", #expr, strerror(errno)) /** - * Wrapper for pthread_mutex_init(). + * Wrapper for pthread_create(). * * @return * 0 on success, -1 on error. */ +#define thread_create(thread, attr, fn, arg) \ + ((errno = pthread_create(thread, attr, fn, arg)) ? -1 : 0) + +/** + * Wrapper for pthread_join(). + */ +#define thread_join(thread, ret) \ + thread_verify(pthread_join(thread, ret), errno == 0) + +/** + * Wrapper for pthread_mutex_init(). + */ #define mutex_init(mutex, attr) \ ((errno = pthread_mutex_init(mutex, attr)) ? -1 : 0) @@ -29,7 +41,7 @@ * Wrapper for pthread_mutex_lock(). */ #define mutex_lock(mutex) \ - lock_verify(pthread_mutex_lock(mutex), errno == 0) + thread_verify(pthread_mutex_lock(mutex), errno == 0) /** * Wrapper for pthread_mutex_trylock(). @@ -38,19 +50,19 @@ * Whether the mutex was locked. */ #define mutex_trylock(mutex) \ - (lock_verify(pthread_mutex_trylock(mutex), errno == 0 || errno == EBUSY), errno == 0) + (thread_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) + thread_verify(pthread_mutex_unlock(mutex), errno == 0) /** * Wrapper for pthread_mutex_destroy(). */ #define mutex_destroy(mutex) \ - lock_verify(pthread_mutex_destroy(mutex), errno == 0) + thread_verify(pthread_mutex_destroy(mutex), errno == 0) /** * Wrapper for pthread_cond_init(). @@ -62,24 +74,30 @@ * Wrapper for pthread_cond_wait(). */ #define cond_wait(cond, mutex) \ - lock_verify(pthread_cond_wait(cond, mutex), errno == 0) + thread_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) + thread_verify(pthread_cond_signal(cond), errno == 0) /** * Wrapper for pthread_cond_broadcast(). */ #define cond_broadcast(cond) \ - lock_verify(pthread_cond_broadcast(cond), errno == 0) + thread_verify(pthread_cond_broadcast(cond), errno == 0) /** * Wrapper for pthread_cond_destroy(). */ #define cond_destroy(cond) \ - lock_verify(pthread_cond_destroy(cond), errno == 0) + thread_verify(pthread_cond_destroy(cond), errno == 0) + +/** + * Wrapper for pthread_once(). + */ +#define call_once(once, fn) \ + thread_verify(pthread_once(once, fn), errno == 0) -#endif // BFS_LOCK_H +#endif // BFS_THREAD_H diff --git a/src/xregex.c b/src/xregex.c index 88df082..beb6676 100644 --- a/src/xregex.c +++ b/src/xregex.c @@ -5,6 +5,7 @@ #include "alloc.h" #include "config.h" #include "diag.h" +#include "thread.h" #include "sanity.h" #include <errno.h> #include <pthread.h> @@ -106,9 +107,7 @@ static void bfs_onig_once(void) { /** Initialize Oniguruma. */ static int bfs_onig_initialize(OnigEncoding *enc) { static pthread_once_t once = PTHREAD_ONCE_INIT; - if (pthread_once(&once, bfs_onig_once) != 0) { - return ONIGERR_FAIL_TO_INITIALIZE; - } + call_once(&once, bfs_onig_once); *enc = bfs_onig_enc; return bfs_onig_status; |