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/thread.cpp | |
| 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/thread.cpp')
| -rw-r--r-- | src/core/hle/kernel/thread.cpp | 50 |
1 files changed, 39 insertions, 11 deletions
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 | ||