]> code.delx.au - pulseaudio/blob - src/pulse/thread-mainloop.c
Merge dead branch 'lennart'
[pulseaudio] / src / pulse / thread-mainloop.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
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 of the License,
10 or (at your option) any later version.
11
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.
16
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
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <signal.h>
28 #include <stdio.h>
29
30 #ifdef HAVE_POLL_H
31 #include <poll.h>
32 #else
33 #include <pulsecore/poll.h>
34 #endif
35
36 #include <pulse/xmalloc.h>
37 #include <pulse/mainloop.h>
38
39 #include <pulsecore/log.h>
40 #include <pulsecore/hashmap.h>
41 #include <pulsecore/thread.h>
42 #include <pulsecore/mutex.h>
43 #include <pulsecore/macro.h>
44
45 #include "thread-mainloop.h"
46
47 struct pa_threaded_mainloop {
48 pa_mainloop *real_mainloop;
49 int n_waiting;
50
51 pa_thread* thread;
52 pa_mutex* mutex;
53 pa_cond* cond, *accept_cond;
54 };
55
56 static inline int in_worker(pa_threaded_mainloop *m) {
57 return pa_thread_self() == m->thread;
58 }
59
60 static int poll_func(struct pollfd *ufds, unsigned long nfds, int timeout, void *userdata) {
61 pa_mutex *mutex = userdata;
62 int r;
63
64 pa_assert(mutex);
65
66 /* Before entering poll() we unlock the mutex, so that
67 * avahi_simple_poll_quit() can succeed from another thread. */
68
69 pa_mutex_unlock(mutex);
70 r = poll(ufds, nfds, timeout);
71 pa_mutex_lock(mutex);
72
73 return r;
74 }
75
76 static void thread(void *userdata) {
77 pa_threaded_mainloop *m = userdata;
78
79 #ifndef OS_IS_WIN32
80 sigset_t mask;
81
82 /* Make sure that signals are delivered to the main thread */
83 sigfillset(&mask);
84 pthread_sigmask(SIG_BLOCK, &mask, NULL);
85 #endif
86
87 pa_mutex_lock(m->mutex);
88
89 pa_mainloop_run(m->real_mainloop, NULL);
90
91 pa_mutex_unlock(m->mutex);
92 }
93
94 pa_threaded_mainloop *pa_threaded_mainloop_new(void) {
95 pa_threaded_mainloop *m;
96
97 m = pa_xnew(pa_threaded_mainloop, 1);
98
99 if (!(m->real_mainloop = pa_mainloop_new())) {
100 pa_xfree(m);
101 return NULL;
102 }
103
104 m->mutex = pa_mutex_new(TRUE, TRUE);
105 m->cond = pa_cond_new();
106 m->accept_cond = pa_cond_new();
107 m->thread = NULL;
108
109 pa_mainloop_set_poll_func(m->real_mainloop, poll_func, m->mutex);
110
111 m->n_waiting = 0;
112
113 return m;
114 }
115
116 void pa_threaded_mainloop_free(pa_threaded_mainloop* m) {
117 pa_assert(m);
118
119 /* Make sure that this function is not called from the helper thread */
120 pa_assert((m->thread && !pa_thread_is_running(m->thread)) || !in_worker(m));
121
122 pa_threaded_mainloop_stop(m);
123
124 if (m->thread)
125 pa_thread_free(m->thread);
126
127 pa_mainloop_free(m->real_mainloop);
128
129 pa_mutex_free(m->mutex);
130 pa_cond_free(m->cond);
131 pa_cond_free(m->accept_cond);
132
133 pa_xfree(m);
134 }
135
136 int pa_threaded_mainloop_start(pa_threaded_mainloop *m) {
137 pa_assert(m);
138
139 pa_assert(!m->thread || !pa_thread_is_running(m->thread));
140
141 if (!(m->thread = pa_thread_new(thread, m)))
142 return -1;
143
144 return 0;
145 }
146
147 void pa_threaded_mainloop_stop(pa_threaded_mainloop *m) {
148 pa_assert(m);
149
150 if (!m->thread || !pa_thread_is_running(m->thread))
151 return;
152
153 /* Make sure that this function is not called from the helper thread */
154 pa_assert(!in_worker(m));
155
156 pa_mutex_lock(m->mutex);
157 pa_mainloop_quit(m->real_mainloop, 0);
158 pa_mutex_unlock(m->mutex);
159
160 pa_thread_join(m->thread);
161 }
162
163 void pa_threaded_mainloop_lock(pa_threaded_mainloop *m) {
164 pa_assert(m);
165
166 /* Make sure that this function is not called from the helper thread */
167 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
168
169 pa_mutex_lock(m->mutex);
170 }
171
172 void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m) {
173 pa_assert(m);
174
175 /* Make sure that this function is not called from the helper thread */
176 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
177
178 pa_mutex_unlock(m->mutex);
179 }
180
181 void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept) {
182 pa_assert(m);
183
184 pa_cond_signal(m->cond, 1);
185
186 if (wait_for_accept && m->n_waiting > 0)
187 pa_cond_wait(m->accept_cond, m->mutex);
188 }
189
190 void pa_threaded_mainloop_wait(pa_threaded_mainloop *m) {
191 pa_assert(m);
192
193 /* Make sure that this function is not called from the helper thread */
194 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
195
196 m->n_waiting ++;
197
198 pa_cond_wait(m->cond, m->mutex);
199
200 pa_assert(m->n_waiting > 0);
201 m->n_waiting --;
202 }
203
204 void pa_threaded_mainloop_accept(pa_threaded_mainloop *m) {
205 pa_assert(m);
206
207 /* Make sure that this function is not called from the helper thread */
208 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
209
210 pa_cond_signal(m->accept_cond, 0);
211 }
212
213 int pa_threaded_mainloop_get_retval(pa_threaded_mainloop *m) {
214 pa_assert(m);
215
216 return pa_mainloop_get_retval(m->real_mainloop);
217 }
218
219 pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m) {
220 pa_assert(m);
221
222 return pa_mainloop_get_api(m->real_mainloop);
223 }
224
225 int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m) {
226 pa_assert(m);
227
228 return m->thread && pa_thread_self() == m->thread;
229 }