Git init
[framework/multimedia/pulseaudio.git] / 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 #ifdef HAVE_POLL_H
35 #include <poll.h>
36 #else
37 #include <pulsecore/poll.h>
38 #endif
39
40 #include <pulse/xmalloc.h>
41 #include <pulse/mainloop.h>
42 #include <pulse/i18n.h>
43
44 #include <pulsecore/log.h>
45 #include <pulsecore/hashmap.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/mutex.h>
48 #include <pulsecore/macro.h>
49
50 #include "thread-mainloop.h"
51
52 struct pa_threaded_mainloop {
53     pa_mainloop *real_mainloop;
54     int n_waiting, n_waiting_for_accept;
55
56     pa_thread* thread;
57     pa_mutex* mutex;
58     pa_cond* cond, *accept_cond;
59 };
60
61 static inline int in_worker(pa_threaded_mainloop *m) {
62     return pa_thread_self() == m->thread;
63 }
64
65 static int poll_func(struct pollfd *ufds, unsigned long nfds, int timeout, void *userdata) {
66     pa_mutex *mutex = userdata;
67     int r;
68
69     pa_assert(mutex);
70
71     /* Before entering poll() we unlock the mutex, so that
72      * avahi_simple_poll_quit() can succeed from another thread. */
73
74     pa_mutex_unlock(mutex);
75     r = poll(ufds, nfds, timeout);
76     pa_mutex_lock(mutex);
77
78     return r;
79 }
80
81 static void thread(void *userdata) {
82     pa_threaded_mainloop *m = userdata;
83
84 #ifndef OS_IS_WIN32
85     sigset_t mask;
86
87     /* Make sure that signals are delivered to the main thread */
88     sigfillset(&mask);
89     pthread_sigmask(SIG_BLOCK, &mask, NULL);
90 #endif
91
92     pa_mutex_lock(m->mutex);
93
94     pa_mainloop_run(m->real_mainloop, NULL);
95
96     pa_mutex_unlock(m->mutex);
97 }
98
99 pa_threaded_mainloop *pa_threaded_mainloop_new(void) {
100     pa_threaded_mainloop *m;
101
102     pa_init_i18n();
103
104     m = pa_xnew(pa_threaded_mainloop, 1);
105
106     if (!(m->real_mainloop = pa_mainloop_new())) {
107         pa_xfree(m);
108         return NULL;
109     }
110
111     m->mutex = pa_mutex_new(TRUE, TRUE);
112     m->cond = pa_cond_new();
113     m->accept_cond = pa_cond_new();
114     m->thread = NULL;
115
116     pa_mainloop_set_poll_func(m->real_mainloop, poll_func, m->mutex);
117
118     m->n_waiting = 0;
119
120     return m;
121 }
122
123 void pa_threaded_mainloop_free(pa_threaded_mainloop* m) {
124     pa_assert(m);
125
126     /* Make sure that this function is not called from the helper thread */
127     pa_assert((m->thread && !pa_thread_is_running(m->thread)) || !in_worker(m));
128
129     pa_threaded_mainloop_stop(m);
130
131     if (m->thread)
132         pa_thread_free(m->thread);
133
134     pa_mainloop_free(m->real_mainloop);
135
136     pa_mutex_free(m->mutex);
137     pa_cond_free(m->cond);
138     pa_cond_free(m->accept_cond);
139
140     pa_xfree(m);
141 }
142
143 int pa_threaded_mainloop_start(pa_threaded_mainloop *m) {
144     pa_assert(m);
145
146     pa_assert(!m->thread || !pa_thread_is_running(m->thread));
147
148     if (!(m->thread = pa_thread_new(thread, m)))
149         return -1;
150
151     return 0;
152 }
153
154 void pa_threaded_mainloop_stop(pa_threaded_mainloop *m) {
155     pa_assert(m);
156
157     if (!m->thread || !pa_thread_is_running(m->thread))
158         return;
159
160     /* Make sure that this function is not called from the helper thread */
161     pa_assert(!in_worker(m));
162
163     pa_mutex_lock(m->mutex);
164     pa_mainloop_quit(m->real_mainloop, 0);
165     pa_mutex_unlock(m->mutex);
166
167     pa_thread_join(m->thread);
168 }
169
170 void pa_threaded_mainloop_lock(pa_threaded_mainloop *m) {
171     pa_assert(m);
172
173     /* Make sure that this function is not called from the helper thread */
174     pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
175
176     pa_mutex_lock(m->mutex);
177 }
178
179 void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m) {
180     pa_assert(m);
181
182     /* Make sure that this function is not called from the helper thread */
183     pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
184
185     pa_mutex_unlock(m->mutex);
186 }
187
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 void pa_threaded_mainloop_wait(pa_threaded_mainloop *m) {
202     pa_assert(m);
203
204     /* Make sure that this function is not called from the helper thread */
205     pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
206
207     m->n_waiting ++;
208
209     pa_cond_wait(m->cond, m->mutex);
210
211     pa_assert(m->n_waiting > 0);
212     m->n_waiting --;
213 }
214
215 void pa_threaded_mainloop_accept(pa_threaded_mainloop *m) {
216     pa_assert(m);
217
218     /* Make sure that this function is not called from the helper thread */
219     pa_assert(!m->thread || !pa_thread_is_running(m->thread) || !in_worker(m));
220
221     pa_assert(m->n_waiting_for_accept > 0);
222     m->n_waiting_for_accept --;
223
224     pa_cond_signal(m->accept_cond, 0);
225 }
226
227 int pa_threaded_mainloop_get_retval(pa_threaded_mainloop *m) {
228     pa_assert(m);
229
230     return pa_mainloop_get_retval(m->real_mainloop);
231 }
232
233 pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m) {
234     pa_assert(m);
235
236     return pa_mainloop_get_api(m->real_mainloop);
237 }
238
239 int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m) {
240     pa_assert(m);
241
242     return m->thread && pa_thread_self() == m->thread;
243 }