Browse Source

Added SDL_LinuxSetThreadPriority() to directly set the priority of a Linux thread (tid)
This function tries using RealtimeKit connecting over DBUS as needed.

Sam Lantinga 7 years ago
parent
commit
432312561f

+ 12 - 0
include/SDL_system.h

@@ -76,6 +76,18 @@ extern DECLSPEC SDL_bool SDLCALL SDL_DXGIGetOutputInfo( int displayIndex, int *a
 #endif /* __WIN32__ */
 #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 */
 /* Platform specific functions for iOS */
 #if defined(__IPHONEOS__) && __IPHONEOS__
 #if defined(__IPHONEOS__) && __IPHONEOS__
 
 

+ 1 - 0
src/dynapi/SDL_dynapi_overrides.h

@@ -670,3 +670,4 @@
 #define SDL_log10 SDL_log10_REAL
 #define SDL_log10 SDL_log10_REAL
 #define SDL_log10f SDL_log10f_REAL
 #define SDL_log10f SDL_log10f_REAL
 #define SDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex_REAL
 #define SDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex_REAL
+#define SDL_LinuxSetThreadPriority SDL_LinuxSetThreadPriority_REAL

+ 3 - 0
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(double,SDL_log10,(double a),(a),return)
 SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return)
 SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return)
 SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForDeviceIndex,(int 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

+ 25 - 17
src/thread/pthread/SDL_systhread.c

@@ -238,24 +238,11 @@ rtkit_setpriority(pid_t thread, int nice_level)
 
 
 #endif /* !SDL_USE_LIBDBUS */
 #endif /* !SDL_USE_LIBDBUS */
 
 
+#if __LINUX__
 int
 int
-SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
+SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
 {
 {
-#if __NACL__ 
-    /* FIXME: Setting thread priority does not seem to be supported in NACL */
-    return 0;
-#elif __LINUX__
-    int value;
-    pid_t thread = syscall(SYS_gettid);
-
-    if (priority == SDL_THREAD_PRIORITY_LOW) {
-        value = 19;
-    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
-        value = -20;
-    } else {
-        value = 0;
-    }
-    if (setpriority(PRIO_PROCESS, thread, value) < 0) {
+    if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) < 0) {
         /* Note that this fails if you're trying to set high priority
         /* 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!
            and you don't have root permission. BUT DON'T RUN AS ROOT!
 
 
@@ -268,11 +255,32 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
            README and sample code at:
            README and sample code at:
              http://git.0pointer.net/rtkit.git
              http://git.0pointer.net/rtkit.git
          */
          */
-        if (rtkit_setpriority(thread, value) == SDL_FALSE) {
+        if (rtkit_setpriority((pid_t)threadID, priority) == SDL_FALSE) {
             return SDL_SetError("setpriority() failed");
             return SDL_SetError("setpriority() failed");
         }
         }
     }
     }
     return 0;
     return 0;
+}
+#endif /* __LINUX__ */
+
+int
+SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
+{
+#if __NACL__ 
+    /* FIXME: Setting thread priority does not seem to be supported in NACL */
+    return 0;
+#elif __LINUX__
+    int value;
+    pid_t thread = syscall(SYS_gettid);
+
+    if (priority == SDL_THREAD_PRIORITY_LOW) {
+        value = 19;
+    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
+        value = -20;
+    } else {
+        value = 0;
+    }
+    return SDL_LinuxSetThreadPriority(thread, value);
 #else
 #else
     struct sched_param sched;
     struct sched_param sched;
     int policy;
     int policy;