diff options
Diffstat (limited to 'src/thread.h')
-rw-r--r-- | src/thread.h | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/src/thread.h b/src/thread.h index b2edf17..3dd8422 100644 --- a/src/thread.h +++ b/src/thread.h @@ -8,13 +8,10 @@ #ifndef BFS_THREAD_H #define BFS_THREAD_H -#include "diag.h" -#include <errno.h> #include <pthread.h> -#include <string.h> -#define thread_verify(expr, cond) \ - bfs_verify((errno = (expr), (cond)), "%s: %s", #expr, strerror(errno)) +/** Thread entry point type. */ +typedef void *thread_fn(void *arg); /** * Wrapper for pthread_create(). @@ -22,26 +19,27 @@ * @return * 0 on success, -1 on error. */ -#define thread_create(thread, attr, fn, arg) \ - ((errno = pthread_create(thread, attr, fn, arg)) ? -1 : 0) +int thread_create(pthread_t *thread, const pthread_attr_t *attr, thread_fn *fn, void *arg); + +/** + * Set the name of a thread. + */ +void thread_setname(pthread_t thread, const char *name); /** * Wrapper for pthread_join(). */ -#define thread_join(thread, ret) \ - thread_verify(pthread_join(thread, ret), errno == 0) +void thread_join(pthread_t thread, void **ret); /** * Wrapper for pthread_mutex_init(). */ -#define mutex_init(mutex, attr) \ - ((errno = pthread_mutex_init(mutex, attr)) ? -1 : 0) +int mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr); /** * Wrapper for pthread_mutex_lock(). */ -#define mutex_lock(mutex) \ - thread_verify(pthread_mutex_lock(mutex), errno == 0) +void mutex_lock(pthread_mutex_t *mutex); /** * Wrapper for pthread_mutex_trylock(). @@ -49,55 +47,49 @@ * @return * Whether the mutex was locked. */ -#define mutex_trylock(mutex) \ - (thread_verify(pthread_mutex_trylock(mutex), errno == 0 || errno == EBUSY), errno == 0) +bool mutex_trylock(pthread_mutex_t *mutex); /** * Wrapper for pthread_mutex_unlock(). */ -#define mutex_unlock(mutex) \ - thread_verify(pthread_mutex_unlock(mutex), errno == 0) +void mutex_unlock(pthread_mutex_t *mutex); /** * Wrapper for pthread_mutex_destroy(). */ -#define mutex_destroy(mutex) \ - thread_verify(pthread_mutex_destroy(mutex), errno == 0) +void mutex_destroy(pthread_mutex_t *mutex); /** * Wrapper for pthread_cond_init(). */ -#define cond_init(cond, attr) \ - ((errno = pthread_cond_init(cond, attr)) ? -1 : 0) +int cond_init(pthread_cond_t *cond, pthread_condattr_t *attr); /** * Wrapper for pthread_cond_wait(). */ -#define cond_wait(cond, mutex) \ - thread_verify(pthread_cond_wait(cond, mutex), errno == 0) +void cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); /** * Wrapper for pthread_cond_signal(). */ -#define cond_signal(cond) \ - thread_verify(pthread_cond_signal(cond), errno == 0) +void cond_signal(pthread_cond_t *cond); /** * Wrapper for pthread_cond_broadcast(). */ -#define cond_broadcast(cond) \ - thread_verify(pthread_cond_broadcast(cond), errno == 0) +void cond_broadcast(pthread_cond_t *cond); /** * Wrapper for pthread_cond_destroy(). */ -#define cond_destroy(cond) \ - thread_verify(pthread_cond_destroy(cond), errno == 0) +void cond_destroy(pthread_cond_t *cond); + +/** pthread_once() callback type. */ +typedef void once_fn(void); /** * Wrapper for pthread_once(). */ -#define call_once(once, fn) \ - thread_verify(pthread_once(once, fn), errno == 0) +void invoke_once(pthread_once_t *once, once_fn *fn); #endif // BFS_THREAD_H |