Implemented SDL_SetThreadPriority()
authorSam Lantinga <slouken@libsdl.org>
Fri, 25 Mar 2011 10:47:49 -0700
changeset 550682a09d5d0f07
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
     1.1 --- a/include/SDL_thread.h	Fri Mar 25 10:26:25 2011 -0700
     1.2 +++ b/include/SDL_thread.h	Fri Mar 25 10:47:49 2011 -0700
     1.3 @@ -50,6 +50,16 @@
     1.4  /* The SDL thread ID */
     1.5  typedef unsigned long SDL_threadID;
     1.6  
     1.7 +/* The SDL thread priority
     1.8 + *
     1.9 + * Note: On many systems you require special privileges to set high priority.
    1.10 + */
    1.11 +typedef enum {
    1.12 +    SDL_THREAD_PRIORITY_LOW,
    1.13 +    SDL_THREAD_PRIORITY_NORMAL,
    1.14 +    SDL_THREAD_PRIORITY_HIGH
    1.15 +} SDL_ThreadPriority;
    1.16 +
    1.17  /* The function passed to SDL_CreateThread()
    1.18     It is passed a void* user context parameter and returns an int.
    1.19   */
    1.20 @@ -147,6 +157,11 @@
    1.21  extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
    1.22  
    1.23  /**
    1.24 + *  Set the thread priority
    1.25 + */
    1.26 +extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority);
    1.27 +
    1.28 +/**
    1.29   *  Wait for a thread to finish.
    1.30   *  
    1.31   *  The return code for the thread function is placed in the area
     2.1 --- a/src/audio/SDL_audio.c	Fri Mar 25 10:26:25 2011 -0700
     2.2 +++ b/src/audio/SDL_audio.c	Fri Mar 25 10:47:49 2011 -0700
     2.3 @@ -974,6 +974,7 @@
     2.4              SDL_SetError("Couldn't create audio thread");
     2.5              return 0;
     2.6          }
     2.7 +        SDL_SetThreadPriority(device->thread, SDL_THREAD_PRIORITY_HIGH);
     2.8      }
     2.9  
    2.10      return id + 1;
     3.1 --- a/src/thread/SDL_systhread.h	Fri Mar 25 10:26:25 2011 -0700
     3.2 +++ b/src/thread/SDL_systhread.h	Fri Mar 25 10:47:49 2011 -0700
     3.3 @@ -43,6 +43,9 @@
     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 +
    3.10  /* This function waits for the thread to finish and frees any data
    3.11     allocated by SDL_SYS_CreateThread()
    3.12   */
     4.1 --- a/src/thread/SDL_thread.c	Fri Mar 25 10:26:25 2011 -0700
     4.2 +++ b/src/thread/SDL_thread.c	Fri Mar 25 10:47:49 2011 -0700
     4.3 @@ -281,6 +281,29 @@
     4.4      return (thread);
     4.5  }
     4.6  
     4.7 +SDL_threadID
     4.8 +SDL_GetThreadID(SDL_Thread * thread)
     4.9 +{
    4.10 +    SDL_threadID id;
    4.11 +
    4.12 +    if (thread) {
    4.13 +        id = thread->threadid;
    4.14 +    } else {
    4.15 +        id = SDL_ThreadID();
    4.16 +    }
    4.17 +    return id;
    4.18 +}
    4.19 +
    4.20 +int
    4.21 +SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
    4.22 +{
    4.23 +    if (!thread) {
    4.24 +        SDL_SetError("SDL_SetThreadPriority() passed NULL thread");
    4.25 +        return -1;
    4.26 +    }
    4.27 +    return SDL_SYS_SetThreadPriority(thread, priority);
    4.28 +}
    4.29 +
    4.30  void
    4.31  SDL_WaitThread(SDL_Thread * thread, int *status)
    4.32  {
    4.33 @@ -294,17 +317,4 @@
    4.34      }
    4.35  }
    4.36  
    4.37 -SDL_threadID
    4.38 -SDL_GetThreadID(SDL_Thread * thread)
    4.39 -{
    4.40 -    SDL_threadID id;
    4.41 -
    4.42 -    if (thread) {
    4.43 -        id = thread->threadid;
    4.44 -    } else {
    4.45 -        id = SDL_ThreadID();
    4.46 -    }
    4.47 -    return id;
    4.48 -}
    4.49 -
    4.50  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/thread/beos/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
     5.2 +++ b/src/thread/beos/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
     5.3 @@ -90,6 +90,20 @@
     5.4      return ((SDL_threadID) find_thread(NULL));
     5.5  }
     5.6  
     5.7 +int
     5.8 +SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
     5.9 +{
    5.10 +    int32 new_priority = B_NORMAL_PRIORITY;
    5.11 +
    5.12 +    if (priority == SDL_THREAD_PRIORITY_LOW) {
    5.13 +        new_priority = B_LOW_PRIORITY;
    5.14 +    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
    5.15 +        new_priority = B_URGENT_DISPLAY_PRIORITY;
    5.16 +    }
    5.17 +    set_thread_priority(thread->handle, new_priority);
    5.18 +    return 0;
    5.19 +}
    5.20 +
    5.21  void
    5.22  SDL_SYS_WaitThread(SDL_Thread * thread)
    5.23  {
     6.1 --- a/src/thread/generic/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
     6.2 +++ b/src/thread/generic/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
     6.3 @@ -45,6 +45,12 @@
     6.4      return (0);
     6.5  }
     6.6  
     6.7 +int
     6.8 +SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
     6.9 +{
    6.10 +    return (0);
    6.11 +}
    6.12 +
    6.13  void
    6.14  SDL_SYS_WaitThread(SDL_Thread * thread)
    6.15  {
     7.1 --- a/src/thread/nds/SDL_syscond.c	Fri Mar 25 10:26:25 2011 -0700
     7.2 +++ b/src/thread/nds/SDL_syscond.c	Fri Mar 25 10:47:49 2011 -0700
     7.3 @@ -227,3 +227,5 @@
     7.4  {
     7.5      return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
     7.6  }
     7.7 +
     7.8 +/* vi: set ts=4 sw=4 expandtab: */
     8.1 --- a/src/thread/nds/SDL_sysmutex.c	Fri Mar 25 10:26:25 2011 -0700
     8.2 +++ b/src/thread/nds/SDL_sysmutex.c	Fri Mar 25 10:47:49 2011 -0700
     8.3 @@ -140,3 +140,5 @@
     8.4      return 0;
     8.5  #endif /* DISABLE_THREADS */
     8.6  }
     8.7 +
     8.8 +/* vi: set ts=4 sw=4 expandtab: */
     9.1 --- a/src/thread/nds/SDL_syssem.c	Fri Mar 25 10:26:25 2011 -0700
     9.2 +++ b/src/thread/nds/SDL_syssem.c	Fri Mar 25 10:47:49 2011 -0700
     9.3 @@ -226,3 +226,5 @@
     9.4  }
     9.5  
     9.6  #endif /* DISABLE_THREADS */
     9.7 +
     9.8 +/* vi: set ts=4 sw=4 expandtab: */
    10.1 --- a/src/thread/nds/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
    10.2 +++ b/src/thread/nds/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
    10.3 @@ -56,8 +56,16 @@
    10.4      return;
    10.5  }
    10.6  
    10.7 +int
    10.8 +SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
    10.9 +{
   10.10 +    return (0);
   10.11 +}
   10.12 +
   10.13  void
   10.14  SDL_SYS_KillThread(SDL_Thread * thread)
   10.15  {
   10.16      return;
   10.17  }
   10.18 +
   10.19 +/* vi: set ts=4 sw=4 expandtab: */
    11.1 --- a/src/thread/pthread/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
    11.2 +++ b/src/thread/pthread/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
    11.3 @@ -91,6 +91,33 @@
    11.4      return ((SDL_threadID) pthread_self());
    11.5  }
    11.6  
    11.7 +int
    11.8 +SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
    11.9 +{
   11.10 +    struct sched_param sched;
   11.11 +    int policy;
   11.12 +
   11.13 +    if (pthread_getschedparam(thread->handle, &policy, &sched) < 0) {
   11.14 +        SDL_SetError("pthread_getschedparam() failed");
   11.15 +        return -1;
   11.16 +    }
   11.17 +    if (priority == SDL_THREAD_PRIORITY_LOW) {
   11.18 +        sched.sched_priority = sched_get_priority_min(policy);
   11.19 +    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
   11.20 +        sched.sched_priority = sched_get_priority_max(policy);
   11.21 +    } else {
   11.22 +        int min_priority = sched_get_priority_min(policy);
   11.23 +        int max_priority = sched_get_priority_max(policy);
   11.24 +        int priority = (min_priority + (max_priority - min_priority) / 2);
   11.25 +        sched.sched_priority = priority;
   11.26 +    }
   11.27 +    if (pthread_setschedparam(thread->handle, policy, &sched) < 0) {
   11.28 +        SDL_SetError("pthread_setschedparam() failed");
   11.29 +        return -1;
   11.30 +    }
   11.31 +    return 0;
   11.32 +}
   11.33 +
   11.34  void
   11.35  SDL_SYS_WaitThread(SDL_Thread * thread)
   11.36  {
    12.1 --- a/src/thread/windows/SDL_systhread.c	Fri Mar 25 10:26:25 2011 -0700
    12.2 +++ b/src/thread/windows/SDL_systhread.c	Fri Mar 25 10:47:49 2011 -0700
    12.3 @@ -155,6 +155,25 @@
    12.4      return ((SDL_threadID) GetCurrentThreadId());
    12.5  }
    12.6  
    12.7 +int
    12.8 +SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
    12.9 +{
   12.10 +    BOOL result;
   12.11 +
   12.12 +    if (priority == SDL_THREAD_PRIORITY_LOW) {
   12.13 +        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_LOWEST);
   12.14 +    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
   12.15 +        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_HIGHEST);
   12.16 +    } else {
   12.17 +        result = SetThreadPriority(thread->handle, THREAD_PRIORITY_NORMAL);
   12.18 +    }
   12.19 +    if (!result) {
   12.20 +        WIN_SetError("SetThreadPriority()");
   12.21 +        return -1;
   12.22 +    }
   12.23 +    return 0;
   12.24 +}
   12.25 +
   12.26  void
   12.27  SDL_SYS_WaitThread(SDL_Thread * thread)
   12.28  {