Ladybird/Android: Add EventLoopImplementation for ALooper

Timers run in their own thread, to take advantage of existing Java
Executor features. By hooking into ALooper, we can spin the main
Activity's UI thread event loop without causing a fuss, or spinning the
CPU by just polling our event loop constantly.
This commit is contained in:
Andrew Kaster
2023-09-08 22:12:42 -06:00
committed by Andrew Kaster
parent d63fb8fa61
commit d93911928b
11 changed files with 510 additions and 40 deletions

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ALooperEventLoopImplementation.h"
#include <LibCore/EventLoop.h>
#include <jni.h>
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);
if (auto timer_data = thread_data.timers.get(id); timer_data.has_value()) {
auto receiver = timer_data->receiver.strong_ref();
if (!receiver)
return;
if (timer_data->visibility == Core::TimerShouldFireWhenNotVisible::No)
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);
}
}