Commit f0b1c9b8 authored by Sam Lantinga's avatar Sam Lantinga

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!)
parent fb417a9d
...@@ -157,9 +157,9 @@ extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void); ...@@ -157,9 +157,9 @@ extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread); 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. * Wait for a thread to finish.
......
...@@ -329,6 +329,9 @@ SDL_RunAudio(void *devicep) ...@@ -329,6 +329,9 @@ SDL_RunAudio(void *devicep)
int silence; int silence;
Uint32 delay; 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 */ /* For streaming when the buffer sizes don't match up */
Uint8 *istream; Uint8 *istream;
int istream_len = 0; int istream_len = 0;
...@@ -974,7 +977,6 @@ open_audio_device(const char *devname, int iscapture, ...@@ -974,7 +977,6 @@ open_audio_device(const char *devname, int iscapture,
SDL_SetError("Couldn't create audio thread"); SDL_SetError("Couldn't create audio thread");
return 0; return 0;
} }
SDL_SetThreadPriority(device->thread, SDL_THREAD_PRIORITY_HIGH);
} }
return id + 1; return id + 1;
......
...@@ -43,8 +43,8 @@ extern int SDL_SYS_CreateThread(SDL_Thread * thread, void *args); ...@@ -43,8 +43,8 @@ extern int SDL_SYS_CreateThread(SDL_Thread * thread, void *args);
/* This function does any necessary setup in the child thread */ /* This function does any necessary setup in the child thread */
extern void SDL_SYS_SetupThread(void); extern void SDL_SYS_SetupThread(void);
/* This function sets thread priority */ /* This function sets the current thread priority */
extern int SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority); extern int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority);
/* This function waits for the thread to finish and frees any data /* This function waits for the thread to finish and frees any data
allocated by SDL_SYS_CreateThread() allocated by SDL_SYS_CreateThread()
......
...@@ -295,13 +295,9 @@ SDL_GetThreadID(SDL_Thread * thread) ...@@ -295,13 +295,9 @@ SDL_GetThreadID(SDL_Thread * thread)
} }
int int
SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority) SDL_SetThreadPriority(SDL_ThreadPriority priority)
{ {
if (!thread) { return SDL_SYS_SetThreadPriority(priority);
SDL_SetError("SDL_SetThreadPriority() passed NULL thread");
return -1;
}
return SDL_SYS_SetThreadPriority(thread, priority);
} }
void void
......
...@@ -93,14 +93,16 @@ SDL_ThreadID(void) ...@@ -93,14 +93,16 @@ SDL_ThreadID(void)
int int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority) SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
{ {
int32 new_priority = B_NORMAL_PRIORITY; int32 value;
if (priority == SDL_THREAD_PRIORITY_LOW) { if (priority == SDL_THREAD_PRIORITY_LOW) {
new_priority = B_LOW_PRIORITY; value = B_LOW_PRIORITY;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) { } 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; return 0;
} }
......
...@@ -46,7 +46,7 @@ SDL_ThreadID(void) ...@@ -46,7 +46,7 @@ SDL_ThreadID(void)
} }
int int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority) SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{ {
return (0); return (0);
} }
......
...@@ -57,7 +57,7 @@ SDL_SYS_WaitThread(SDL_Thread * thread) ...@@ -57,7 +57,7 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
} }
int int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority) SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{ {
return (0); return (0);
} }
......
...@@ -23,6 +23,11 @@ ...@@ -23,6 +23,11 @@
#include <pthread.h> #include <pthread.h>
#include <signal.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.h"
#include "../SDL_thread_c.h" #include "../SDL_thread_c.h"
...@@ -34,6 +39,7 @@ static const int sig_list[] = { ...@@ -34,6 +39,7 @@ static const int sig_list[] = {
SIGVTALRM, SIGPROF, 0 SIGVTALRM, SIGPROF, 0
}; };
static void * static void *
RunThread(void *data) RunThread(void *data)
{ {
...@@ -92,12 +98,29 @@ SDL_ThreadID(void) ...@@ -92,12 +98,29 @@ SDL_ThreadID(void)
} }
int 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; struct sched_param sched;
int policy; 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"); SDL_SetError("pthread_getschedparam() failed");
return -1; return -1;
} }
...@@ -108,14 +131,14 @@ SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority) ...@@ -108,14 +131,14 @@ SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
} else { } else {
int min_priority = sched_get_priority_min(policy); int min_priority = sched_get_priority_min(policy);
int max_priority = sched_get_priority_max(policy); int max_priority = sched_get_priority_max(policy);
int priority = (min_priority + (max_priority - min_priority) / 2); sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
sched.sched_priority = priority;
} }
if (pthread_setschedparam(thread->handle, policy, &sched) < 0) { if (pthread_setschedparam(thread, policy, &sched) < 0) {
SDL_SetError("pthread_setschedparam() failed"); SDL_SetError("pthread_setschedparam() failed");
return -1; return -1;
} }
return 0; return 0;
#endif /* linux */
} }
void void
......
...@@ -156,18 +156,18 @@ SDL_ThreadID(void) ...@@ -156,18 +156,18 @@ SDL_ThreadID(void)
} }
int 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) { if (priority == SDL_THREAD_PRIORITY_LOW) {
result = SetThreadPriority(thread->handle, THREAD_PRIORITY_LOWEST); value = THREAD_PRIORITY_LOWEST;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) { } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
result = SetThreadPriority(thread->handle, THREAD_PRIORITY_HIGHEST); value = THREAD_PRIORITY_HIGHEST;
} else { } else {
result = SetThreadPriority(thread->handle, THREAD_PRIORITY_NORMAL); value = THREAD_PRIORITY_NORMAL;
} }
if (!result) { if (!SetThreadPriority(GetCurrentThread(), value)) {
WIN_SetError("SetThreadPriority()"); WIN_SetError("SetThreadPriority()");
return -1; return -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment