Path: blob/master/thirdparty/sdl/timer/windows/SDL_systimer.c
9917 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122#ifdef SDL_TIMER_WINDOWS2324#include "../../core/windows/SDL_windows.h"2526/* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag was added in Windows 10 version 1803. */27#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION28#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x229#endif3031typedef HANDLE (WINAPI *CreateWaitableTimerExW_t)(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCWSTR lpTimerName, DWORD dwFlags, DWORD dwDesiredAccess);32static CreateWaitableTimerExW_t pCreateWaitableTimerExW;3334typedef BOOL (WINAPI *SetWaitableTimerEx_t)(HANDLE hTimer, const LARGE_INTEGER *lpDueTime, LONG lPeriod, PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine, PREASON_CONTEXT WakeContext, ULONG TolerableDelay);35static SetWaitableTimerEx_t pSetWaitableTimerEx;3637static void SDL_CleanupWaitableHandle(void *handle)38{39CloseHandle(handle);40}4142static HANDLE SDL_GetWaitableTimer(void)43{44static SDL_TLSID TLS_timer_handle;45HANDLE timer;4647if (!pCreateWaitableTimerExW || !pSetWaitableTimerEx) {48static bool initialized;4950if (!initialized) {51HMODULE module = GetModuleHandle(TEXT("kernel32.dll"));52if (module) {53pCreateWaitableTimerExW = (CreateWaitableTimerExW_t)GetProcAddress(module, "CreateWaitableTimerExW");54pSetWaitableTimerEx = (SetWaitableTimerEx_t)GetProcAddress(module, "SetWaitableTimerEx");55}56initialized = true;57}5859if (!pCreateWaitableTimerExW || !pSetWaitableTimerEx) {60return NULL;61}62}6364timer = SDL_GetTLS(&TLS_timer_handle);65if (!timer) {66timer = pCreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);67if (timer) {68SDL_SetTLS(&TLS_timer_handle, timer, SDL_CleanupWaitableHandle);69}70}71return timer;72}7374static HANDLE SDL_GetWaitableEvent(void)75{76static SDL_TLSID TLS_event_handle;77HANDLE event;7879event = SDL_GetTLS(&TLS_event_handle);80if (!event) {81event = CreateEvent(NULL, FALSE, FALSE, NULL);82if (event) {83SDL_SetTLS(&TLS_event_handle, event, SDL_CleanupWaitableHandle);84}85}86return event;87}8889Uint64 SDL_GetPerformanceCounter(void)90{91LARGE_INTEGER counter;92const BOOL rc = QueryPerformanceCounter(&counter);93SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later.94return (Uint64)counter.QuadPart;95}9697Uint64 SDL_GetPerformanceFrequency(void)98{99LARGE_INTEGER frequency;100const BOOL rc = QueryPerformanceFrequency(&frequency);101SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later.102return (Uint64)frequency.QuadPart;103}104105void SDL_SYS_DelayNS(Uint64 ns)106{107HANDLE timer = SDL_GetWaitableTimer();108if (timer) {109LARGE_INTEGER due_time;110due_time.QuadPart = -((LONGLONG)ns / 100);111if (pSetWaitableTimerEx(timer, &due_time, 0, NULL, NULL, NULL, 0)) {112WaitForSingleObject(timer, INFINITE);113}114return;115}116117const Uint64 max_delay = 0xffffffffLL * SDL_NS_PER_MS;118if (ns > max_delay) {119ns = max_delay;120}121const DWORD delay = (DWORD)SDL_NS_TO_MS(ns);122123HANDLE event = SDL_GetWaitableEvent();124if (event) {125WaitForSingleObjectEx(event, delay, FALSE);126return;127}128129Sleep(delay);130}131132#endif // SDL_TIMER_WINDOWS133134135