From b61a87c03cfeb760252d5bdfdd6c666fd74a91e4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 26 Apr 2023 18:51:07 +0200 Subject: [PATCH] LibCore: Move post_event() back to EventLoopImplementation This shouldn't have been moved to EventLoopManager, as the manager is global and one-per-process, and the implementation is one-per-loop. This makes cross-thread event posting work again, and unbreaks SoundPlayer (and probably other things as well.) --- Ladybird/EventLoopImplementationQt.cpp | 9 ++++----- Ladybird/EventLoopImplementationQt.h | 3 +-- Userland/Libraries/LibCore/EventLoop.cpp | 2 +- .../LibCore/EventLoopImplementation.cpp | 19 +++++-------------- .../LibCore/EventLoopImplementation.h | 7 +++---- .../LibCore/EventLoopImplementationUnix.cpp | 14 +++++++------- .../LibCore/EventLoopImplementationUnix.h | 3 +-- 7 files changed, 22 insertions(+), 35 deletions(-) diff --git a/Ladybird/EventLoopImplementationQt.cpp b/Ladybird/EventLoopImplementationQt.cpp index 214b275eac9..f874a12b5f4 100644 --- a/Ladybird/EventLoopImplementationQt.cpp +++ b/Ladybird/EventLoopImplementationQt.cpp @@ -74,12 +74,11 @@ void EventLoopImplementationQt::wake() m_event_loop.wakeUp(); } -void EventLoopManagerQt::deferred_invoke(Function function) +void EventLoopImplementationQt::post_event(Core::Object& receiver, NonnullOwnPtr&& event) { - VERIFY(function); - QTimer::singleShot(0, [function = move(function)] { - function(); - }); + m_thread_event_queue.post_event(receiver, move(event)); + if (&m_thread_event_queue != &Core::ThreadEventQueue::current()) + wake(); } static void qt_timer_fired(int timer_id, Core::TimerShouldFireWhenNotVisible should_fire_when_not_visible, Core::Object& object) diff --git a/Ladybird/EventLoopImplementationQt.h b/Ladybird/EventLoopImplementationQt.h index 6a99378aa5b..815be303d8e 100644 --- a/Ladybird/EventLoopImplementationQt.h +++ b/Ladybird/EventLoopImplementationQt.h @@ -22,8 +22,6 @@ public: virtual ~EventLoopManagerQt() override; virtual NonnullOwnPtr make_implementation() override; - virtual void deferred_invoke(Function) override; - virtual int register_timer(Core::Object&, int milliseconds, bool should_reload, Core::TimerShouldFireWhenNotVisible) override; virtual bool unregister_timer(int timer_id) override; @@ -52,6 +50,7 @@ public: virtual size_t pump(PumpMode) override; virtual void quit(int) override; virtual void wake() override; + virtual void post_event(Core::Object& receiver, NonnullOwnPtr&&) override; // FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them. virtual void unquit() override { } diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index 330fd31a875..e20e9730ef2 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/Userland/Libraries/LibCore/EventLoop.cpp @@ -88,7 +88,7 @@ size_t EventLoop::pump(WaitMode mode) void EventLoop::post_event(Object& receiver, NonnullOwnPtr&& event) { - EventLoopManager::the().post_event(receiver, move(event)); + m_impl->post_event(receiver, move(event)); } void EventLoop::add_job(NonnullRefPtr>> job_promise) diff --git a/Userland/Libraries/LibCore/EventLoopImplementation.cpp b/Userland/Libraries/LibCore/EventLoopImplementation.cpp index bc6a39e3178..9b14ab24825 100644 --- a/Userland/Libraries/LibCore/EventLoopImplementation.cpp +++ b/Userland/Libraries/LibCore/EventLoopImplementation.cpp @@ -12,7 +12,10 @@ namespace Core { -EventLoopImplementation::EventLoopImplementation() = default; +EventLoopImplementation::EventLoopImplementation() + : m_thread_event_queue(ThreadEventQueue::current()) +{ +} EventLoopImplementation::~EventLoopImplementation() = default; @@ -29,20 +32,8 @@ void EventLoopManager::install(Core::EventLoopManager& manager) s_event_loop_manager = &manager; } -EventLoopManager::EventLoopManager() - : m_thread_event_queue(ThreadEventQueue::current()) -{ -} +EventLoopManager::EventLoopManager() = default; EventLoopManager::~EventLoopManager() = default; -void EventLoopManager::post_event(Object& receiver, NonnullOwnPtr&& event) -{ - m_thread_event_queue.post_event(receiver, move(event)); - - // Wake up this EventLoopImplementation if this is a cross-thread event posting. - if (&ThreadEventQueue::current() != &m_thread_event_queue) - wake(); -} - } diff --git a/Userland/Libraries/LibCore/EventLoopImplementation.h b/Userland/Libraries/LibCore/EventLoopImplementation.h index ee556b6ec8c..d480f85002b 100644 --- a/Userland/Libraries/LibCore/EventLoopImplementation.h +++ b/Userland/Libraries/LibCore/EventLoopImplementation.h @@ -29,11 +29,8 @@ public: virtual void register_notifier(Notifier&) = 0; virtual void unregister_notifier(Notifier&) = 0; - void post_event(Object& receiver, NonnullOwnPtr&&); virtual void did_post_event() = 0; - virtual void deferred_invoke(Function) = 0; - // FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them. virtual int register_signal(int signal_number, Function handler) = 0; virtual void unregister_signal(int handler_id) = 0; @@ -42,7 +39,6 @@ public: protected: EventLoopManager(); - ThreadEventQueue& m_thread_event_queue; }; class EventLoopImplementation { @@ -59,6 +55,8 @@ public: virtual void quit(int) = 0; virtual void wake() = 0; + virtual void post_event(Object& receiver, NonnullOwnPtr&&) = 0; + // FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them. virtual void unquit() = 0; virtual bool was_exit_requested() const = 0; @@ -66,6 +64,7 @@ public: protected: EventLoopImplementation(); + ThreadEventQueue& m_thread_event_queue; }; } diff --git a/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp b/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp index 1dc525d1f2f..a5d94edab43 100644 --- a/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp +++ b/Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp @@ -126,6 +126,13 @@ bool EventLoopImplementationUnix::was_exit_requested() const return m_exit_requested; } +void EventLoopImplementationUnix::post_event(Object& receiver, NonnullOwnPtr&& event) +{ + m_thread_event_queue.post_event(receiver, move(event)); + if (&m_thread_event_queue != &ThreadEventQueue::current()) + wake(); +} + void EventLoopImplementationUnix::wake() { int wake_event = 0; @@ -138,13 +145,6 @@ void EventLoopManagerUnix::wake() MUST(Core::System::write(ThreadData::the().wake_pipe_fds[1], { &wake_event, sizeof(wake_event) })); } -void EventLoopManagerUnix::deferred_invoke(Function invokee) -{ - // FIXME: Get rid of the useless DeferredInvocationContext object. - auto context = DeferredInvocationContext::construct(); - post_event(context, make(context, move(invokee))); -} - void EventLoopManagerUnix::wait_for_events(EventLoopImplementation::PumpMode mode) { auto& thread_data = ThreadData::the(); diff --git a/Userland/Libraries/LibCore/EventLoopImplementationUnix.h b/Userland/Libraries/LibCore/EventLoopImplementationUnix.h index 9e0c9cfe783..4d34c984dd3 100644 --- a/Userland/Libraries/LibCore/EventLoopImplementationUnix.h +++ b/Userland/Libraries/LibCore/EventLoopImplementationUnix.h @@ -16,8 +16,6 @@ public: virtual NonnullOwnPtr make_implementation() override; - virtual void deferred_invoke(Function) override; - virtual int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible) override; virtual bool unregister_timer(int timer_id) override; @@ -55,6 +53,7 @@ public: virtual void unquit() override; virtual bool was_exit_requested() const override; virtual void notify_forked_and_in_child() override; + virtual void post_event(Object& receiver, NonnullOwnPtr&&) override; private: bool m_exit_requested { false };