The API sets the priority for the current thread, not an arbitrary thread.
authorSam Lantinga <slouken@libsdl.org>
Fri, 25 Mar 2011 12:44:06 -0700
changeset 55095b1b4d820d10
parent 5508 60b63c32f06d
child 5510 55519e27e5d1
The API sets the priority for the current thread, not an arbitrary thread.

Implemented thread priority as the 'nice' value on Linux. High priority threads require root permissions (you shouldn't give your game root permissions though!)
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_systhread.c
src/thread/pthread/SDL_systhread.c
src/thread/windows/SDL_systhread.c
     1.1 --- a/include/SDL_thread.h	Fri Mar 25 11:09:57 2011 -0700
     1.2 +++ b/include/SDL_thread.h	Fri Mar 25 12:44:06 2011 -0700
     1.3 @@ -157,9 +157,9 @@
     1.4  extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
     1.5  
     1.6  /**
     1.7 - *  Set the thread priority
     1.8 + *  Set the priority for the current thread
     1.9   */
    1.10 -extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority);
    1.11 +extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
    1.12  
    1.13  /**
    1.14   *  Wait for a thread to finish.
     2.1 --- a/src/audio/SDL_audio.c	Fri Mar 25 11:09:57 2011 -0700
     2.2 +++ b/src/audio/SDL_audio.c	Fri Mar 25 12:44:06 2011 -0700
     2.3 @@ -329,6 +329,9 @@
     2.4      int silence;
     2.5      Uint32 delay;
     2.6  
     2.7 +    /* The audio mixing is always a high priority thread */
     2.8 +    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
     2.9 +
    2.10      /* For streaming when the buffer sizes don't match up */
    2.11      Uint8 *istream;
    2.12      int istream_len = 0;
    2.13 @@ -974,7 +977,6 @@
    2.14              SDL_SetError("Couldn't create audio thread");
    2.15              return 0;
    2.16          }
    2.17 -        SDL_SetThreadPriority(device->thread, SDL_THREAD_PRIORITY_HIGH);
    2.18      }
    2.19  
    2.20      return id + 1;
     3.1 --- a/src/thread/SDL_systhread.h	Fri Mar 25 11:09:57 2011 -0700
     3.2 +++ b/src/thread/SDL_systhread.h	Fri Mar 25 12:44:06 2011 -0700
     3.3 @@ -43,8 +43,8 @@
     3.4  /* This function does any necessary setup in the child thread */
     3.5  extern void SDL_SYS_SetupThread(void);
     3.6  
     3.7 -/* This function sets thread priority */
     3.8 -extern int SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority);
     3.9 +/* This function sets the current thread priority */
    3.10 +extern int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority);
    3.11  
    3.12  /* This function waits for the thread to finish and frees any data
    3.13     allocated by SDL_SYS_CreateThread()
     4.1 --- a/src/thread/SDL_thread.c	Fri Mar 25 11:09:57 2011 -0700
     4.2 +++ b/src/thread/SDL_thread.c	Fri Mar 25 12:44:06 2011 -0700
     4.3 @@ -295,13 +295,9 @@
     4.4  }
     4.5  
     4.6  int
     4.7 -SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
     4.8 +SDL_SetThreadPriority(SDL_ThreadPriority priority)
     4.9  {
    4.10 -    if (!thread) {
    4.11 -        SDL_SetError("SDL_SetThreadPriority() passed NULL thread");
    4.12 -        return -1;
    4.13 -    }
    4.14 -    return SDL_SYS_SetThreadPriority(thread, priority);
    4.15 +    return SDL_SYS_SetThreadPriority(priority);
    4.16  }
    4.17  
    4.18  void
     5.1 --- a/src/thread/beos/SDL_systhread.c	Fri Mar 25 11:09:57 2011 -0700
     5.2 +++ b/src/thread/beos/SDL_systhread.c	Fri Mar 25 12:44:06 2011 -0700
     5.3 @@ -93,14 +93,16 @@
     5.4  int
     5.5  SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
     5.6  {
     5.7 -    int32 new_priority = B_NORMAL_PRIORITY;
     5.8 +    int32 value;
     5.9  
    5.10      if (priority == SDL_THREAD_PRIORITY_LOW) {
    5.11 -        new_priority = B_LOW_PRIORITY;
    5.12 +        value = B_LOW_PRIORITY;
    5.13      } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
    5.14 -        new_priority = B_URGENT_DISPLAY_PRIORITY;
    5.15 +        value = B_URGENT_DISPLAY_PRIORITY;
    5.16 +    } else {
    5.17 +        value = B_NORMAL_PRIORITY;
    5.18      }
    5.19 -    set_thread_priority(thread->handle, new_priority);
    5.20 +    set_thread_priority(find_thread(NULL), value);
    5.21      return 0;
    5.22  }
    5.23  
     6.1 --- a/src/thread/generic/SDL_systhread.c	Fri Mar 25 11:09:57 2011 -0700
     6.2 +++ b/src/thread/generic/SDL_systhread.c	Fri Mar 25 12:44:06 2011 -0700
     6.3 @@ -46,7 +46,7 @@
     6.4  }
     6.5  
     6.6  int
     6.7 -SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
     6.8 +SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
     6.9  {
    6.10      return (0);
    6.11  }
     7.1 --- a/src/thread/nds/SDL_systhread.c	Fri Mar 25 11:09:57 2011 -0700
     7.2 +++ b/src/thread/nds/SDL_systhread.c	Fri Mar 25 12:44:06 2011 -0700
     7.3 @@ -57,7 +57,7 @@
     7.4  }
     7.5  
     7.6  int
     7.7 -SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
     7.8 +SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
     7.9  {
    7.10      return (0);
    7.11  }
     8.1 --- a/src/thread/pthread/SDL_systhread.c	Fri Mar 25 11:09:57 2011 -0700
     8.2 +++ b/src/thread/pthread/SDL_systhread.c	Fri Mar 25 12:44:06 2011 -0700
     8.3 @@ -23,6 +23,11 @@
     8.4  
     8.5  #include <pthread.h>
     8.6  #include <signal.h>
     8.7 +#ifdef linux
     8.8 +#include <sys/time.h>
     8.9 +#include <sys/resource.h>
    8.10 +#include <sys/syscall.h>
    8.11 +#endif
    8.12  
    8.13  #include "SDL_thread.h"
    8.14  #include "../SDL_thread_c.h"
    8.15 @@ -34,6 +39,7 @@
    8.16      SIGVTALRM, SIGPROF, 0
    8.17  };
    8.18  
    8.19 +
    8.20  static void *
    8.21  RunThread(void *data)
    8.22  {
    8.23 @@ -92,12 +98,29 @@
    8.24  }
    8.25  
    8.26  int
    8.27 -SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
    8.28 +SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
    8.29  {
    8.30 +#ifdef linux
    8.31 +    int value;
    8.32 +
    8.33 +    if (priority == SDL_THREAD_PRIORITY_LOW) {
    8.34 +        value = 19;
    8.35 +    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
    8.36 +        value = -20;
    8.37 +    } else {
    8.38 +        value = 0;
    8.39 +    }
    8.40 +    if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
    8.41 +        SDL_SetError("setpriority() failed");
    8.42 +        return -1;
    8.43 +    }
    8.44 +    return 0;
    8.45 +#else
    8.46      struct sched_param sched;
    8.47      int policy;
    8.48 +    pthread_t thread = pthread_self();
    8.49  
    8.50 -    if (pthread_getschedparam(thread->handle, &policy, &sched) < 0) {
    8.51 +    if (pthread_getschedparam(thread, &policy, &sched) < 0) {
    8.52          SDL_SetError("pthread_getschedparam() failed");
    8.53          return -1;
    8.54      }
    8.55 @@ -108,14 +131,14 @@
    8.56      } else {
    8.57          int min_priority = sched_get_priority_min(policy);
    8.58          int max_priority = sched_get_priority_max(policy);
    8.59 -        int priority = (min_priority + (max_priority - min_priority) / 2);
    8.60 -        sched.sched_priority = priority;
    8.61 +        sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
    8.62      }
    8.63 -    if (pthread_setschedparam(thread->handle, policy, &sched) < 0) {
    8.64 +    if (pthread_setschedparam(thread, policy, &sched) < 0) {
    8.65          SDL_SetError("pthread_setschedparam() failed");
    8.66          return -1;
    8.67      }
    8.68      return 0;
    8.69 +#endif /* linux */
    8.70  }
    8.71  
    8.72  void
     9.1 --- a/src/thread/windows/SDL_systhread.c	Fri Mar 25 11:09:57 2011 -0700
     9.2 +++ b/src/thread/windows/SDL_systhread.c	Fri Mar 25 12:44:06 2011 -0700
     9.3 @@ -156,18 +156,18 @@
     9.4  }
     9.5  
     9.6  int
     9.7 -SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
     9.8 +SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
     9.9  {
    9.10 -    BOOL result;
    9.11 +    int value;
    9.12  
    9.13      if (priority == SDL_THREAD_PRIORITY_LOW) {
    9.14 -        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_LOWEST);
    9.15 +        value = THREAD_PRIORITY_LOWEST;
    9.16      } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
    9.17 -        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_HIGHEST);
    9.18 +        value = THREAD_PRIORITY_HIGHEST;
    9.19      } else {
    9.20 -        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_NORMAL);
    9.21 +        value = THREAD_PRIORITY_NORMAL;
    9.22      }
    9.23 -    if (!result) {
    9.24 +    if (!SetThreadPriority(GetCurrentThread(), value)) {
    9.25          WIN_SetError("SetThreadPriority()");
    9.26          return -1;
    9.27      }