Fixed bug 4992 - UWP/WinRT does not set thread priority when using SDL_SetThreadPriority
authorSam Lantinga <slouken@libsdl.org>
Sun, 01 Mar 2020 12:50:42 -0800
changeset 1355961b564f66050
parent 13558 ed7c27865ea7
child 13560 414b888d2b9f
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
     1.1 --- a/src/thread/stdcpp/SDL_systhread.cpp	Thu Feb 27 13:53:32 2020 -0800
     1.2 +++ b/src/thread/stdcpp/SDL_systhread.cpp	Sun Mar 01 12:50:42 2020 -0800
     1.3 @@ -96,19 +96,30 @@
     1.4  int
     1.5  SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
     1.6  {
     1.7 -    // Thread priorities do not look to be settable via C++11's thread
     1.8 -    // interface, at least as of this writing (Nov 2012).  std::thread does
     1.9 -    // provide access to the OS' native handle, however, and some form of
    1.10 -    // priority-setting could, in theory, be done through this interface.
    1.11 -    //
    1.12 -    // WinRT: UPDATE (Aug 20, 2013): thread priorities cannot be changed
    1.13 -    // on WinRT, at least not for any thread that's already been created.
    1.14 -    // WinRT threads appear to be based off of the WinRT class,
    1.15 -    // ThreadPool, more info on which can be found at:
    1.16 -    // http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading.threadpool.aspx
    1.17 -    //
    1.18 -    // For compatibility sake, 0 will be returned here.
    1.19 -    return (0);
    1.20 +#ifdef __WINRT__
    1.21 +    int value;
    1.22 +
    1.23 +    if (priority == SDL_THREAD_PRIORITY_LOW) {
    1.24 +        value = THREAD_PRIORITY_LOWEST;
    1.25 +    }
    1.26 +    else if (priority == SDL_THREAD_PRIORITY_HIGH) {
    1.27 +        value = THREAD_PRIORITY_HIGHEST;
    1.28 +    }
    1.29 +    else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
    1.30 +        // FIXME: WinRT does not support TIME_CRITICAL! -flibit
    1.31 +        SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "TIME_CRITICAL unsupported, falling back to HIGHEST");
    1.32 +        value = THREAD_PRIORITY_HIGHEST;
    1.33 +    }
    1.34 +    else {
    1.35 +        value = THREAD_PRIORITY_NORMAL;
    1.36 +    }
    1.37 +    if (!SetThreadPriority(GetCurrentThread(), value)) {
    1.38 +        return WIN_SetError("SetThreadPriority()");
    1.39 +    }
    1.40 +    return 0;
    1.41 +#else
    1.42 +    return SDL_Unsupported();
    1.43 +#endif
    1.44  }
    1.45  
    1.46  extern "C"