From 3937e807fe5b4ff06c814be9c6938ca979835f61 Mon Sep 17 00:00:00 2001 From: Karl Heyes Date: Fri, 9 Jan 2009 03:18:03 +0000 Subject: [PATCH] Add handlers for spinlocks if available, map to mutexes when not. svn path=/icecast/trunk/thread/; revision=15614 --- thread/thread.c | 30 +++++++++++++++++++++++++++++- thread/thread.h | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/thread/thread.c b/thread/thread.c index 528cfab..e842448 100644 --- a/thread/thread.c +++ b/thread/thread.c @@ -195,6 +195,7 @@ void thread_shutdown(void) avl_tree_free(_mutextree, _free_mutex); #endif avl_tree_free(_threadtree, _free_thread); + _threadtree = NULL; } #ifdef THREAD_DEBUG @@ -222,6 +223,7 @@ static void _block_signals(void) sigdelset(&ss, SIGKILL); sigdelset(&ss, SIGSTOP); sigdelset(&ss, SIGSEGV); + sigdelset(&ss, SIGCHLD); sigdelset(&ss, SIGBUS); if (pthread_sigmask(SIG_BLOCK, &ss, NULL) != 0) { #ifdef THREAD_DEBUG @@ -594,7 +596,7 @@ void thread_exit_c(long val, int line, char *file) avl_delete(_threadtree, th, _free_thread); _mutex_unlock(&_threadtree_mutex); } - + pthread_exit ((void*)val); } @@ -811,3 +813,29 @@ static int _free_thread(void *key) } +#ifdef HAVE_PTHREAD_SPIN_LOCK +void thread_spin_create (spin_t *spin) +{ + int x = pthread_spin_init (&spin->lock, PTHREAD_PROCESS_PRIVATE); + if (x) + abort(); +} + +void thread_spin_destroy (spin_t *spin) +{ + pthread_spin_destroy (&spin->lock); +} + +void thread_spin_lock (spin_t *spin) +{ + int x = pthread_spin_lock (&spin->lock); + if (x != 0) + abort(); +} + +void thread_spin_unlock (spin_t *spin) +{ + pthread_spin_unlock (&spin->lock); +} +#endif + diff --git a/thread/thread.h b/thread/thread.h index 5a608bd..9b50040 100644 --- a/thread/thread.h +++ b/thread/thread.h @@ -89,6 +89,24 @@ typedef struct { pthread_rwlock_t sys_rwlock; } rwlock_t; +#ifdef HAVE_PTHREAD_SPIN_LOCK +typedef struct +{ + pthread_spinlock_t lock; +} spin_t; + +void thread_spin_create (spin_t *spin); +void thread_spin_destroy (spin_t *spin); +void thread_spin_lock (spin_t *spin); +void thread_spin_unlock (spin_t *spin); +#else +typedef mutex_t spin_t; +#define thread_spin_create(x) thread_mutex_create(x) +#define thread_spin_destroy(x) thread_mutex_destroy(x) +#define thread_spin_lock(x) thread_mutex_lock(x) +#define thread_spin_unlock(x) thread_mutex_unlock(x) +#endif + #define thread_create(n,x,y,z) thread_create_c(n,x,y,z,__LINE__,__FILE__) #define thread_mutex_create(x) thread_mutex_create_c(x,__LINE__,__FILE__) #define thread_mutex_lock(x) thread_mutex_lock_c(x,__LINE__,__FILE__)