]> code.delx.au - pulseaudio/blob - src/pulse/thread-mainloop.c
Clean up <poll.h> includes
[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.1 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 #ifndef OS_IS_WIN32
28 #include <pthread.h>
29 #endif
30
31 #include <signal.h>
32 #include <stdio.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulse/mainloop.h>
36 #include <pulse/i18n.h>
37
38 #include <pulsecore/log.h>
39 #include <pulsecore/hashmap.h>
40 #include <pulsecore/thread.h>
41 #include <pulsecore/mutex.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/poll.h>
44
45 #include "thread-mainloop.h"
46
47 struct pa_threaded_mainloop {
48 pa_mainloop *real_mainloop;
49 volatile int n_waiting, n_waiting_for_accept;
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 pa_init_i18n();
98
99 m = pa_xnew(pa_threaded_mainloop, 1);
100
101 if (!(m->real_mainloop = pa_mainloop_new())) {
102 pa_xfree(m);
103 return NULL;
104 }
105
106 m->mutex = pa_mutex_new(TRUE, TRUE);
107 m->cond = pa_cond_new();
108 m->accept_cond = pa_cond_new();
109 m->thread = NULL;
110
111 pa_mainloop_set_poll_func(m->real_mainloop, poll_func, m->mutex);
112
113 m->n_waiting = 0;
114 m->n_waiting_for_accept = 0;
115
116 return m;
117 }
118
119 void pa_threaded_mainloop_free(pa_threaded_mainloop* m) {
120 pa_assert(m);
121
122 /* Make sure that this function is not called from the helper thread */
123 pa_assert((m->thread && !pa_thread_is_running(m->thread)) || !in_worker(m));
124
125 pa_threaded_mainloop_stop(m);
126
127 if (m->thread)
128 pa_thread_free(m->thread);
129
130 pa_mainloop_free(m->real_mainloop);
131
132 pa_mutex_free(m->mutex);
133 pa_cond_free(m->cond);
134 pa_cond_free(m->accept_cond);
135
136 pa_xfree(m);
137 }
138
139 int pa_threaded_mainloop_start(pa_threaded_mainloop *m) {
140 pa_assert(m);
141
142 pa_assert(!m->thread || !pa_thread_is_running(m->thread));
143
144 if (!(m->thread = pa_thread_new("threaded-ml", thread, m)))
145 return -1;
146
147 return 0;
148 }
149
150 void pa_threaded_mainloop_stop(pa_threaded_mainloop *m) {
151 pa_assert(m);
152
153 if (!m->thread || !pa_thread_is_running(m->thread))
154 return;
155
156 /* Make sure that this function is not called from the helper thread */
157 pa_assert(!in_worker(m));
158
159 pa_mutex_lock(m->mutex);
160 pa_mainloop_quit(m->real_mainloop, 0);
161 pa_mutex_unlock(m->mutex);
162
163 pa_thread_join(m->thread);
164 }
165
166 void pa_threaded_mainloop_lock(pa_threaded_mainloop *m) {
167 pa_assert(m);
168
169 /* Make sure that this function is not called from the helper thread */
170 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
171
172 pa_mutex_lock(m->mutex);
173 }
174
175 void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m) {
176 pa_assert(m);
177
178 /* Make sure that this function is not called from the helper thread */
179 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
180
181 pa_mutex_unlock(m->mutex);
182 }
183
184 /* Called with the lock taken */
185 void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept) {
186 pa_assert(m);
187
188 pa_cond_signal(m->cond, 1);
189
190 if (wait_for_accept) {
191 m->n_waiting_for_accept ++;
192
193 while (m->n_waiting_for_accept > 0)
194 pa_cond_wait(m->accept_cond, m->mutex);
195 }
196 }
197
198 /* Called with the lock taken */
199 void pa_threaded_mainloop_wait(pa_threaded_mainloop *m) {
200 pa_assert(m);
201
202 /* Make sure that this function is not called from the helper thread */
203 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
204
205 m->n_waiting ++;
206
207 pa_cond_wait(m->cond, m->mutex);
208
209 pa_assert(m->n_waiting > 0);
210 m->n_waiting --;
211 }
212
213 /* Called with the lock taken */
214 void pa_threaded_mainloop_accept(pa_threaded_mainloop *m) {
215 pa_assert(m);
216
217 /* Make sure that this function is not called from the helper thread */
218 pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
219
220 pa_assert(m->n_waiting_for_accept > 0);
221 m->n_waiting_for_accept --;
222
223 pa_cond_signal(m->accept_cond, 0);
224 }
225
226 int pa_threaded_mainloop_get_retval(pa_threaded_mainloop *m) {
227 pa_assert(m);
228
229 return pa_mainloop_get_retval(m->real_mainloop);
230 }
231
232 pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m) {
233 pa_assert(m);
234
235 return pa_mainloop_get_api(m->real_mainloop);
236 }
237
238 int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m) {
239 pa_assert(m);
240
241 return m->thread && pa_thread_self() == m->thread;
242 }