1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:07:36 +00:00

Ladybird+LibCore: Use QCoreApplication to drive the main Qt event loop

Using QEventLoop works for everything but it breaks *one* little feature
that we care about: automatically quitting the app when all windows have
been closed.

That only works if you drive the outermost main event loop with a
QCoreApplication instead of a QEventLoop. This is unfortunate, as it
complicates our API a little bit, but I'm sure we can think of a way to
make this nicer someday.

In order for QCoreApplication::exec() to process our own
ThreadEventQueue, we now have a zero-timer that we kick whenever new
events are posted to the thread queue.
This commit is contained in:
Andreas Kling 2023-04-25 16:53:07 +02:00
parent 0f22dfa634
commit c21eb30a2b
9 changed files with 79 additions and 25 deletions

View file

@ -170,4 +170,9 @@ bool EventLoop::was_exit_requested() const
return m_impl->was_exit_requested();
}
void EventLoop::did_post_event(Badge<Core::ThreadEventQueue>)
{
m_impl->did_post_event();
}
}

View file

@ -19,6 +19,7 @@
namespace Core {
class EventLoopImplementation;
class ThreadEventQueue;
// The event loop enables asynchronous (not parallel or multi-threaded) computing by efficiently handling events from various sources.
// Event loops are most important for GUI programs, where the various GUI updates and action callbacks run on the EventLoop,
@ -95,6 +96,9 @@ public:
static Function<NonnullOwnPtr<EventLoopImplementation>()> make_implementation;
void did_post_event(Badge<ThreadEventQueue>);
EventLoopImplementation& impl() { return *m_impl; }
private:
void wait_for_event(WaitMode);
Optional<Time> get_next_timer_expiration();

View file

@ -37,6 +37,8 @@ public:
virtual void register_notifier(Notifier&) = 0;
virtual void unregister_notifier(Notifier&) = 0;
virtual void did_post_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;

View file

@ -522,4 +522,8 @@ void EventLoopImplementationUnix::unregister_notifier(Notifier& notifier)
ThreadData::the().notifiers.remove(&notifier);
}
void EventLoopImplementationUnix::did_post_event()
{
}
}

View file

@ -31,6 +31,8 @@ public:
virtual void register_notifier(Notifier&) override;
virtual void unregister_notifier(Notifier&) override;
virtual void did_post_event() override;
virtual void unquit() override;
virtual bool was_exit_requested() const override;
virtual void notify_forked_and_in_child() override;

View file

@ -62,8 +62,11 @@ ThreadEventQueue::~ThreadEventQueue() = default;
void ThreadEventQueue::post_event(Core::Object& receiver, NonnullOwnPtr<Core::Event> event)
{
Threading::MutexLocker lock(m_private->mutex);
m_private->queued_events.empend(receiver, move(event));
{
Threading::MutexLocker lock(m_private->mutex);
m_private->queued_events.empend(receiver, move(event));
}
Core::EventLoop::current().did_post_event({});
}
void ThreadEventQueue::add_job(NonnullRefPtr<Promise<NonnullRefPtr<Object>>> promise)