-/* $Id$ */
-
/***
This file is part of PulseAudio.
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) {
* 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;
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())) {
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;
}
pa_cond_free(m->cond);
pa_cond_free(m->accept_cond);
+ pa_xfree(m->name);
pa_xfree(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;
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);
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);
}
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);
+}