From 4fafe755cb400c97bc4d343da36e14e1cc7ef25b Mon Sep 17 00:00:00 2001 From: Matt Borgerson Date: Wed, 21 Jan 2026 17:20:14 -0700 Subject: [PATCH] ui: Migrate custom precise delay to SDL_DelayPrecise --- ui/xemu.c | 57 +++++-------------------------------------------------- 1 file changed, 5 insertions(+), 52 deletions(-) diff --git a/ui/xemu.c b/ui/xemu.c index 67cd1877db..2f5f64c332 100644 --- a/ui/xemu.c +++ b/ui/xemu.c @@ -97,7 +97,6 @@ void xb_surface_gl_create_texture(DisplaySurface *surface); void xb_surface_gl_update_texture(DisplaySurface *surface, int x, int y, int w, int h); void xb_surface_gl_destroy_texture(DisplaySurface *surface); -static void sleep_ns(int64_t ns); static void process_key(struct xemu_console *scon, SDL_KeyboardEvent *ev); static void gl_update(DisplayChangeListener *dcl, int x, int y, int w, int h); static void gl_switch(DisplayChangeListener *dcl, DisplaySurface *new_surface); @@ -1094,47 +1093,14 @@ static void gl_refresh(DisplayChangeListener *dcl) bql_unlock(); qemu_mutex_unlock_main_loop(); - /* - * Throttle to make sure swaps happen at 60Hz - */ + /* Throttle to 60Hz */ static int64_t last_update = 0; int64_t deadline = last_update + 16666666; - -#ifdef DEBUG_XEMU_C - int64_t sleep_acc = 0; - int64_t spin_acc = 0; -#endif - -#ifndef _WIN32 - const int64_t sleep_threshold = 2000000; -#else - const int64_t sleep_threshold = 250000; -#endif - - while (1) { - int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); - int64_t time_remaining = deadline - now; - if (now < deadline) { - if (time_remaining > sleep_threshold) { - // Try to sleep until the until reaching the sleep threshold. - sleep_ns(time_remaining - sleep_threshold); -#ifdef DEBUG_XEMU_C - sleep_acc += qemu_clock_get_ns(QEMU_CLOCK_REALTIME)-now; -#endif - } else { - // Simply spin to avoid extra delays incurred with swapping to - // another process and back in the event of being within - // threshold to desired event. -#ifdef DEBUG_XEMU_C - spin_acc++; -#endif - } - } else { - DPRINTF("zzZz %g %ld\n", (double)sleep_acc/1000000.0, spin_acc); - last_update = now; - break; - } + int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + if (now < deadline) { + SDL_DelayPrecise(deadline - now); } + last_update = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); } @@ -1180,19 +1146,6 @@ static void *call_qemu_main(void *opaque) exit(status); } -/* Note: only supports millisecond resolution on Windows */ -static void sleep_ns(int64_t ns) -{ -#ifndef _WIN32 - struct timespec sleep_delay, rem_delay; - sleep_delay.tv_sec = ns / 1000000000LL; - sleep_delay.tv_nsec = ns % 1000000000LL; - nanosleep(&sleep_delay, &rem_delay); -#else - Sleep(ns / SCALE_MS); -#endif -} - #ifdef _WIN32 static const wchar_t *get_executable_name(void) {