ladybird/Libraries/LibCore/EventLoopImplementationWindows.h
Andreas Kling 96ce468b60 LibCore: Add thread-safe weak deferred_invoke()
Add a thread-safe deferred_invoke() API on WeakEventLoopReference that
queues work onto the owning thread's event queue and wakes that thread
via EventLoopManager hooks. This avoids calling wake() from foreign
threads during teardown.

Implement current_thread_handle()/wake_thread() in each backend and
track per-thread data so handles are validated before waking:

- Unix: wake via per-thread wake pipe
- Windows: wake via thread wake event
- macOS: wake via stored CFRunLoopRef
- Qt: wake via event target or QEventLoop::wakeUp()
- Android: wake via stored ALooper
2026-01-25 09:32:51 +01:00

57 lines
1.8 KiB
C++

/*
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullOwnPtr.h>
#include <LibCore/EventLoopImplementation.h>
namespace Core {
class EventLoopManagerWindows final : public EventLoopManager {
public:
virtual ~EventLoopManagerWindows() override = default;
virtual NonnullOwnPtr<EventLoopImplementation> make_implementation() override;
virtual intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload) override;
virtual void unregister_timer(intptr_t timer_id) override;
virtual void register_notifier(Notifier&) override;
virtual void unregister_notifier(Notifier&) override;
virtual void did_post_event() override;
virtual EventLoopThreadHandle current_thread_handle() override;
virtual void wake_thread(EventLoopThreadHandle) override;
virtual int register_signal(int signal_number, Function<void(int)> handler) override;
virtual void unregister_signal(int handler_id) override;
};
class EventLoopImplementationWindows final : public EventLoopImplementation {
public:
static NonnullOwnPtr<EventLoopImplementationWindows> create() { return make<EventLoopImplementationWindows>(); }
EventLoopImplementationWindows();
virtual ~EventLoopImplementationWindows() override;
virtual int exec() override;
virtual size_t pump(PumpMode) override;
virtual void quit(int) override;
virtual void wake() override;
virtual bool was_exit_requested() const override { return m_exit_requested; }
private:
bool m_exit_requested { false };
int m_exit_code { 0 };
// The wake event handle of this event loop needs to be accessible from other threads.
void*& m_wake_event;
};
using EventLoopManagerPlatform = EventLoopManagerWindows;
}