]> code.delx.au - pulseaudio/blobdiff - src/pulse/thread-mainloop.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / pulse / thread-mainloop.c
index 6b66696c400790caae7c4fdbea5b151ab8fa2972..435e9f6d481fb250b94f23b0360d3bdc6f501c5c 100644 (file)
@@ -6,7 +6,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
 #include <config.h>
 #endif
 
+#ifndef OS_IS_WIN32
+#include <pthread.h>
+#endif
+
 #include <signal.h>
 #include <stdio.h>
 
-#ifdef HAVE_POLL_H
-#include <poll.h>
-#else
-#include <pulsecore/poll.h>
-#endif
-
 #include <pulse/xmalloc.h>
 #include <pulse/mainloop.h>
 
+#include <pulsecore/i18n.h>
 #include <pulsecore/log.h>
-#include <pulsecore/hashmap.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/mutex.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/poll.h>
 
 #include "thread-mainloop.h"
 
 struct pa_threaded_mainloop {
     pa_mainloop *real_mainloop;
-    int n_waiting;
+    volatile int n_waiting, n_waiting_for_accept;
 
     pa_thread* thread;
     pa_mutex* mutex;
     pa_cond* cond, *accept_cond;
+
+    char *name;
 };
 
 static inline int in_worker(pa_threaded_mainloop *m) {
@@ -67,7 +68,7 @@ static int poll_func(struct pollfd *ufds, unsigned long nfds, int timeout, void
      * avahi_simple_poll_quit() can succeed from another thread. */
 
     pa_mutex_unlock(mutex);
-    r = poll(ufds, nfds, timeout);
+    r = pa_poll(ufds, nfds, timeout);
     pa_mutex_lock(mutex);
 
     return r;
@@ -94,6 +95,8 @@ static void thread(void *userdata) {
 pa_threaded_mainloop *pa_threaded_mainloop_new(void) {
     pa_threaded_mainloop *m;
 
+    pa_init_i18n();
+
     m = pa_xnew(pa_threaded_mainloop, 1);
 
     if (!(m->real_mainloop = pa_mainloop_new())) {
@@ -101,14 +104,16 @@ pa_threaded_mainloop *pa_threaded_mainloop_new(void) {
         return NULL;
     }
 
-    m->mutex = pa_mutex_new(TRUE, TRUE);
+    m->mutex = pa_mutex_new(true, true);
     m->cond = pa_cond_new();
     m->accept_cond = pa_cond_new();
     m->thread = NULL;
+    m->name = NULL;
 
     pa_mainloop_set_poll_func(m->real_mainloop, poll_func, m->mutex);
 
     m->n_waiting = 0;
+    m->n_waiting_for_accept = 0;
 
     return m;
 }
@@ -130,6 +135,7 @@ void pa_threaded_mainloop_free(pa_threaded_mainloop* m) {
     pa_cond_free(m->cond);
     pa_cond_free(m->accept_cond);
 
+    pa_xfree(m->name);
     pa_xfree(m);
 }
 
@@ -138,7 +144,7 @@ int pa_threaded_mainloop_start(pa_threaded_mainloop *m) {
 
     pa_assert(!m->thread || !pa_thread_is_running(m->thread));
 
-    if (!(m->thread = pa_thread_new(thread, m)))
+    if (!(m->thread = pa_thread_new(m->name ? m->name : "threaded-ml", thread, m)))
         return -1;
 
     return 0;
@@ -178,15 +184,21 @@ void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m) {
     pa_mutex_unlock(m->mutex);
 }
 
+/* Called with the lock taken */
 void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept) {
     pa_assert(m);
 
     pa_cond_signal(m->cond, 1);
 
-    if (wait_for_accept && m->n_waiting > 0)
-        pa_cond_wait(m->accept_cond, m->mutex);
+    if (wait_for_accept) {
+        m->n_waiting_for_accept ++;
+
+        while (m->n_waiting_for_accept > 0)
+            pa_cond_wait(m->accept_cond, m->mutex);
+    }
 }
 
+/* Called with the lock taken */
 void pa_threaded_mainloop_wait(pa_threaded_mainloop *m) {
     pa_assert(m);
 
@@ -201,12 +213,16 @@ void pa_threaded_mainloop_wait(pa_threaded_mainloop *m) {
     m->n_waiting --;
 }
 
+/* Called with the lock taken */
 void pa_threaded_mainloop_accept(pa_threaded_mainloop *m) {
     pa_assert(m);
 
     /* Make sure that this function is not called from the helper thread */
     pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
 
+    pa_assert(m->n_waiting_for_accept > 0);
+    m->n_waiting_for_accept --;
+
     pa_cond_signal(m->accept_cond, 0);
 }
 
@@ -227,3 +243,13 @@ int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m) {
 
     return m->thread && pa_thread_self() == m->thread;
 }
+
+void pa_threaded_mainloop_set_name(pa_threaded_mainloop *m, const char *name) {
+    pa_assert(m);
+    pa_assert(name);
+
+    m->name = pa_xstrdup(name);
+
+    if (m->thread)
+        pa_thread_set_name(m->thread, m->name);
+}