[xiph-commits] r15614 - in icecast/trunk: icecast/src thread

karl at svn.xiph.org karl at svn.xiph.org
Thu Jan 8 19:18:04 PST 2009


Author: karl
Date: 2009-01-08 19:18:03 -0800 (Thu, 08 Jan 2009)
New Revision: 15614

Modified:
   icecast/trunk/icecast/src/connection.c
   icecast/trunk/icecast/src/fserve.c
   icecast/trunk/thread/thread.c
   icecast/trunk/thread/thread.h
Log:
Add handlers for spinlocks if available, map to mutexes when not.


Modified: icecast/trunk/icecast/src/connection.c
===================================================================
--- icecast/trunk/icecast/src/connection.c	2009-01-08 03:35:54 UTC (rev 15613)
+++ icecast/trunk/icecast/src/connection.c	2009-01-09 03:18:03 UTC (rev 15614)
@@ -103,7 +103,7 @@
     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 @@
 {
     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 @@
  
     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 @@
 {
     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;
 }

Modified: icecast/trunk/icecast/src/fserve.c
===================================================================
--- icecast/trunk/icecast/src/fserve.c	2009-01-08 03:35:54 UTC (rev 15613)
+++ icecast/trunk/icecast/src/fserve.c	2009-01-09 03:18:03 UTC (rev 15614)
@@ -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 @@
     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_shutdown(void)
 {
-    thread_mutex_lock (&pending_lock);
+    thread_spin_lock (&pending_lock);
     run_fserv = 0;
     while (pending_list)
     {
@@ -127,8 +129,8 @@
     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 @@
     }
     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 @@
     /* 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 @@
         /* 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 @@
                 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 @@
     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 @@
         else
             type = strdup ("application/octet-stream");
     }
-    thread_mutex_unlock (&pending_lock);
+    thread_spin_unlock (&pending_lock);
     return type;
 }
 
@@ -622,7 +624,7 @@
  */
 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 @@
         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 @@
     }
     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);
 }
 

Modified: icecast/trunk/thread/thread.c
===================================================================
--- icecast/trunk/thread/thread.c	2009-01-08 03:35:54 UTC (rev 15613)
+++ icecast/trunk/thread/thread.c	2009-01-09 03:18:03 UTC (rev 15614)
@@ -195,6 +195,7 @@
         avl_tree_free(_mutextree, _free_mutex);
 #endif
         avl_tree_free(_threadtree, _free_thread);
+        _threadtree = NULL;
     }
 
 #ifdef THREAD_DEBUG
@@ -222,6 +223,7 @@
         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 @@
         avl_delete(_threadtree, th, _free_thread);
         _mutex_unlock(&_threadtree_mutex);
     }
-    
+
     pthread_exit ((void*)val);
 }
 
@@ -811,3 +813,29 @@
 }
 
 
+#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
+

Modified: icecast/trunk/thread/thread.h
===================================================================
--- icecast/trunk/thread/thread.h	2009-01-08 03:35:54 UTC (rev 15613)
+++ icecast/trunk/thread/thread.h	2009-01-09 03:18:03 UTC (rev 15614)
@@ -89,6 +89,24 @@
     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__)



More information about the commits mailing list