summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2022-07-13 10:36:57 +0200
committerLinus Groh <mail@linusgroh.de>2022-07-22 19:35:41 +0100
commit125122a9abe412fc8f197c7c2c96c43f7229dd28 (patch)
tree4a37f59a57866c05c933d16841a3e287570c806d /Userland/Libraries/LibAudio
parent763cda227fbdd74d3a80ca0e2c76136f5c3fcf01 (diff)
downloadserenity-125122a9abe412fc8f197c7c2c96c43f7229dd28.zip
LibAudio: Prevent racy eternal deadlock of the audio enqueue thread
The audio enqueuer thread goes to sleep when there is no more audio data present, and through normal Core::EventLoop events it can be woken up. However, that waking up only happens when the thread is not currently running, so that the wake-up events don't queue up and cause weirdness. The atomic variable responsible for keeping track of whether the thread is active can lead to a racy deadlock however, where the audio enqueuer thread will never wake up again despite there being audio data to enqueue. Consider this scenario: - Main thread calls into async_enqueue. It detects that according to the atomic variable, the other thread is still running, skipping the event queue wake. - Enqueuer thread has just finished playing the last chunk of audio and detects that there is no audio left. It enters the if block with the dbgln "Reached end of provided audio data..." - Main thread enqueues audio, making the user sample queue non-empty. - Enqueuer thread does not check this condition again, instead setting the atomic variable to indicate that it is not running. It exits into an event loop sleep. - Main thread exits async_enqueue. The calling audio enqueuing system (see e.g. Piano, but all of them function similarly) will wait until the enqueuer thread has played enough samples before async_enqueue is called again. However, since the enqueuer thread will never play any audio, this condition is never fulfilled and audio playback deadlocks This commit fixes that by allowing the event loop to not enqueue an event that already exists, therefore overloading the audio enqueuer event loop by at maximum one message in weird situations. We entirely get rid of the atomic variable and the race condition is prevented.
Diffstat (limited to 'Userland/Libraries/LibAudio')
-rw-r--r--Userland/Libraries/LibAudio/ConnectionToServer.cpp6
-rw-r--r--Userland/Libraries/LibAudio/ConnectionToServer.h1
2 files changed, 1 insertions, 6 deletions
diff --git a/Userland/Libraries/LibAudio/ConnectionToServer.cpp b/Userland/Libraries/LibAudio/ConnectionToServer.cpp
index e42ba7a07a..7f063f8502 100644
--- a/Userland/Libraries/LibAudio/ConnectionToServer.cpp
+++ b/Userland/Libraries/LibAudio/ConnectionToServer.cpp
@@ -62,8 +62,7 @@ ErrorOr<void> ConnectionToServer::async_enqueue(FixedArray<Sample>&& samples)
update_good_sleep_time();
m_user_queue->append(move(samples));
// Wake the background thread to make sure it starts enqueuing audio.
- if (!m_audio_enqueuer_active.load())
- m_enqueuer_loop->post_event(*this, make<Core::CustomEvent>(0), Core::EventLoop::ShouldWake::Yes);
+ m_enqueuer_loop->wake_once(*this, 0);
async_start_playback();
return {};
@@ -87,8 +86,6 @@ void ConnectionToServer::custom_event(Core::CustomEvent&)
{
Array<Sample, AUDIO_BUFFER_SIZE> next_chunk;
while (true) {
- m_audio_enqueuer_active.store(true);
-
if (m_user_queue->is_empty()) {
dbgln("Reached end of provided audio data, going to sleep");
break;
@@ -107,7 +104,6 @@ void ConnectionToServer::custom_event(Core::CustomEvent&)
if (result.is_error())
dbgln("Error while writing samples to shared buffer: {}", result.error());
}
- m_audio_enqueuer_active.store(false);
}
ErrorOr<void, AudioQueue::QueueStatus> ConnectionToServer::realtime_enqueue(Array<Sample, AUDIO_BUFFER_SIZE> samples)
diff --git a/Userland/Libraries/LibAudio/ConnectionToServer.h b/Userland/Libraries/LibAudio/ConnectionToServer.h
index 154d7bb5b5..e14deab6c3 100644
--- a/Userland/Libraries/LibAudio/ConnectionToServer.h
+++ b/Userland/Libraries/LibAudio/ConnectionToServer.h
@@ -83,7 +83,6 @@ private:
NonnullRefPtr<Threading::Thread> m_background_audio_enqueuer;
Core::EventLoop* m_enqueuer_loop;
Threading::Mutex m_enqueuer_loop_destruction;
- Atomic<bool> m_audio_enqueuer_active { false };
// A good amount of time to sleep when the queue is full.
// (Only used for non-realtime enqueues)