]>
code.delx.au - pulseaudio/blob - src/pulse/thread-mainloop.c
2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/xmalloc.h>
35 #include <pulse/mainloop.h>
37 #include <pulsecore/i18n.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/thread.h>
40 #include <pulsecore/mutex.h>
41 #include <pulsecore/macro.h>
42 #include <pulsecore/poll.h>
44 #include "thread-mainloop.h"
46 struct pa_threaded_mainloop
{
47 pa_mainloop
*real_mainloop
;
48 volatile int n_waiting
, n_waiting_for_accept
;
52 pa_cond
* cond
, *accept_cond
;
57 static inline int in_worker(pa_threaded_mainloop
*m
) {
58 return pa_thread_self() == m
->thread
;
61 static int poll_func(struct pollfd
*ufds
, unsigned long nfds
, int timeout
, void *userdata
) {
62 pa_mutex
*mutex
= userdata
;
67 /* Before entering poll() we unlock the mutex, so that
68 * avahi_simple_poll_quit() can succeed from another thread. */
70 pa_mutex_unlock(mutex
);
71 r
= pa_poll(ufds
, nfds
, timeout
);
77 static void thread(void *userdata
) {
78 pa_threaded_mainloop
*m
= userdata
;
83 /* Make sure that signals are delivered to the main thread */
85 pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
88 pa_mutex_lock(m
->mutex
);
90 pa_mainloop_run(m
->real_mainloop
, NULL
);
92 pa_mutex_unlock(m
->mutex
);
95 pa_threaded_mainloop
*pa_threaded_mainloop_new(void) {
96 pa_threaded_mainloop
*m
;
100 m
= pa_xnew(pa_threaded_mainloop
, 1);
102 if (!(m
->real_mainloop
= pa_mainloop_new())) {
107 m
->mutex
= pa_mutex_new(true, true);
108 m
->cond
= pa_cond_new();
109 m
->accept_cond
= pa_cond_new();
113 pa_mainloop_set_poll_func(m
->real_mainloop
, poll_func
, m
->mutex
);
116 m
->n_waiting_for_accept
= 0;
121 void pa_threaded_mainloop_free(pa_threaded_mainloop
* m
) {
124 /* Make sure that this function is not called from the helper thread */
125 pa_assert((m
->thread
&& !pa_thread_is_running(m
->thread
)) || !in_worker(m
));
127 pa_threaded_mainloop_stop(m
);
130 pa_thread_free(m
->thread
);
132 pa_mainloop_free(m
->real_mainloop
);
134 pa_mutex_free(m
->mutex
);
135 pa_cond_free(m
->cond
);
136 pa_cond_free(m
->accept_cond
);
142 int pa_threaded_mainloop_start(pa_threaded_mainloop
*m
) {
145 pa_assert(!m
->thread
|| !pa_thread_is_running(m
->thread
));
147 if (!(m
->thread
= pa_thread_new(m
->name
? m
->name
: "threaded-ml", thread
, m
)))
153 void pa_threaded_mainloop_stop(pa_threaded_mainloop
*m
) {
156 if (!m
->thread
|| !pa_thread_is_running(m
->thread
))
159 /* Make sure that this function is not called from the helper thread */
160 pa_assert(!in_worker(m
));
162 pa_mutex_lock(m
->mutex
);
163 pa_mainloop_quit(m
->real_mainloop
, 0);
164 pa_mutex_unlock(m
->mutex
);
166 pa_thread_join(m
->thread
);
169 void pa_threaded_mainloop_lock(pa_threaded_mainloop
*m
) {
172 /* Make sure that this function is not called from the helper thread */
173 pa_assert(!m
->thread
|| !pa_thread_is_running(m
->thread
) || !in_worker(m
));
175 pa_mutex_lock(m
->mutex
);
178 void pa_threaded_mainloop_unlock(pa_threaded_mainloop
*m
) {
181 /* Make sure that this function is not called from the helper thread */
182 pa_assert(!m
->thread
|| !pa_thread_is_running(m
->thread
) || !in_worker(m
));
184 pa_mutex_unlock(m
->mutex
);
187 /* Called with the lock taken */
188 void pa_threaded_mainloop_signal(pa_threaded_mainloop
*m
, int wait_for_accept
) {
191 pa_cond_signal(m
->cond
, 1);
193 if (wait_for_accept
) {
194 m
->n_waiting_for_accept
++;
196 while (m
->n_waiting_for_accept
> 0)
197 pa_cond_wait(m
->accept_cond
, m
->mutex
);
201 /* Called with the lock taken */
202 void pa_threaded_mainloop_wait(pa_threaded_mainloop
*m
) {
205 /* Make sure that this function is not called from the helper thread */
206 pa_assert(!m
->thread
|| !pa_thread_is_running(m
->thread
) || !in_worker(m
));
210 pa_cond_wait(m
->cond
, m
->mutex
);
212 pa_assert(m
->n_waiting
> 0);
216 /* Called with the lock taken */
217 void pa_threaded_mainloop_accept(pa_threaded_mainloop
*m
) {
220 /* Make sure that this function is not called from the helper thread */
221 pa_assert(!m
->thread
|| !pa_thread_is_running(m
->thread
) || !in_worker(m
));
223 pa_assert(m
->n_waiting_for_accept
> 0);
224 m
->n_waiting_for_accept
--;
226 pa_cond_signal(m
->accept_cond
, 0);
229 int pa_threaded_mainloop_get_retval(pa_threaded_mainloop
*m
) {
232 return pa_mainloop_get_retval(m
->real_mainloop
);
235 pa_mainloop_api
* pa_threaded_mainloop_get_api(pa_threaded_mainloop
*m
) {
238 return pa_mainloop_get_api(m
->real_mainloop
);
241 int pa_threaded_mainloop_in_thread(pa_threaded_mainloop
*m
) {
244 return m
->thread
&& pa_thread_self() == m
->thread
;
247 void pa_threaded_mainloop_set_name(pa_threaded_mainloop
*m
, const char *name
) {
251 m
->name
= pa_xstrdup(name
);
254 pa_thread_set_name(m
->thread
, m
->name
);