2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
2022-01-02 13:52:38 +00:00
|
|
|
* Copyright (c) 2022, kleines Filmröllchen <malu.bertsch@gmail.com>
|
2022-02-26 16:09:45 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <AK/Badge.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/Event.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
2024-10-31 07:44:19 +00:00
|
|
|
#include <LibCore/EventLoopImplementation.h>
|
2023-08-06 16:09:39 +00:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2022-12-29 12:20:44 +00:00
|
|
|
#include <LibCore/Promise.h>
|
2023-04-23 17:45:12 +00:00
|
|
|
#include <LibCore/ThreadEventQueue.h>
|
2022-01-23 13:47:10 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2023-04-24 10:25:14 +00:00
|
|
|
namespace {
|
2023-07-12 01:48:56 +00:00
|
|
|
OwnPtr<Vector<EventLoop&>>& event_loop_stack_uninitialized()
|
2022-01-02 14:14:25 +00:00
|
|
|
{
|
2023-08-03 04:03:54 +00:00
|
|
|
thread_local OwnPtr<Vector<EventLoop&>> s_event_loop_stack = nullptr;
|
2023-07-12 01:48:56 +00:00
|
|
|
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;
|
2022-01-02 14:14:25 +00:00
|
|
|
}
|
2021-01-08 19:09:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 08:31:49 +00:00
|
|
|
EventLoop::EventLoop()
|
2023-04-25 15:38:48 +00:00
|
|
|
: m_impl(EventLoopManager::the().make_implementation())
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
if (event_loop_stack().is_empty()) {
|
|
|
|
event_loop_stack().append(*this);
|
2022-04-23 23:48:11 +00:00
|
|
|
}
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop::~EventLoop()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
if (!event_loop_stack().is_empty() && &event_loop_stack().last() == this) {
|
|
|
|
event_loop_stack().take_last();
|
|
|
|
}
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 01:48:56 +00:00
|
|
|
bool EventLoop::is_running()
|
|
|
|
{
|
|
|
|
auto& stack = event_loop_stack_uninitialized();
|
|
|
|
return stack != nullptr && !stack->is_empty();
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop& EventLoop::current()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-11-26 05:21:52 +00:00
|
|
|
if (event_loop_stack().is_empty())
|
|
|
|
dbgln("No EventLoop is present, unable to return current one!");
|
2023-04-24 10:25:14 +00:00
|
|
|
return event_loop_stack().last();
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::quit(int code)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-05-11 19:58:40 +00:00
|
|
|
ThreadEventQueue::current().cancel_all_pending_jobs();
|
2023-04-24 10:25:14 +00:00
|
|
|
m_impl->quit(code);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::unquit()
|
2019-10-25 05:18:31 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
m_impl->unquit();
|
2019-10-25 05:18:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
struct EventLoopPusher {
|
2019-04-10 15:30:34 +00:00
|
|
|
public:
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoopPusher(EventLoop& event_loop)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
event_loop_stack().append(event_loop);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
~EventLoopPusher()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
event_loop_stack().take_last();
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
int EventLoop::exec()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoopPusher pusher(*this);
|
2023-04-24 10:25:14 +00:00
|
|
|
return m_impl->exec();
|
2019-05-18 11:39:21 +00:00
|
|
|
}
|
2019-04-29 13:57:49 +00:00
|
|
|
|
2021-09-25 17:32:14 +00:00
|
|
|
void EventLoop::spin_until(Function<bool()> goal_condition)
|
|
|
|
{
|
|
|
|
EventLoopPusher pusher(*this);
|
2023-07-11 22:56:28 +00:00
|
|
|
while (!m_impl->was_exit_requested() && !goal_condition())
|
2021-09-25 17:32:14 +00:00
|
|
|
pump();
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:55:48 +00:00
|
|
|
size_t EventLoop::pump(WaitMode mode)
|
2019-05-18 11:39:21 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
return m_impl->pump(mode == WaitMode::WaitForEvents ? EventLoopImplementation::PumpMode::WaitForEvents : EventLoopImplementation::PumpMode::DontWaitForEvents);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
void EventLoop::post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&& event)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-26 16:51:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
void EventLoop::add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>> job_promise)
|
2022-12-29 12:20:44 +00:00
|
|
|
{
|
2023-04-23 17:45:12 +00:00
|
|
|
ThreadEventQueue::current().add_job(move(job_promise));
|
2022-12-29 12:20:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 10:25:14 +00:00
|
|
|
int EventLoop::register_signal(int signal_number, Function<void(int)> handler)
|
2020-07-06 21:48:02 +00:00
|
|
|
{
|
2023-04-25 15:38:48 +00:00
|
|
|
return EventLoopManager::the().register_signal(signal_number, move(handler));
|
2020-07-06 21:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::unregister_signal(int handler_id)
|
|
|
|
{
|
2023-04-25 15:38:48 +00:00
|
|
|
EventLoopManager::the().unregister_signal(handler_id);
|
2020-09-07 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 10:25:14 +00:00
|
|
|
void EventLoop::notify_forked(ForkEvent)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
current().m_impl->notify_forked_and_in_child();
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 05:11:38 +00:00
|
|
|
intptr_t EventLoop::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-25 15:38:48 +00:00
|
|
|
return EventLoopManager::the().register_timer(object, milliseconds, should_reload, fire_when_not_visible);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 05:11:38 +00:00
|
|
|
void EventLoop::unregister_timer(intptr_t timer_id)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2024-02-18 05:10:23 +00:00
|
|
|
EventLoopManager::the().unregister_timer(timer_id);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-25 15:38:48 +00:00
|
|
|
EventLoopManager::the().register_notifier(notifier);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2023-04-25 15:38:48 +00:00
|
|
|
EventLoopManager::the().unregister_notifier(notifier);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
2019-07-14 08:20:57 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::wake()
|
2019-07-14 08:20:57 +00:00
|
|
|
{
|
2023-04-24 10:25:14 +00:00
|
|
|
m_impl->wake();
|
2019-07-14 08:20:57 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2023-04-23 19:19:37 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2023-04-24 10:25:14 +00:00
|
|
|
bool EventLoop::was_exit_requested() const
|
|
|
|
{
|
|
|
|
return m_impl->was_exit_requested();
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|