]> code.delx.au - pulseaudio/blobdiff - src/pulse/thread-mainloop.c
Remove unnecessary #includes
[pulseaudio] / src / pulse / thread-mainloop.c
index c77cc64e307ebb218ca6d07238aa7fb77712899f..b07ad789aff2321eac90e14d382a66af660a306f 100644 (file)
 #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 <pulse/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;
@@ -68,7 +66,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;
@@ -112,6 +110,7 @@ pa_threaded_mainloop *pa_threaded_mainloop_new(void) {
     pa_mainloop_set_poll_func(m->real_mainloop, poll_func, m->mutex);
 
     m->n_waiting = 0;
+    m->n_waiting_for_accept = 0;
 
     return m;
 }
@@ -141,7 +140,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("threaded-ml", thread, m)))
         return -1;
 
     return 0;
@@ -181,15 +180,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);
 
@@ -204,12 +209,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);
 }