LibCore: Don't require heap-allocated event to hold deferred invocation

Instead of creating a DeferredInvocationEvent every time we
deferred_invoke, we now let the QueuedEvent store the invokee Function
object directly.
This commit is contained in:
Andreas Kling
2025-12-03 11:53:04 +01:00
committed by Andreas Kling
parent 69515f8c85
commit 2a1c5dc108
Notes: github-actions[bot] 2025-12-03 12:27:45 +00:00
6 changed files with 28 additions and 17 deletions

View File

@@ -42,21 +42,6 @@ private:
bool m_accepted { true };
};
class DeferredInvocationEvent : public Event {
friend class EventLoop;
friend class ThreadEventQueue;
public:
DeferredInvocationEvent(Function<void()>&& invokee)
: Event(Event::Type::DeferredInvoke)
, m_invokee(move(invokee))
{
}
private:
Function<void()> m_invokee;
};
class TimerEvent final : public Event {
public:
explicit TimerEvent()

View File

@@ -149,7 +149,7 @@ void EventLoop::wake()
void EventLoop::deferred_invoke(Function<void()> invokee)
{
m_impl->post_event(nullptr, make<Core::DeferredInvocationEvent>(move(invokee)));
m_impl->deferred_invoke(move(invokee));
}
void deferred_invoke(Function<void()> invokee)

View File

@@ -23,6 +23,11 @@ EventLoopImplementation::EventLoopImplementation()
EventLoopImplementation::~EventLoopImplementation() = default;
void EventLoopImplementation::deferred_invoke(Function<void()>&& invokee)
{
m_thread_event_queue.deferred_invoke(move(invokee));
}
static EventLoopManager* s_event_loop_manager = nullptr;
EventLoopManager& EventLoopManager::the()
{

View File

@@ -56,6 +56,8 @@ public:
virtual void post_event(EventReceiver*, NonnullOwnPtr<Event>&&) = 0;
virtual void deferred_invoke(Function<void()>&&);
protected:
EventLoopImplementation();
ThreadEventQueue& m_thread_event_queue;

View File

@@ -34,10 +34,17 @@ struct ThreadEventQueue::Private {
{
}
QueuedEvent(Function<void()>&& invokee)
: m_invokee(move(invokee))
, event_type(Event::Type::DeferredInvoke)
{
}
~QueuedEvent() = default;
WeakPtr<EventReceiver> receiver;
OwnPtr<Event> event;
Function<void()> m_invokee;
u8 event_type { Event::Type::Invalid };
};
@@ -92,6 +99,15 @@ void ThreadEventQueue::post_event(Core::EventReceiver* receiver, Core::Event::Ty
Core::EventLoopManager::the().did_post_event();
}
void ThreadEventQueue::deferred_invoke(Function<void()>&& invokee)
{
{
Threading::MutexLocker lock(m_private->mutex);
m_private->queued_events.empend(move(invokee));
}
Core::EventLoopManager::the().did_post_event();
}
void ThreadEventQueue::add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>> promise)
{
Threading::MutexLocker lock(m_private->mutex);
@@ -136,7 +152,7 @@ size_t ThreadEventQueue::process()
}
} else {
if (queued_event.event_type == Event::Type::DeferredInvoke) {
static_cast<DeferredInvocationEvent&>(*queued_event.event).m_invokee();
queued_event.m_invokee();
} else {
// Receiver gone, drop the event.
}

View File

@@ -29,6 +29,9 @@ public:
void post_event(EventReceiver*, NonnullOwnPtr<Event>);
void post_event(EventReceiver*, Core::Event::Type);
// Post a deferred invocation to the event queue.
void deferred_invoke(Function<void()>&&);
// Used by Threading::BackgroundAction.
void add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>>);
void cancel_all_pending_jobs();