Skip to content

Commit 4f4d465

Browse files
committed
use NtDelayExecution for SDL_SYS_DelayNS
1 parent 98bed62 commit 4f4d465

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/timer/windows/SDL_systimer.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,42 @@ Uint64 SDL_GetPerformanceFrequency(void)
103103
return (Uint64)frequency.QuadPart;
104104
}
105105

106+
#ifdef SDL_TIMER_WINDOWS_USE_NTDELAYEXECUTION
107+
108+
typedef LONG (NTAPI *NtDelayExecution_t)(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval);
109+
static NtDelayExecution_t pNtDelayExecution;
110+
111+
static void SDL_NtDelayExecution(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval)
112+
{
113+
if (!pNtDelayExecution) {
114+
static bool initialized;
115+
116+
if (!initialized) {
117+
HMODULE module = GetModuleHandle(TEXT("ntdll.dll"));
118+
if (module) {
119+
pNtDelayExecution = (NtDelayExecution_t)GetProcAddress(module, "NtDelayExecution");
120+
}
121+
initialized = true;
122+
}
123+
124+
if (!pNtDelayExecution) {
125+
return;
126+
}
127+
}
128+
129+
pNtDelayExecution(Alertable, DelayInterval);
130+
}
131+
132+
void SDL_SYS_DelayNS(Uint64 ns)
133+
{
134+
Sint64 tick = ns;
135+
tick /= 100;
136+
tick *= -1;
137+
SDL_NtDelayExecution(0, (PLARGE_INTEGER)&tick);
138+
}
139+
140+
#else
141+
106142
void SDL_SYS_DelayNS(Uint64 ns)
107143
{
108144
HANDLE timer = SDL_GetWaitableTimer();
@@ -130,4 +166,6 @@ void SDL_SYS_DelayNS(Uint64 ns)
130166
Sleep(delay);
131167
}
132168

169+
#endif // SDL_TIMER_WINDOWS_USE_NTDELAYEXECUTION
170+
133171
#endif // SDL_TIMER_WINDOWS

0 commit comments

Comments
 (0)