Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
The API sets the priority for the current thread, not an arbitrary th…
Browse files Browse the repository at this point in the history
…read.

Implemented thread priority as the 'nice' value on Linux.  High priority threads require root permissions (you shouldn't give your game root permissions though!)
  • Loading branch information
slouken committed Mar 25, 2011
1 parent 19ad193 commit c49ab1b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 28 deletions.
4 changes: 2 additions & 2 deletions include/SDL_thread.h
Expand Up @@ -157,9 +157,9 @@ extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);

/**
* Set the thread priority
* Set the priority for the current thread
*/
extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority);
extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);

/**
* Wait for a thread to finish.
Expand Down
4 changes: 3 additions & 1 deletion src/audio/SDL_audio.c
Expand Up @@ -329,6 +329,9 @@ SDL_RunAudio(void *devicep)
int silence;
Uint32 delay;

/* The audio mixing is always a high priority thread */
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);

/* For streaming when the buffer sizes don't match up */
Uint8 *istream;
int istream_len = 0;
Expand Down Expand Up @@ -974,7 +977,6 @@ open_audio_device(const char *devname, int iscapture,
SDL_SetError("Couldn't create audio thread");
return 0;
}
SDL_SetThreadPriority(device->thread, SDL_THREAD_PRIORITY_HIGH);
}

return id + 1;
Expand Down
4 changes: 2 additions & 2 deletions src/thread/SDL_systhread.h
Expand Up @@ -43,8 +43,8 @@ extern int SDL_SYS_CreateThread(SDL_Thread * thread, void *args);
/* 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 sets the current thread priority */
extern int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority);

/* This function waits for the thread to finish and frees any data
allocated by SDL_SYS_CreateThread()
Expand Down
8 changes: 2 additions & 6 deletions src/thread/SDL_thread.c
Expand Up @@ -295,13 +295,9 @@ SDL_GetThreadID(SDL_Thread * thread)
}

int
SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
SDL_SetThreadPriority(SDL_ThreadPriority priority)
{
if (!thread) {
SDL_SetError("SDL_SetThreadPriority() passed NULL thread");
return -1;
}
return SDL_SYS_SetThreadPriority(thread, priority);
return SDL_SYS_SetThreadPriority(priority);
}

void
Expand Down
10 changes: 6 additions & 4 deletions src/thread/beos/SDL_systhread.c
Expand Up @@ -93,14 +93,16 @@ SDL_ThreadID(void)
int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
{
int32 new_priority = B_NORMAL_PRIORITY;
int32 value;

if (priority == SDL_THREAD_PRIORITY_LOW) {
new_priority = B_LOW_PRIORITY;
value = B_LOW_PRIORITY;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) {
new_priority = B_URGENT_DISPLAY_PRIORITY;
value = B_URGENT_DISPLAY_PRIORITY;
} else {
value = B_NORMAL_PRIORITY;
}
set_thread_priority(thread->handle, new_priority);
set_thread_priority(find_thread(NULL), value);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/thread/generic/SDL_systhread.c
Expand Up @@ -46,7 +46,7 @@ SDL_ThreadID(void)
}

int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
return (0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/thread/nds/SDL_systhread.c
Expand Up @@ -57,7 +57,7 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
}

int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
return (0);
}
Expand Down
33 changes: 28 additions & 5 deletions src/thread/pthread/SDL_systhread.c
Expand Up @@ -23,6 +23,11 @@

#include <pthread.h>
#include <signal.h>
#ifdef linux
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#endif

#include "SDL_thread.h"
#include "../SDL_thread_c.h"
Expand All @@ -34,6 +39,7 @@ static const int sig_list[] = {
SIGVTALRM, SIGPROF, 0
};


static void *
RunThread(void *data)
{
Expand Down Expand Up @@ -92,12 +98,29 @@ SDL_ThreadID(void)
}

int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
#ifdef linux
int value;

if (priority == SDL_THREAD_PRIORITY_LOW) {
value = 19;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) {
value = -20;
} else {
value = 0;
}
if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
SDL_SetError("setpriority() failed");
return -1;
}
return 0;
#else
struct sched_param sched;
int policy;
pthread_t thread = pthread_self();

if (pthread_getschedparam(thread->handle, &policy, &sched) < 0) {
if (pthread_getschedparam(thread, &policy, &sched) < 0) {
SDL_SetError("pthread_getschedparam() failed");
return -1;
}
Expand All @@ -108,14 +131,14 @@ SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
} 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;
sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
}
if (pthread_setschedparam(thread->handle, policy, &sched) < 0) {
if (pthread_setschedparam(thread, policy, &sched) < 0) {
SDL_SetError("pthread_setschedparam() failed");
return -1;
}
return 0;
#endif /* linux */
}

void
Expand Down
12 changes: 6 additions & 6 deletions src/thread/windows/SDL_systhread.c
Expand Up @@ -156,18 +156,18 @@ SDL_ThreadID(void)
}

int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
BOOL result;
int value;

if (priority == SDL_THREAD_PRIORITY_LOW) {
result = SetThreadPriority(thread->handle, THREAD_PRIORITY_LOWEST);
value = THREAD_PRIORITY_LOWEST;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) {
result = SetThreadPriority(thread->handle, THREAD_PRIORITY_HIGHEST);
value = THREAD_PRIORITY_HIGHEST;
} else {
result = SetThreadPriority(thread->handle, THREAD_PRIORITY_NORMAL);
value = THREAD_PRIORITY_NORMAL;
}
if (!result) {
if (!SetThreadPriority(GetCurrentThread(), value)) {
WIN_SetError("SetThreadPriority()");
return -1;
}
Expand Down

0 comments on commit c49ab1b

Please sign in to comment.