]> code.delx.au - pulseaudio/blob - src/pulse/thread-mainloop.h
Merge dead branch 'prepare-0.9.10'
[pulseaudio] / src / pulse / thread-mainloop.h
1 #ifndef foothreadmainloophfoo
2 #define foothreadmainloophfoo
3
4 /***
5 This file is part of PulseAudio.
6
7 Copyright 2006 Lennart Poettering
8 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published
12 by the Free Software Foundation; either version 2 of the License,
13 or (at your option) any later version.
14
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 USA.
24 ***/
25
26 #include <pulse/mainloop-api.h>
27 #include <pulse/cdecl.h>
28
29 PA_C_DECL_BEGIN
30
31 /** \page threaded_mainloop Threaded Main Loop
32 *
33 * \section overv_sec Overview
34 *
35 * The threaded main loop implementation is a special version of the primary
36 * main loop implementation (see \ref mainloop). For the basic design, see
37 * its documentation.
38 *
39 * The added feature in the threaded main loop is that it spawns a new thread
40 * that runs the real main loop. This allows a synchronous application to use
41 * the asynchronous API without risking to stall the PulseAudio library.
42 *
43 * \section creat_sec Creation
44 *
45 * A pa_threaded_mainloop object is created using pa_threaded_mainloop_new().
46 * This will only allocate the required structures though, so to use it the
47 * thread must also be started. This is done through
48 * pa_threaded_mainloop_start(), after which you can start using the main loop.
49 *
50 * \section destr_sec Destruction
51 *
52 * When the PulseAudio connection has been terminated, the thread must be
53 * stopped and the resources freed. Stopping the thread is done using
54 * pa_threaded_mainloop_stop(), which must be called without the lock (see
55 * below) held. When that function returns, the thread is stopped and the
56 * pa_threaded_mainloop object can be freed using pa_threaded_mainloop_free().
57 *
58 * \section lock_sec Locking
59 *
60 * Since the PulseAudio API doesn't allow concurrent accesses to objects,
61 * a locking scheme must be used to guarantee safe usage. The threaded main
62 * loop API provides such a scheme through the functions
63 * pa_threaded_mainloop_lock() and pa_threaded_mainloop_unlock().
64 *
65 * The lock is recursive, so it's safe to use it multiple times from the same
66 * thread. Just make sure you call pa_threaded_mainloop_unlock() the same
67 * number of times you called pa_threaded_mainloop_lock().
68 *
69 * The lock needs to be held whenever you call any PulseAudio function that
70 * uses an object associated with this main loop. Make sure you do not hold
71 * on to the lock more than necessary though, as the threaded main loop stops
72 * while the lock is held.
73 *
74 * Example:
75 *
76 * \code
77 * void my_check_stream_func(pa_threaded_mainloop *m, pa_stream *s) {
78 * pa_stream_state_t state;
79 *
80 * pa_threaded_mainloop_lock(m);
81 *
82 * state = pa_stream_get_state(s);
83 *
84 * pa_threaded_mainloop_unlock(m);
85 *
86 * if (state == PA_STREAM_READY)
87 * printf("Stream is ready!");
88 * else
89 * printf("Stream is not ready!");
90 * }
91 * \endcode
92 *
93 * \section cb_sec Callbacks
94 *
95 * Callbacks in PulseAudio are asynchronous, so they require extra care when
96 * using them together with a threaded main loop.
97 *
98 * The easiest way to turn the callback based operations into synchronous
99 * ones, is to simply wait for the callback to be called and continue from
100 * there. This is the approach chosen in PulseAudio's threaded API.
101 *
102 * \subsection basic_subsec Basic callbacks
103 *
104 * For the basic case, where all that is required is to wait for the callback
105 * to be invoked, the code should look something like this:
106 *
107 * Example:
108 *
109 * \code
110 * static void my_drain_callback(pa_stream*s, int success, void *userdata) {
111 * pa_threaded_mainloop *m;
112 *
113 * m = (pa_threaded_mainloop*)userdata;
114 * assert(m);
115 *
116 * pa_threaded_mainloop_signal(m, 0);
117 * }
118 *
119 * void my_drain_stream_func(pa_threaded_mainloop *m, pa_stream *s) {
120 * pa_operation *o;
121 *
122 * pa_threaded_mainloop_lock(m);
123 *
124 * o = pa_stream_drain(s, my_drain_callback, m);
125 * assert(o);
126 *
127 * while (pa_operation_get_state(o) != OPERATION_DONE)
128 * pa_threaded_mainloop_wait(m);
129 *
130 * pa_operation_unref(o);
131 *
132 * pa_threaded_mainloop_unlock(m);
133 * }
134 * \endcode
135 *
136 * The main function, my_drain_stream_func(), will wait for the callback to
137 * be called using pa_threaded_mainloop_wait().
138 *
139 * If your application is multi-threaded, then this waiting must be done
140 * inside a while loop. The reason for this is that multiple threads might be
141 * using pa_threaded_mainloop_wait() at the same time. Each thread must
142 * therefore verify that it was its callback that was invoked.
143 *
144 * The callback, my_drain_callback(), indicates to the main function that it
145 * has been called using pa_threaded_mainloop_signal().
146 *
147 * As you can see, both pa_threaded_mainloop_wait() may only be called with
148 * the lock held. The same thing is true for pa_threaded_mainloop_signal(),
149 * but as the lock is held before the callback is invoked, you do not have to
150 * deal with that.
151 *
152 * The functions will not dead lock because the wait function will release
153 * the lock before waiting and then regrab it once it has been signaled.
154 * For those of you familiar with threads, the behaviour is that of a
155 * condition variable.
156 *
157 * \subsection data_subsec Data callbacks
158 *
159 * For many callbacks, simply knowing that they have been called is
160 * insufficient. The callback also receives some data that is desired. To
161 * access this data safely, we must extend our example a bit:
162 *
163 * \code
164 * static int *drain_result;
165 *
166 * static void my_drain_callback(pa_stream*s, int success, void *userdata) {
167 * pa_threaded_mainloop *m;
168 *
169 * m = (pa_threaded_mainloop*)userdata;
170 * assert(m);
171 *
172 * drain_result = &success;
173 *
174 * pa_threaded_mainloop_signal(m, 1);
175 * }
176 *
177 * void my_drain_stream_func(pa_threaded_mainloop *m, pa_stream *s) {
178 * pa_operation *o;
179 *
180 * pa_threaded_mainloop_lock(m);
181 *
182 * o = pa_stream_drain(s, my_drain_callback, m);
183 * assert(o);
184 *
185 * while (pa_operation_get_state(o) != OPERATION_DONE)
186 * pa_threaded_mainloop_wait(m);
187 *
188 * pa_operation_unref(o);
189 *
190 * if (*drain_result)
191 * printf("Success!");
192 * else
193 * printf("Bitter defeat...");
194 *
195 * pa_threaded_mainloop_accept(m);
196 *
197 * pa_threaded_mainloop_unlock(m);
198 * }
199 * \endcode
200 *
201 * The example is a bit silly as it would probably have been easier to just
202 * copy the contents of success, but for larger data structures this can be
203 * wasteful.
204 *
205 * The difference here compared to the basic callback is the 1 sent to
206 * pa_threaded_mainloop_signal() and the call to
207 * pa_threaded_mainloop_accept(). What will happen is that
208 * pa_threaded_mainloop_signal() will signal the main function and then stop.
209 * The main function is then free to use the data in the callback until
210 * pa_threaded_mainloop_accept() is called, which will allow the callback
211 * to continue.
212 *
213 * Note that pa_threaded_mainloop_accept() must be called some time between
214 * exiting the while loop and unlocking the main loop! Failure to do so will
215 * result in a race condition. I.e. it is not ok to release the lock and
216 * regrab it before calling pa_threaded_mainloop_accept().
217 *
218 * \subsection async_subsec Asynchronous callbacks
219 *
220 * PulseAudio also has callbacks that are completely asynchronous, meaning
221 * that they can be called at any time. The threading main loop API provides
222 * the locking mechanism to handle concurrent accesses, but nothing else.
223 * Applications will have to handle communication from the callback to the
224 * main program through some own system.
225 *
226 * The callbacks that are completely asynchronous are:
227 *
228 * \li State callbacks for contexts, streams, etc.
229 * \li Subscription notifications
230 */
231
232 /** \file
233 *
234 * A thread based event loop implementation based on pa_mainloop. The
235 * event loop is run in a helper thread in the background. A few
236 * synchronization primitives are available to access the objects
237 * attached to the event loop safely. */
238
239 /** An opaque threaded main loop object */
240 typedef struct pa_threaded_mainloop pa_threaded_mainloop;
241
242 /** Allocate a new threaded main loop object. You have to call
243 * pa_threaded_mainloop_start() before the event loop thread starts
244 * running. */
245 pa_threaded_mainloop *pa_threaded_mainloop_new(void);
246
247 /** Free a threaded main loop object. If the event loop thread is
248 * still running, it is terminated using pa_threaded_mainloop_stop()
249 * first. */
250 void pa_threaded_mainloop_free(pa_threaded_mainloop* m);
251
252 /** Start the event loop thread. */
253 int pa_threaded_mainloop_start(pa_threaded_mainloop *m);
254
255 /** Terminate the event loop thread cleanly. Make sure to unlock the
256 * mainloop object before calling this function. */
257 void pa_threaded_mainloop_stop(pa_threaded_mainloop *m);
258
259 /** Lock the event loop object, effectively blocking the event loop
260 * thread from processing events. You can use this to enforce
261 * exclusive access to all objects attached to the event loop. This
262 * lock is recursive. This function may not be called inside the event
263 * loop thread. Events that are dispatched from the event loop thread
264 * are executed with this lock held. */
265 void pa_threaded_mainloop_lock(pa_threaded_mainloop *m);
266
267 /** Unlock the event loop object, inverse of pa_threaded_mainloop_lock() */
268 void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m);
269
270 /** Wait for an event to be signalled by the event loop thread. You
271 * can use this to pass data from the event loop thread to the main
272 * thread in synchronized fashion. This function may not be called
273 * inside the event loop thread. Prior to this call the event loop
274 * object needs to be locked using pa_threaded_mainloop_lock(). While
275 * waiting the lock will be released, immediately before returning it
276 * will be acquired again. */
277 void pa_threaded_mainloop_wait(pa_threaded_mainloop *m);
278
279 /** Signal all threads waiting for a signalling event in
280 * pa_threaded_mainloop_wait(). If wait_for_release is non-zero, do
281 * not return before the signal was accepted by a
282 * pa_threaded_mainloop_accept() call. While waiting for that condition
283 * the event loop object is unlocked. */
284 void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept);
285
286 /** Accept a signal from the event thread issued with
287 * pa_threaded_mainloop_signal(). This call should only be used in
288 * conjunction with pa_threaded_mainloop_signal() with a non-zero
289 * wait_for_accept value. */
290 void pa_threaded_mainloop_accept(pa_threaded_mainloop *m);
291
292 /** Return the return value as specified with the main loop's quit() routine. */
293 int pa_threaded_mainloop_get_retval(pa_threaded_mainloop *m);
294
295 /** Return the abstract main loop abstraction layer vtable for this main loop. */
296 pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m);
297
298 /** Returns non-zero when called from withing the event loop thread. \since 0.9.7 */
299 int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m);
300
301 PA_C_DECL_END
302
303 #endif