[pulseaudio-commits] r1370 - in /trunk/src: pulsecore/thread-posix.c tests/thread-test.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Mon Sep 4 15:04:35 PDT 2006


Author: lennart
Date: Tue Sep  5 00:04:33 2006
New Revision: 1370

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=1370&root=pulseaudio&view=rev
Log:
fix pa_thread_is_running() for foreign threads; fix a memory leak for foreign threads

Modified:
    trunk/src/pulsecore/thread-posix.c
    trunk/src/tests/thread-test.c

Modified: trunk/src/pulsecore/thread-posix.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/thread-posix.c?rev=1370&root=pulseaudio&r1=1369&r2=1370&view=diff
==============================================================================
--- trunk/src/pulsecore/thread-posix.c (original)
+++ trunk/src/pulsecore/thread-posix.c Tue Sep  5 00:04:33 2006
@@ -26,6 +26,7 @@
 #include <assert.h>
 #include <pthread.h>
 #include <sched.h>
+#include <errno.h>
 
 #include <atomic_ops.h>
 
@@ -56,8 +57,18 @@
 static pa_mutex *once_mutex;
 static pthread_once_t thread_once_once = PTHREAD_ONCE_INIT;
 
+static void tls_free_cb(void *p) {
+    pa_thread *t = p;
+
+    assert(t);
+    
+    if (!t->thread_func) 
+        /* This is a foreign thread, we need to free the struct */
+        pa_xfree(t);
+}
+
 static void thread_tls_once_func(void) {
-    thread_tls = pa_tls_new(NULL);
+    thread_tls = pa_tls_new(tls_free_cb);
     assert(thread_tls);
 }
 
@@ -80,6 +91,8 @@
 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
     pa_thread *t;
 
+    assert(thread_func);
+    
     t = pa_xnew(pa_thread, 1);
     t->thread_func = thread_func;
     t->userdata = userdata;
@@ -98,6 +111,17 @@
 int pa_thread_is_running(pa_thread *t) {
     AO_t r;
     assert(t);
+
+    if (!t->thread_func) {
+        /* Mhmm, this is a foreign thread, t->running is not
+         * necessarily valid. We misuse pthread_getschedparam() to
+         * check if the thread is valid. This might not be portable. */
+
+        int policy;
+        struct sched_param param;
+        
+        return pthread_getschedparam(t->id, &policy, &param) >= 0 || errno != ESRCH;
+    }
 
     r = AO_load_full(&t->running);
     return r == 1 || r == 2;

Modified: trunk/src/tests/thread-test.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/tests/thread-test.c?rev=1370&root=pulseaudio&r1=1369&r2=1370&view=diff
==============================================================================
--- trunk/src/tests/thread-test.c (original)
+++ trunk/src/tests/thread-test.c Tue Sep  5 00:04:33 2006
@@ -87,6 +87,8 @@
     int i, k;
     pa_thread* t[THREADS_MAX];
 
+    assert(pa_thread_is_running(pa_thread_self()));
+    
     mutex = pa_mutex_new(0);
     cond1 = pa_cond_new();
     cond2 = pa_cond_new();




More information about the pulseaudio-commits mailing list