Skip to content

Commit 44d40fa

Browse files
committed
LibCore: Remove the dummy EventReceiver from deferred_invoke()
The DeferredInvocationContext only existed to satisfy the requirement in ThreadEventQueue that each event has an EventReceiver. However, deferred_invoke() was not even using the EventReceiver to call its callback. Therefore, we don't need to allocate one for every deferred invocation. This also prevents WeakPtr::strong_ref() from racing and leaking the context object when invoking a function across threads.
1 parent 4d27e9a commit 44d40fa

13 files changed

+19
-42
lines changed

Libraries/LibCore/DeferredInvocationContext.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

Libraries/LibCore/Event.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <AK/Function.h>
1212
#include <AK/Types.h>
1313
#include <AK/WeakPtr.h>
14-
#include <LibCore/DeferredInvocationContext.h>
1514
#include <LibCore/Forward.h>
1615

1716
namespace Core {
@@ -49,15 +48,13 @@ class DeferredInvocationEvent : public Event {
4948
friend class ThreadEventQueue;
5049

5150
public:
52-
DeferredInvocationEvent(NonnullRefPtr<DeferredInvocationContext> context, Function<void()> invokee)
51+
DeferredInvocationEvent(Function<void()>&& invokee)
5352
: Event(Event::Type::DeferredInvoke)
54-
, m_context(move(context))
5553
, m_invokee(move(invokee))
5654
{
5755
}
5856

5957
private:
60-
NonnullRefPtr<DeferredInvocationContext> m_context;
6158
Function<void()> m_invokee;
6259
};
6360

Libraries/LibCore/EventLoop.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ size_t EventLoop::pump(WaitMode mode)
102102
return m_impl->pump(mode == WaitMode::WaitForEvents ? EventLoopImplementation::PumpMode::WaitForEvents : EventLoopImplementation::PumpMode::DontWaitForEvents);
103103
}
104104

105-
void EventLoop::post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&& event)
105+
void EventLoop::post_event(RefPtr<EventReceiver> const& receiver, NonnullOwnPtr<Event>&& event)
106106
{
107107
m_impl->post_event(receiver, move(event));
108108
}
@@ -149,8 +149,7 @@ void EventLoop::wake()
149149

150150
void EventLoop::deferred_invoke(Function<void()> invokee)
151151
{
152-
auto context = DeferredInvocationContext::construct();
153-
post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
152+
post_event(nullptr, make<Core::DeferredInvocationEvent>(move(invokee)));
154153
}
155154

156155
void deferred_invoke(Function<void()> invokee)

Libraries/LibCore/EventLoop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class EventLoop {
6868
void spin_until(Function<bool()>);
6969

7070
// Post an event to this event loop.
71-
void post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&&);
71+
void post_event(RefPtr<EventReceiver> const& receiver, NonnullOwnPtr<Event>&&);
7272

7373
void add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>> job_promise);
7474

Libraries/LibCore/EventLoopImplementation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class EventLoopImplementation {
5454
virtual void wake() = 0;
5555
virtual bool was_exit_requested() const = 0;
5656

57-
virtual void post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&&) = 0;
57+
virtual void post_event(RefPtr<EventReceiver> const&, NonnullOwnPtr<Event>&&) = 0;
5858

5959
protected:
6060
EventLoopImplementation();

Libraries/LibCore/EventLoopImplementationUnix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ void EventLoopImplementationUnix::quit(int code)
314314
m_exit_code = code;
315315
}
316316

