Added SDL_THREAD_PRIORITY_TIME_CRITICAL

Sam Lantinga 2018-04-23 22:07:56 -07:00
parent db94dfb1d5
commit f521b22eb5
6 changed files with 16 additions and 5 deletions

View File

@ -54,12 +54,13 @@ typedef unsigned int SDL_TLSID;
/** /**
* The SDL thread priority. * The SDL thread priority.
* *
* \note On many systems you require special privileges to set high priority. * \note On many systems you require special privileges to set high or time critical priority.
*/ */
typedef enum { typedef enum {
SDL_THREAD_PRIORITY_LOW, SDL_THREAD_PRIORITY_LOW,
SDL_THREAD_PRIORITY_NORMAL, SDL_THREAD_PRIORITY_NORMAL,
SDL_THREAD_PRIORITY_HIGH SDL_THREAD_PRIORITY_HIGH,
SDL_THREAD_PRIORITY_TIME_CRITICAL
} SDL_ThreadPriority; } SDL_ThreadPriority;
/** /**

View File

@ -651,7 +651,7 @@ SDL_RunAudio(void *devicep)
SDL_assert(!device->iscapture); SDL_assert(!device->iscapture);
/* The audio mixing is always a high priority thread */ /* The audio mixing is always a high priority thread */
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); SDL_SetThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
/* Perform any thread setup */ /* Perform any thread setup */
device->threadid = SDL_ThreadID(); device->threadid = SDL_ThreadID();

View File

@ -97,6 +97,8 @@ int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
if (priority == SDL_THREAD_PRIORITY_LOW) { if (priority == SDL_THREAD_PRIORITY_LOW) {
value = 19; value = 19;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) { } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
value = -10;
} else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
value = -20; value = -20;
} else { } else {
value = 0; value = 0;

View File

@ -275,6 +275,8 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
if (priority == SDL_THREAD_PRIORITY_LOW) { if (priority == SDL_THREAD_PRIORITY_LOW) {
value = 19; value = 19;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) { } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
value = -10;
} else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
value = -20; value = -20;
} else { } else {
value = 0; value = 0;
@ -290,12 +292,15 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
} }
if (priority == SDL_THREAD_PRIORITY_LOW) { if (priority == SDL_THREAD_PRIORITY_LOW) {
sched.sched_priority = sched_get_priority_min(policy); sched.sched_priority = sched_get_priority_min(policy);
} else if (priority == SDL_THREAD_PRIORITY_HIGH) { } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
sched.sched_priority = sched_get_priority_max(policy); sched.sched_priority = sched_get_priority_max(policy);
} 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);
sched.sched_priority = (min_priority + (max_priority - min_priority) / 2); sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
if (priority == SDL_THREAD_PRIORITY_HIGH) {
sched.sched_priority += (max_priority - min_priority) / 4);
}
} }
if (pthread_setschedparam(thread, policy, &sched) != 0) { if (pthread_setschedparam(thread, policy, &sched) != 0) {
return SDL_SetError("pthread_setschedparam() failed"); return SDL_SetError("pthread_setschedparam() failed");

View File

@ -231,6 +231,8 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
value = THREAD_PRIORITY_LOWEST; value = THREAD_PRIORITY_LOWEST;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) { } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
value = THREAD_PRIORITY_HIGHEST; value = THREAD_PRIORITY_HIGHEST;
} else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
value = THREAD_PRIORITY_TIME_CRITICAL;
} else { } else {
value = THREAD_PRIORITY_NORMAL; value = THREAD_PRIORITY_NORMAL;
} }

View File

@ -38,6 +38,7 @@ getprioritystr(SDL_ThreadPriority priority)
case SDL_THREAD_PRIORITY_LOW: return "SDL_THREAD_PRIORITY_LOW"; case SDL_THREAD_PRIORITY_LOW: return "SDL_THREAD_PRIORITY_LOW";
case SDL_THREAD_PRIORITY_NORMAL: return "SDL_THREAD_PRIORITY_NORMAL"; case SDL_THREAD_PRIORITY_NORMAL: return "SDL_THREAD_PRIORITY_NORMAL";
case SDL_THREAD_PRIORITY_HIGH: return "SDL_THREAD_PRIORITY_HIGH"; case SDL_THREAD_PRIORITY_HIGH: return "SDL_THREAD_PRIORITY_HIGH";
case SDL_THREAD_PRIORITY_TIME_CRITICAL: return "SDL_THREAD_PRIORITY_TIME_CRITICAL";
} }
return "???"; return "???";
@ -56,7 +57,7 @@ ThreadFunc(void *data)
if (testprio) { if (testprio) {
SDL_Log("SDL_SetThreadPriority(%s):%d\n", getprioritystr(prio), SDL_SetThreadPriority(prio)); SDL_Log("SDL_SetThreadPriority(%s):%d\n", getprioritystr(prio), SDL_SetThreadPriority(prio));
if (++prio > SDL_THREAD_PRIORITY_HIGH) if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL)
prio = SDL_THREAD_PRIORITY_LOW; prio = SDL_THREAD_PRIORITY_LOW;
} }