Ladybird/Android: Post timer events to the correct event loop

The FIXME at the bottom of Timer.nativeRun was on the money, and was
the cause of some leaked timers. Solve the issue by passing the
EventLoopImplementation to the JVM Timer object and retrieving it's
thread_local timer list, and posting the events to the Impl rather than
trying to invoke the receiver's timer event callback directly in
the Timer thread. Because the implementation of
ALooperEventLoopManager::did_post_event() reaches for
EventLoop::current(), we also need to make sure that the timer thread
acutally *has* an EventLoop, even if we don't expect to use it for
anything. And we need to make sure to pump it to clear out any of the
poke() invocations sending 4 bytes to the wake pipe for that thread.
This commit is contained in:
Andrew Kaster
2023-09-14 15:37:06 -06:00
committed by Andrew Kaster
parent 10d7ec2027
commit ec05b4bc0a
3 changed files with 34 additions and 20 deletions

View File

@@ -6,11 +6,16 @@
#include "ALooperEventLoopImplementation.h"
#include <LibCore/EventLoop.h>
#include <LibCore/ThreadEventQueue.h>
#include <jni.h>
extern "C" JNIEXPORT void JNICALL Java_org_serenityos_ladybird_TimerExecutorService_00024Timer_nativeRun(JNIEnv*, jobject /* thiz */, jlong native_data, jlong id)
extern "C" JNIEXPORT void JNICALL
Java_org_serenityos_ladybird_TimerExecutorService_00024Timer_nativeRun(JNIEnv*, jobject /* thiz */, jlong native_data, jlong id)
{
auto& thread_data = *reinterpret_cast<Ladybird::EventLoopThreadData*>(native_data);
static Core::EventLoop s_event_loop; // Here to exist for this thread
auto& event_loop_impl = *reinterpret_cast<Ladybird::ALooperEventLoopImplementation*>(native_data);
auto& thread_data = event_loop_impl.thread_data();
if (auto timer_data = thread_data.timers.get(id); timer_data.has_value()) {
auto receiver = timer_data->receiver.strong_ref();
@@ -21,9 +26,8 @@ extern "C" JNIEXPORT void JNICALL Java_org_serenityos_ladybird_TimerExecutorServ
if (!receiver->is_visible_for_timer_purposes())
return;
Core::TimerEvent event(id);
// FIXME: Should the dispatch happen on the thread that registered the timer?
receiver->dispatch_event(event);
event_loop_impl.post_event(*receiver, make<Core::TimerEvent>(id));
}
// Flush the event loop on this thread to keep any garbage from building up
s_event_loop.pump(Core::EventLoop::WaitMode::PollForEvents);
}