Implemented SDL_SetThreadPriority()
authorSam Lantinga <slouken@libsdl.org>
Fri, 25 Mar 2011 10:47:49 -0700
changeset 5506 82a09d5d0f07
parent 5505 c4bb4c8ea6fd
child 5507 6275a16e63c2
Implemented SDL_SetThreadPriority()
include/SDL_thread.h
src/audio/SDL_audio.c
src/thread/SDL_systhread.h
src/thread/SDL_thread.c
src/thread/beos/SDL_systhread.c
src/thread/generic/SDL_systhread.c
src/thread/nds/SDL_syscond.c
src/thread/nds/SDL_sysmutex.c
src/thread/nds/SDL_syssem.c
src/thread/nds/SDL_systhread.c
src/thread/pthread/SDL_systhread.c
src/thread/windows/SDL_systhread.c
--- a/include/SDL_thread.h	Fri Mar 25 10:26:25 2011 -0700
+++ b/include/SDL_thread.h	Fri Mar 25 10:47:49 2011 -0700
@@ -50,6 +50,16 @@
 /* The SDL thread ID */
 typedef unsigned long SDL_threadID;
 
+/* The SDL thread priority
+ *
+ * Note: On many systems you require special privileges to set high priority.
+ */
+typedef enum {
+    SDL_THREAD_PRIORITY_LOW,
+    SDL_THREAD_PRIORITY_NORMAL,
+    SDL_THREAD_PRIORITY_HIGH
+} SDL_ThreadPriority;
+
 /* The function passed to SDL_CreateThread()
    It is passed a void* user context parameter and returns an int.
  */
@@ -147,6 +157,11 @@
 extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
 
 /**
+ *  Set the thread priority
+ */
+extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority);
+
+/**
  *  Wait for a thread to finish.
  *  
  *  The return code for the thread function is placed in the area
--- a/src/audio/SDL_audio.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/audio/SDL_audio.c	Fri Mar 25 10:47:49 2011 -0700
@@ -974,6 +974,7 @@
             SDL_SetError("Couldn't create audio thread");
             return 0;
         }
+        SDL_SetThreadPriority(device->thread, SDL_THREAD_PRIORITY_HIGH);
     }
 
     return id + 1;
--- a/src/thread/SDL_systhread.h	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/SDL_systhread.h	Fri Mar 25 10:47:49 2011 -0700
@@ -43,6 +43,9 @@
 /* This function does any necessary setup in the child thread */
 extern void SDL_SYS_SetupThread(void);
 
+/* This function sets thread priority */
+extern int SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority);
+
 /* This function waits for the thread to finish and frees any data
    allocated by SDL_SYS_CreateThread()
  */
--- a/src/thread/SDL_thread.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/SDL_thread.c	Fri Mar 25 10:47:49 2011 -0700
@@ -281,6 +281,29 @@
     return (thread);
 }
 
+SDL_threadID
+SDL_GetThreadID(SDL_Thread * thread)
+{
+    SDL_threadID id;
+
+    if (thread) {
+        id = thread->threadid;
+    } else {
+        id = SDL_ThreadID();
+    }
+    return id;
+}
+
+int
+SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
+{
+    if (!thread) {
+        SDL_SetError("SDL_SetThreadPriority() passed NULL thread");
+        return -1;
+    }
+    return SDL_SYS_SetThreadPriority(thread, priority);
+}
+
 void
 SDL_WaitThread(SDL_Thread * thread, int *status)
 {
@@ -294,17 +317,4 @@
     }
 }
 
-SDL_threadID
-SDL_GetThreadID(SDL_Thread * thread)
-{
-    SDL_threadID id;
-
-    if (thread) {
-        id = thread->threadid;
-    } else {
-        id = SDL_ThreadID();
-    }
-    return id;
-}
-
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/thread/beos/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/beos/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
@@ -90,6 +90,20 @@
     return ((SDL_threadID) find_thread(NULL));
 }
 
+int
+SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
+{
+    int32 new_priority = B_NORMAL_PRIORITY;
+
+    if (priority == SDL_THREAD_PRIORITY_LOW) {
+        new_priority = B_LOW_PRIORITY;
+    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
+        new_priority = B_URGENT_DISPLAY_PRIORITY;
+    }
+    set_thread_priority(thread->handle, new_priority);
+    return 0;
+}
+
 void
 SDL_SYS_WaitThread(SDL_Thread * thread)
 {
--- a/src/thread/generic/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/generic/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
@@ -45,6 +45,12 @@
     return (0);
 }
 
+int
+SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
+{
+    return (0);
+}
+
 void
 SDL_SYS_WaitThread(SDL_Thread * thread)
 {
--- a/src/thread/nds/SDL_syscond.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/nds/SDL_syscond.c	Fri Mar 25 10:47:49 2011 -0700
@@ -227,3 +227,5 @@
 {
     return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
 }
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/thread/nds/SDL_sysmutex.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/nds/SDL_sysmutex.c	Fri Mar 25 10:47:49 2011 -0700
@@ -140,3 +140,5 @@
     return 0;
 #endif /* DISABLE_THREADS */
 }
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/thread/nds/SDL_syssem.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/nds/SDL_syssem.c	Fri Mar 25 10:47:49 2011 -0700
@@ -226,3 +226,5 @@
 }
 
 #endif /* DISABLE_THREADS */
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/thread/nds/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/nds/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
@@ -56,8 +56,16 @@
     return;
 }
 
+int
+SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
+{
+    return (0);
+}
+
 void
 SDL_SYS_KillThread(SDL_Thread * thread)
 {
     return;
 }
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/thread/pthread/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/pthread/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
@@ -91,6 +91,33 @@
     return ((SDL_threadID) pthread_self());
 }
 
+int
+SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
+{
+    struct sched_param sched;
+    int policy;
+
+    if (pthread_getschedparam(thread->handle, &policy, &sched) < 0) {
+        SDL_SetError("pthread_getschedparam() failed");
+        return -1;
+    }
+    if (priority == SDL_THREAD_PRIORITY_LOW) {
+        sched.sched_priority = sched_get_priority_min(policy);
+    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
+        sched.sched_priority = sched_get_priority_max(policy);
+    } else {
+        int min_priority = sched_get_priority_min(policy);
+        int max_priority = sched_get_priority_max(policy);
+        int priority = (min_priority + (max_priority - min_priority) / 2);
+        sched.sched_priority = priority;
+    }
+    if (pthread_setschedparam(thread->handle, policy, &sched) < 0) {
+        SDL_SetError("pthread_setschedparam() failed");
+        return -1;
+    }
+    return 0;
+}
+
 void
 SDL_SYS_WaitThread(SDL_Thread * thread)
 {
--- a/src/thread/windows/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
+++ b/src/thread/windows/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
@@ -155,6 +155,25 @@
     return ((SDL_threadID) GetCurrentThreadId());
 }
 
+int
+SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
+{
+    BOOL result;
+
+    if (priority == SDL_THREAD_PRIORITY_LOW) {
+        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_LOWEST);
+    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
+        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_HIGHEST);
+    } else {
+        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_NORMAL);
+    }
+    if (!result) {
+        WIN_SetError("SetThreadPriority()");
+        return -1;
+    }
+    return 0;
+}
+
 void
 SDL_SYS_WaitThread(SDL_Thread * thread)
 {