317-
void EventLoopImplementationUnix::post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&& event)
317+
void EventLoopImplementationUnix::post_event(RefPtr<EventReceiver> const& receiver, NonnullOwnPtr<Event>&& event)
318318
{
319319
m_thread_event_queue.post_event(receiver, move(event));
320320
if (&m_thread_event_queue != &ThreadEventQueue::current())

Libraries/LibCore/EventLoopImplementationUnix.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <AK/NonnullOwnPtr.h>
910
#include <AK/Time.h>
1011
#include <LibCore/EventLoopImplementation.h>
1112

@@ -50,7 +51,7 @@ class EventLoopImplementationUnix final : public EventLoopImplementation {
5051

5152
virtual void wake() override;
5253

53-
virtual void post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&&) override;
54+
virtual void post_event(RefPtr<EventReceiver> const&, NonnullOwnPtr<Event>&&) override;
5455

5556
private:
5657
bool m_exit_requested { false };

Libraries/LibCore/ThreadEventQueue.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
#include <AK/Vector.h>
8-
#include <LibCore/DeferredInvocationContext.h>
98
#include <LibCore/EventLoopImplementation.h>
109
#include <LibCore/EventReceiver.h>
1110
#include <LibCore/Promise.h>
@@ -22,7 +21,7 @@ struct ThreadEventQueue::Private {
2221
AK_MAKE_DEFAULT_MOVABLE(QueuedEvent);
2322

2423
public:
25-
QueuedEvent(EventReceiver& receiver, NonnullOwnPtr<Event> event)
24+
QueuedEvent(RefPtr<EventReceiver> const& receiver, NonnullOwnPtr<Event> event)
2625
: receiver(receiver)
2726
, event(move(event))
2827
{
@@ -67,7 +66,7 @@ ThreadEventQueue::ThreadEventQueue()
6766

6867
ThreadEventQueue::~ThreadEventQueue() = default;
6968

70-
void ThreadEventQueue::post_event(Core::EventReceiver& receiver, NonnullOwnPtr<Core::Event> event)
69+
void ThreadEventQueue::post_event(RefPtr<Core::EventReceiver> const& receiver, NonnullOwnPtr<Core::Event> event)
7170
{
7271
{
7372
Threading::MutexLocker lock(m_private->mutex);
@@ -106,16 +105,16 @@ size_t ThreadEventQueue::process()
106105
auto receiver = queued_event.receiver.strong_ref();
107106
auto& event = *queued_event.event;
108107

109-
if (!receiver) {
108+
if (event.type() == Event::Type::DeferredInvoke) {
109+
static_cast<DeferredInvocationEvent&>(event).m_invokee();
110+
} else if (!receiver) {
110111
switch (event.type()) {
111112
case Event::Quit:
112113
VERIFY_NOT_REACHED();
113114
default:
114115
// Receiver disappeared, drop the event on the floor.
115116
break;
116117
}
117-
} else if (event.type() == Event::Type::DeferredInvoke) {
118-
static_cast<DeferredInvocationEvent&>(event).m_invokee();
119118
} else {
120119
NonnullRefPtr<EventReceiver> protector(*receiver);
121120
receiver->dispatch_event(event);

Libraries/LibCore/ThreadEventQueue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <AK/NonnullOwnPtr.h>
1010
#include <AK/OwnPtr.h>
11+
#include <LibCore/Forward.h>
1112

1213
namespace Core {
1314

@@ -25,7 +26,7 @@ class ThreadEventQueue {
2526
size_t process();
2627

2728
// Posts an event to the event queue.
28-
void post_event(EventReceiver& receiver, NonnullOwnPtr<Event>);
29+
void post_event(RefPtr<EventReceiver> const&, NonnullOwnPtr<Event>);
2930

3031
// Used by Threading::BackgroundAction.
3132
void add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>>);

Libraries/LibWebView/EventLoop/EventLoopImplementationMacOS.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class WEBVIEW_API EventLoopImplementationMacOS final : public Core::EventLoopImp
4141
virtual void quit(int) override;
4242
virtual void wake() override;
4343
virtual bool was_exit_requested() const override;
44-
virtual void post_event(Core::EventReceiver& receiver, NonnullOwnPtr<Core::Event>&&) override;
44+
virtual void post_event(RefPtr<Core::EventReceiver> const&, NonnullOwnPtr<Core::Event>&&) override;
4545

4646
virtual ~EventLoopImplementationMacOS() override;
4747

0 commit comments

Comments
 (0)