ladybird/Libraries/LibCore/EventLoop.cpp

170 lines
4.1 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2022, kleines Filmröllchen <malu.bertsch@gmail.com>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Badge.h>
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
2024-10-31 07:44:19 +00:00
#include <LibCore/EventLoopImplementation.h>
#include <LibCore/EventReceiver.h>
#include <LibCore/Promise.h>
#include <LibCore/ThreadEventQueue.h>
namespace Core {
namespace {
OwnPtr<Vector<EventLoop&>>& event_loop_stack_uninitialized()
{
thread_local OwnPtr<Vector<EventLoop&>> s_event_loop_stack = nullptr;
return s_event_loop_stack;
}
Vector<EventLoop&>& event_loop_stack()
{
auto& the_stack = event_loop_stack_uninitialized();
if (the_stack == nullptr)
the_stack = make<Vector<EventLoop&>>();
return *the_stack;
}
}
EventLoop::EventLoop()
: m_impl(EventLoopManager::the().make_implementation())
{
if (event_loop_stack().is_empty()) {
event_loop_stack().append(*this);
}
}
EventLoop::~EventLoop()
{
if (!event_loop_stack().is_empty() && &event_loop_stack().last() == this) {
event_loop_stack().take_last();
}
}
bool EventLoop::is_running()
{
auto& stack = event_loop_stack_uninitialized();
return stack != nullptr && !stack->is_empty();
}
EventLoop& EventLoop::current()
{
if (event_loop_stack().is_empty())
dbgln("No EventLoop is present, unable to return current one!");
return event_loop_stack().last();
}
void EventLoop::quit(int code)
{
ThreadEventQueue::current().cancel_all_pending_jobs();
m_impl->quit(code);
}
void EventLoop::unquit()
{
m_impl->unquit();
}
struct EventLoopPusher {
public:
EventLoopPusher(EventLoop& event_loop)
{
event_loop_stack().append(event_loop);
}
~EventLoopPusher()
{
event_loop_stack().take_last();
}
};
int EventLoop::exec()
{
EventLoopPusher pusher(*this);
return m_impl->exec();
}
void EventLoop::spin_until(Function<bool()> goal_condition)
{
EventLoopPusher pusher(*this);
while (!m_impl->was_exit_requested() && !goal_condition())
pump();
}
size_t EventLoop::pump(WaitMode mode)
{
return m_impl->pump(mode == WaitMode::WaitForEvents ? EventLoopImplementation::PumpMode::WaitForEvents : EventLoopImplementation::PumpMode::DontWaitForEvents);
}
void EventLoop::post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&& event)
{
m_impl->post_event(receiver, move(event));
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.
2022-07-13 08:36:57 +00:00
}
void EventLoop::add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>> job_promise)
{
ThreadEventQueue::current().add_job(move(job_promise));
}
int EventLoop::register_signal(int signal_number, Function<void(int)> handler)
{
return EventLoopManager::the().register_signal(signal_number, move(handler));
}
void EventLoop::unregister_signal(int handler_id)
{
EventLoopManager::the().unregister_signal(handler_id);
}
void EventLoop::notify_forked(ForkEvent)
{
current().m_impl->notify_forked_and_in_child();
}
intptr_t EventLoop::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
{
return EventLoopManager::the().register_timer(object, milliseconds, should_reload, fire_when_not_visible);
}
void EventLoop::unregister_timer(intptr_t timer_id)
{
EventLoopManager::the().unregister_timer(timer_id);
}
void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
{
EventLoopManager::the().register_notifier(notifier);
}
void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
{
EventLoopManager::the().unregister_notifier(notifier);
}
void EventLoop::wake()
{
m_impl->wake();
}
void EventLoop::deferred_invoke(Function<void()> invokee)
{
auto context = DeferredInvocationContext::construct();
post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
}
void deferred_invoke(Function<void()> invokee)
{
EventLoop::current().deferred_invoke(move(invokee));
}
bool EventLoop::was_exit_requested() const
{
return m_impl->was_exit_requested();
}
}