diff options
| author | 2015-04-09 23:05:49 -0400 | |
|---|---|---|
| committer | 2015-04-09 23:05:49 -0400 | |
| commit | 6f1143885bcc02642b707b51355fe4b6cd5375c7 (patch) | |
| tree | fe1307919e7087df41c498b971016ffa931d6594 /src/core/hle/kernel | |
| parent | Merge pull request #690 from Zaneo/sharedmemory (diff) | |
| parent | SVC: Assert on unsupported CreateThread processor ID. (diff) | |
| download | yuzu-6f1143885bcc02642b707b51355fe4b6cd5375c7.tar.gz yuzu-6f1143885bcc02642b707b51355fe4b6cd5375c7.tar.xz yuzu-6f1143885bcc02642b707b51355fe4b6cd5375c7.zip | |
Merge pull request #683 from bunnei/thread-priority
Thread priority and scheduler improvements
Diffstat (limited to 'src/core/hle/kernel')
| -rw-r--r-- | src/core/hle/kernel/address_arbiter.cpp | 4 | ||||
| -rw-r--r-- | src/core/hle/kernel/kernel.cpp | 2 | ||||
| -rw-r--r-- | src/core/hle/kernel/mutex.cpp | 10 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.cpp | 50 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.h | 32 |
5 files changed, 70 insertions, 28 deletions
diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp index 42f8ce2d9..19135266c 100644 --- a/src/core/hle/kernel/address_arbiter.cpp +++ b/src/core/hle/kernel/address_arbiter.cpp | |||
| @@ -46,14 +46,12 @@ ResultCode AddressArbiter::ArbitrateAddress(ArbitrationType type, VAddr address, | |||
| 46 | case ArbitrationType::WaitIfLessThan: | 46 | case ArbitrationType::WaitIfLessThan: |
| 47 | if ((s32)Memory::Read32(address) <= value) { | 47 | if ((s32)Memory::Read32(address) <= value) { |
| 48 | Kernel::WaitCurrentThread_ArbitrateAddress(address); | 48 | Kernel::WaitCurrentThread_ArbitrateAddress(address); |
| 49 | HLE::Reschedule(__func__); | ||
| 50 | } | 49 | } |
| 51 | break; | 50 | break; |
| 52 | case ArbitrationType::WaitIfLessThanWithTimeout: | 51 | case ArbitrationType::WaitIfLessThanWithTimeout: |
| 53 | if ((s32)Memory::Read32(address) <= value) { | 52 | if ((s32)Memory::Read32(address) <= value) { |
| 54 | Kernel::WaitCurrentThread_ArbitrateAddress(address); | 53 | Kernel::WaitCurrentThread_ArbitrateAddress(address); |
| 55 | GetCurrentThread()->WakeAfterDelay(nanoseconds); | 54 | GetCurrentThread()->WakeAfterDelay(nanoseconds); |
| 56 | HLE::Reschedule(__func__); | ||
| 57 | } | 55 | } |
| 58 | break; | 56 | break; |
| 59 | case ArbitrationType::DecrementAndWaitIfLessThan: | 57 | case ArbitrationType::DecrementAndWaitIfLessThan: |
| @@ -62,7 +60,6 @@ ResultCode AddressArbiter::ArbitrateAddress(ArbitrationType type, VAddr address, | |||
| 62 | Memory::Write32(address, memory_value); | 60 | Memory::Write32(address, memory_value); |
| 63 | if (memory_value <= value) { | 61 | if (memory_value <= value) { |
| 64 | Kernel::WaitCurrentThread_ArbitrateAddress(address); | 62 | Kernel::WaitCurrentThread_ArbitrateAddress(address); |
| 65 | HLE::Reschedule(__func__); | ||
| 66 | } | 63 | } |
| 67 | break; | 64 | break; |
| 68 | } | 65 | } |
| @@ -73,7 +70,6 @@ ResultCode AddressArbiter::ArbitrateAddress(ArbitrationType type, VAddr address, | |||
| 73 | if (memory_value <= value) { | 70 | if (memory_value <= value) { |
| 74 | Kernel::WaitCurrentThread_ArbitrateAddress(address); | 71 | Kernel::WaitCurrentThread_ArbitrateAddress(address); |
| 75 | GetCurrentThread()->WakeAfterDelay(nanoseconds); | 72 | GetCurrentThread()->WakeAfterDelay(nanoseconds); |
| 76 | HLE::Reschedule(__func__); | ||
| 77 | } | 73 | } |
| 78 | break; | 74 | break; |
| 79 | } | 75 | } |
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 498b2ec98..6261b82b6 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp | |||
| @@ -154,7 +154,7 @@ void Shutdown() { | |||
| 154 | */ | 154 | */ |
| 155 | bool LoadExec(u32 entry_point) { | 155 | bool LoadExec(u32 entry_point) { |
| 156 | // 0x30 is the typical main thread priority I've seen used so far | 156 | // 0x30 is the typical main thread priority I've seen used so far |
| 157 | g_main_thread = Kernel::SetupMainThread(Kernel::DEFAULT_STACK_SIZE, entry_point, 0x30); | 157 | g_main_thread = Kernel::SetupMainThread(Kernel::DEFAULT_STACK_SIZE, entry_point, THREADPRIO_DEFAULT); |
| 158 | 158 | ||
| 159 | return true; | 159 | return true; |
| 160 | } | 160 | } |
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index be2c49706..ebc9e79d7 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp | |||
| @@ -56,7 +56,15 @@ SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) { | |||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | bool Mutex::ShouldWait() { | 58 | bool Mutex::ShouldWait() { |
| 59 | return lock_count > 0 && holding_thread != GetCurrentThread();; | 59 | auto thread = GetCurrentThread(); |
| 60 | bool wait = lock_count > 0 && holding_thread != thread; | ||
| 61 | |||
| 62 | // If the holding thread of the mutex is lower priority than this thread, that thread should | ||
| 63 | // temporarily inherit this thread's priority | ||
| 64 | if (wait && thread->current_priority < holding_thread->current_priority) | ||
| 65 | holding_thread->BoostPriority(thread->current_priority); | ||
| 66 | |||
| 67 | return wait; | ||
| 60 | } | 68 | } |
| 61 | 69 | ||
| 62 | void Mutex::Acquire() { | 70 | void Mutex::Acquire() { |
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index be1aed615..33d66b986 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp | |||
| @@ -140,6 +140,28 @@ void ArbitrateAllThreads(u32 address) { | |||
| 140 | } | 140 | } |
| 141 | } | 141 | } |
| 142 | 142 | ||
| 143 | /// Boost low priority threads (temporarily) that have been starved | ||
| 144 | static void PriorityBoostStarvedThreads() { | ||
| 145 | u64 current_ticks = CoreTiming::GetTicks(); | ||
| 146 | |||
| 147 | for (auto& thread : thread_list) { | ||
| 148 | // TODO(bunnei): Threads that have been waiting to be scheduled for `boost_ticks` (or | ||
| 149 | // longer) will have their priority temporarily adjusted to 1 higher than the highest | ||
| 150 | // priority thread to prevent thread starvation. This general behavior has been verified | ||
| 151 | // on hardware. However, this is almost certainly not perfect, and the real CTR OS scheduler | ||
| 152 | // should probably be reversed to verify this. | ||
| 153 | |||
| 154 | const u64 boost_timeout = 2000000; // Boost threads that have been ready for > this long | ||
| 155 | |||
| 156 | u64 delta = current_ticks - thread->last_running_ticks; | ||
| 157 | |||
| 158 | if (thread->status == THREADSTATUS_READY && delta > boost_timeout && !thread->idle) { | ||
| 159 | const s32 priority = std::max(ready_queue.get_first()->current_priority - 1, 0); | ||
| 160 | thread->BoostPriority(priority); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 143 | /** | 165 | /** |
| 144 | * Switches the CPU's active thread context to that of the specified thread | 166 | * Switches the CPU's active thread context to that of the specified thread |
| 145 | * @param new_thread The thread to switch to | 167 | * @param new_thread The thread to switch to |
| @@ -151,6 +173,7 @@ static void SwitchContext(Thread* new_thread) { | |||
| 151 | 173 | ||
| 152 | // Save context for previous thread | 174 | // Save context for previous thread |
| 153 | if (previous_thread) { | 175 | if (previous_thread) { |
| 176 | previous_thread->last_running_ticks = CoreTiming::GetTicks(); | ||
| 154 | Core::g_app_core->SaveContext(previous_thread->context); | 177 | Core::g_app_core->SaveContext(previous_thread->context); |
| 155 | 178 | ||
| 156 | if (previous_thread->status == THREADSTATUS_RUNNING) { | 179 | if (previous_thread->status == THREADSTATUS_RUNNING) { |
| @@ -168,6 +191,9 @@ static void SwitchContext(Thread* new_thread) { | |||
| 168 | ready_queue.remove(new_thread->current_priority, new_thread); | 191 | ready_queue.remove(new_thread->current_priority, new_thread); |
| 169 | new_thread->status = THREADSTATUS_RUNNING; | 192 | new_thread->status = THREADSTATUS_RUNNING; |
| 170 | 193 | ||
| 194 | // Restores thread to its nominal priority if it has been temporarily changed | ||
| 195 | new_thread->current_priority = new_thread->nominal_priority; | ||
| 196 | |||
| 171 | Core::g_app_core->LoadContext(new_thread->context); | 197 | Core::g_app_core->LoadContext(new_thread->context); |
| 172 | } else { | 198 | } else { |
| 173 | current_thread = nullptr; | 199 | current_thread = nullptr; |
| @@ -364,7 +390,8 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, | |||
| 364 | thread->status = THREADSTATUS_DORMANT; | 390 | thread->status = THREADSTATUS_DORMANT; |
| 365 | thread->entry_point = entry_point; | 391 | thread->entry_point = entry_point; |
| 366 | thread->stack_top = stack_top; | 392 | thread->stack_top = stack_top; |
| 367 | thread->initial_priority = thread->current_priority = priority; | 393 | thread->nominal_priority = thread->current_priority = priority; |
| 394 | thread->last_running_ticks = CoreTiming::GetTicks(); | ||
| 368 | thread->processor_id = processor_id; | 395 | thread->processor_id = processor_id; |
| 369 | thread->wait_set_output = false; | 396 | thread->wait_set_output = false; |
| 370 | thread->wait_all = false; | 397 | thread->wait_all = false; |
| @@ -400,17 +427,15 @@ static void ClampPriority(const Thread* thread, s32* priority) { | |||
| 400 | void Thread::SetPriority(s32 priority) { | 427 | void Thread::SetPriority(s32 priority) { |
| 401 | ClampPriority(this, &priority); | 428 | ClampPriority(this, &priority); |
| 402 | 429 | ||
| 403 | if (current_priority == priority) { | 430 | // If thread was ready, adjust queues |
| 404 | return; | 431 | if (status == THREADSTATUS_READY) |
| 405 | } | 432 | ready_queue.move(this, current_priority, priority); |
| 406 | 433 | ||
| 407 | if (status == THREADSTATUS_READY) { | 434 | nominal_priority = current_priority = priority; |
| 408 | // If thread was ready, adjust queues | 435 | } |
| 409 | ready_queue.remove(current_priority, this); | 436 | |
| 410 | ready_queue.prepare(priority); | 437 | void Thread::BoostPriority(s32 priority) { |
| 411 | ready_queue.push_back(priority, this); | 438 | ready_queue.move(this, current_priority, priority); |
| 412 | } | ||
| 413 | |||
| 414 | current_priority = priority; | 439 | current_priority = priority; |
| 415 | } | 440 | } |
| 416 | 441 | ||
| @@ -440,6 +465,9 @@ SharedPtr<Thread> SetupMainThread(u32 stack_size, u32 entry_point, s32 priority) | |||
| 440 | 465 | ||
| 441 | void Reschedule() { | 466 | void Reschedule() { |
| 442 | Thread* prev = GetCurrentThread(); | 467 | Thread* prev = GetCurrentThread(); |
| 468 | |||
| 469 | PriorityBoostStarvedThreads(); | ||
| 470 | |||
| 443 | Thread* next = PopNextReadyThread(); | 471 | Thread* next = PopNextReadyThread(); |
| 444 | HLE::g_reschedule = false; | 472 | HLE::g_reschedule = false; |
| 445 | 473 | ||
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index cfd073a70..233bcbdbd 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h | |||
| @@ -17,17 +17,19 @@ | |||
| 17 | #include "core/hle/kernel/kernel.h" | 17 | #include "core/hle/kernel/kernel.h" |
| 18 | #include "core/hle/result.h" | 18 | #include "core/hle/result.h" |
| 19 | 19 | ||
| 20 | enum ThreadPriority { | 20 | enum ThreadPriority : s32{ |
| 21 | THREADPRIO_HIGHEST = 0, ///< Highest thread priority | 21 | THREADPRIO_HIGHEST = 0, ///< Highest thread priority |
| 22 | THREADPRIO_DEFAULT = 16, ///< Default thread priority for userland apps | 22 | THREADPRIO_USERLAND_MAX = 24, ///< Highest thread priority for userland apps |
| 23 | THREADPRIO_LOW = 31, ///< Low range of thread priority for userland apps | 23 | THREADPRIO_DEFAULT = 48, ///< Default thread priority for userland apps |
| 24 | THREADPRIO_LOWEST = 63, ///< Thread priority max checked by svcCreateThread | 24 | THREADPRIO_LOWEST = 63, ///< Lowest thread priority |
| 25 | }; | 25 | }; |
| 26 | 26 | ||
| 27 | enum ThreadProcessorId { | 27 | enum ThreadProcessorId : s32 { |
| 28 | THREADPROCESSORID_0 = 0xFFFFFFFE, ///< Enables core appcode | 28 | THREADPROCESSORID_DEFAULT = -2, ///< Run thread on default core specified by exheader |
| 29 | THREADPROCESSORID_1 = 0xFFFFFFFD, ///< Enables core syscore | 29 | THREADPROCESSORID_ALL = -1, ///< Run thread on either core |
| 30 | THREADPROCESSORID_ALL = 0xFFFFFFFC, ///< Enables both cores | 30 | THREADPROCESSORID_0 = 0, ///< Run thread on core 0 (AppCore) |
| 31 | THREADPROCESSORID_1 = 1, ///< Run thread on core 1 (SysCore) | ||
| 32 | THREADPROCESSORID_MAX = 2, ///< Processor ID must be less than this | ||
| 31 | }; | 33 | }; |
| 32 | 34 | ||
| 33 | enum ThreadStatus { | 35 | enum ThreadStatus { |
| @@ -88,6 +90,12 @@ public: | |||
| 88 | void SetPriority(s32 priority); | 90 | void SetPriority(s32 priority); |
| 89 | 91 | ||
| 90 | /** | 92 | /** |
| 93 | * Temporarily boosts the thread's priority until the next time it is scheduled | ||
| 94 | * @param priority The new priority | ||
| 95 | */ | ||
| 96 | void BoostPriority(s32 priority); | ||
| 97 | |||
| 98 | /** | ||
| 91 | * Gets the thread's thread ID | 99 | * Gets the thread's thread ID |
| 92 | * @return The thread's ID | 100 | * @return The thread's ID |
| 93 | */ | 101 | */ |
| @@ -135,8 +143,10 @@ public: | |||
| 135 | u32 entry_point; | 143 | u32 entry_point; |
| 136 | u32 stack_top; | 144 | u32 stack_top; |
| 137 | 145 | ||
| 138 | s32 initial_priority; | 146 | s32 nominal_priority; ///< Nominal thread priority, as set by the emulated application |
| 139 | s32 current_priority; | 147 | s32 current_priority; ///< Current thread priority, can be temporarily changed |
| 148 | |||
| 149 | u64 last_running_ticks; ///< CPU tick when thread was last running | ||
| 140 | 150 | ||
| 141 | s32 processor_id; | 151 | s32 processor_id; |
| 142 | 152 | ||