2006-04-30 23:34:17 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2006-04-30 23:34:17 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2006-04-30 23:34:17 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2006-04-30 23:34:17 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2006-04-30 23:34:17 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-06-25 12:39:09 +02:00
|
|
|
#ifndef OS_IS_WIN32
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
|
#include <poll.h>
|
2006-05-02 08:41:41 +00:00
|
|
|
#else
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/poll.h>
|
2006-05-02 08:41:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulse/mainloop.h>
|
2008-08-06 18:54:13 +02:00
|
|
|
#include <pulse/i18n.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/hashmap.h>
|
2006-09-04 21:29:17 +00:00
|
|
|
#include <pulsecore/thread.h>
|
|
|
|
|
#include <pulsecore/mutex.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2006-04-30 23:34:17 +00:00
|
|
|
|
|
|
|
|
#include "thread-mainloop.h"
|
|
|
|
|
|
|
|
|
|
struct pa_threaded_mainloop {
|
|
|
|
|
pa_mainloop *real_mainloop;
|
2006-05-11 14:57:24 +00:00
|
|
|
int n_waiting;
|
2006-09-04 21:29:17 +00:00
|
|
|
|
|
|
|
|
pa_thread* thread;
|
|
|
|
|
pa_mutex* mutex;
|
|
|
|
|
pa_cond* cond, *accept_cond;
|
2006-04-30 23:34:17 +00:00
|
|
|
};
|
|
|
|
|
|
2006-05-11 14:57:24 +00:00
|
|
|
static inline int in_worker(pa_threaded_mainloop *m) {
|
2006-09-04 21:29:17 +00:00
|
|
|
return pa_thread_self() == m->thread;
|
2006-05-11 14:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
static int poll_func(struct pollfd *ufds, unsigned long nfds, int timeout, void *userdata) {
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex *mutex = userdata;
|
2006-04-30 23:34:17 +00:00
|
|
|
int r;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(mutex);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
/* Before entering poll() we unlock the mutex, so that
|
|
|
|
|
* avahi_simple_poll_quit() can succeed from another thread. */
|
|
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_unlock(mutex);
|
2006-04-30 23:34:17 +00:00
|
|
|
r = poll(ufds, nfds, timeout);
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_lock(mutex);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
static void thread(void *userdata) {
|
2006-04-30 23:34:17 +00:00
|
|
|
pa_threaded_mainloop *m = userdata;
|
2006-05-11 14:57:24 +00:00
|
|
|
|
|
|
|
|
#ifndef OS_IS_WIN32
|
2006-04-30 23:34:17 +00:00
|
|
|
sigset_t mask;
|
|
|
|
|
|
|
|
|
|
/* Make sure that signals are delivered to the main thread */
|
|
|
|
|
sigfillset(&mask);
|
|
|
|
|
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
2006-05-11 14:57:24 +00:00
|
|
|
#endif
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_lock(m->mutex);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
pa_mainloop_run(m->real_mainloop, NULL);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_unlock(m->mutex);
|
2006-04-30 23:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop *pa_threaded_mainloop_new(void) {
|
|
|
|
|
pa_threaded_mainloop *m;
|
|
|
|
|
|
2008-08-06 18:54:13 +02:00
|
|
|
pa_init_i18n();
|
|
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
m = pa_xnew(pa_threaded_mainloop, 1);
|
|
|
|
|
|
|
|
|
|
if (!(m->real_mainloop = pa_mainloop_new())) {
|
|
|
|
|
pa_xfree(m);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-14 16:11:51 +00:00
|
|
|
m->mutex = pa_mutex_new(TRUE, TRUE);
|
2006-09-04 21:29:17 +00:00
|
|
|
m->cond = pa_cond_new();
|
|
|
|
|
m->accept_cond = pa_cond_new();
|
|
|
|
|
m->thread = NULL;
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mainloop_set_poll_func(m->real_mainloop, poll_func, m->mutex);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-05-06 20:58:02 +00:00
|
|
|
m->n_waiting = 0;
|
2006-04-30 23:34:17 +00:00
|
|
|
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_threaded_mainloop_free(pa_threaded_mainloop* m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
|
|
|
|
/* Make sure that this function is not called from the helper thread */
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert((m->thread && !pa_thread_is_running(m->thread)) || !in_worker(m));
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_threaded_mainloop_stop(m);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-11-06 10:14:16 +00:00
|
|
|
if (m->thread)
|
|
|
|
|
pa_thread_free(m->thread);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mainloop_free(m->real_mainloop);
|
|
|
|
|
|
|
|
|
|
pa_mutex_free(m->mutex);
|
|
|
|
|
pa_cond_free(m->cond);
|
|
|
|
|
pa_cond_free(m->accept_cond);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
pa_xfree(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_threaded_mainloop_start(pa_threaded_mainloop *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(!m->thread || !pa_thread_is_running(m->thread));
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
if (!(m->thread = pa_thread_new(thread, m)))
|
2006-05-11 14:57:24 +00:00
|
|
|
return -1;
|
2006-04-30 23:34:17 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_threaded_mainloop_stop(pa_threaded_mainloop *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
if (!m->thread || !pa_thread_is_running(m->thread))
|
2006-04-30 23:34:17 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Make sure that this function is not called from the helper thread */
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(!in_worker(m));
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_lock(m->mutex);
|
2006-04-30 23:34:17 +00:00
|
|
|
pa_mainloop_quit(m->real_mainloop, 0);
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_unlock(m->mutex);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_thread_join(m->thread);
|
2006-04-30 23:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_threaded_mainloop_lock(pa_threaded_mainloop *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
/* Make sure that this function is not called from the helper thread */
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_lock(m->mutex);
|
2006-04-30 23:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
/* Make sure that this function is not called from the helper thread */
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_mutex_unlock(m->mutex);
|
2006-04-30 23:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-11 13:01:24 +00:00
|
|
|
void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_cond_signal(m->cond, 1);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
if (wait_for_accept && m->n_waiting > 0)
|
|
|
|
|
pa_cond_wait(m->accept_cond, m->mutex);
|
2006-04-30 23:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_threaded_mainloop_wait(pa_threaded_mainloop *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-04-30 23:34:17 +00:00
|
|
|
/* Make sure that this function is not called from the helper thread */
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
|
2006-04-30 23:34:17 +00:00
|
|
|
|
2006-05-06 20:58:02 +00:00
|
|
|
m->n_waiting ++;
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_cond_wait(m->cond, m->mutex);
|
2006-05-11 14:57:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m->n_waiting > 0);
|
2006-05-06 20:58:02 +00:00
|
|
|
m->n_waiting --;
|
2006-05-11 13:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_threaded_mainloop_accept(pa_threaded_mainloop *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-11 13:01:24 +00:00
|
|
|
/* Make sure that this function is not called from the helper thread */
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
|
2006-05-11 13:01:24 +00:00
|
|
|
|
2006-09-04 21:29:17 +00:00
|
|
|
pa_cond_signal(m->accept_cond, 0);
|
2006-04-30 23:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_threaded_mainloop_get_retval(pa_threaded_mainloop *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
|
|
|
|
return pa_mainloop_get_retval(m->real_mainloop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2006-04-30 23:34:17 +00:00
|
|
|
|
|
|
|
|
return pa_mainloop_get_api(m->real_mainloop);
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m) {
|
|
|
|
|
pa_assert(m);
|
|
|
|
|
|
|
|
|
|
return m->thread && pa_thread_self() == m->thread;
|
|
|
|
|
}
|