Skip to content

Commit

Permalink
Dynamically load CreateWaitableTimerExW and SetWaitableTimerEx
Browse files Browse the repository at this point in the history
These functions are not available on Windows XP
  • Loading branch information
slouken committed Jan 29, 2025
1 parent 4176e18 commit a1fd5c9
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/timer/windows/SDL_systimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

#include "../../core/windows/SDL_windows.h"

typedef HANDLE (*CreateWaitableTimerExW_t)(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCWSTR lpTimerName, DWORD dwFlags, DWORD dwDesiredAccess);
static CreateWaitableTimerExW_t CreateWaitableTimerExWFunc;

typedef BOOL (*SetWaitableTimerEx_t)( HANDLE hTimer, const LARGE_INTEGER *lpDueTime, LONG lPeriod, PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine, PREASON_CONTEXT WakeContext, ULONG TolerableDelay);
static SetWaitableTimerEx_t SetWaitableTimerExFunc;

static void SDL_CleanupWaitableHandle(void *handle)
{
Expand All @@ -36,9 +41,26 @@ static HANDLE SDL_GetWaitableTimer(void)
static SDL_TLSID TLS_timer_handle;
HANDLE timer;

if (!CreateWaitableTimerExWFunc || !SetWaitableTimerExFunc) {
static bool initialized;

if (!initialized) {
HMODULE module = GetModuleHandle(TEXT("kernel32.dll"));
if (module) {
CreateWaitableTimerExWFunc = (CreateWaitableTimerExW_t)GetProcAddress(module, "CreateWaitableTimerExW");
SetWaitableTimerExFunc = (SetWaitableTimerEx_t)GetProcAddress(module, "SetWaitableTimerEx");
}
initialized = true;
}

if (!CreateWaitableTimerExWFunc || !SetWaitableTimerExFunc) {
return NULL;
}
}

timer = SDL_GetTLS(&TLS_timer_handle);
if (!timer) {
timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
timer = CreateWaitableTimerExWFunc(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
if (timer) {
SDL_SetTLS(&TLS_timer_handle, timer, SDL_CleanupWaitableHandle);
}
Expand Down Expand Up @@ -89,7 +111,7 @@ void SDL_SYS_DelayNS(Uint64 ns)
if (timer) {
LARGE_INTEGER due_time;
due_time.QuadPart = -((LONGLONG)ns / 100);
if (SetWaitableTimerEx(timer, &due_time, 0, NULL, NULL, NULL, 0)) {
if (SetWaitableTimerExFunc(timer, &due_time, 0, NULL, NULL, NULL, 0)) {
WaitForSingleObject(timer, INFINITE);
}
return;
Expand Down

0 comments on commit a1fd5c9

Please sign in to comment.