From 432312561f8ac6a5c86c0cc5ab28c26f128e8c32 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 23 Apr 2018 19:18:52 -0700 Subject: [PATCH] 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 | 12 ++++++++ src/dynapi/SDL_dynapi_overrides.h | 1 + src/dynapi/SDL_dynapi_procs.h | 3 ++ src/thread/pthread/SDL_systhread.c | 44 ++++++++++++++++++------------ 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/include/SDL_system.h b/include/SDL_system.h index 7b776fdf1..77186b597 100644 --- a/include/SDL_system.h +++ b/include/SDL_system.h @@ -76,6 +76,18 @@ extern DECLSPEC SDL_bool SDLCALL SDL_DXGIGetOutputInfo( int displayIndex, int *a #endif /* __WIN32__ */ +/* Platform specific functions for Linux */ +#ifdef __LINUX__ + +/** + \brief Sets the UNIX nice value for a thread, using setpriority() if possible, and RealtimeKit if available. + + \return 0 on success, or -1 on error. + */ +extern DECLSPEC int SDLCALL SDL_LinuxSetThreadPriority(Sint64 threadID, int priority); + +#endif /* __LINUX__ */ + /* Platform specific functions for iOS */ #if defined(__IPHONEOS__) && __IPHONEOS__ diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 77a73e52c..b454e5a14 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -670,3 +670,4 @@ #define SDL_log10 SDL_log10_REAL #define SDL_log10f SDL_log10f_REAL #define SDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex_REAL +#define SDL_LinuxSetThreadPriority SDL_LinuxSetThreadPriority_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 1b57a2a05..e4e6907cb 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -708,3 +708,6 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_IsAndroidTV,(void),(),return) SDL_DYNAPI_PROC(double,SDL_log10,(double a),(a),return) SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return) SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForDeviceIndex,(int a),(a),return) +#ifdef __LINUX__ +SDL_DYNAPI_PROC(int,SDL_LinuxSetThreadPriority,(Sint64 a, int b),(a,b),return) +#endif diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c index 3abb4da13..8e48368b1 100644 --- a/src/thread/pthread/SDL_systhread.c +++ b/src/thread/pthread/SDL_systhread.c @@ -238,6 +238,31 @@ rtkit_setpriority(pid_t thread, int nice_level) #endif /* !SDL_USE_LIBDBUS */ +#if __LINUX__ +int +SDL_LinuxSetThreadPriority(Sint64 threadID, int priority) +{ + if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) < 0) { + /* Note that this fails if you're trying to set high priority + and you don't have root permission. BUT DON'T RUN AS ROOT! + + You can grant the ability to increase thread priority by + running the following command on your application binary: + sudo setcap 'cap_sys_nice=eip' + + Let's try setting priority with RealtimeKit... + + README and sample code at: + http://git.0pointer.net/rtkit.git + */ + if (rtkit_setpriority((pid_t)threadID, priority) == SDL_FALSE) { + return SDL_SetError("setpriority() failed"); + } + } + return 0; +} +#endif /* __LINUX__ */ + int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) { @@ -255,24 +280,7 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) } else { value = 0; } - if (setpriority(PRIO_PROCESS, thread, value) < 0) { - /* Note that this fails if you're trying to set high priority - and you don't have root permission. BUT DON'T RUN AS ROOT! - - You can grant the ability to increase thread priority by - running the following command on your application binary: - sudo setcap 'cap_sys_nice=eip' - - Let's try setting priority with RealtimeKit... - - README and sample code at: - http://git.0pointer.net/rtkit.git - */ - if (rtkit_setpriority(thread, value) == SDL_FALSE) { - return SDL_SetError("setpriority() failed"); - } - } - return 0; + return SDL_LinuxSetThreadPriority(thread, value); #else struct sched_param sched; int policy;