diff options
| author | 2015-03-23 23:55:21 -0400 | |
|---|---|---|
| committer | 2015-04-09 19:05:21 -0400 | |
| commit | 7b9f428b23e1761e7b6c177d2e8eb9219ac6b7f6 (patch) | |
| tree | 597dff81a4b2935daaa7890c932b00cea4d6ae53 /src/core/hle/kernel/thread.cpp | |
| parent | SVC: Reschedule on svcCreateThread. (diff) | |
| download | yuzu-7b9f428b23e1761e7b6c177d2e8eb9219ac6b7f6.tar.gz yuzu-7b9f428b23e1761e7b6c177d2e8eb9219ac6b7f6.tar.xz yuzu-7b9f428b23e1761e7b6c177d2e8eb9219ac6b7f6.zip | |
Thread: Implement priority boost for starved threads.
SVC: Return correct error code on invalid CreateThread processor ID.
SVC: Assert when creating a thread with an invalid userland priority.
Diffstat (limited to 'src/core/hle/kernel/thread.cpp')
| -rw-r--r-- | src/core/hle/kernel/thread.cpp | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index be1aed615..3a1e15ac6 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp | |||
| @@ -140,6 +140,29 @@ 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 boost_priority = std::max(ready_queue.get_first()->current_priority - 1, 0); | ||
| 160 | ready_queue.move(thread, thread->current_priority, boost_priority); | ||
| 161 | thread->current_priority = boost_priority; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 143 | /** | 166 | /** |
| 144 | * Switches the CPU's active thread context to that of the specified thread | 167 | * Switches the CPU's active thread context to that of the specified thread |
| 145 | * @param new_thread The thread to switch to | 168 | * @param new_thread The thread to switch to |
| @@ -151,6 +174,7 @@ static void SwitchContext(Thread* new_thread) { | |||
| 151 | 174 | ||
| 152 | // Save context for previous thread | 175 | // Save context for previous thread |
| 153 | if (previous_thread) { | 176 | if (previous_thread) { |
| 177 | previous_thread->last_running_ticks = CoreTiming::GetTicks(); | ||
| 154 | Core::g_app_core->SaveContext(previous_thread->context); | 178 | Core::g_app_core->SaveContext(previous_thread->context); |
| 155 | 179 | ||
| 156 | if (previous_thread->status == THREADSTATUS_RUNNING) { | 180 | if (previous_thread->status == THREADSTATUS_RUNNING) { |
| @@ -168,6 +192,9 @@ static void SwitchContext(Thread* new_thread) { | |||
| 168 | ready_queue.remove(new_thread->current_priority, new_thread); | 192 | ready_queue.remove(new_thread->current_priority, new_thread); |
| 169 | new_thread->status = THREADSTATUS_RUNNING; | 193 | new_thread->status = THREADSTATUS_RUNNING; |
| 170 | 194 | ||
| 195 | // Restores thread to its nominal priority if it has been temporarily changed | ||
| 196 | new_thread->current_priority = new_thread->nominal_priority; | ||
| 197 | |||
| 171 | Core::g_app_core->LoadContext(new_thread->context); | 198 | Core::g_app_core->LoadContext(new_thread->context); |
| 172 | } else { | 199 | } else { |
| 173 | current_thread = nullptr; | 200 | current_thread = nullptr; |
| @@ -364,7 +391,8 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, | |||
| 364 | thread->status = THREADSTATUS_DORMANT; | 391 | thread->status = THREADSTATUS_DORMANT; |
| 365 | thread->entry_point = entry_point; | 392 | thread->entry_point = entry_point; |
| 366 | thread->stack_top = stack_top; | 393 | thread->stack_top = stack_top; |
| 367 | thread->initial_priority = thread->current_priority = priority; | 394 | thread->nominal_priority = thread->current_priority = priority; |
| 395 | thread->last_running_ticks = CoreTiming::GetTicks(); | ||
| 368 | thread->processor_id = processor_id; | 396 | thread->processor_id = processor_id; |
| 369 | thread->wait_set_output = false; | 397 | thread->wait_set_output = false; |
| 370 | thread->wait_all = false; | 398 | thread->wait_all = false; |
| @@ -400,18 +428,11 @@ static void ClampPriority(const Thread* thread, s32* priority) { | |||
| 400 | void Thread::SetPriority(s32 priority) { | 428 | void Thread::SetPriority(s32 priority) { |
| 401 | ClampPriority(this, &priority); | 429 | ClampPriority(this, &priority); |
| 402 | 430 | ||
| 403 | if (current_priority == priority) { | 431 | // If thread was ready, adjust queues |
| 404 | return; | 432 | if (status == THREADSTATUS_READY) |
| 405 | } | 433 | ready_queue.move(this, current_priority, priority); |
| 406 | 434 | ||
| 407 | if (status == THREADSTATUS_READY) { | 435 | nominal_priority = current_priority = priority; |
| 408 | // If thread was ready, adjust queues | ||
| 409 | ready_queue.remove(current_priority, this); | ||
| 410 | ready_queue.prepare(priority); | ||
| 411 | ready_queue.push_back(priority, this); | ||
| 412 | } | ||
| 413 | |||
| 414 | current_priority = priority; | ||
| 415 | } | 436 | } |
| 416 | 437 | ||
| 417 | SharedPtr<Thread> SetupIdleThread() { | 438 | SharedPtr<Thread> SetupIdleThread() { |
| @@ -440,6 +461,9 @@ SharedPtr<Thread> SetupMainThread(u32 stack_size, u32 entry_point, s32 priority) | |||
| 440 | 461 | ||
| 441 | void Reschedule() { | 462 | void Reschedule() { |
| 442 | Thread* prev = GetCurrentThread(); | 463 | Thread* prev = GetCurrentThread(); |
| 464 | |||
| 465 | PriorityBoostStarvedThreads(); | ||
| 466 | |||
| 443 | Thread* next = PopNextReadyThread(); | 467 | Thread* next = PopNextReadyThread(); |
| 444 | HLE::g_reschedule = false; | 468 | HLE::g_reschedule = false; |
| 445 | 469 | ||