diff --git a/src/connection.c b/src/connection.c index ab7cdd61..b660938e 100644 --- a/src/connection.c +++ b/src/connection.c @@ -103,7 +103,7 @@ typedef struct avl_tree *contents; } cache_file_contents; -static mutex_t _connection_mutex; +static spin_t _connection_lock; static volatile unsigned long _current_id = 0; static int _initialized = 0; @@ -141,7 +141,7 @@ void connection_initialize(void) { if (_initialized) return; - thread_mutex_create(&_connection_mutex); + thread_spin_create (&_connection_lock); thread_mutex_create(&move_clients_mutex); thread_rwlock_create(&_source_shutdown_rwlock); thread_cond_create(&global.shutdown_cond); @@ -171,7 +171,7 @@ void connection_shutdown(void) thread_cond_destroy(&global.shutdown_cond); thread_rwlock_destroy(&_source_shutdown_rwlock); - thread_mutex_destroy(&_connection_mutex); + thread_spin_destroy (&_connection_lock); thread_mutex_destroy(&move_clients_mutex); _initialized = 0; @@ -181,9 +181,9 @@ static unsigned long _next_connection_id(void) { unsigned long id; - thread_mutex_lock(&_connection_mutex); + thread_spin_lock (&_connection_lock); id = _current_id++; - thread_mutex_unlock(&_connection_mutex); + thread_spin_unlock (&_connection_lock); return id; } diff --git a/src/fserve.c b/src/fserve.c index 38b83a11..46881966 100644 --- a/src/fserve.c +++ b/src/fserve.c @@ -66,10 +66,10 @@ #define BUFSIZE 4096 -static fserve_t *active_list = NULL; -static volatile fserve_t *pending_list = NULL; +fserve_t *active_list = NULL; +fserve_t *pending_list = NULL; -static mutex_t pending_lock; +static spin_t pending_lock; static avl_tree *mimetypes = NULL; static volatile int run_fserv = 0; @@ -97,7 +97,9 @@ void fserve_initialize(void) ice_config_t *config = config_get_config(); mimetypes = NULL; - thread_mutex_create (&pending_lock); + active_list = NULL; + pending_list = NULL; + thread_spin_create (&pending_lock); fserve_recheck_mime_types (config); config_release_config(); @@ -108,7 +110,7 @@ void fserve_initialize(void) void fserve_shutdown(void) { - thread_mutex_lock (&pending_lock); + thread_spin_lock (&pending_lock); run_fserv = 0; while (pending_list) { @@ -127,8 +129,8 @@ void fserve_shutdown(void) if (mimetypes) avl_tree_free (mimetypes, _delete_mapping); - thread_mutex_unlock (&pending_lock); - thread_mutex_destroy (&pending_lock); + thread_spin_unlock (&pending_lock); + thread_spin_destroy (&pending_lock); INFO0("file serving stopped"); } @@ -155,9 +157,9 @@ int fserve_client_waiting (void) } if (!ufds) { - thread_mutex_lock (&pending_lock); + thread_spin_lock (&pending_lock); run_fserv = 0; - thread_mutex_unlock (&pending_lock); + thread_spin_unlock (&pending_lock); return -1; } else if (poll(ufds, fserve_clients, 200) > 0) @@ -196,9 +198,9 @@ int fserve_client_waiting (void) /* hack for windows, select needs at least 1 descriptor */ if (fd_max == SOCK_ERROR) { - thread_mutex_lock (&pending_lock); + thread_spin_lock (&pending_lock); run_fserv = 0; - thread_mutex_unlock (&pending_lock); + thread_spin_unlock (&pending_lock); return -1; } else @@ -236,7 +238,7 @@ static int wait_for_fds(void) /* add any new clients here */ if (pending_list) { - thread_mutex_lock (&pending_lock); + thread_spin_lock (&pending_lock); fclient = (fserve_t*)pending_list; while (fclient) @@ -249,7 +251,7 @@ static int wait_for_fds(void) fserve_clients++; } pending_list = NULL; - thread_mutex_unlock (&pending_lock); + thread_spin_unlock (&pending_lock); } /* drop out of here if someone is ready */ ret = fserve_client_waiting(); @@ -338,7 +340,7 @@ char *fserve_content_type (const char *path) void *result; char *type; - thread_mutex_lock (&pending_lock); + thread_spin_lock (&pending_lock); if (mimetypes && !avl_get_by_key (mimetypes, &exttype, &result)) { mime_type *mime = result; @@ -367,7 +369,7 @@ char *fserve_content_type (const char *path) else type = strdup ("application/octet-stream"); } - thread_mutex_unlock (&pending_lock); + thread_spin_unlock (&pending_lock); return type; } @@ -622,7 +624,7 @@ fail: */ static void fserve_add_pending (fserve_t *fclient) { - thread_mutex_lock (&pending_lock); + thread_spin_lock (&pending_lock); fclient->next = (fserve_t *)pending_list; pending_list = fclient; if (run_fserv == 0) @@ -631,7 +633,7 @@ static void fserve_add_pending (fserve_t *fclient) DEBUG0 ("fserve handler waking up"); thread_create("File Serving Thread", fserv_thread_function, NULL, THREAD_DETACHED); } - thread_mutex_unlock (&pending_lock); + thread_spin_unlock (&pending_lock); } @@ -758,10 +760,10 @@ void fserve_recheck_mime_types (ice_config_t *config) } fclose(mimefile); - thread_mutex_lock (&pending_lock); + thread_spin_lock (&pending_lock); if (mimetypes) avl_tree_free (mimetypes, _delete_mapping); mimetypes = new_mimetypes; - thread_mutex_unlock (&pending_lock); + thread_spin_unlock (&pending_lock); } diff --git a/src/thread/thread.c b/src/thread/thread.c index 528cfab4..e842448e 100644 --- a/src/thread/thread.c +++ b/src/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/src/thread/thread.h b/src/thread/thread.h index 5a608bdb..9b50040b 100644 --- a/src/thread/thread.h +++ b/src/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__)