]> code.delx.au - pulseaudio/blob - src/pulse/thread-mainloop.c
volume: Increase PA_SW_VOLUME_SNPRINT_DB_MAX
[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
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>
43
44 #include "thread-mainloop.h"
45
46 struct pa_threaded_mainloop {
47 pa_mainloop *real_mainloop;
48 volatile int n_waiting, n_waiting_for_accept;
49
50 pa_thread* thread;
51 pa_mutex* mutex;
52 pa_cond* cond, *accept_cond;
53
54 char *name;
55 };
56
57 static inline int in_worker(pa_threaded_mainloop *m) {
58 return pa_thread_self() == m->thread;
59 }
60
61 static int poll_func(struct pollfd *ufds, unsigned long nfds, int timeout, void *userdata) {
62 pa_mutex *mutex = userdata;
63 int r;
64
65 pa_assert(mutex);
66
67 /* Before entering poll() we unlock the mutex, so that
68 * avahi_simple_poll_quit() can succeed from another thread. */
69
70 pa_mutex_unlock(mutex);
71 r = pa_poll(ufds, nfds, timeout);
72 pa_mutex_lock(mutex);
73
74 return r;
75 }
76
77 static void thread(void *userdata) {
78 pa_threaded_mainloop *m = userdata;
79
80 #ifndef OS_IS_WIN32
81 sigset_t mask;
82
83 /* Make sure that signals are delivered to the main thread */
84 sigfillset(&mask);
85 pthread_sigmask(SIG_BLOCK, &mask, NULL);
86 #endif
87
88 pa_mutex_lock(m->mutex);
89
90 pa_mainloop_run(m->real_mainloop, NULL);
91
92 pa_mutex_unlock(m->mutex);
93 }
94
95 pa_threaded_mainloop *pa_threaded_mainloop_new(void) {
96 pa_threaded_mainloop *m;
97
98 pa_init_i18n();
99
100 m = pa_xnew(pa_threaded_mainloop, 1);
101
102 if (!(m->real_mainloop = pa_mainloop_new())) {
103 pa_xfree(m);
104 return NULL;
105 }
106
107 m->mutex = pa_mutex_new(true, true);
108 m->cond = pa_cond_new();
109 m->accept_cond = pa_cond_new();
110 m->thread = NULL;
111 m->name = NULL;
112
113 pa_mainloop_set_poll_func(m->real_mainloop, poll_func, m->mutex);
114
115 m->n_waiting = 0;
116 m->n_waiting_for_accept = 0;
117
118 return m;
119 }
120
121 void pa_threaded_mainloop_free(pa_threaded_mainloop* m) {
122 pa_assert(m);
123
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));
126
127 pa_threaded_mainloop_stop(m);
128
129 if (m->thread)
130 pa_thread_free(m->thread);
131
132 pa_mainloop_free(m->real_mainloop);
133
134 pa_mutex_free(m->mutex);
135 pa_cond_free(m->cond);
136 pa_cond_free(m->accept_cond);
137
138 pa_xfree(m->name);
139 pa_xfree(m);
140 }
141
142 int pa_threaded_mainloop_start(pa_threaded_mainloop *m) {
143 pa_assert(m);
144
145 pa_assert(!m->thread || !pa_thread_is_running(m->thread));
146
147 if (!(m->thread = pa_thread_new(m->name ? m->name : "threaded-ml", thread, m)))
148 return -1;
149
150 return 0;
151 }
152
153 void pa_threaded_mainloop_stop(pa_threaded_mainloop *m) {
154 pa_assert(m);
155
156 if (!m->thread || !pa_thread_is_running(m->thread))
157 return;
158
159 /* Make sure that this function is not called from the helper thread */
160 pa_assert(!in_worker(m));
161
162 pa_mutex_lock(m->mutex);
163 pa_mainloop_quit(m->real_mainloop, 0);
164 pa_mutex_unlock(m->mutex);
165
166 pa_thread_join(m->thread);
167 }
168
169 void pa_threaded_mainloop_lock(pa_threaded_mainloop *m) {
170 pa_assert(m);
171
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));
174
175 pa_mutex_lock(m->mutex);
176 }
177
178 void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m) {
179 pa_assert(m);
180
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));
183
184 pa_mutex_unlock(m->mutex);
185 }
186
187 /* Called with the lock taken */
188 void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept) {
189 pa_assert(m);
190
191 pa_cond_signal(m->cond, 1);
192
193 if (wait_for_accept) {
194 m->n_waiting_for_accept ++;
195
196 while (m->n_waiting_for_accept > 0)
197 pa_cond_wait(m->accept_cond, m->mutex);
198 }
199 }
200
201 /* Called with the lock taken */
202 void pa_threaded_mainloop_wait(pa_threaded_mainloop *m) {
203 pa_assert(m);
204
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));
207
208 m->n_waiting ++;
209
210 pa_cond_wait(m->cond, m->mutex);
211
212 pa_assert(m->n_waiting > 0);
213 m->n_waiting --;
214 }
215
216 /* Called with the lock taken */
217 void pa_threaded_mainloop_accept(pa_threaded_mainloop *m) {
218 pa_assert(m);
219
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));
222
223 pa_assert(m->n_waiting_for_accept > 0);
224 m->n_waiting_for_accept --;
225
226 pa_cond_signal(m->accept_cond, 0);
227 }
228
229 int pa_threaded_mainloop_get_retval(pa_threaded_mainloop *m) {
230 pa_assert(m);
231
232 return pa_mainloop_get_retval(m->real_mainloop);
233 }
234
235 pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m) {
236 pa_assert(m);
237
238 return pa_mainloop_get_api(m->real_mainloop);
239 }
240
241 int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m) {
242 pa_assert(m);
243
244 return m->thread && pa_thread_self() == m->thread;
245 }
246
247 void pa_threaded_mainloop_set_name(pa_threaded_mainloop *m, const char *name) {
248 pa_assert(m);
249 pa_assert(name);
250
251 m->name = pa_xstrdup(name);
252
253 if (m->thread)
254 pa_thread_set_name(m->thread, m->name);
255 }