Skip to content

Commit

Permalink
Kernel: Add priority boost code for starved threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragios authored and Dragios committed Oct 20, 2017
1 parent f47bf6c commit e12de2e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/core/hle/kernel/thread.cpp
Expand Up @@ -143,6 +143,29 @@ void ArbitrateAllThreads(u32 address) {
}
}

/// Boost low priority threads (temporarily) that have been starved
static void PriorityBoostStarvedThreads() {
u64 current_ticks = CoreTiming::GetTicks();

for (auto& thread : thread_list) {
// TODO(bunnei): Threads that have been waiting to be scheduled for `boost_ticks` (or
// longer) will have their priority temporarily adjusted to 1 higher than the highest
// priority thread to prevent thread starvation. This general behavior has been verified
// on hardware. However, this is almost certainly not perfect, and the real CTR OS scheduler
// should probably be reversed to verify this.

const u64 boost_timeout = 2000000; // Boost threads that have been ready for > this long

u64 delta = current_ticks - thread->last_running_ticks;

if (thread->status == THREADSTATUS_READY && delta > boost_timeout) {
const s32 priority = std::max(ready_queue.get_first()->current_priority - 1,
static_cast<unsigned int>(0));
thread->BoostPriority(priority);
}
}
}

/**
* Switches the CPU's active thread context to that of the specified thread
* @param new_thread The thread to switch to
Expand Down Expand Up @@ -177,6 +200,10 @@ static void SwitchContext(Thread* new_thread) {

ready_queue.remove(new_thread->current_priority, new_thread);
new_thread->status = THREADSTATUS_RUNNING;

// Restores thread to its nominal priority if it has been temporarily changed
new_thread->current_priority = new_thread->nominal_priority;


if (previous_process != current_thread->owner_process) {
Kernel::g_current_process = current_thread->owner_process;
Expand Down Expand Up @@ -513,6 +540,8 @@ bool HaveReadyThreads() {
}

void Reschedule() {
PriorityBoostStarvedThreads();

Thread* cur = GetCurrentThread();
Thread* next = PopNextReadyThread();

Expand Down

0 comments on commit e12de2e

Please sign in to comment.