1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +00:00

Ladybird: Allow posting events to the Qt event loop from other threads

Previously, a QTimer was used to start processing of our event queue in
the main Qt event loop. Unfortunately, QTimers are not thread-safe, and
disallow starting of a timer from a different thread than it was
created in.

Instead, use a dummy QObject to post a custom QEvent to the main loop
from whatever thread we like, and process our event queue when it is
received by our dummy object.
This commit is contained in:
Zaggy1024 2023-07-04 04:37:36 -05:00 committed by Andrew Kaster
parent 6131d879f7
commit 78e1defbfe
6 changed files with 72 additions and 7 deletions

View file

@ -5,6 +5,7 @@
*/
#include "EventLoopImplementationQt.h"
#include "EventLoopImplementationQtEventTarget.h"
#include <AK/IDAllocator.h>
#include <LibCore/Event.h>
#include <LibCore/Notifier.h>
@ -149,16 +150,21 @@ void EventLoopManagerQt::unregister_notifier(Core::Notifier& notifier)
void EventLoopManagerQt::did_post_event()
{
m_process_core_events_timer.start();
QCoreApplication::postEvent(m_main_thread_event_target.ptr(), new QtEventLoopManagerEvent(QtEventLoopManagerEvent::process_event_queue_event_type()));
}
bool EventLoopManagerQt::event_target_received_event(Badge<EventLoopImplementationQtEventTarget>, QEvent* event)
{
if (event->type() == QtEventLoopManagerEvent::process_event_queue_event_type()) {
Core::ThreadEventQueue::current().process();
return true;
}
return false;
}
EventLoopManagerQt::EventLoopManagerQt()
: m_main_thread_event_target(make<EventLoopImplementationQtEventTarget>())
{
m_process_core_events_timer.setSingleShot(true);
m_process_core_events_timer.setInterval(0);
QObject::connect(&m_process_core_events_timer, &QTimer::timeout, [] {
Core::ThreadEventQueue::current().process();
});
}
EventLoopManagerQt::~EventLoopManagerQt() = default;