VERY experimental Nintendo Switch 2 emulator
C++
hle: kernel: KThread: Reorganize thread priority defaults.
bunnei0ae0bc434051
diff --git a/src/core/hle/kernel/global_scheduler_context.h b/src/core/hle/kernel/global_scheduler_context.hindex a365ffdafe..11592843e8 100644--- a/src/core/hle/kernel/global_scheduler_context.h+++ b/src/core/hle/kernel/global_scheduler_context.h@@ -13,6 +13,7 @@#include "core/hle/kernel/k_priority_queue.h"#include "core/hle/kernel/k_scheduler_lock.h"#include "core/hle/kernel/k_thread.h"+#include "core/hle/kernel/svc_types.h"namespace Kernel {@@ -20,8 +21,12 @@ class KernelCore;class SchedulerLock;using KSchedulerPriorityQueue =- KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, THREADPRIO_LOWEST, THREADPRIO_HIGHEST>;-constexpr s32 HighestCoreMigrationAllowedPriority = 2;+ KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, Svc::LowestThreadPriority,+ Svc::HighestThreadPriority>;++static constexpr s32 HighestCoreMigrationAllowedPriority = 2;+static_assert(Svc::LowestThreadPriority >= HighestCoreMigrationAllowedPriority);+static_assert(Svc::HighestThreadPriority <= HighestCoreMigrationAllowedPriority);class GlobalSchedulerContext final {friend class KScheduler;diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cppindex 0f34a8a694..0e6300760a 100644--- a/src/core/hle/kernel/k_scheduler.cpp+++ b/src/core/hle/kernel/k_scheduler.cpp@@ -763,9 +763,9 @@ void KScheduler::Initialize() {std::string name = "Idle Thread Id:" + std::to_string(core_id);std::function<void(void*)> init_func = Core::CpuManager::GetIdleThreadStartFunc();void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater();- auto thread_res = KThread::Create(system, ThreadType::Kernel, name, 0, THREADPRIO_LOWEST, 0,- static_cast<u32>(core_id), 0, nullptr, std::move(init_func),- init_func_parameter);+ auto thread_res = KThread::Create(system, ThreadType::Kernel, name, 0,+ Svc::LowestThreadPriority, 0, static_cast<u32>(core_id), 0,+ nullptr, std::move(init_func), init_func_parameter);idle_thread = thread_res.Unwrap().get();{diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cppindex 0f349dad26..518c5d5df9 100644--- a/src/core/hle/kernel/k_thread.cpp+++ b/src/core/hle/kernel/k_thread.cpp@@ -8,6 +8,7 @@#include <vector>#include "common/assert.h"+#include "common/common_funcs.h"#include "common/common_types.h"#include "common/fiber.h"#include "common/logging/log.h"@@ -25,6 +26,7 @@#include "core/hle/kernel/memory/memory_layout.h"#include "core/hle/kernel/object.h"#include "core/hle/kernel/process.h"+#include "core/hle/kernel/svc_results.h"#include "core/hle/kernel/time_manager.h"#include "core/hle/result.h"#include "core/memory.h"@@ -124,11 +126,9 @@ ResultVal<std::shared_ptr<KThread>> KThread::Create(Core::System& system, Threadstd::function<void(void*)>&& thread_start_func,void* thread_start_parameter) {auto& kernel = system.Kernel();- // Check if priority is in ranged. Lowest priority -> highest priority id.- if (priority > THREADPRIO_LOWEST) {- LOG_ERROR(Kernel_SVC, "Invalid thread priority: {}", priority);- return ERR_INVALID_THREAD_PRIORITY;- }++ R_UNLESS(Svc::HighestThreadPriority <= priority && priority <= Svc::LowestThreadPriority,+ Svc::ResultInvalidPriority);if (processor_id > THREADPROCESSORID_MAX) {LOG_ERROR(Kernel_SVC, "Invalid processor id: {}", processor_id);@@ -186,8 +186,7 @@ ResultVal<std::shared_ptr<KThread>> KThread::Create(Core::System& system, Thread}void KThread::SetBasePriority(u32 priority) {- ASSERT_MSG(priority <= THREADPRIO_LOWEST && priority >= THREADPRIO_HIGHEST,- "Invalid priority value.");+ ASSERT(Svc::HighestThreadPriority <= priority && priority <= Svc::LowestThreadPriority);KScopedSchedulerLock lock(kernel);diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.hindex bef480dd7e..83a6d36ae3 100644--- a/src/core/hle/kernel/k_thread.h+++ b/src/core/hle/kernel/k_thread.h@@ -39,15 +39,6 @@ class KernelCore;class Process;class KScheduler;-enum ThreadPriority : u32 {- THREADPRIO_HIGHEST = 0, ///< Highest thread priority- THREADPRIO_MAX_CORE_MIGRATION = 2, ///< Highest priority for a core migration- THREADPRIO_USERLAND_MAX = 24, ///< Highest thread priority for userland apps- THREADPRIO_DEFAULT = 44, ///< Default thread priority for userland apps- THREADPRIO_LOWEST = 63, ///< Lowest thread priority- THREADPRIO_COUNT = 64, ///< Total number of possible thread priorities.-};-enum class ThreadType : u32 {Main = 0,Kernel = 1,@@ -129,6 +120,9 @@ class KThread final : public KSynchronizationObject, public boost::intrusive::lifriend class Process;public:+ static constexpr s32 DefaultThreadPriority = 44;+ static constexpr s32 IdleThreadPriority = 64;+explicit KThread(KernelCore& kernel);~KThread() override;diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cppindex 711b9d5206..70a8ef34b1 100644--- a/src/core/hle/kernel/svc.cpp+++ b/src/core/hle/kernel/svc.cpp@@ -1130,11 +1130,9 @@ static ResultCode GetThreadPriority32(Core::System& system, u32* priority, Handlstatic ResultCode SetThreadPriority(Core::System& system, Handle handle, u32 priority) {LOG_TRACE(Kernel_SVC, "called");- if (priority > THREADPRIO_LOWEST) {- LOG_ERROR(- Kernel_SVC,- "An invalid priority was specified, expected {} but got {} for thread_handle={:08X}",- THREADPRIO_LOWEST, priority, handle);+ if (priority > Svc::LowestThreadPriority) {+ LOG_ERROR(Kernel_SVC, "An invalid priority was specified {} for thread_handle={:08X}",+ priority, handle);return ERR_INVALID_THREAD_PRIORITY;}@@ -1472,7 +1470,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr ereturn ERR_INVALID_PROCESSOR_ID;}- if (priority > THREADPRIO_LOWEST) {+ if (priority > Svc::LowestThreadPriority) {LOG_ERROR(Kernel_SVC,"Invalid thread priority specified ({}). Must be within the range 0-64",priority);diff --git a/src/core/hle/kernel/svc_results.h b/src/core/hle/kernel/svc_results.hindex 78282f0213..74adabc111 100644--- a/src/core/hle/kernel/svc_results.h+++ b/src/core/hle/kernel/svc_results.h@@ -11,6 +11,7 @@ namespace Kernel::Svc {constexpr ResultCode ResultTerminationRequested{ErrorModule::Kernel, 59};constexpr ResultCode ResultInvalidAddress{ErrorModule::Kernel, 102};constexpr ResultCode ResultInvalidCurrentMemory{ErrorModule::Kernel, 106};+constexpr ResultCode ResultInvalidPriority{ErrorModule::Kernel, 112};constexpr ResultCode ResultInvalidHandle{ErrorModule::Kernel, 114};constexpr ResultCode ResultTimedOut{ErrorModule::Kernel, 117};constexpr ResultCode ResultCancelled{ErrorModule::Kernel, 118};diff --git a/src/core/hle/kernel/svc_types.h b/src/core/hle/kernel/svc_types.hindex d623f7a50d..09b858f2a8 100644--- a/src/core/hle/kernel/svc_types.h+++ b/src/core/hle/kernel/svc_types.h@@ -77,4 +77,7 @@ enum class ArbitrationType : u32 {WaitIfEqual = 2,};+constexpr inline s32 LowestThreadPriority = 63;+constexpr inline s32 HighestThreadPriority = 0;+} // namespace Kernel::Svcdiff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cppindex 4f55314c7d..f976d0a9cc 100644--- a/src/core/loader/nro.cpp+++ b/src/core/loader/nro.cpp@@ -219,8 +219,8 @@ AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::Process& process, Core::Sy}is_loaded = true;- return {ResultStatus::Success,- LoadParameters{Kernel::THREADPRIO_DEFAULT, Core::Memory::DEFAULT_STACK_SIZE}};+ return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,+ Core::Memory::DEFAULT_STACK_SIZE}};}ResultStatus AppLoader_NRO::ReadIcon(std::vector<u8>& buffer) {diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cppindex 50e6cd0807..ea347ea837 100644--- a/src/core/loader/nso.cpp+++ b/src/core/loader/nso.cpp@@ -179,8 +179,8 @@ AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process, Core::SyLOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);is_loaded = true;- return {ResultStatus::Success,- LoadParameters{Kernel::THREADPRIO_DEFAULT, Core::Memory::DEFAULT_STACK_SIZE}};+ return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,+ Core::Memory::DEFAULT_STACK_SIZE}};}ResultStatus AppLoader_NSO::ReadNSOModules(Modules& modules) {