diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/timer/windows/SDL_systimer.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/timer/windows/SDL_systimer.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/timer/windows/SDL_systimer.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/timer/windows/SDL_systimer.c b/contrib/SDL-3.2.8/src/timer/windows/SDL_systimer.c new file mode 100644 index 0000000..f6f4b15 --- /dev/null +++ b/contrib/SDL-3.2.8/src/timer/windows/SDL_systimer.c | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_TIMER_WINDOWS | ||
| 24 | |||
| 25 | #include "../../core/windows/SDL_windows.h" | ||
| 26 | |||
| 27 | /* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag was added in Windows 10 version 1803. */ | ||
| 28 | #ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION | ||
| 29 | #define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x2 | ||
| 30 | #endif | ||
| 31 | |||
| 32 | typedef HANDLE (WINAPI *CreateWaitableTimerExW_t)(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCWSTR lpTimerName, DWORD dwFlags, DWORD dwDesiredAccess); | ||
| 33 | static CreateWaitableTimerExW_t pCreateWaitableTimerExW; | ||
| 34 | |||
| 35 | typedef BOOL (WINAPI *SetWaitableTimerEx_t)(HANDLE hTimer, const LARGE_INTEGER *lpDueTime, LONG lPeriod, PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine, PREASON_CONTEXT WakeContext, ULONG TolerableDelay); | ||
| 36 | static SetWaitableTimerEx_t pSetWaitableTimerEx; | ||
| 37 | |||
| 38 | static void SDL_CleanupWaitableHandle(void *handle) | ||
| 39 | { | ||
| 40 | CloseHandle(handle); | ||
| 41 | } | ||
| 42 | |||
| 43 | static HANDLE SDL_GetWaitableTimer(void) | ||
| 44 | { | ||
| 45 | static SDL_TLSID TLS_timer_handle; | ||
| 46 | HANDLE timer; | ||
| 47 | |||
| 48 | if (!pCreateWaitableTimerExW || !pSetWaitableTimerEx) { | ||
| 49 | static bool initialized; | ||
| 50 | |||
| 51 | if (!initialized) { | ||
| 52 | HMODULE module = GetModuleHandle(TEXT("kernel32.dll")); | ||
| 53 | if (module) { | ||
| 54 | pCreateWaitableTimerExW = (CreateWaitableTimerExW_t)GetProcAddress(module, "CreateWaitableTimerExW"); | ||
| 55 | pSetWaitableTimerEx = (SetWaitableTimerEx_t)GetProcAddress(module, "SetWaitableTimerEx"); | ||
| 56 | } | ||
| 57 | initialized = true; | ||
| 58 | } | ||
| 59 | |||
| 60 | if (!pCreateWaitableTimerExW || !pSetWaitableTimerEx) { | ||
| 61 | return NULL; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | timer = SDL_GetTLS(&TLS_timer_handle); | ||
| 66 | if (!timer) { | ||
| 67 | timer = pCreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); | ||
| 68 | if (timer) { | ||
| 69 | SDL_SetTLS(&TLS_timer_handle, timer, SDL_CleanupWaitableHandle); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | return timer; | ||
| 73 | } | ||
| 74 | |||
| 75 | static HANDLE SDL_GetWaitableEvent(void) | ||
| 76 | { | ||
| 77 | static SDL_TLSID TLS_event_handle; | ||
| 78 | HANDLE event; | ||
| 79 | |||
| 80 | event = SDL_GetTLS(&TLS_event_handle); | ||
| 81 | if (!event) { | ||
| 82 | event = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
| 83 | if (event) { | ||
| 84 | SDL_SetTLS(&TLS_event_handle, event, SDL_CleanupWaitableHandle); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | return event; | ||
| 88 | } | ||
| 89 | |||
| 90 | Uint64 SDL_GetPerformanceCounter(void) | ||
| 91 | { | ||
| 92 | LARGE_INTEGER counter; | ||
| 93 | const BOOL rc = QueryPerformanceCounter(&counter); | ||
| 94 | SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later. | ||
| 95 | return (Uint64)counter.QuadPart; | ||
| 96 | } | ||
| 97 | |||
| 98 | Uint64 SDL_GetPerformanceFrequency(void) | ||
| 99 | { | ||
| 100 | LARGE_INTEGER frequency; | ||
| 101 | const BOOL rc = QueryPerformanceFrequency(&frequency); | ||
| 102 | SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later. | ||
| 103 | return (Uint64)frequency.QuadPart; | ||
| 104 | } | ||
| 105 | |||
| 106 | void SDL_SYS_DelayNS(Uint64 ns) | ||
| 107 | { | ||
| 108 | HANDLE timer = SDL_GetWaitableTimer(); | ||
| 109 | if (timer) { | ||
| 110 | LARGE_INTEGER due_time; | ||
| 111 | due_time.QuadPart = -((LONGLONG)ns / 100); | ||
| 112 | if (pSetWaitableTimerEx(timer, &due_time, 0, NULL, NULL, NULL, 0)) { | ||
| 113 | WaitForSingleObject(timer, INFINITE); | ||
| 114 | } | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | const Uint64 max_delay = 0xffffffffLL * SDL_NS_PER_MS; | ||
| 119 | if (ns > max_delay) { | ||
| 120 | ns = max_delay; | ||
| 121 | } | ||
| 122 | const DWORD delay = (DWORD)SDL_NS_TO_MS(ns); | ||
| 123 | |||
| 124 | HANDLE event = SDL_GetWaitableEvent(); | ||
| 125 | if (event) { | ||
| 126 | WaitForSingleObjectEx(event, delay, FALSE); | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | Sleep(delay); | ||
| 131 | } | ||
| 132 | |||
| 133 | #endif // SDL_TIMER_WINDOWS | ||
