Commit ed58514e authored by Karl Heyes's avatar Karl Heyes

Add handlers for spinlocks if available, map to mutexes when not.

svn path=/icecast/trunk/icecast/; revision=15614
parent add7342a
......@@ -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;
}
......
......@@ -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);
}
......@@ -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
......@@ -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
......@@ -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__)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment