From 6d5087fc33c185b35ee458c4cc728f0823465d75 Mon Sep 17 00:00:00 2001 From: Waozi Date: Sun, 25 Oct 2026 13:32:14 -0300 Subject: [PATCH] Keep Android background ticks outside practice UI frames --- src/platform/android/android_timer_host.zi | 29 ++++++++++++++++++++-- src/platform/app_host.zi | 2 ++ tests/android_timer_host_test.c | 8 ++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/platform/android/android_timer_host.zi b/src/platform/android/android_timer_host.zi index 1d5a6a662..aac996596 100644 --- a/src/platform/android/android_timer_host.zi +++ b/src/platform/android/android_timer_host.zi @@ -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) { diff --git a/src/platform/app_host.zi b/src/platform/app_host.zi index 8ffc741ab..085a84c4f 100644 --- a/src/platform/app_host.zi +++ b/src/platform/app_host.zi @@ -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 diff --git a/tests/android_timer_host_test.c b/tests/android_timer_host_test.c index 5df0dcf2d..673984a2c 100644 --- a/tests/android_timer_host_test.c +++ b/tests/android_timer_host_test.c @@ -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();