From f521b22eb509b3f436b9af9441185430a7627d1e Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 23 Apr 2018 22:07:56 -0700 Subject: [PATCH] Added SDL_THREAD_PRIORITY_TIME_CRITICAL --- include/SDL_thread.h | 5 +++-- src/audio/SDL_audio.c | 2 +- src/thread/psp/SDL_systhread.c | 2 ++ src/thread/pthread/SDL_systhread.c | 7 ++++++- src/thread/windows/SDL_systhread.c | 2 ++ test/testthread.c | 3 ++- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/SDL_thread.h b/include/SDL_thread.h index 82a43fc032af0..5d01d5a52c86f 100644 --- a/include/SDL_thread.h +++ b/include/SDL_thread.h @@ -54,12 +54,13 @@ typedef unsigned int SDL_TLSID; /** * 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 { SDL_THREAD_PRIORITY_LOW, SDL_THREAD_PRIORITY_NORMAL, - SDL_THREAD_PRIORITY_HIGH + SDL_THREAD_PRIORITY_HIGH, + SDL_THREAD_PRIORITY_TIME_CRITICAL } SDL_ThreadPriority; /** diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index 50ce43cb9f197..364e25e3ca63f 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -651,7 +651,7 @@ SDL_RunAudio(void *devicep) SDL_assert(!device->iscapture); /* 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 */ device->threadid = SDL_ThreadID(); diff --git a/src/thread/psp/SDL_systhread.c b/src/thread/psp/SDL_systhread.c index 9286be59e23bc..284f182a6a299 100644 --- a/src/thread/psp/SDL_systhread.c +++ b/src/thread/psp/SDL_systhread.c @@ -97,6 +97,8 @@ int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) if (priority == SDL_THREAD_PRIORITY_LOW) { value = 19; } else if (priority == SDL_THREAD_PRIORITY_HIGH) { + value = -10; + } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) { value = -20; } else { value = 0; diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c index adb7b77bc70cc..4842d9c80be44 100644 --- a/src/thread/pthread/SDL_systhread.c +++ b/src/thread/pthread/SDL_systhread.c @@ -275,6 +275,8 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) if (priority == SDL_THREAD_PRIORITY_LOW) { value = 19; } else if (priority == SDL_THREAD_PRIORITY_HIGH) { + value = -10; + } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) { value = -20; } else { value = 0; @@ -290,12 +292,15 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) } if (priority == SDL_THREAD_PRIORITY_LOW) { 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); } else { int min_priority = sched_get_priority_min(policy); int max_priority = sched_get_priority_max(policy); 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) { return SDL_SetError("pthread_setschedparam() failed"); diff --git a/src/thread/windows/SDL_systhread.c b/src/thread/windows/SDL_systhread.c index 90036c92077e1..251510d48035d 100644 --- a/src/thread/windows/SDL_systhread.c +++ b/src/thread/windows/SDL_systhread.c @@ -231,6 +231,8 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) value = THREAD_PRIORITY_LOWEST; } else if (priority == SDL_THREAD_PRIORITY_HIGH) { value = THREAD_PRIORITY_HIGHEST; + } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) { + value = THREAD_PRIORITY_TIME_CRITICAL; } else { value = THREAD_PRIORITY_NORMAL; } diff --git a/test/testthread.c b/test/testthread.c index efba681e8a6c4..4555a1e945747 100644 --- a/test/testthread.c +++ b/test/testthread.c @@ -38,6 +38,7 @@ getprioritystr(SDL_ThreadPriority priority) case SDL_THREAD_PRIORITY_LOW: return "SDL_THREAD_PRIORITY_LOW"; case SDL_THREAD_PRIORITY_NORMAL: return "SDL_THREAD_PRIORITY_NORMAL"; case SDL_THREAD_PRIORITY_HIGH: return "SDL_THREAD_PRIORITY_HIGH"; + case SDL_THREAD_PRIORITY_TIME_CRITICAL: return "SDL_THREAD_PRIORITY_TIME_CRITICAL"; } return "???"; @@ -56,7 +57,7 @@ ThreadFunc(void *data) if (testprio) { 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; }