mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 02:54:54 +03:00
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.
This commit is contained in:
parent
763cda227f
commit
125122a9ab
Notes:
sideshowbarker
2024-07-17 08:39:16 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/125122a9ab Pull-request: https://github.com/SerenityOS/serenity/pull/14424 Reviewed-by: https://github.com/linusg Reviewed-by: https://github.com/sin-ack ✅
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -501,6 +501,23 @@ void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event, Shoul
|
||||
wake();
|
||||
}
|
||||
|
||||
void EventLoop::wake_once(Object& receiver, int custom_event_type)
|
||||
{
|
||||
Threading::MutexLocker lock(m_private->lock);
|
||||
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop::wake_once: event type {}", custom_event_type);
|
||||
auto identical_events = m_queued_events.find_if([&](auto& queued_event) {
|
||||
if (queued_event.receiver.is_null())
|
||||
return false;
|
||||
auto const& event = queued_event.event;
|
||||
auto is_receiver_identical = queued_event.receiver.ptr() == &receiver;
|
||||
auto event_id_matches = event->type() == Event::Type::Custom && static_cast<CustomEvent const*>(event.ptr())->custom_type() == custom_event_type;
|
||||
return is_receiver_identical && event_id_matches;
|
||||
});
|
||||
// Event is not in the queue yet, so we want to wake.
|
||||
if (identical_events.is_end())
|
||||
post_event(receiver, make<CustomEvent>(custom_event_type), ShouldWake::Yes);
|
||||
}
|
||||
|
||||
SignalHandlers::SignalHandlers(int signo, void (*handle_signal)(int))
|
||||
: m_signo(signo)
|
||||
, m_original_handler(signal(signo, handle_signal))
|
||||
|
@ -56,6 +56,7 @@ public:
|
||||
void spin_until(Function<bool()>);
|
||||
|
||||
void post_event(Object& receiver, NonnullOwnPtr<Event>&&, ShouldWake = ShouldWake::No);
|
||||
void wake_once(Object& receiver, int custom_event_type);
|
||||
|
||||
static EventLoop& current();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user