hle: kernel: KThread: Reorganize thread priority defaults.

This commit is contained in:
bunnei 2021-01-01 02:06:06 -08:00
parent 0530292b97
commit 1e55498110
9 changed files with 31 additions and 31 deletions

View file

@ -13,6 +13,7 @@
#include "core/hle/kernel/k_priority_queue.h" #include "core/hle/kernel/k_priority_queue.h"
#include "core/hle/kernel/k_scheduler_lock.h" #include "core/hle/kernel/k_scheduler_lock.h"
#include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/svc_types.h"
namespace Kernel { namespace Kernel {
@ -20,8 +21,12 @@ class KernelCore;
class SchedulerLock; class SchedulerLock;
using KSchedulerPriorityQueue = using KSchedulerPriorityQueue =
KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, THREADPRIO_LOWEST, THREADPRIO_HIGHEST>; KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, Svc::LowestThreadPriority,
constexpr s32 HighestCoreMigrationAllowedPriority = 2; Svc::HighestThreadPriority>;
static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
static_assert(Svc::LowestThreadPriority >= HighestCoreMigrationAllowedPriority);
static_assert(Svc::HighestThreadPriority <= HighestCoreMigrationAllowedPriority);
class GlobalSchedulerContext final { class GlobalSchedulerContext final {
friend class KScheduler; friend class KScheduler;

View file

@ -763,9 +763,9 @@ void KScheduler::Initialize() {
std::string name = "Idle Thread Id:" + std::to_string(core_id); std::string name = "Idle Thread Id:" + std::to_string(core_id);
std::function<void(void*)> init_func = Core::CpuManager::GetIdleThreadStartFunc(); std::function<void(void*)> init_func = Core::CpuManager::GetIdleThreadStartFunc();
void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater(); void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater();
auto thread_res = KThread::Create(system, ThreadType::Kernel, name, 0, THREADPRIO_LOWEST, 0, auto thread_res = KThread::Create(system, ThreadType::Kernel, name, 0,
static_cast<u32>(core_id), 0, nullptr, std::move(init_func), Svc::LowestThreadPriority, 0, static_cast<u32>(core_id), 0,
init_func_parameter); nullptr, std::move(init_func), init_func_parameter);
idle_thread = thread_res.Unwrap().get(); idle_thread = thread_res.Unwrap().get();
{ {

View file

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include "common/assert.h" #include "common/assert.h"
#include "common/common_funcs.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/fiber.h" #include "common/fiber.h"
#include "common/logging/log.h" #include "common/logging/log.h"
@ -25,6 +26,7 @@
#include "core/hle/kernel/memory/memory_layout.h" #include "core/hle/kernel/memory/memory_layout.h"
#include "core/hle/kernel/object.h" #include "core/hle/kernel/object.h"
#include "core/hle/kernel/process.h" #include "core/hle/kernel/process.h"
#include "core/hle/kernel/svc_results.h"
#include "core/hle/kernel/time_manager.h" #include "core/hle/kernel/time_manager.h"
#include "core/hle/result.h" #include "core/hle/result.h"
#include "core/memory.h" #include "core/memory.h"
@ -124,11 +126,9 @@ ResultVal<std::shared_ptr<KThread>> KThread::Create(Core::System& system, Thread
std::function<void(void*)>&& thread_start_func, std::function<void(void*)>&& thread_start_func,
void* thread_start_parameter) { void* thread_start_parameter) {
auto& kernel = system.Kernel(); auto& kernel = system.Kernel();
// Check if priority is in ranged. Lowest priority -> highest priority id.
if (priority > THREADPRIO_LOWEST) { R_UNLESS(Svc::HighestThreadPriority <= priority && priority <= Svc::LowestThreadPriority,
LOG_ERROR(Kernel_SVC, "Invalid thread priority: {}", priority); Svc::ResultInvalidPriority);
return ERR_INVALID_THREAD_PRIORITY;
}
if (processor_id > THREADPROCESSORID_MAX) { if (processor_id > THREADPROCESSORID_MAX) {
LOG_ERROR(Kernel_SVC, "Invalid processor id: {}", processor_id); 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) { void KThread::SetBasePriority(u32 priority) {
ASSERT_MSG(priority <= THREADPRIO_LOWEST && priority >= THREADPRIO_HIGHEST, ASSERT(Svc::HighestThreadPriority <= priority && priority <= Svc::LowestThreadPriority);
"Invalid priority value.");
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);

View file

@ -39,15 +39,6 @@ class KernelCore;
class Process; class Process;
class KScheduler; 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 { enum class ThreadType : u32 {
Main = 0, Main = 0,
Kernel = 1, Kernel = 1,
@ -129,6 +120,9 @@ class KThread final : public KSynchronizationObject, public boost::intrusive::li
friend class Process; friend class Process;
public: public:
static constexpr s32 DefaultThreadPriority = 44;
static constexpr s32 IdleThreadPriority = 64;
explicit KThread(KernelCore& kernel); explicit KThread(KernelCore& kernel);
~KThread() override; ~KThread() override;

View file

@ -1130,11 +1130,9 @@ static ResultCode GetThreadPriority32(Core::System& system, u32* priority, Handl
static ResultCode SetThreadPriority(Core::System& system, Handle handle, u32 priority) { static ResultCode SetThreadPriority(Core::System& system, Handle handle, u32 priority) {
LOG_TRACE(Kernel_SVC, "called"); LOG_TRACE(Kernel_SVC, "called");
if (priority > THREADPRIO_LOWEST) { if (priority > Svc::LowestThreadPriority) {
LOG_ERROR( LOG_ERROR(Kernel_SVC, "An invalid priority was specified {} for thread_handle={:08X}",
Kernel_SVC, priority, handle);
"An invalid priority was specified, expected {} but got {} for thread_handle={:08X}",
THREADPRIO_LOWEST, priority, handle);
return ERR_INVALID_THREAD_PRIORITY; return ERR_INVALID_THREAD_PRIORITY;
} }
@ -1472,7 +1470,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e
return ERR_INVALID_PROCESSOR_ID; return ERR_INVALID_PROCESSOR_ID;
} }
if (priority > THREADPRIO_LOWEST) { if (priority > Svc::LowestThreadPriority) {
LOG_ERROR(Kernel_SVC, LOG_ERROR(Kernel_SVC,
"Invalid thread priority specified ({}). Must be within the range 0-64", "Invalid thread priority specified ({}). Must be within the range 0-64",
priority); priority);

View file

@ -11,6 +11,7 @@ namespace Kernel::Svc {
constexpr ResultCode ResultTerminationRequested{ErrorModule::Kernel, 59}; constexpr ResultCode ResultTerminationRequested{ErrorModule::Kernel, 59};
constexpr ResultCode ResultInvalidAddress{ErrorModule::Kernel, 102}; constexpr ResultCode ResultInvalidAddress{ErrorModule::Kernel, 102};
constexpr ResultCode ResultInvalidCurrentMemory{ErrorModule::Kernel, 106}; constexpr ResultCode ResultInvalidCurrentMemory{ErrorModule::Kernel, 106};
constexpr ResultCode ResultInvalidPriority{ErrorModule::Kernel, 112};
constexpr ResultCode ResultInvalidHandle{ErrorModule::Kernel, 114}; constexpr ResultCode ResultInvalidHandle{ErrorModule::Kernel, 114};
constexpr ResultCode ResultTimedOut{ErrorModule::Kernel, 117}; constexpr ResultCode ResultTimedOut{ErrorModule::Kernel, 117};
constexpr ResultCode ResultCancelled{ErrorModule::Kernel, 118}; constexpr ResultCode ResultCancelled{ErrorModule::Kernel, 118};

View file

@ -77,4 +77,7 @@ enum class ArbitrationType : u32 {
WaitIfEqual = 2, WaitIfEqual = 2,
}; };
constexpr inline s32 LowestThreadPriority = 63;
constexpr inline s32 HighestThreadPriority = 0;
} // namespace Kernel::Svc } // namespace Kernel::Svc

View file

@ -219,8 +219,8 @@ AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::Process& process, Core::Sy
} }
is_loaded = true; is_loaded = true;
return {ResultStatus::Success, return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,
LoadParameters{Kernel::THREADPRIO_DEFAULT, Core::Memory::DEFAULT_STACK_SIZE}}; Core::Memory::DEFAULT_STACK_SIZE}};
} }
ResultStatus AppLoader_NRO::ReadIcon(std::vector<u8>& buffer) { ResultStatus AppLoader_NRO::ReadIcon(std::vector<u8>& buffer) {

View file

@ -179,8 +179,8 @@ AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process, Core::Sy
LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address); LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
is_loaded = true; is_loaded = true;
return {ResultStatus::Success, return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,
LoadParameters{Kernel::THREADPRIO_DEFAULT, Core::Memory::DEFAULT_STACK_SIZE}}; Core::Memory::DEFAULT_STACK_SIZE}};
} }
ResultStatus AppLoader_NSO::ReadNSOModules(Modules& modules) { ResultStatus AppLoader_NSO::ReadNSOModules(Modules& modules) {