Skip to content

Commit

Permalink
kernel thread: Implement priority boost starved threads hack
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamer64ytb committed Jul 25, 2024
1 parent 830ec84 commit 6414b77
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ struct Values {
SwitchableSetting<bool> force_hw_vertex_shaders{false, "force_hw_vertex_shaders"};
SwitchableSetting<bool> disable_surface_texture_copy{false, "disable_surface_texture_copy"};
SwitchableSetting<bool> disable_flush_cpu_write{false, "disable_flush_cpu_write"};
SwitchableSetting<bool> priority_boost_starved_threads{true, "priority_boost_starved_threads"};

// Audio
bool audio_muted;
Expand Down
22 changes: 22 additions & 0 deletions src/core/hle/kernel/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ void ThreadManager::SwitchContext(Thread* new_thread) {
kernel.SetCurrentProcessForCPU(current_thread->owner_process.lock(), cpu->GetID());
}

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

cpu->LoadContext(new_thread->context);
cpu->SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress());
} else {
Expand Down Expand Up @@ -469,7 +472,26 @@ bool ThreadManager::HaveReadyThreads() {
return ready_queue.get_first() != nullptr;
}

void ThreadManager::PriorityBoostStarvedThreads() {
const u64 current_ticks = kernel.timing.GetTicks();

for (auto& thread : thread_list) {
const u64 boost_ticks = 1400000;

u64 delta = current_ticks - thread->last_running_ticks;

if (thread->status == ThreadStatus::Ready && delta > boost_ticks) {
const s32 priority = std::max(ready_queue.get_first()->current_priority, 40u);
thread->BoostPriority(priority);
}
}
}

void ThreadManager::Reschedule() {
if (Settings::values.priority_boost_starved_threads) {
PriorityBoostStarvedThreads();
}

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

Expand Down
3 changes: 3 additions & 0 deletions src/core/hle/kernel/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ class ThreadManager {
*/
void ThreadWakeupCallback(u64 thread_id, s64 cycles_late);

/// Boost low priority starved threads
void PriorityBoostStarvedThreads();

Kernel::KernelSystem& kernel;
Core::ARM_Interface* cpu;

Expand Down

0 comments on commit 6414b77

Please sign in to comment.