From 4f4d4651040a8ebb492f57fc10a66b06de075f3f Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Wed, 27 Aug 2025 13:58:45 -0700 Subject: [PATCH] use NtDelayExecution for SDL_SYS_DelayNS --- src/timer/windows/SDL_systimer.c | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/timer/windows/SDL_systimer.c b/src/timer/windows/SDL_systimer.c index f6f4b15c72b7f..3104f51733a62 100644 --- a/src/timer/windows/SDL_systimer.c +++ b/src/timer/windows/SDL_systimer.c @@ -103,6 +103,42 @@ Uint64 SDL_GetPerformanceFrequency(void) return (Uint64)frequency.QuadPart; } +#ifdef SDL_TIMER_WINDOWS_USE_NTDELAYEXECUTION + +typedef LONG (NTAPI *NtDelayExecution_t)(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval); +static NtDelayExecution_t pNtDelayExecution; + +static void SDL_NtDelayExecution(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval) +{ + if (!pNtDelayExecution) { + static bool initialized; + + if (!initialized) { + HMODULE module = GetModuleHandle(TEXT("ntdll.dll")); + if (module) { + pNtDelayExecution = (NtDelayExecution_t)GetProcAddress(module, "NtDelayExecution"); + } + initialized = true; + } + + if (!pNtDelayExecution) { + return; + } + } + + pNtDelayExecution(Alertable, DelayInterval); +} + +void SDL_SYS_DelayNS(Uint64 ns) +{ + Sint64 tick = ns; + tick /= 100; + tick *= -1; + SDL_NtDelayExecution(0, (PLARGE_INTEGER)&tick); +} + +#else + void SDL_SYS_DelayNS(Uint64 ns) { HANDLE timer = SDL_GetWaitableTimer(); @@ -130,4 +166,6 @@ void SDL_SYS_DelayNS(Uint64 ns) Sleep(delay); } +#endif // SDL_TIMER_WINDOWS_USE_NTDELAYEXECUTION + #endif // SDL_TIMER_WINDOWS