[xiph-cvs] cvs commit: icecast/src stats.c

Jack Moffitt jack at xiph.org
Wed Feb 13 16:41:58 PST 2002



jack        02/02/13 16:41:57

  Modified:    src      stats.c
  Log:
  Fix race condition on thread shutdown.

Revision  Changes    Path
1.10      +29 -4     icecast/src/stats.c

Index: stats.c
===================================================================
RCS file: /usr/local/cvsroot/icecast/src/stats.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- stats.c	2002/02/14 00:28:41	1.9
+++ stats.c	2002/02/14 00:41:57	1.10
@@ -33,6 +33,7 @@
 
 int _stats_running = 1;
 long _stats_thread_id;
+int _stats_threads = 0;
 
 stats_t _stats;
 mutex_t _stats_mutex;
@@ -79,11 +80,20 @@
 
 void stats_shutdown()
 {
+	int n;
 
         /* wait for thread to exit */
         _stats_running = 0;
         thread_join(_stats_thread_id);
 
+	/* wait for other threads to shut down */
+	do {
+		thread_sleep(300000);
+		thread_mutex_lock(&_stats_mutex);
+		n = _stats_threads;
+		thread_mutex_unlock(&_stats_mutex);
+	} while (n > 0);
+
         /* free the queues */
 
         /* destroy the queue mutexes */
@@ -579,11 +589,16 @@
         mutex_t local_event_mutex;
         stats_event_t *event;
 
+	/* increment the thread count */
+	thread_mutex_lock(&_stats_mutex);
+	_stats_threads++;
+	thread_mutex_unlock(&_stats_mutex);
+
         thread_mutex_create(&local_event_mutex);
 
         _atomic_get_and_register(&local_event_queue, &local_event_mutex);
 
-	while (global.running == ICE_RUNNING) {
+	while (_stats_running) {
                 thread_mutex_lock(&local_event_mutex);
                 event = _get_event_from_queue(&local_event_queue);
                 if (event != NULL) {
@@ -596,7 +611,6 @@
                 } else {
                         thread_mutex_unlock(&local_event_mutex);
                         thread_cond_wait(&_event_signal_cond);
-			if (!_stats_running) break;
                         continue;
                 }
                                
@@ -605,6 +619,10 @@
 
         thread_mutex_destroy(&local_event_mutex);
 
+	thread_mutex_lock(&_stats_mutex);
+	_stats_threads--;
+	thread_mutex_unlock(&_stats_mutex);
+
         thread_exit(0);
         
         return NULL;
@@ -626,11 +644,15 @@
 
         callback = arg;
 
+	thread_mutex_lock(&_stats_mutex);
+	_stats_threads++;
+	thread_mutex_unlock(&_stats_mutex);
+
         thread_mutex_create(&local_event_mutex);
 
         _atomic_get_and_register(&local_event_queue, &local_event_mutex);
 
-	while (global.running == ICE_RUNNING) {
+	while (_stats_running) {
                 thread_mutex_lock(&local_event_mutex);
                 event = _get_event_from_queue(&local_event_queue);
                 if (event != NULL) {
@@ -639,7 +661,6 @@
                 } else {
                         thread_mutex_unlock(&local_event_mutex);
                         thread_cond_wait(&_event_signal_cond);
-			if (!_stats_running) break;
                         continue;
                 }
                 
@@ -647,6 +668,10 @@
         }
 
         thread_mutex_destroy(&local_event_mutex);
+
+	thread_mutex_lock(&_stats_mutex);
+	_stats_threads--;
+	thread_mutex_unlock(&_stats_mutex);
 
         thread_exit(0);
 

<p><p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list