The added feature in the threaded main loop is that it spawns a new thread that runs the real main loop. This allows a synchronous application to use the asynchronous API without risking to stall the PulseAudio library.
The lock is recursive, so it's safe to use it multiple times from the same thread. Just make sure you call pa_threaded_mainloop_unlock() the same number of times you called pa_threaded_mainloop_lock().
The lock needs to be held whenever you call any PulseAudio function that uses an object associated with this main loop. Make sure you do not hold on to the lock more than necessary though, as the threaded main loop stops while the lock is held.
Example:
void my_check_stream_func(pa_threaded_mainloop *m, pa_stream *s) { pa_stream_state_t state; pa_threaded_mainloop_lock(m); state = pa_stream_get_state(s); pa_threaded_mainloop_unlock(m); if (state == PA_STREAM_READY) printf("Stream is ready!"); else printf("Stream is not ready!"); }
The easiest way to turn the callback based operations into synchronous ones, is to simply wait for the callback to be called and continue from there. This is the approach chosen in PulseAudio's threaded API.
Example:
static void my_drain_callback(pa_stream *s, int success, void *userdata) { pa_threaded_mainloop *m; m = userdata; assert(m); pa_threaded_mainloop_signal(m, 0); } void my_drain_stream_func(pa_threaded_mainloop *m, pa_stream *s) { pa_operation *o; pa_threaded_mainloop_lock(m); o = pa_stream_drain(s, my_drain_callback, m); assert(o); while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) pa_threaded_mainloop_wait(m); pa_operation_unref(o); pa_threaded_mainloop_unlock(m); }
The main function, my_drain_stream_func(), will wait for the callback to be called using pa_threaded_mainloop_wait().
If your application is multi-threaded, then this waiting must be done inside a while loop. The reason for this is that multiple threads might be using pa_threaded_mainloop_wait() at the same time. Each thread must therefore verify that it was its callback that was invoked.
The callback, my_drain_callback(), indicates to the main function that it has been called using pa_threaded_mainloop_signal().
As you can see, both pa_threaded_mainloop_wait() may only be called with the lock held. The same thing is true for pa_threaded_mainloop_signal(), but as the lock is held before the callback is invoked, you do not have to deal with that.
The functions will not dead lock because the wait function will release the lock before waiting and then regrab it once it has been signaled. For those of you familiar with threads, the behaviour is that of a condition variable.
static int *drain_result; static void my_drain_callback(pa_stream*s, int success, void *userdata) { pa_threaded_mainloop *m; m = userdata; assert(m); drain_result = &success; pa_threaded_mainloop_signal(m, 1); } void my_drain_stream_func(pa_threaded_mainloop *m, pa_stream *s) { pa_operation *o; pa_threaded_mainloop_lock(m); o = pa_stream_drain(s, my_drain_callback, m); assert(o); while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) pa_threaded_mainloop_wait(m); pa_operation_unref(o); if (*drain_result) printf("Success!"); else printf("Bitter defeat..."); pa_threaded_mainloop_accept(m); pa_threaded_mainloop_unlock(m); }
The example is a bit silly as it would probably have been easier to just copy the contents of success, but for larger data structures this can be wasteful.
The difference here compared to the basic callback is the 1 sent to pa_threaded_mainloop_signal() and the call to pa_threaded_mainloop_accept(). What will happen is that pa_threaded_mainloop_signal() will signal the main function and then stop. The main function is then free to use the data in the callback until pa_threaded_mainloop_accept() is called, which will allow the callback to continue.
Note that pa_threaded_mainloop_accept() must be called some time between exiting the while loop and unlocking the main loop! Failure to do so will result in a race condition. I.e. it is not ok to release the lock and regrab it before calling pa_threaded_mainloop_accept().
The callbacks that are completely asynchronous are: