summaryrefslogtreecommitdiffstats
path: root/libdimension/threads.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2011-08-02 23:33:27 -0600
committerTavian Barnes <tavianator@gmail.com>2011-08-02 23:33:27 -0600
commit5bb6d7edbf4edbc135a4d9aeda19cd7994cb7077 (patch)
tree719e01ed959714ad804621981b639758f3568492 /libdimension/threads.h
parentebe36194a9f2e97345a296f7fe8c844690d03e85 (diff)
downloaddimension-5bb6d7edbf4edbc135a4d9aeda19cd7994cb7077.tar.xz
Wrap pthread API to reduce duplicated error tests.
Diffstat (limited to 'libdimension/threads.h')
-rw-r--r--libdimension/threads.h151
1 files changed, 142 insertions, 9 deletions
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 <pthread.h>
+
/**
* Thread callback type.
* @param[in,out] ptr An arbitrary pointer.
@@ -31,6 +33,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.
* @param[in] thread An ID for this thread, in [0, \p nthreads).
@@ -41,15 +52,6 @@ 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.
* @param[in,out] arg The pointer to pass to the thread callbacks.
@@ -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);