1 module pulse.threadmainloop; 2 3 version(linux): 4 5 import pulse.mainloopapi; 6 7 extern (C): 8 9 /*** 10 This file is part of PulseAudio. 11 12 Copyright 2006 Lennart Poettering 13 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB 14 15 PulseAudio is free software; you can redistribute it and/or modify 16 it under the terms of the GNU Lesser General Public License as published 17 by the Free Software Foundation; either version 2.1 of the License, 18 or (at your option) any later version. 19 20 PulseAudio is distributed in the hope that it will be useful, but 21 WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 General Public License for more details. 24 25 You should have received a copy of the GNU Lesser General Public License 26 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 27 ***/ 28 29 /** \page threaded_mainloop Threaded Main Loop 30 * 31 * \section overv_sec Overview 32 * 33 * The threaded main loop implementation is a special version of the primary 34 * main loop implementation (see \ref mainloop). For the basic design, see 35 * its documentation. 36 * 37 * The added feature in the threaded main loop is that it spawns a new thread 38 * that runs the real main loop. This allows a synchronous application to use 39 * the asynchronous API without risking stalling the PulseAudio library. 40 * 41 * \section creat_sec Creation 42 * 43 * A pa_threaded_mainloop object is created using pa_threaded_mainloop_new(). 44 * This will only allocate the required structures though, so to use it the 45 * thread must also be started. This is done through 46 * pa_threaded_mainloop_start(), after which you can start using the main loop. 47 * 48 * \section destr_sec Destruction 49 * 50 * When the PulseAudio connection has been terminated, the thread must be 51 * stopped and the resources freed. Stopping the thread is done using 52 * pa_threaded_mainloop_stop(), which must be called without the lock (see 53 * below) held. When that function returns, the thread is stopped and the 54 * pa_threaded_mainloop object can be freed using pa_threaded_mainloop_free(). 55 * 56 * \section lock_sec Locking 57 * 58 * Since the PulseAudio API doesn't allow concurrent accesses to objects, 59 * a locking scheme must be used to guarantee safe usage. The threaded main 60 * loop API provides such a scheme through the functions 61 * pa_threaded_mainloop_lock() and pa_threaded_mainloop_unlock(). 62 * 63 * The lock is recursive, so it's safe to use it multiple times from the same 64 * thread. Just make sure you call pa_threaded_mainloop_unlock() the same 65 * number of times you called pa_threaded_mainloop_lock(). 66 * 67 * The lock needs to be held whenever you call any PulseAudio function that 68 * uses an object associated with this main loop. Those objects include 69 * pa_mainloop, pa_context, pa_stream and pa_operation, and the various event 70 * objects (pa_io_event, pa_time_event, pa_defer_event). Make sure you do not 71 * hold on to the lock more than necessary though, as the threaded main loop 72 * stops 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 = 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) == PA_OPERATION_RUNNING) 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 140 * done inside a while loop. The reason for this is that multiple 141 * threads might be using pa_threaded_mainloop_wait() at the same 142 * time. Each thread must therefore verify that it was its callback 143 * that was invoked. Also the underlying OS synchronization primitives 144 * are usually not free of spurious wake-ups, so a 145 * pa_threaded_mainloop_wait() must be called within a loop even if 146 * you have only one thread waiting. 147 * 148 * The callback, my_drain_callback(), indicates to the main function that it 149 * has been called using pa_threaded_mainloop_signal(). 150 * 151 * As you can see, pa_threaded_mainloop_wait() may only be called with 152 * the lock held. The same thing is true for pa_threaded_mainloop_signal(), 153 * but as the lock is held before the callback is invoked, you do not have to 154 * deal with that. 155 * 156 * The functions will not dead lock because the wait function will release 157 * the lock before waiting and then regrab it once it has been signalled. 158 * For those of you familiar with threads, the behaviour is that of a 159 * condition variable. 160 * 161 * \subsection data_subsec Data callbacks 162 * 163 * For many callbacks, simply knowing that they have been called is 164 * insufficient. The callback also receives some data that is desired. To 165 * access this data safely, we must extend our example a bit: 166 * 167 * \code 168 * static int * volatile drain_result = NULL; 169 * 170 * static void my_drain_callback(pa_stream*s, int success, void *userdata) { 171 * pa_threaded_mainloop *m; 172 * 173 * m = userdata; 174 * assert(m); 175 * 176 * drain_result = &success; 177 * 178 * pa_threaded_mainloop_signal(m, 1); 179 * } 180 * 181 * void my_drain_stream_func(pa_threaded_mainloop *m, pa_stream *s) { 182 * pa_operation *o; 183 * 184 * pa_threaded_mainloop_lock(m); 185 * 186 * o = pa_stream_drain(s, my_drain_callback, m); 187 * assert(o); 188 * 189 * while (drain_result == NULL) 190 * pa_threaded_mainloop_wait(m); 191 * 192 * pa_operation_unref(o); 193 * 194 * if (*drain_result) 195 * printf("Success!"); 196 * else 197 * printf("Bitter defeat..."); 198 * 199 * pa_threaded_mainloop_accept(m); 200 * 201 * pa_threaded_mainloop_unlock(m); 202 * } 203 * \endcode 204 * 205 * The example is a bit silly as it would probably have been easier to just 206 * copy the contents of success, but for larger data structures this can be 207 * wasteful. 208 * 209 * The difference here compared to the basic callback is the value 1 passed 210 * to pa_threaded_mainloop_signal() and the call to 211 * pa_threaded_mainloop_accept(). What will happen is that 212 * pa_threaded_mainloop_signal() will signal the main function and then wait. 213 * The main function is then free to use the data in the callback until 214 * pa_threaded_mainloop_accept() is called, which will allow the callback 215 * to continue. 216 * 217 * Note that pa_threaded_mainloop_accept() must be called some time between 218 * exiting the while loop and unlocking the main loop! Failure to do so will 219 * result in a race condition. I.e. it is not ok to release the lock and 220 * regrab it before calling pa_threaded_mainloop_accept(). 221 * 222 * \subsection async_subsec Asynchronous callbacks 223 * 224 * PulseAudio also has callbacks that are completely asynchronous, meaning 225 * that they can be called at any time. The threaded main loop API provides 226 * the locking mechanism to handle concurrent accesses, but nothing else. 227 * Applications will have to handle communication from the callback to the 228 * main program through their own mechanisms. 229 * 230 * The callbacks that are completely asynchronous are: 231 * 232 * \li State callbacks for contexts, streams, etc. 233 * \li Subscription notifications 234 */ 235 236 /** \file 237 * 238 * A thread based event loop implementation based on pa_mainloop. The 239 * event loop is run in a helper thread in the background. A few 240 * synchronization primitives are available to access the objects 241 * attached to the event loop safely. 242 * 243 * See also \subpage threaded_mainloop 244 */ 245 246 /** An opaque threaded main loop object */ 247 struct pa_threaded_mainloop; 248 249 /** Allocate a new threaded main loop object. You have to call 250 * pa_threaded_mainloop_start() before the event loop thread starts 251 * running. Free with pa_threaded_mainloop_free. */ 252 pa_threaded_mainloop* pa_threaded_mainloop_new (); 253 254 /** Free a threaded main loop object. If the event loop thread is 255 * still running, terminate it with pa_threaded_mainloop_stop() 256 * first. */ 257 void pa_threaded_mainloop_free (pa_threaded_mainloop* m); 258 259 /** Start the event loop thread. Returns zero on success, negative on error. */ 260 int pa_threaded_mainloop_start (pa_threaded_mainloop* m); 261 262 /** Terminate the event loop thread cleanly. Make sure to unlock the 263 * mainloop object before calling this function. */ 264 void pa_threaded_mainloop_stop (pa_threaded_mainloop* m); 265 266 /** Lock the event loop object, effectively blocking the event loop 267 * thread from processing events. You can use this to enforce 268 * exclusive access to all objects attached to the event loop. This 269 * lock is recursive. This function may not be called inside the event 270 * loop thread. Events that are dispatched from the event loop thread 271 * are executed with this lock held. */ 272 void pa_threaded_mainloop_lock (pa_threaded_mainloop* m); 273 274 /** Unlock the event loop object, inverse of pa_threaded_mainloop_lock(). */ 275 void pa_threaded_mainloop_unlock (pa_threaded_mainloop* m); 276 277 /** Wait for an event to be signalled by the event loop thread. You 278 * can use this to pass data from the event loop thread to the main 279 * thread in a synchronized fashion. This function may not be called 280 * inside the event loop thread. Prior to this call the event loop 281 * object needs to be locked using pa_threaded_mainloop_lock(). While 282 * waiting the lock will be released. Immediately before returning it 283 * will be acquired again. This function may spuriously wake up even 284 * without pa_threaded_mainloop_signal() being called. You need to 285 * make sure to handle that! */ 286 void pa_threaded_mainloop_wait (pa_threaded_mainloop* m); 287 288 /** Signal all threads waiting for a signalling event in 289 * pa_threaded_mainloop_wait(). If wait_for_accept is non-zero, do 290 * not return before the signal was accepted by a 291 * pa_threaded_mainloop_accept() call. While waiting for that condition 292 * the event loop object is unlocked. */ 293 void pa_threaded_mainloop_signal (pa_threaded_mainloop* m, int wait_for_accept); 294 295 /** Accept a signal from the event thread issued with 296 * pa_threaded_mainloop_signal(). This call should only be used in 297 * conjunction with pa_threaded_mainloop_signal() with a non-zero 298 * wait_for_accept value. */ 299 void pa_threaded_mainloop_accept (pa_threaded_mainloop* m); 300 301 /** Return the return value as specified with the main loop's 302 * pa_mainloop_quit() routine. */ 303 int pa_threaded_mainloop_get_retval (const(pa_threaded_mainloop)* m); 304 305 /** Return the main loop abstraction layer vtable for this main loop. 306 * There is no need to free this object as it is owned by the loop 307 * and is destroyed when the loop is freed. */ 308 pa_mainloop_api* pa_threaded_mainloop_get_api (pa_threaded_mainloop* m); 309 310 /** Returns non-zero when called from within the event loop thread. \since 0.9.7 */ 311 int pa_threaded_mainloop_in_thread (pa_threaded_mainloop* m); 312 313 /** Sets the name of the thread. \since 5.0 */ 314 void pa_threaded_mainloop_set_name (pa_threaded_mainloop* m, const(char)* name); 315 316 /** Runs the given callback in the mainloop thread without the lock held. The 317 * caller is responsible for ensuring that PulseAudio data structures are only 318 * accessed in a thread-safe way (that is, APIs that take pa_context and 319 * pa_stream are not thread-safe, and should not accessed without some 320 * synchronisation). This is the only situation in which 321 * pa_threaded_mainloop_lock() and pa_threaded_mainloop_unlock() may be used 322 * in the mainloop thread context. \since 13.0 */ 323 void pa_threaded_mainloop_once_unlocked ( 324 pa_threaded_mainloop* m, 325 void function (pa_threaded_mainloop* m, void* userdata) callback, 326 void* userdata); 327