Skip to content
This repository has been archived by the owner on Jun 8, 2018. It is now read-only.

Fix pulseaudio locking #2

Merged
merged 1 commit into from
Jan 7, 2016

Conversation

sqweek
Copy link

@sqweek sqweek commented Dec 30, 2015

@illuusio I think you're right about wrapping the context calls with locks. thread-mainloop.h says:

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.

And then goes on to discuss locking:

Since the PulseAudio API doesn't allow concurrent accesses to objects,
a locking scheme must be used to guarantee safe usage. The threaded main
loop API provides such a scheme through the functions
pa_threaded_mainloop_lock() and pa_threaded_mainloop_unlock().

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.

It also explictly says that the lock must be held when calling pa_threaded_mainloop_wait. So this means all the stuff in PaPulseAudio_Initialize does need to happen while locked, therefore I've reinstated the locks that were removed in 207937b (actually I just hold the lock for the whole time we are doing pulse stuff). The only other place I could find making a pulse call without the lock held was Terminate.

When using a threaded-mainloop, pulseaudio calls require
synchronisation via pa_threaded_mainloop_lock and _unlock.

This fixes errors of the form:

 Assertion 'c->callback' failed at pulsecore/socket-client.c:126, function do_call(). Aborting.
 Assertion 'c->defer_event == e' failed at pulsecore/socket-client.c:170, function connect_defer_cb(). Aborting.
illuusio added a commit that referenced this pull request Jan 7, 2016
Fix PulseAudio threading mainloop crashers with adding needed locking for resources that are not thread safe.
@illuusio illuusio merged commit fd0cb42 into illuusio:hostapi-pulseaudio Jan 7, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants