SDL
1/*
2Simple DirectMedia Layer
3Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any damages
7arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose,
10including commercial applications, and to alter it and redistribute it
11freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must not
14claim that you wrote the original software. If you use this software
15in a product, an acknowledgment in the product documentation would be
16appreciated but is not required.
172. Altered source versions must be plainly marked as such, and must not be
18misrepresented as being the original software.
193. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"22
23#ifdef SDL_TIMER_WINDOWS24
25#include "../../core/windows/SDL_windows.h"26
27
28#ifdef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION29static void SDL_CleanupWaitableTimer(void *timer)30{
31CloseHandle(timer);32}
33
34HANDLE SDL_GetWaitableTimer(void)35{
36static SDL_TLSID TLS_timer_handle;37HANDLE timer;38
39timer = SDL_GetTLS(&TLS_timer_handle);40if (!timer) {41timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);42if (timer) {43SDL_SetTLS(&TLS_timer_handle, timer, SDL_CleanupWaitableTimer);44}45}46return timer;47}
48#endif // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION49
50Uint64 SDL_GetPerformanceCounter(void)51{
52LARGE_INTEGER counter;53const BOOL rc = QueryPerformanceCounter(&counter);54SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later.55return (Uint64)counter.QuadPart;56}
57
58Uint64 SDL_GetPerformanceFrequency(void)59{
60LARGE_INTEGER frequency;61const BOOL rc = QueryPerformanceFrequency(&frequency);62SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later.63return (Uint64)frequency.QuadPart;64}
65
66void SDL_SYS_DelayNS(Uint64 ns)67{
68/* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag was added in Windows 10 version 1803.69*
70* Sleep() is not publicly available to apps in early versions of WinRT.
71*
72* Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
73* Windows Phone 8.1.
74*
75* Use the compiler version to determine availability.
76*
77* NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
78* NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
79* Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
80* apps and libraries.
81*/
82#ifdef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION83HANDLE timer = SDL_GetWaitableTimer();84if (timer) {85LARGE_INTEGER due_time;86due_time.QuadPart = -((LONGLONG)ns / 100);87if (SetWaitableTimerEx(timer, &due_time, 0, NULL, NULL, NULL, 0)) {88WaitForSingleObject(timer, INFINITE);89}90return;91}92#endif93
94{95const Uint64 max_delay = 0xffffffffLL * SDL_NS_PER_MS;96if (ns > max_delay) {97ns = max_delay;98}99
100#if defined(SDL_PLATFORM_WINRT) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)101static HANDLE mutex = 0;102if (!mutex) {103mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);104}105WaitForSingleObjectEx(mutex, (DWORD)SDL_NS_TO_MS(ns), FALSE);106#else107Sleep((DWORD)SDL_NS_TO_MS(ns));108#endif109}110}
111
112#endif // SDL_TIMER_WINDOWS113