mirror of
https://github.com/SerenityOS/serenity.git
synced 2026-11-07 04:29:30 -10:00
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. (cherry-picked from commit 2a1c5dc108697a5f3722432c0c1ec51a8e94a3e6)
This commit is contained in:
1 parent
3de118dc9d
commit
8f88beb00c
7 files changed
+29
-22
No files matched your search
@@ -45,21 +45,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()
|
||||
|
||||
@@ -181,7 +181,7 @@ void EventLoop::adopt_coroutine(Coroutine<void>&& coroutine)
|
||||
|
||||
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)
|
||||
|
||||
@@ -19,6 +19,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;
|
||||
EventLoopManager& EventLoopManager::the()
|
||||
{
|
||||
|
||||
@@ -54,6 +54,7 @@ public:
|
||||
virtual void wake() = 0;
|
||||
|
||||
virtual void post_event(EventReceiver*, NonnullOwnPtr<Event>&&) = 0;
|
||||
virtual void deferred_invoke(Function<void()>&&);
|
||||
|
||||
// FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them.
|
||||
virtual void unquit() = 0;
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <LibCore/Forward.h>
|
||||
@@ -28,6 +29,7 @@ public:
|
||||
// Posts an event to the event queue.
|
||||
void post_event(EventReceiver*, NonnullOwnPtr<Event>);
|
||||
void post_event(EventReceiver*, Core::Event::Type);
|
||||
void deferred_invoke(Function<void()>&&);
|
||||
|
||||
// Used by Threading::BackgroundAction.
|
||||
void add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>>);
|
||||
|
||||
@@ -21,11 +21,9 @@ struct Spinner {
|
||||
void await_suspend(std::coroutine_handle<> awaiter)
|
||||
{
|
||||
m_awaiter = awaiter;
|
||||
Core::ThreadEventQueue::current().post_event(
|
||||
nullptr,
|
||||
make<Core::DeferredInvocationEvent>([&] {
|
||||
m_awaiter.resume();
|
||||
}));
|
||||
Core::ThreadEventQueue::current().deferred_invoke([&] {
|
||||
m_awaiter.resume();
|
||||
});
|
||||
}
|
||||
|
||||
void await_resume() { m_awaiter = {}; }
|
||||
|
||||
Reference in new issue
Block a user