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