2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
2022-01-02 10:52:38 -03:00
|
|
|
* Copyright (c) 2022, kleines Filmröllchen <malu.bertsch@gmail.com>
|
2022-02-26 13:09:45 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <AK/Badge.h>
|
2026-02-21 08:13:28 -03:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2024-10-31 04:44:19 -03:00
|
|
|
#include <LibCore/EventLoopImplementation.h>
|
2023-08-06 13:09:39 -03:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2022-12-29 09:20:44 -03:00
|
|
|
#include <LibCore/Promise.h>
|
2023-04-23 14:45:12 -03:00
|
|
|
#include <LibCore/ThreadEventQueue.h>
|
2022-01-23 10:47:10 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
|
|
|
|
|
2023-04-24 07:25:14 -03:00
|
|
|
namespace {
|
2025-05-13 08:06:33 -03:00
|
|
|
|
2023-07-11 22:48:56 -03:00
|
|
|
OwnPtr<Vector<EventLoop&>>& event_loop_stack_uninitialized()
|
2022-01-02 11:14:25 -03:00
|
|
|
{
|
2023-08-03 01:03:54 -03:00
|
|
|
thread_local OwnPtr<Vector<EventLoop&>> s_event_loop_stack = nullptr;
|
2023-07-11 22:48:56 -03: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 11:14:25 -03:00
|
|
|
}
|
2025-05-13 08:06:33 -03:00
|
|
|
|
2021-01-08 16:09:39 -03:00
|
|
|
}
|
|
|
|
|
|
2023-04-24 05:31:49 -03:00
|
|
|
EventLoop::EventLoop()
|
2023-04-25 12:38:48 -03:00
|
|
|
: m_impl(EventLoopManager::the().make_implementation())
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2023-04-24 07:25:14 -03:00
|
|
|
if (event_loop_stack().is_empty()) {
|
|
|
|
|
event_loop_stack().append(*this);
|
2022-04-23 20:48:11 -03:00
|
|
|
}
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
EventLoop::~EventLoop()
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2025-12-05 16:48:48 -03:00
|
|
|
if (m_weak)
|
|
|
|
|
m_weak->revoke();
|
2023-04-24 07:25:14 -03:00
|
|
|
if (!event_loop_stack().is_empty() && &event_loop_stack().last() == this) {
|
|
|
|
|
event_loop_stack().take_last();
|
|
|
|
|
}
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2023-07-11 22:48:56 -03:00
|
|
|
bool EventLoop::is_running()
|
|
|
|
|
{
|
|
|
|
|
auto& stack = event_loop_stack_uninitialized();
|
|
|
|
|
return stack != nullptr && !stack->is_empty();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
EventLoop& EventLoop::current()
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2023-11-26 02:21:52 -03:00
|
|
|
if (event_loop_stack().is_empty())
|
|
|
|
|
dbgln("No EventLoop is present, unable to return current one!");
|
2023-04-24 07:25:14 -03:00
|
|
|
return event_loop_stack().last();
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-05 16:48:48 -03:00
|
|
|
NonnullRefPtr<WeakEventLoopReference> EventLoop::current_weak()
|
|
|
|
|
{
|
|
|
|
|
auto& event_loop = current();
|
|
|
|
|
if (!event_loop.m_weak)
|
|
|
|
|
event_loop.m_weak = adopt_ref(*new (nothrow) WeakEventLoopReference(event_loop));
|
|
|
|
|
return *event_loop.m_weak;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void EventLoop::quit(int code)
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2023-04-24 07:25:14 -03:00
|
|
|
m_impl->quit(code);
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-29 18:41:18 -03:00
|
|
|
bool EventLoop::was_exit_requested()
|
|
|
|
|
{
|
|
|
|
|
return m_impl->was_exit_requested();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
struct EventLoopPusher {
|
2019-04-10 12:30:34 -03:00
|
|
|
public:
|
2020-02-02 08:34:39 -03:00
|
|
|
EventLoopPusher(EventLoop& event_loop)
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2023-04-24 07:25:14 -03:00
|
|
|
event_loop_stack().append(event_loop);
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
2020-02-02 08:34:39 -03:00
|
|
|
~EventLoopPusher()
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2023-04-24 07:25:14 -03:00
|
|
|
event_loop_stack().take_last();
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
int EventLoop::exec()
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2020-02-02 08:34:39 -03:00
|
|
|
EventLoopPusher pusher(*this);
|
2023-04-24 07:25:14 -03:00
|
|
|
return m_impl->exec();
|
2019-05-18 08:39:21 -03:00
|
|
|
}
|
2019-04-29 10:57:49 -03:00
|
|
|
|
2021-09-25 14:32:14 -03:00
|
|
|
void EventLoop::spin_until(Function<bool()> goal_condition)
|
|
|
|
|
{
|
|
|
|
|
EventLoopPusher pusher(*this);
|
2025-01-05 00:42:22 -03:00
|
|
|
while (!goal_condition())
|
2021-09-25 14:32:14 -03:00
|
|
|
pump();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 20:55:48 -03:00
|
|
|
size_t EventLoop::pump(WaitMode mode)
|
2019-05-18 08:39:21 -03:00
|
|
|
{
|
2023-04-24 07:25:14 -03:00
|
|
|
return m_impl->pump(mode == WaitMode::WaitForEvents ? EventLoopImplementation::PumpMode::WaitForEvents : EventLoopImplementation::PumpMode::DontWaitForEvents);
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2023-04-24 07:25:14 -03:00
|
|
|
int EventLoop::register_signal(int signal_number, Function<void(int)> handler)
|
2020-07-06 18:48:02 -03:00
|
|
|
{
|
2023-04-25 12:38:48 -03:00
|
|
|
return EventLoopManager::the().register_signal(signal_number, move(handler));
|
2020-07-06 18:48:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EventLoop::unregister_signal(int handler_id)
|
|
|
|
|
{
|
2023-04-25 12:38:48 -03:00
|
|
|
EventLoopManager::the().unregister_signal(handler_id);
|
2020-09-07 15:14:42 -03:00
|
|
|
}
|
|
|
|
|
|
2025-08-11 06:45:39 -03:00
|
|
|
intptr_t EventLoop::register_timer(EventReceiver& object, int milliseconds, bool should_reload)
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2025-08-11 06:45:39 -03:00
|
|
|
return EventLoopManager::the().register_timer(object, milliseconds, should_reload);
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2024-02-18 02:11:38 -03:00
|
|
|
void EventLoop::unregister_timer(intptr_t timer_id)
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2024-02-18 02:10:23 -03:00
|
|
|
EventLoopManager::the().unregister_timer(timer_id);
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2023-04-25 12:38:48 -03:00
|
|
|
EventLoopManager::the().register_notifier(notifier);
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2023-04-25 12:38:48 -03:00
|
|
|
EventLoopManager::the().unregister_notifier(notifier);
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
2019-07-14 05:20:57 -03:00
|
|
|
|
2025-11-22 20:02:26 -03:00
|
|
|
void EventLoop::register_process(pid_t pid, ESCAPING Function<void(pid_t)> exit_handler)
|
|
|
|
|
{
|
|
|
|
|
EventLoopManager::the().register_process(pid, move(exit_handler));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EventLoop::unregister_process(pid_t pid)
|
|
|
|
|
{
|
|
|
|
|
EventLoopManager::the().unregister_process(pid);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void EventLoop::wake()
|
2019-07-14 05:20:57 -03:00
|
|
|
{
|
2023-04-24 07:25:14 -03:00
|
|
|
m_impl->wake();
|
2019-07-14 05:20:57 -03:00
|
|
|
}
|
2020-02-02 08:34:39 -03:00
|
|
|
|
2023-04-23 16:19:37 -03:00
|
|
|
void EventLoop::deferred_invoke(Function<void()> invokee)
|
|
|
|
|
{
|
2025-12-03 07:53:04 -03:00
|
|
|
m_impl->deferred_invoke(move(invokee));
|
2023-04-23 16:19:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void deferred_invoke(Function<void()> invokee)
|
|
|
|
|
{
|
|
|
|
|
EventLoop::current().deferred_invoke(move(invokee));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 16:48:48 -03:00
|
|
|
WeakEventLoopReference::WeakEventLoopReference(EventLoop& event_loop)
|
|
|
|
|
: m_event_loop(&event_loop)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WeakEventLoopReference::revoke()
|
|
|
|
|
{
|
2025-10-30 09:29:29 -03:00
|
|
|
Sync::RWLockLocker<Sync::LockMode::Write> locker { m_lock };
|
2025-12-05 16:48:48 -03:00
|
|
|
m_event_loop = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StrongEventLoopReference WeakEventLoopReference::take()
|
|
|
|
|
{
|
|
|
|
|
return StrongEventLoopReference(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StrongEventLoopReference::StrongEventLoopReference(WeakEventLoopReference& event_loop_weak)
|
|
|
|
|
{
|
|
|
|
|
event_loop_weak.m_lock.lock_read();
|
|
|
|
|
m_event_loop_weak = &event_loop_weak;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StrongEventLoopReference::~StrongEventLoopReference()
|
|
|
|
|
{
|
2026-03-07 07:57:09 -03:00
|
|
|
m_event_loop_weak->m_lock.unlock_read();
|
2025-12-05 16:48:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StrongEventLoopReference::is_alive() const
|
|
|
|
|
{
|
|
|
|
|
return m_event_loop_weak->m_event_loop != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StrongEventLoopReference::operator bool() const
|
|
|
|
|
{
|
|
|
|
|
return is_alive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EventLoop* StrongEventLoopReference::operator*() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_alive());
|
|
|
|
|
return m_event_loop_weak->m_event_loop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EventLoop* StrongEventLoopReference::operator->() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_alive());
|
|
|
|
|
return m_event_loop_weak->m_event_loop;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
}
|