Преглед изворни кода

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.
Zaggy1024 пре 1 година
родитељ
комит
37acdc9ba7
1 измењених фајлова са 7 додато и 0 уклоњено
  1. 7 0
      Userland/Libraries/LibAudio/PulseAudioWrappers.cpp

+ 7 - 0
Userland/Libraries/LibAudio/PulseAudioWrappers.cpp

@@ -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();