From bd5da73afd831e0c82ed5b9d11cfcd09ed6b59c6 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 1 Mar 2020 12:50:42 -0800 Subject: [PATCH] Fixed bug 4992 - UWP/WinRT does not set thread priority when using SDL_SetThreadPriority Ethan Lee Attached is a diff that I used to get SetThreadPriority working locally. I still have no idea what the minimum SDK version is since Microsoft never documented it, but it's worth pointing out that they're much more aggressive about using the latest VS and UWP SDK anyway (for example, an updated Xbox is no longer compatible with VS2017, and updates are required to have a network connection of any kind). --- src/thread/stdcpp/SDL_systhread.cpp | 37 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/thread/stdcpp/SDL_systhread.cpp b/src/thread/stdcpp/SDL_systhread.cpp index 785abf2283340..fc839d3bbc6f6 100644 --- a/src/thread/stdcpp/SDL_systhread.cpp +++ b/src/thread/stdcpp/SDL_systhread.cpp @@ -96,19 +96,30 @@ extern "C" int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) { - // Thread priorities do not look to be settable via C++11's thread - // interface, at least as of this writing (Nov 2012). std::thread does - // provide access to the OS' native handle, however, and some form of - // priority-setting could, in theory, be done through this interface. - // - // WinRT: UPDATE (Aug 20, 2013): thread priorities cannot be changed - // on WinRT, at least not for any thread that's already been created. - // WinRT threads appear to be based off of the WinRT class, - // ThreadPool, more info on which can be found at: - // http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading.threadpool.aspx - // - // For compatibility sake, 0 will be returned here. - return (0); +#ifdef __WINRT__ + int value; + + if (priority == SDL_THREAD_PRIORITY_LOW) { + value = THREAD_PRIORITY_LOWEST; + } + else if (priority == SDL_THREAD_PRIORITY_HIGH) { + value = THREAD_PRIORITY_HIGHEST; + } + else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) { + // FIXME: WinRT does not support TIME_CRITICAL! -flibit + SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "TIME_CRITICAL unsupported, falling back to HIGHEST"); + value = THREAD_PRIORITY_HIGHEST; + } + else { + value = THREAD_PRIORITY_NORMAL; + } + if (!SetThreadPriority(GetCurrentThread(), value)) { + return WIN_SetError("SetThreadPriority()"); + } + return 0; +#else + return SDL_Unsupported(); +#endif } extern "C"