thread-mainloop.h 12 KB

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