From 0eeecde67c89cf33521711ae61d567b2f43ff80a Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 23:25:32 -0700 Subject: hle: kernel: Migrate idle threads. --- src/core/hle/kernel/k_scheduler.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src/core/hle/kernel/k_scheduler.cpp') diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index d1df97305..c048d86a3 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -623,7 +623,7 @@ KThread* KScheduler::GetCurrentThread() const { if (auto result = current_thread.load(); result) { return result; } - return idle_thread; + return idle_thread.get(); } u64 KScheduler::GetLastContextSwitchTicks() const { @@ -708,7 +708,7 @@ void KScheduler::ScheduleImpl() { // We never want to schedule a null thread, so use the idle thread if we don't have a next. if (next_thread == nullptr) { - next_thread = idle_thread; + next_thread = idle_thread.get(); } // If we're not actually switching thread, there's nothing to do. @@ -769,7 +769,7 @@ void KScheduler::SwitchToCurrent() { break; } } - auto thread = next_thread ? next_thread : idle_thread; + auto thread = next_thread ? next_thread : idle_thread.get(); Common::Fiber::YieldTo(switch_fiber, *thread->GetHostContext()); } while (!is_switch_pending()); } @@ -792,13 +792,9 @@ void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) } void KScheduler::Initialize() { - std::string name = "Idle Thread Id:" + std::to_string(core_id); - std::function init_func = Core::CpuManager::GetIdleThreadStartFunc(); - void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater(); - auto thread_res = KThread::CreateThread( - system, ThreadType::Main, name, 0, KThread::IdleThreadPriority, 0, - static_cast(core_id), 0, nullptr, std::move(init_func), init_func_parameter); - idle_thread = thread_res.Unwrap().get(); + idle_thread = std::make_unique(system.Kernel()); + ASSERT(KThread::InitializeIdleThread(system, idle_thread.get(), core_id).IsSuccess()); + idle_thread->SetName(fmt::format("IdleThread:{}", core_id)); } KScopedSchedulerLock::KScopedSchedulerLock(KernelCore& kernel) -- cgit v1.2.3 From ab704acab80d17f9a8e34dcbb753d60de2a86f86 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 Apr 2021 16:56:11 -0700 Subject: hle: kernel: Ensure all kernel objects with KAutoObject are properly created. --- src/core/hle/kernel/k_scheduler.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/hle/kernel/k_scheduler.cpp') diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index c048d86a3..09382f7dd 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -793,6 +793,7 @@ void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) void KScheduler::Initialize() { idle_thread = std::make_unique(system.Kernel()); + KAutoObject::Create(idle_thread.get()); ASSERT(KThread::InitializeIdleThread(system, idle_thread.get(), core_id).IsSuccess()); idle_thread->SetName(fmt::format("IdleThread:{}", core_id)); } -- cgit v1.2.3 From b6156e735cd78d4b7863491ae6bdc63e44404b73 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 Apr 2021 22:10:14 -0700 Subject: hle: kernel: Move slab heap management to KernelCore. --- src/core/hle/kernel/k_scheduler.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel/k_scheduler.cpp') diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 09382f7dd..1feda9303 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -617,7 +617,9 @@ KScheduler::KScheduler(Core::System& system, s32 core_id) : system(system), core state.highest_priority_thread = nullptr; } -KScheduler::~KScheduler() = default; +KScheduler::~KScheduler() { + idle_thread->Close(); +} KThread* KScheduler::GetCurrentThread() const { if (auto result = current_thread.load(); result) { -- cgit v1.2.3 From 89edbe8aa20d278d6f2c5ab735163f0d96ff88d2 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 Apr 2021 22:42:23 -0700 Subject: hle: kernel: Refactor several threads/events/sharedmemory to use slab heaps. --- src/core/hle/kernel/k_scheduler.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/core/hle/kernel/k_scheduler.cpp') diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 1feda9303..38c6b50fa 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -618,14 +618,17 @@ KScheduler::KScheduler(Core::System& system, s32 core_id) : system(system), core } KScheduler::~KScheduler() { - idle_thread->Close(); + if (idle_thread) { + idle_thread->Close(); + idle_thread = nullptr; + } } KThread* KScheduler::GetCurrentThread() const { if (auto result = current_thread.load(); result) { return result; } - return idle_thread.get(); + return idle_thread; } u64 KScheduler::GetLastContextSwitchTicks() const { @@ -710,7 +713,7 @@ void KScheduler::ScheduleImpl() { // We never want to schedule a null thread, so use the idle thread if we don't have a next. if (next_thread == nullptr) { - next_thread = idle_thread.get(); + next_thread = idle_thread; } // If we're not actually switching thread, there's nothing to do. @@ -771,7 +774,7 @@ void KScheduler::SwitchToCurrent() { break; } } - auto thread = next_thread ? next_thread : idle_thread.get(); + auto thread = next_thread ? next_thread : idle_thread; Common::Fiber::YieldTo(switch_fiber, *thread->GetHostContext()); } while (!is_switch_pending()); } @@ -794,9 +797,8 @@ void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) } void KScheduler::Initialize() { - idle_thread = std::make_unique(system.Kernel()); - KAutoObject::Create(idle_thread.get()); - ASSERT(KThread::InitializeIdleThread(system, idle_thread.get(), core_id).IsSuccess()); + idle_thread = KThread::Create(system.Kernel()); + ASSERT(KThread::InitializeIdleThread(system, idle_thread, core_id).IsSuccess()); idle_thread->SetName(fmt::format("IdleThread:{}", core_id)); } -- cgit v1.2.3 From 2a7eff57a8048933a89c1a8f8d6dced7b5d604f2 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 22:04:28 -0700 Subject: hle: kernel: Rename Process to KProcess. --- src/core/hle/kernel/k_scheduler.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/core/hle/kernel/k_scheduler.cpp') diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 38c6b50fa..0115fe6d1 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -15,12 +15,12 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/cpu_manager.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/time_manager.h" namespace Kernel { @@ -71,7 +71,7 @@ u64 KScheduler::UpdateHighestPriorityThread(KThread* highest_thread) { } if (state.should_count_idle) { if (highest_thread != nullptr) { - if (Process* process = highest_thread->GetOwnerProcess(); process != nullptr) { + if (KProcess* process = highest_thread->GetOwnerProcess(); process != nullptr) { process->SetRunningThread(core_id, highest_thread, state.idle_count); } } else { @@ -104,7 +104,7 @@ u64 KScheduler::UpdateHighestPriorityThreadsImpl(KernelCore& kernel) { if (top_thread != nullptr) { // If the thread has no waiters, we need to check if the process has a thread pinned. if (top_thread->GetNumKernelWaiters() == 0) { - if (Process* parent = top_thread->GetOwnerProcess(); parent != nullptr) { + if (KProcess* parent = top_thread->GetOwnerProcess(); parent != nullptr) { if (KThread* pinned = parent->GetPinnedThread(static_cast(core_id)); pinned != nullptr && pinned != top_thread) { // We prefer our parent's pinned thread if possible. However, we also don't @@ -411,7 +411,7 @@ void KScheduler::YieldWithoutCoreMigration(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -450,7 +450,7 @@ void KScheduler::YieldWithCoreMigration(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -538,7 +538,7 @@ void KScheduler::YieldToAnyThread(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -724,7 +724,7 @@ void KScheduler::ScheduleImpl() { current_thread.store(next_thread); - Process* const previous_process = system.Kernel().CurrentProcess(); + KProcess* const previous_process = system.Kernel().CurrentProcess(); UpdateLastContextSwitchTime(previous_thread, previous_process); @@ -780,7 +780,7 @@ void KScheduler::SwitchToCurrent() { } } -void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) { +void KScheduler::UpdateLastContextSwitchTime(KThread* thread, KProcess* process) { const u64 prev_switch_ticks = last_context_switch_time; const u64 most_recent_switch_ticks = system.CoreTiming().GetCPUTicks(); const u64 update_ticks = most_recent_switch_ticks - prev_switch_ticks; -- cgit v1.2.3