From 5bb6d7edbf4edbc135a4d9aeda19cd7994cb7077 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 2 Aug 2011 23:33:27 -0600 Subject: Wrap pthread API to reduce duplicated error tests. --- libdimension/threads.h | 151 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 142 insertions(+), 9 deletions(-) (limited to 'libdimension/threads.h') diff --git a/libdimension/threads.h b/libdimension/threads.h index 8e88e7c..229acb9 100644 --- a/libdimension/threads.h +++ b/libdimension/threads.h @@ -23,6 +23,8 @@ * Background threading interface. */ +#include + /** * Thread callback type. * @param[in,out] ptr An arbitrary pointer. @@ -30,6 +32,15 @@ */ typedef int dmnsn_thread_fn(void *ptr); +/** + * Create a thread that cleans up after itself on errors. + * @param[in,out] progress The progress object to associate with the thread. + * @param[in] thread_fn The thread callback. + * @param[in,out] arg The pointer to pass to the thread callback. + */ +DMNSN_INTERNAL void dmnsn_new_thread(dmnsn_progress *progress, + dmnsn_thread_fn *thread_fn, void *arg); + /** * Thread callback type for parallel tasks. * @param[in,out] ptr An arbitrary pointer. @@ -40,15 +51,6 @@ typedef int dmnsn_thread_fn(void *ptr); typedef int dmnsn_ccthread_fn(void *ptr, unsigned int thread, unsigned int nthreads); -/** - * Create a thread that cleans up after itself on errors. - * @param[in,out] progress The progress object to associate with the thread. - * @param[in] thread_fn The thread callback. - * @param[in,out] arg The pointer to pass to the thread callback. - */ -DMNSN_INTERNAL void dmnsn_new_thread(dmnsn_progress *progress, - dmnsn_thread_fn *thread_fn, void *arg); - /** * Run \p nthreads threads in parallel. * @param[in] ccthread_fn The routine to run in each concurrent thread. @@ -58,3 +60,134 @@ DMNSN_INTERNAL void dmnsn_new_thread(dmnsn_progress *progress, */ DMNSN_INTERNAL int dmnsn_execute_concurrently(dmnsn_ccthread_fn *ccthread_fn, void *arg, unsigned int nthreads); + +/** + * Initialize a mutex, bailing out on failure. + * @param[out] mutex The mutex to initialize. + */ +DMNSN_INTERNAL void dmnsn_initialize_mutex(pthread_mutex_t *mutex); + +/** dmnsn_lock_mutex() implementation. */ +DMNSN_INTERNAL void dmnsn_lock_mutex_impl(pthread_mutex_t *mutex); +/** dmnsn_unlock_mutex() implementation. */ +DMNSN_INTERNAL void dmnsn_unlock_mutex_impl(pthread_mutex_t *mutex); + +/** + * Lock a mutex, bailing out on failure. + * Contains a {, so must be used in the same block as dmnsn_unlock_mutex(). + * @param[in,out] mutex The mutex to lock. + */ +#define dmnsn_lock_mutex(mutex) dmnsn_lock_mutex_impl((mutex)); { + +/** + * Lock a mutex, bailing out on failure. + * Contains a }, so must be used in the same block as dmnsn_lock_mutex(). + * @param[in,out] mutex The mutex to unlock. + */ +#define dmnsn_unlock_mutex(mutex) dmnsn_unlock_mutex_impl((mutex)); } + +/** + * Destroy a mutex, warning on failure. + * @param[in,out] mutex The mutex to destroy. + */ +DMNSN_INTERNAL void dmnsn_destroy_mutex(pthread_mutex_t *mutex); + +/** + * Initialize a read-write lock, bailing out on failure. + * @param[out] rwlock The read-write lock to initialize. + */ +DMNSN_INTERNAL void dmnsn_initialize_rwlock(pthread_rwlock_t *rwlock); + +/** dmnsn_read_lock() implementation. */ +DMNSN_INTERNAL void dmnsn_read_lock_impl(pthread_rwlock_t *rwlock); +/** dmnsn_write_lock() implementation. */ +DMNSN_INTERNAL void dmnsn_write_lock_impl(pthread_rwlock_t *rwlock); +/** dmnsn_unlock_rwlock() implementation. */ +DMNSN_INTERNAL void dmnsn_unlock_rwlock_impl(pthread_rwlock_t *rwlock); + +/** + * Lock a read-write lock, bailing out on failure. + * Contains a {, so must be used in the same block as dmnsn_unlock_rwlock(). + * @param[in,out] rwlock The read-write lock to lock. + */ +#define dmnsn_read_lock(rwlock) dmnsn_read_lock_impl((rwlock)); { + +/** + * Lock a read-write lock, bailing out on failure. + * Contains a {, so must be used in the same block as dmnsn_unlock_rwlock(). + * @param[in,out] rwlock The read-write lock to lock. + */ +#define dmnsn_write_lock(rwlock) dmnsn_write_lock_impl((rwlock)); { + +/** + * Lock a read-write lock, bailing out on failure. + * Contains a }, so must be used in the same block as dmnsn_read_lock() or + * dmnsn_write_lock(). + * @param[in,out] rwlock The read-write lock to lock. + */ +#define dmnsn_unlock_rwlock(rwlock) dmnsn_unlock_rwlock_impl((rwlock)); } + +/** + * Destroy a read-write lock, warning on failure. + * @param[in,out] rwlock The read-write lock to destroy. + */ +DMNSN_INTERNAL void dmnsn_destroy_rwlock(pthread_rwlock_t *rwlock); + +/** + * Initialize a condition variable, bailing out on failure. + * @param[out] cond The condition variable to initialize. + */ +DMNSN_INTERNAL void dmnsn_initialize_cond(pthread_cond_t *cond); + +/** + * Wait on a condition variable, bailing out on error. + * @param[in] cond The condition variable to wait on. + * @param[in] mutex The associated mutex. + */ +DMNSN_INTERNAL void dmnsn_cond_wait(pthread_cond_t *cond, + pthread_mutex_t *mutex); + +/** + * Signal a condition variable, bailing out on error. + * @param[in] cond The condition variable to signal. + */ +DMNSN_INTERNAL void dmnsn_cond_broadcast(pthread_cond_t *cond); + +/** + * Destroy a condition variable, warning on failure. + * @param[in,out] cond The condition variable to destroy. + */ +DMNSN_INTERNAL void dmnsn_destroy_cond(pthread_cond_t *cond); + +/** + * Once-called callback type. + */ +typedef void dmnsn_once_fn(void); + +/** + * Call a function exactly once, bailing out on failure. + * @param[in,out] once The once control. + * @param[in] once_fn The function to call. + */ +DMNSN_INTERNAL void dmnsn_once(pthread_once_t *once, dmnsn_once_fn *once_fn); + +/** + * Initialize a thread-local storage key, bailing out on failure. + * @param[out] key The key to initialize. + * @param[in] destructor An optional destructor callback. + */ +DMNSN_INTERNAL void dmnsn_key_create(pthread_key_t *key, + dmnsn_callback_fn *destructor); + +/** + * Set a thread-specific pointer, bailing out on failure. + * @param[in] key The thread-local storage key. + * @param[in] value The value to set. + */ +DMNSN_INTERNAL void dmnsn_setspecific(pthread_key_t key, const void *value); + +/** + * Destroy a thread-local storage key, warning out on failure. + * @param[out] key The key to destroy. + */ +DMNSN_INTERNAL void dmnsn_key_delete(pthread_key_t key); -- cgit v1.2.3