mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:57:35 +00:00
LibAudio: Unlock the mutex in PulseAudioContext::instance() before exit
The mutex used to protect from multiple threads creating PulseAudio contexts simultaneously could remain locked when an application exited. The static variables' destructors could be called on the main thread while another thread is running `PulseAudioContext::instance()` and synchronously connecting to a PulseAudio daemon. This would cause an assertion in Mutex that it is unlocked upon its destructor being called. By creating a static `ScopeGuard` that locks and immediately unlocks the mutex, we can ensure that the main thread waits for the connection to succeed or fail. In most cases, this will not take long, but if the connection is timing out, it could take a matter of seconds.
This commit is contained in:
parent
f22d7bbb90
commit
37acdc9ba7
1 changed files with 7 additions and 0 deletions
|
@ -16,6 +16,13 @@ ErrorOr<NonnullRefPtr<PulseAudioContext>> PulseAudioContext::instance()
|
|||
// Use a weak pointer to allow the context to be shut down if we stop outputting audio.
|
||||
static WeakPtr<PulseAudioContext> the_instance;
|
||||
static Threading::Mutex instantiation_mutex;
|
||||
// Lock and unlock the mutex to ensure that the mutex is fully unlocked at application
|
||||
// exit.
|
||||
atexit([]() {
|
||||
instantiation_mutex.lock();
|
||||
instantiation_mutex.unlock();
|
||||
});
|
||||
|
||||
auto instantiation_locker = Threading::MutexLocker(instantiation_mutex);
|
||||
|
||||
RefPtr<PulseAudioContext> strong_instance_pointer = the_instance.strong_ref();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue