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.)
This commit is contained in:
Andreas Kling 2023-04-26 18:51:07 +02:00 committed by Sam Atkins
parent 7035a19645
commit b61a87c03c
7 changed files with 22 additions and 35 deletions

View file

@ -74,12 +74,11 @@ void EventLoopImplementationQt::wake()
m_event_loop.wakeUp();
}
void EventLoopManagerQt::deferred_invoke(Function<void()> function)
void EventLoopImplementationQt::post_event(Core::Object& receiver, NonnullOwnPtr<Core::Event>&& 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)

View file

@ -22,8 +22,6 @@ public:
virtual ~EventLoopManagerQt() override;
virtual NonnullOwnPtr<Core::EventLoopImplementation> make_implementation() override;
virtual void deferred_invoke(Function<void()>) 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<Core::Event>&&) override;
// FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them.
virtual void unquit() override { }

View file

@ -88,7 +88,7 @@ size_t EventLoop::pump(WaitMode mode)
void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event)
{
EventLoopManager::the().post_event(receiver, move(event));
m_impl->post_event(receiver, move(event));
}
void EventLoop::add_job(NonnullRefPtr<Promise<NonnullRefPtr<Object>>> job_promise)

View file

@ -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>&& 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();
}
}

View file

@ -29,11 +29,8 @@ public:
virtual void register_notifier(Notifier&) = 0;
virtual void unregister_notifier(Notifier&) = 0;
void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
virtual void did_post_event() = 0;
virtual void deferred_invoke(Function<void()>) = 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<void(int)> 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<Event>&&) = 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;
};
}

View file

@ -126,6 +126,13 @@ bool EventLoopImplementationUnix::was_exit_requested() const
return m_exit_requested;
}
void EventLoopImplementationUnix::post_event(Object& receiver, NonnullOwnPtr<Event>&& 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<void()> invokee)
{
// FIXME: Get rid of the useless DeferredInvocationContext object.
auto context = DeferredInvocationContext::construct();
post_event(context, make<DeferredInvocationEvent>(context, move(invokee)));
}
void EventLoopManagerUnix::wait_for_events(EventLoopImplementation::PumpMode mode)
{
auto& thread_data = ThreadData::the();

View file

@ -16,8 +16,6 @@ public:
virtual NonnullOwnPtr<EventLoopImplementation> make_implementation() override;
virtual void deferred_invoke(Function<void()>) 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<Event>&&) override;
private:
bool m_exit_requested { false };