From e4a1ead897575ee9222b4fc1021aaa9cc58f12c8 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sun, 26 Jan 2020 14:07:22 -0400 Subject: [PATCH] Core: Refactor CpuCoreManager to CpuManager and Cpu to Core Manager. This commit instends on better naming the new purpose of this classes. --- src/core/CMakeLists.txt | 8 +-- src/core/arm/dynarmic/arm_dynarmic.cpp | 2 +- src/core/core.cpp | 44 ++++++++-------- src/core/core.h | 10 ++-- src/core/{core_cpu.cpp => core_manager.cpp} | 14 ++--- src/core/{core_cpu.h => core_manager.h} | 6 +-- src/core/cpu_core_manager.h | 52 ------------------- .../{cpu_core_manager.cpp => cpu_manager.cpp} | 45 +++++++--------- src/core/cpu_manager.h | 52 +++++++++++++++++++ src/core/gdbstub/gdbstub.cpp | 2 +- src/core/hle/kernel/address_arbiter.cpp | 1 - src/core/hle/kernel/scheduler.cpp | 1 - src/core/hle/kernel/svc.cpp | 2 +- src/core/hle/kernel/thread.cpp | 3 +- src/core/hle/kernel/wait_object.cpp | 1 - 15 files changed, 115 insertions(+), 128 deletions(-) rename src/core/{core_cpu.cpp => core_manager.cpp} (85%) rename src/core/{core_cpu.h => core_manager.h} (91%) delete mode 100644 src/core/cpu_core_manager.h rename src/core/{cpu_core_manager.cpp => cpu_manager.cpp} (59%) create mode 100644 src/core/cpu_manager.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d5b8091ae..d342cafe0 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -15,14 +15,14 @@ add_library(core STATIC constants.h core.cpp core.h - core_cpu.cpp - core_cpu.h + core_manager.cpp + core_manager.h core_timing.cpp core_timing.h core_timing_util.cpp core_timing_util.h - cpu_core_manager.cpp - cpu_core_manager.h + cpu_manager.cpp + cpu_manager.h crypto/aes_util.cpp crypto/aes_util.h crypto/encryption_layer.cpp diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp index e825c0526..f468e57e4 100644 --- a/src/core/arm/dynarmic/arm_dynarmic.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic.cpp @@ -10,7 +10,7 @@ #include "common/microprofile.h" #include "core/arm/dynarmic/arm_dynarmic.h" #include "core/core.h" -#include "core/core_cpu.h" +#include "core/core_manager.h" #include "core/core_timing.h" #include "core/core_timing_util.h" #include "core/gdbstub/gdbstub.h" diff --git a/src/core/core.cpp b/src/core/core.cpp index 27b8d3408..bfe952515 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -11,9 +11,9 @@ #include "common/string_util.h" #include "core/arm/exclusive_monitor.h" #include "core/core.h" -#include "core/core_cpu.h" +#include "core/core_manager.h" #include "core/core_timing.h" -#include "core/cpu_core_manager.h" +#include "core/cpu_manager.h" #include "core/file_sys/bis_factory.h" #include "core/file_sys/card_image.h" #include "core/file_sys/mode.h" @@ -114,14 +114,14 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs, struct System::Impl { explicit Impl(System& system) : kernel{system}, fs_controller{system}, memory{system}, - cpu_core_manager{system}, reporter{system}, applet_manager{system} {} + cpu_manager{system}, reporter{system}, applet_manager{system} {} - Cpu& CurrentCpuCore() { - return cpu_core_manager.GetCurrentCore(); + CoreManager& CurrentCoreManager() { + return cpu_manager.GetCurrentCoreManager(); } Kernel::PhysicalCore& CurrentPhysicalCore() { - const auto i = cpu_core_manager.GetCurrentCoreIndex(); + const auto i = cpu_manager.GetActiveCoreIndex(); return kernel.PhysicalCore(i); } @@ -132,7 +132,7 @@ struct System::Impl { ResultStatus RunLoop(bool tight_loop) { status = ResultStatus::Success; - cpu_core_manager.RunLoop(tight_loop); + cpu_manager.RunLoop(tight_loop); return status; } @@ -142,7 +142,7 @@ struct System::Impl { core_timing.Initialize(); kernel.Initialize(); - cpu_core_manager.Initialize(); + cpu_manager.Initialize(); const auto current_time = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()); @@ -281,7 +281,7 @@ struct System::Impl { gpu_core.reset(); // Close all CPU/threading state - cpu_core_manager.Shutdown(); + cpu_manager.Shutdown(); // Shutdown kernel and core timing kernel.Shutdown(); @@ -351,7 +351,7 @@ struct System::Impl { std::unique_ptr gpu_core; std::unique_ptr interrupt_manager; Memory::Memory memory; - CpuCoreManager cpu_core_manager; + CpuManager cpu_manager; bool is_powered_on = false; bool exit_lock = false; @@ -386,12 +386,12 @@ struct System::Impl { System::System() : impl{std::make_unique(*this)} {} System::~System() = default; -Cpu& System::CurrentCpuCore() { - return impl->CurrentCpuCore(); +CoreManager& System::CurrentCoreManager() { + return impl->CurrentCoreManager(); } -const Cpu& System::CurrentCpuCore() const { - return impl->CurrentCpuCore(); +const CoreManager& System::CurrentCoreManager() const { + return impl->CurrentCoreManager(); } System::ResultStatus System::RunLoop(bool tight_loop) { @@ -415,13 +415,11 @@ bool System::IsPoweredOn() const { } void System::PrepareReschedule() { - CurrentCpuCore().PrepareReschedule(); + CurrentCoreManager().PrepareReschedule(); } void System::PrepareReschedule(const u32 core_index) { - if (core_index < GlobalScheduler().CpuCoresCount()) { - CpuCore(core_index).PrepareReschedule(); - } + impl->kernel.PrepareReschedule(core_index); } PerfStatsResults System::GetAndResetPerfStats() { @@ -445,7 +443,7 @@ const ARM_Interface& System::CurrentArmInterface() const { } std::size_t System::CurrentCoreIndex() const { - return CurrentCpuCore().CoreIndex(); + return impl->cpu_manager.GetActiveCoreIndex(); } Kernel::Scheduler& System::CurrentScheduler() { @@ -490,13 +488,13 @@ const ARM_Interface& System::ArmInterface(std::size_t core_index) const { return impl->GetPhysicalCore(core_index).ArmInterface(); } -Cpu& System::CpuCore(std::size_t core_index) { - return impl->cpu_core_manager.GetCore(core_index); +CoreManager& System::GetCoreManager(std::size_t core_index) { + return impl->cpu_manager.GetCoreManager(core_index); } -const Cpu& System::CpuCore(std::size_t core_index) const { +const CoreManager& System::GetCoreManager(std::size_t core_index) const { ASSERT(core_index < NUM_CPU_CORES); - return impl->cpu_core_manager.GetCore(core_index); + return impl->cpu_manager.GetCoreManager(core_index); } ExclusiveMonitor& System::Monitor() { diff --git a/src/core/core.h b/src/core/core.h index e240c5c58..e69d68fcf 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -93,7 +93,7 @@ class Memory; namespace Core { class ARM_Interface; -class Cpu; +class CoreManager; class ExclusiveMonitor; class FrameLimiter; class PerfStats; @@ -218,10 +218,10 @@ public: const ARM_Interface& ArmInterface(std::size_t core_index) const; /// Gets a CPU interface to the CPU core with the specified index - Cpu& CpuCore(std::size_t core_index); + CoreManager& GetCoreManager(std::size_t core_index); /// Gets a CPU interface to the CPU core with the specified index - const Cpu& CpuCore(std::size_t core_index) const; + const CoreManager& GetCoreManager(std::size_t core_index) const; /// Gets a reference to the exclusive monitor ExclusiveMonitor& Monitor(); @@ -364,10 +364,10 @@ private: System(); /// Returns the currently running CPU core - Cpu& CurrentCpuCore(); + CoreManager& CurrentCoreManager(); /// Returns the currently running CPU core - const Cpu& CurrentCpuCore() const; + const CoreManager& CurrentCoreManager() const; /** * Initialize the emulated system. diff --git a/src/core/core_cpu.cpp b/src/core/core_manager.cpp similarity index 85% rename from src/core/core_cpu.cpp rename to src/core/core_manager.cpp index bcfdf0198..bb03857d5 100644 --- a/src/core/core_cpu.cpp +++ b/src/core/core_manager.cpp @@ -12,7 +12,7 @@ #include "core/arm/exclusive_monitor.h" #include "core/arm/unicorn/arm_unicorn.h" #include "core/core.h" -#include "core/core_cpu.h" +#include "core/core_manager.h" #include "core/core_timing.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" @@ -23,15 +23,15 @@ namespace Core { -Cpu::Cpu(System& system, std::size_t core_index) +CoreManager::CoreManager(System& system, std::size_t core_index) : global_scheduler{system.GlobalScheduler()}, physical_core{system.Kernel().PhysicalCore(core_index)}, core_timing{system.CoreTiming()}, core_index{core_index} { } -Cpu::~Cpu() = default; +CoreManager::~CoreManager() = default; -void Cpu::RunLoop(bool tight_loop) { +void CoreManager::RunLoop(bool tight_loop) { Reschedule(); // If we don't have a currently active thread then don't execute instructions, @@ -51,15 +51,15 @@ void Cpu::RunLoop(bool tight_loop) { Reschedule(); } -void Cpu::SingleStep() { +void CoreManager::SingleStep() { return RunLoop(false); } -void Cpu::PrepareReschedule() { +void CoreManager::PrepareReschedule() { physical_core.Stop(); } -void Cpu::Reschedule() { +void CoreManager::Reschedule() { // Lock the global kernel mutex when we manipulate the HLE state std::lock_guard lock(HLE::g_hle_lock); diff --git a/src/core/core_cpu.h b/src/core/core_manager.h similarity index 91% rename from src/core/core_cpu.h rename to src/core/core_manager.h index 6f7aec8f9..7bc9679c1 100644 --- a/src/core/core_cpu.h +++ b/src/core/core_manager.h @@ -32,10 +32,10 @@ namespace Core { constexpr unsigned NUM_CPU_CORES{4}; -class Cpu { +class CoreManager { public: - Cpu(System& system, std::size_t core_index); - ~Cpu(); + CoreManager(System& system, std::size_t core_index); + ~CoreManager(); void RunLoop(bool tight_loop = true); diff --git a/src/core/cpu_core_manager.h b/src/core/cpu_core_manager.h deleted file mode 100644 index 2a7f84d5c..000000000 --- a/src/core/cpu_core_manager.h +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include - -namespace Core { - -class Cpu; -class System; - -class CpuCoreManager { -public: - explicit CpuCoreManager(System& system); - CpuCoreManager(const CpuCoreManager&) = delete; - CpuCoreManager(CpuCoreManager&&) = delete; - - ~CpuCoreManager(); - - CpuCoreManager& operator=(const CpuCoreManager&) = delete; - CpuCoreManager& operator=(CpuCoreManager&&) = delete; - - void Initialize(); - void Shutdown(); - - Cpu& GetCore(std::size_t index); - const Cpu& GetCore(std::size_t index) const; - - Cpu& GetCurrentCore(); - const Cpu& GetCurrentCore() const; - - std::size_t GetCurrentCoreIndex() const { - return active_core; - } - - void RunLoop(bool tight_loop); - -private: - static constexpr std::size_t NUM_CPU_CORES = 4; - - std::array, NUM_CPU_CORES> cores; - std::size_t active_core{}; ///< Active core, only used in single thread mode - - System& system; -}; - -} // namespace Core diff --git a/src/core/cpu_core_manager.cpp b/src/core/cpu_manager.cpp similarity index 59% rename from src/core/cpu_core_manager.cpp rename to src/core/cpu_manager.cpp index ab03e8fdf..abc8aad9e 100644 --- a/src/core/cpu_core_manager.cpp +++ b/src/core/cpu_manager.cpp @@ -5,56 +5,49 @@ #include "common/assert.h" #include "core/arm/exclusive_monitor.h" #include "core/core.h" -#include "core/core_cpu.h" +#include "core/core_manager.h" #include "core/core_timing.h" -#include "core/cpu_core_manager.h" +#include "core/cpu_manager.h" #include "core/gdbstub/gdbstub.h" #include "core/settings.h" namespace Core { -namespace { -void RunCpuCore(const System& system, Cpu& cpu_state) { - while (system.IsPoweredOn()) { - cpu_state.RunLoop(true); - } -} -} // Anonymous namespace -CpuCoreManager::CpuCoreManager(System& system) : system{system} {} -CpuCoreManager::~CpuCoreManager() = default; +CpuManager::CpuManager(System& system) : system{system} {} +CpuManager::~CpuManager() = default; -void CpuCoreManager::Initialize() { +void CpuManager::Initialize() { - for (std::size_t index = 0; index < cores.size(); ++index) { - cores[index] = std::make_unique(system, index); + for (std::size_t index = 0; index < core_managers.size(); ++index) { + core_managers[index] = std::make_unique(system, index); } } -void CpuCoreManager::Shutdown() { - for (auto& cpu_core : cores) { +void CpuManager::Shutdown() { + for (auto& cpu_core : core_managers) { cpu_core.reset(); } } -Cpu& CpuCoreManager::GetCore(std::size_t index) { - return *cores.at(index); +CoreManager& CpuManager::GetCoreManager(std::size_t index) { + return *core_managers.at(index); } -const Cpu& CpuCoreManager::GetCore(std::size_t index) const { - return *cores.at(index); +const CoreManager& CpuManager::GetCoreManager(std::size_t index) const { + return *core_managers.at(index); } -Cpu& CpuCoreManager::GetCurrentCore() { +CoreManager& CpuManager::GetCurrentCoreManager() { // Otherwise, use single-threaded mode active_core variable - return *cores[active_core]; + return *core_managers[active_core]; } -const Cpu& CpuCoreManager::GetCurrentCore() const { +const CoreManager& CpuManager::GetCurrentCoreManager() const { // Otherwise, use single-threaded mode active_core variable - return *cores[active_core]; + return *core_managers[active_core]; } -void CpuCoreManager::RunLoop(bool tight_loop) { +void CpuManager::RunLoop(bool tight_loop) { if (GDBStub::IsServerEnabled()) { GDBStub::HandlePacket(); @@ -77,7 +70,7 @@ void CpuCoreManager::RunLoop(bool tight_loop) { for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) { core_timing.SwitchContext(active_core); if (core_timing.CanCurrentContextRun()) { - cores[active_core]->RunLoop(tight_loop); + core_managers[active_core]->RunLoop(tight_loop); } keep_running |= core_timing.CanCurrentContextRun(); } diff --git a/src/core/cpu_manager.h b/src/core/cpu_manager.h new file mode 100644 index 000000000..5371d448e --- /dev/null +++ b/src/core/cpu_manager.h @@ -0,0 +1,52 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include + +namespace Core { + +class CoreManager; +class System; + +class CpuManager { +public: + explicit CpuManager(System& system); + CpuManager(const CpuManager&) = delete; + CpuManager(CpuManager&&) = delete; + + ~CpuManager(); + + CpuManager& operator=(const CpuManager&) = delete; + CpuManager& operator=(CpuManager&&) = delete; + + void Initialize(); + void Shutdown(); + + CoreManager& GetCoreManager(std::size_t index); + const CoreManager& GetCoreManager(std::size_t index) const; + + CoreManager& GetCurrentCoreManager(); + const CoreManager& GetCurrentCoreManager() const; + + std::size_t GetActiveCoreIndex() const { + return active_core; + } + + void RunLoop(bool tight_loop); + +private: + static constexpr std::size_t NUM_CPU_CORES = 4; + + std::array, NUM_CPU_CORES> core_managers; + std::size_t active_core{}; ///< Active core, only used in single thread mode + + System& system; +}; + +} // namespace Core diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp index 37cb28848..67e95999d 100644 --- a/src/core/gdbstub/gdbstub.cpp +++ b/src/core/gdbstub/gdbstub.cpp @@ -35,7 +35,7 @@ #include "common/swap.h" #include "core/arm/arm_interface.h" #include "core/core.h" -#include "core/core_cpu.h" +#include "core/core_manager.h" #include "core/gdbstub/gdbstub.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/scheduler.h" diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp index db189c8e3..2ea3dcb61 100644 --- a/src/core/hle/kernel/address_arbiter.cpp +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -8,7 +8,6 @@ #include "common/assert.h" #include "common/common_types.h" #include "core/core.h" -#include "core/core_cpu.h" #include "core/hle/kernel/address_arbiter.h" #include "core/hle/kernel/errors.h" #include "core/hle/kernel/scheduler.h" diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp index d36fcd7d9..eb196a690 100644 --- a/src/core/hle/kernel/scheduler.cpp +++ b/src/core/hle/kernel/scheduler.cpp @@ -14,7 +14,6 @@ #include "common/logging/log.h" #include "core/arm/arm_interface.h" #include "core/core.h" -#include "core/core_cpu.h" #include "core/core_timing.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index dbcdb0b88..1d99bf7a2 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -15,7 +15,7 @@ #include "common/string_util.h" #include "core/arm/exclusive_monitor.h" #include "core/core.h" -#include "core/core_cpu.h" +#include "core/core_manager.h" #include "core/core_timing.h" #include "core/core_timing_util.h" #include "core/hle/kernel/address_arbiter.h" diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index e84e5ce0d..278e46bad 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -13,7 +13,6 @@ #include "common/thread_queue_list.h" #include "core/arm/arm_interface.h" #include "core/core.h" -#include "core/core_cpu.h" #include "core/core_timing.h" #include "core/core_timing_util.h" #include "core/hle/kernel/errors.h" @@ -356,7 +355,7 @@ void Thread::SetActivity(ThreadActivity value) { // Set status if not waiting if (status == ThreadStatus::Ready || status == ThreadStatus::Running) { SetStatus(ThreadStatus::Paused); - Core::System::GetInstance().CpuCore(processor_id).PrepareReschedule(); + Core::System::GetInstance().PrepareReschedule(processor_id); } } else if (status == ThreadStatus::Paused) { // Ready to reschedule diff --git a/src/core/hle/kernel/wait_object.cpp b/src/core/hle/kernel/wait_object.cpp index 745f2c4e8..aa72889df 100644 --- a/src/core/hle/kernel/wait_object.cpp +++ b/src/core/hle/kernel/wait_object.cpp @@ -7,7 +7,6 @@ #include "common/common_types.h" #include "common/logging/log.h" #include "core/core.h" -#include "core/core_cpu.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/object.h" #include "core/hle/kernel/process.h"