kernel: Add ticks to low priority threads that arbitrate zero threads (#7096)

This commit is contained in:
GPUCode 2023-11-05 00:20:55 +02:00 committed by GitHub
parent 27bad3a699
commit 998b9a9525
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 14 deletions

View file

@ -6,7 +6,6 @@
#include "common/archives.h" #include "common/archives.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/global.h"
#include "core/hle/kernel/address_arbiter.h" #include "core/hle/kernel/address_arbiter.h"
#include "core/hle/kernel/errors.h" #include "core/hle/kernel/errors.h"
#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/kernel.h"
@ -21,7 +20,7 @@ void AddressArbiter::WaitThread(std::shared_ptr<Thread> thread, VAddr wait_addre
waiting_threads.emplace_back(std::move(thread)); waiting_threads.emplace_back(std::move(thread));
} }
void AddressArbiter::ResumeAllThreads(VAddr address) { u64 AddressArbiter::ResumeAllThreads(VAddr address) {
// Determine which threads are waiting on this address, those should be woken up. // Determine which threads are waiting on this address, those should be woken up.
auto itr = std::stable_partition(waiting_threads.begin(), waiting_threads.end(), auto itr = std::stable_partition(waiting_threads.begin(), waiting_threads.end(),
[address](const auto& thread) { [address](const auto& thread) {
@ -31,13 +30,15 @@ void AddressArbiter::ResumeAllThreads(VAddr address) {
}); });
// Wake up all the found threads // Wake up all the found threads
const u64 num_threads = std::distance(itr, waiting_threads.end());
std::for_each(itr, waiting_threads.end(), [](auto& thread) { thread->ResumeFromWait(); }); std::for_each(itr, waiting_threads.end(), [](auto& thread) { thread->ResumeFromWait(); });
// Remove the woken up threads from the wait list. // Remove the woken up threads from the wait list.
waiting_threads.erase(itr, waiting_threads.end()); waiting_threads.erase(itr, waiting_threads.end());
return num_threads;
} }
std::shared_ptr<Thread> AddressArbiter::ResumeHighestPriorityThread(VAddr address) { bool AddressArbiter::ResumeHighestPriorityThread(VAddr address) {
// Determine which threads are waiting on this address, those should be considered for wakeup. // Determine which threads are waiting on this address, those should be considered for wakeup.
auto matches_start = std::stable_partition( auto matches_start = std::stable_partition(
waiting_threads.begin(), waiting_threads.end(), [address](const auto& thread) { waiting_threads.begin(), waiting_threads.end(), [address](const auto& thread) {
@ -54,14 +55,15 @@ std::shared_ptr<Thread> AddressArbiter::ResumeHighestPriorityThread(VAddr addres
return lhs->current_priority < rhs->current_priority; return lhs->current_priority < rhs->current_priority;
}); });
if (itr == waiting_threads.end()) if (itr == waiting_threads.end()) {
return nullptr; return false;
}
auto thread = *itr; auto thread = *itr;
thread->ResumeFromWait(); thread->ResumeFromWait();
waiting_threads.erase(itr); waiting_threads.erase(itr);
return thread;
return true;
} }
AddressArbiter::AddressArbiter(KernelSystem& kernel) AddressArbiter::AddressArbiter(KernelSystem& kernel)
@ -107,17 +109,28 @@ ResultCode AddressArbiter::ArbitrateAddress(std::shared_ptr<Thread> thread, Arbi
switch (type) { switch (type) {
// Signal thread(s) waiting for arbitrate address... // Signal thread(s) waiting for arbitrate address...
case ArbitrationType::Signal: case ArbitrationType::Signal: {
u64 num_threads{};
// Negative value means resume all threads // Negative value means resume all threads
if (value < 0) { if (value < 0) {
ResumeAllThreads(address); num_threads = ResumeAllThreads(address);
} else { } else {
// Resume first N threads // Resume first N threads
for (int i = 0; i < value; i++) for (s32 i = 0; i < value; i++) {
ResumeHighestPriorityThread(address); num_threads += ResumeHighestPriorityThread(address);
}
}
// Prevents lag from low priority threads that spam svcArbitrateAddress and wake no threads
// The tick count is taken directly from official HOS kernel. The priority value is one less
// than official kernel as the affected FMV threads dont meet the priority threshold of 50.
// TODO: Revisit this when scheduler is rewritten and adjust if there isn't a problem there.
if (num_threads == 0 && thread->current_priority >= 49) {
kernel.current_cpu->GetTimer().AddTicks(1614u);
} }
break; break;
}
// Wait current thread (acquire the arbiter)... // Wait current thread (acquire the arbiter)...
case ArbitrationType::WaitIfLessThan: case ArbitrationType::WaitIfLessThan:
if ((s32)kernel.memory.Read32(address) < value) { if ((s32)kernel.memory.Read32(address) < value) {

View file

@ -65,11 +65,11 @@ private:
void WaitThread(std::shared_ptr<Thread> thread, VAddr wait_address); void WaitThread(std::shared_ptr<Thread> thread, VAddr wait_address);
/// Resume all threads found to be waiting on the address under this address arbiter /// Resume all threads found to be waiting on the address under this address arbiter
void ResumeAllThreads(VAddr address); u64 ResumeAllThreads(VAddr address);
/// Resume one thread found to be waiting on the address under this address arbiter and return /// Resume one thread found to be waiting on the address under this address arbiter and return
/// the resumed thread. /// the resumed thread.
std::shared_ptr<Thread> ResumeHighestPriorityThread(VAddr address); bool ResumeHighestPriorityThread(VAddr address);
/// Threads waiting for the address arbiter to be signaled. /// Threads waiting for the address arbiter to be signaled.
std::vector<std::shared_ptr<Thread>> waiting_threads; std::vector<std::shared_ptr<Thread>> waiting_threads;