Commit bca33709 authored by Sam Lantinga's avatar Sam Lantinga

Implemented SDL_SetThreadPriority()

parent 0b1225e3
...@@ -50,6 +50,16 @@ typedef struct SDL_Thread SDL_Thread; ...@@ -50,6 +50,16 @@ typedef struct SDL_Thread SDL_Thread;
/* The SDL thread ID */ /* The SDL thread ID */
typedef unsigned long SDL_threadID; 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() /* The function passed to SDL_CreateThread()
It is passed a void* user context parameter and returns an int. It is passed a void* user context parameter and returns an int.
*/ */
...@@ -146,6 +156,11 @@ extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void); ...@@ -146,6 +156,11 @@ 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
*/
extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority);
/** /**
* Wait for a thread to finish. * Wait for a thread to finish.
* *
......
...@@ -974,6 +974,7 @@ open_audio_device(const char *devname, int iscapture, ...@@ -974,6 +974,7 @@ 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,6 +43,9 @@ extern int SDL_SYS_CreateThread(SDL_Thread * thread, void *args); ...@@ -43,6 +43,9 @@ 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 */
extern int SDL_SYS_SetThreadPriority(SDL_Thread * thread, 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()
*/ */
......
...@@ -281,19 +281,6 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data) ...@@ -281,19 +281,6 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data)
return (thread); return (thread);
} }
void
SDL_WaitThread(SDL_Thread * thread, int *status)
{
if (thread) {
SDL_SYS_WaitThread(thread);
if (status) {
*status = thread->status;
}
SDL_DelThread(thread);
SDL_free(thread);
}
}
SDL_threadID SDL_threadID
SDL_GetThreadID(SDL_Thread * thread) SDL_GetThreadID(SDL_Thread * thread)
{ {
...@@ -307,4 +294,27 @@ SDL_GetThreadID(SDL_Thread * thread) ...@@ -307,4 +294,27 @@ SDL_GetThreadID(SDL_Thread * thread)
return id; 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)
{
if (thread) {
SDL_SYS_WaitThread(thread);
if (status) {
*status = thread->status;
}
SDL_DelThread(thread);
SDL_free(thread);
}
}
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */
...@@ -90,6 +90,20 @@ SDL_ThreadID(void) ...@@ -90,6 +90,20 @@ SDL_ThreadID(void)
return ((SDL_threadID) find_thread(NULL)); 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 void
SDL_SYS_WaitThread(SDL_Thread * thread) SDL_SYS_WaitThread(SDL_Thread * thread)
{ {
......
...@@ -45,6 +45,12 @@ SDL_ThreadID(void) ...@@ -45,6 +45,12 @@ SDL_ThreadID(void)
return (0); return (0);
} }
int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
{
return (0);
}
void void
SDL_SYS_WaitThread(SDL_Thread * thread) SDL_SYS_WaitThread(SDL_Thread * thread)
{ {
......
...@@ -227,3 +227,5 @@ SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex) ...@@ -227,3 +227,5 @@ SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
{ {
return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT); return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
} }
/* vi: set ts=4 sw=4 expandtab: */
...@@ -140,3 +140,5 @@ SDL_mutexV(SDL_mutex * mutex) ...@@ -140,3 +140,5 @@ SDL_mutexV(SDL_mutex * mutex)
return 0; return 0;
#endif /* DISABLE_THREADS */ #endif /* DISABLE_THREADS */
} }
/* vi: set ts=4 sw=4 expandtab: */
...@@ -226,3 +226,5 @@ SDL_SemPost(SDL_sem * sem) ...@@ -226,3 +226,5 @@ SDL_SemPost(SDL_sem * sem)
} }
#endif /* DISABLE_THREADS */ #endif /* DISABLE_THREADS */
/* vi: set ts=4 sw=4 expandtab: */
...@@ -56,8 +56,16 @@ SDL_SYS_WaitThread(SDL_Thread * thread) ...@@ -56,8 +56,16 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
return; return;
} }
int
SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
{
return (0);
}
void void
SDL_SYS_KillThread(SDL_Thread * thread) SDL_SYS_KillThread(SDL_Thread * thread)
{ {
return; return;
} }
/* vi: set ts=4 sw=4 expandtab: */
...@@ -91,6 +91,33 @@ SDL_ThreadID(void) ...@@ -91,6 +91,33 @@ SDL_ThreadID(void)
return ((SDL_threadID) pthread_self()); 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 void
SDL_SYS_WaitThread(SDL_Thread * thread) SDL_SYS_WaitThread(SDL_Thread * thread)
{ {
......
...@@ -155,6 +155,25 @@ SDL_ThreadID(void) ...@@ -155,6 +155,25 @@ SDL_ThreadID(void)
return ((SDL_threadID) GetCurrentThreadId()); 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 void
SDL_SYS_WaitThread(SDL_Thread * thread) SDL_SYS_WaitThread(SDL_Thread * thread)
{ {
......
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