Added SDL_LinuxSetThreadPriority() to directly set the priority of a Linux thread (tid)
authorSam Lantinga <slouken@libsdl.org>
Mon, 23 Apr 2018 19:18:52 -0700
changeset 1194789b721a030c0
parent 11946 eae649a13dad
child 11948 b6ad849f76a4
Added SDL_LinuxSetThreadPriority() to directly set the priority of a Linux thread (tid)
This function tries using RealtimeKit connecting over DBUS as needed.
include/SDL_system.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/thread/pthread/SDL_systhread.c
     1.1 --- a/include/SDL_system.h	Mon Apr 23 17:10:36 2018 -0700
     1.2 +++ b/include/SDL_system.h	Mon Apr 23 19:18:52 2018 -0700
     1.3 @@ -76,6 +76,18 @@
     1.4  #endif /* __WIN32__ */
     1.5  
     1.6  
     1.7 +/* Platform specific functions for Linux */
     1.8 +#ifdef __LINUX__
     1.9 +
    1.10 +/**
    1.11 +   \brief Sets the UNIX nice value for a thread, using setpriority() if possible, and RealtimeKit if available.
    1.12 +
    1.13 +   \return 0 on success, or -1 on error.
    1.14 + */
    1.15 +extern DECLSPEC int SDLCALL SDL_LinuxSetThreadPriority(Sint64 threadID, int priority);
    1.16 + 
    1.17 +#endif /* __LINUX__ */
    1.18 +	
    1.19  /* Platform specific functions for iOS */
    1.20  #if defined(__IPHONEOS__) && __IPHONEOS__
    1.21  
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Mon Apr 23 17:10:36 2018 -0700
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Mon Apr 23 19:18:52 2018 -0700
     2.3 @@ -670,3 +670,4 @@
     2.4  #define SDL_log10 SDL_log10_REAL
     2.5  #define SDL_log10f SDL_log10f_REAL
     2.6  #define SDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex_REAL
     2.7 +#define SDL_LinuxSetThreadPriority SDL_LinuxSetThreadPriority_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Mon Apr 23 17:10:36 2018 -0700
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Mon Apr 23 19:18:52 2018 -0700
     3.3 @@ -708,3 +708,6 @@
     3.4  SDL_DYNAPI_PROC(double,SDL_log10,(double a),(a),return)
     3.5  SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return)
     3.6  SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForDeviceIndex,(int a),(a),return)
     3.7 +#ifdef __LINUX__
     3.8 +SDL_DYNAPI_PROC(int,SDL_LinuxSetThreadPriority,(Sint64 a, int b),(a,b),return)
     3.9 +#endif
     4.1 --- a/src/thread/pthread/SDL_systhread.c	Mon Apr 23 17:10:36 2018 -0700
     4.2 +++ b/src/thread/pthread/SDL_systhread.c	Mon Apr 23 19:18:52 2018 -0700
     4.3 @@ -238,6 +238,31 @@
     4.4  
     4.5  #endif /* !SDL_USE_LIBDBUS */
     4.6  
     4.7 +#if __LINUX__
     4.8 +int
     4.9 +SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
    4.10 +{
    4.11 +    if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) < 0) {
    4.12 +        /* Note that this fails if you're trying to set high priority
    4.13 +           and you don't have root permission. BUT DON'T RUN AS ROOT!
    4.14 +
    4.15 +           You can grant the ability to increase thread priority by
    4.16 +           running the following command on your application binary:
    4.17 +               sudo setcap 'cap_sys_nice=eip' <application>
    4.18 +
    4.19 +           Let's try setting priority with RealtimeKit...
    4.20 +
    4.21 +           README and sample code at:
    4.22 +             http://git.0pointer.net/rtkit.git
    4.23 +         */
    4.24 +        if (rtkit_setpriority((pid_t)threadID, priority) == SDL_FALSE) {
    4.25 +            return SDL_SetError("setpriority() failed");
    4.26 +        }
    4.27 +    }
    4.28 +    return 0;
    4.29 +}
    4.30 +#endif /* __LINUX__ */
    4.31 +
    4.32  int
    4.33  SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
    4.34  {
    4.35 @@ -255,24 +280,7 @@
    4.36      } else {
    4.37          value = 0;
    4.38      }
    4.39 -    if (setpriority(PRIO_PROCESS, thread, value) < 0) {
    4.40 -        /* Note that this fails if you're trying to set high priority
    4.41 -           and you don't have root permission. BUT DON'T RUN AS ROOT!
    4.42 -
    4.43 -           You can grant the ability to increase thread priority by
    4.44 -           running the following command on your application binary:
    4.45 -               sudo setcap 'cap_sys_nice=eip' <application>
    4.46 -
    4.47 -           Let's try setting priority with RealtimeKit...
    4.48 -
    4.49 -           README and sample code at:
    4.50 -             http://git.0pointer.net/rtkit.git
    4.51 -         */
    4.52 -        if (rtkit_setpriority(thread, value) == SDL_FALSE) {
    4.53 -            return SDL_SetError("setpriority() failed");
    4.54 -        }
    4.55 -    }
    4.56 -    return 0;
    4.57 +    return SDL_LinuxSetThreadPriority(thread, value);
    4.58  #else
    4.59      struct sched_param sched;
    4.60      int policy;