Keep Android background ticks outside practice UI frames

This commit is contained in:
waozi committed 2026-10-25 13:32:14 -03:00
1 parent 493de309a2
commit 6d5087fc33
3 files changed
+37 -2

No files matched your search

+27 -2
View File
@@ -34,6 +34,7 @@
timer_running: bool;
timer_stopping: bool;
timer_active: bool;
timer_frame_active: bool;
timer_app: *void;
TimerSleep :: (nanoseconds: isize) {
@@ -56,7 +57,7 @@
while true {
AndroidMutexLock(*timer_mutex)
running := timer_running
active := timer_active
active := timer_active && !timer_frame_active
app := timer_app
AndroidMutexUnlock(*timer_mutex)
if !running {
@@ -70,7 +71,7 @@
AndroidMutexLock(*app_state_mutex)
AndroidMutexLock(*timer_mutex)
app = null
if timer_running && timer_active {
if timer_running && timer_active && !timer_frame_active {
app = timer_app
}
AndroidMutexUnlock(*timer_mutex)
@@ -93,6 +94,30 @@
}
}
// Frames and background ticks share practice VMs and their host codecs. A
// lifecycle callback may request ticking during a frame, but that request
// takes effect only after the frame returns. Wait for a tick already in
// flight without holding the state mutex throughout UI/start/stop effects.
#program_export
android_timer_frame_begin :: () {
#if #defined(ANDROID_BUILD) {
AndroidMutexLock(*timer_mutex)
timer_frame_active = true
AndroidMutexUnlock(*timer_mutex)
AndroidMutexLock(*app_state_mutex)
AndroidMutexUnlock(*app_state_mutex)
}
}
#program_export
android_timer_frame_end :: () {
#if #defined(ANDROID_BUILD) {
AndroidMutexLock(*timer_mutex)
timer_frame_active = false
AndroidMutexUnlock(*timer_mutex)
}
}
#program_export
android_timer_set_app :: (app: *void) {
#if #defined(ANDROID_BUILD) {
+2
View File
@@ -319,6 +319,8 @@ AppNavigationKey :: () -> s32 {
app_frame :: (argument: *void) {
app := cast(*InnerBreeze)argument
#if #defined(ANDROID_BUILD) {
android_timer_frame_begin()
defer { android_timer_frame_end() }
picture_state := AndroidPictureWindowState()
// Closing the floating window ends its practice. A running practice
// keeps frames going in the background, so it ends right away; a
+8
View File
@@ -113,6 +113,14 @@ main(void)
android_timer_start();
android_timer_activate();
wait_for_callbacks(2);
/* A lifecycle callback cannot re-enter the practice VM during a frame. */
android_timer_frame_begin();
count = count_callbacks();
android_timer_activate();
nanosleep(&quiet, NULL);
assert(count_callbacks() == count);
android_timer_frame_end();
wait_for_callbacks(count + 1);
/* No tick runs after the frame quiesces the timer. */
android_timer_quiesce();
count = count_callbacks();