From 7449ba85a6aeaec14af9535cd80e362548ea3f15 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Thu, 11 Oct 2018 16:00:09 -0400 Subject: [PATCH] Kernel: pass ref in Mutex --- src/core/hle/kernel/kernel.h | 9 +++++++++ src/core/hle/kernel/mutex.cpp | 6 +++--- src/core/hle/kernel/mutex.h | 12 +++--------- src/core/hle/kernel/svc.cpp | 2 +- src/core/hle/service/am/am.cpp | 6 +++--- src/core/hle/service/am/am.h | 2 +- src/core/hle/service/apt/apt.cpp | 2 +- src/core/hle/service/csnd/csnd_snd.cpp | 6 +++--- src/core/hle/service/csnd/csnd_snd.h | 4 +++- 9 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 94e2b60f6..28bad6efa 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -12,6 +12,7 @@ namespace Kernel { class AddressArbiter; class Event; +class Mutex; enum class ResetType { OneShot, @@ -41,6 +42,14 @@ public: * @param name Optional name of event */ SharedPtr CreateEvent(ResetType reset_type, std::string name = "Unknown"); + + /** + * Creates a mutex. + * @param initial_locked Specifies if the mutex should be locked initially + * @param name Optional name of mutex + * @return Pointer to new Mutex object + */ + SharedPtr CreateMutex(bool initial_locked, std::string name = "Unknown"); }; } // namespace Kernel diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index 02cbf613c..dc4a55d35 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp @@ -24,11 +24,11 @@ void ReleaseThreadMutexes(Thread* thread) { thread->held_mutexes.clear(); } -Mutex::Mutex() {} +Mutex::Mutex(KernelSystem& kernel) {} Mutex::~Mutex() {} -SharedPtr Mutex::Create(bool initial_locked, std::string name) { - SharedPtr mutex(new Mutex); +SharedPtr KernelSystem::CreateMutex(bool initial_locked, std::string name) { + SharedPtr mutex(new Mutex(*this)); mutex->lock_count = 0; mutex->name = std::move(name); diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h index 3afb99c59..ec0e3f794 100644 --- a/src/core/hle/kernel/mutex.h +++ b/src/core/hle/kernel/mutex.h @@ -16,14 +16,6 @@ class Thread; class Mutex final : public WaitObject { public: - /** - * Creates a mutex. - * @param initial_locked Specifies if the mutex should be locked initially - * @param name Optional name of mutex - * @return Pointer to new Mutex object - */ - static SharedPtr Create(bool initial_locked, std::string name = "Unknown"); - std::string GetTypeName() const override { return "Mutex"; } @@ -61,8 +53,10 @@ public: ResultCode Release(Thread* thread); private: - Mutex(); + explicit Mutex(KernelSystem& kernel); ~Mutex() override; + + friend class KernelSystem; }; /** diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 2c81b75fd..8ea242e6d 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -828,7 +828,7 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { /// Create a mutex static ResultCode CreateMutex(Handle* out_handle, u32 initial_locked) { - SharedPtr mutex = Mutex::Create(initial_locked != 0); + SharedPtr mutex = Core::System::GetInstance().Kernel().CreateMutex(initial_locked != 0); mutex->name = fmt::format("mutex-{:08x}", Core::CPU().GetReg(14)); CASCADE_RESULT(*out_handle, g_handle_table.Create(std::move(mutex))); diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 35e3c0ad9..e9f0c6227 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1455,16 +1455,16 @@ void Module::Interface::GetMetaDataFromCia(Kernel::HLERequestContext& ctx) { rb.PushMappedBuffer(output_buffer); } -Module::Module() { +Module::Module(Core::System& system) { ScanForAllTitles(); - system_updater_mutex = Kernel::Mutex::Create(false, "AM::SystemUpdaterMutex"); + system_updater_mutex = system.Kernel().CreateMutex(false, "AM::SystemUpdaterMutex"); } Module::~Module() = default; void InstallInterfaces(Core::System& system) { auto& service_manager = system.ServiceManager(); - auto am = std::make_shared(); + auto am = std::make_shared(system); std::make_shared(am)->InstallAsService(service_manager); std::make_shared(am)->InstallAsService(service_manager); std::make_shared(am)->InstallAsService(service_manager); diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 543dbd8fe..792225113 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -149,7 +149,7 @@ std::string GetMediaTitlePath(Service::FS::MediaType media_type); class Module final { public: - Module(); + explicit Module(Core::System& system); ~Module(); class Interface : public ServiceFramework { diff --git a/src/core/hle/service/apt/apt.cpp b/src/core/hle/service/apt/apt.cpp index a673fe97e..752e65c2c 100644 --- a/src/core/hle/service/apt/apt.cpp +++ b/src/core/hle/service/apt/apt.cpp @@ -859,7 +859,7 @@ Module::Module(Core::System& system) : system(system) { MemoryPermission::ReadWrite, MemoryPermission::Read, 0, Kernel::MemoryRegion::SYSTEM, "APT:SharedFont"); - lock = Kernel::Mutex::Create(false, "APT_U:Lock"); + lock = system.Kernel().CreateMutex(false, "APT_U:Lock"); } Module::~Module() {} diff --git a/src/core/hle/service/csnd/csnd_snd.cpp b/src/core/hle/service/csnd/csnd_snd.cpp index 6e0488b3a..8fc4f11f4 100644 --- a/src/core/hle/service/csnd/csnd_snd.cpp +++ b/src/core/hle/service/csnd/csnd_snd.cpp @@ -19,7 +19,7 @@ void CSND_SND::Initialize(Kernel::HLERequestContext& ctx) { const u32 offset3 = rp.Pop(); using Kernel::MemoryPermission; - mutex = Kernel::Mutex::Create(false, "CSND:mutex"); + mutex = system.Kernel().CreateMutex(false, "CSND:mutex"); shared_memory = Kernel::SharedMemory::Create(nullptr, size, MemoryPermission::ReadWrite, MemoryPermission::ReadWrite, 0, Kernel::MemoryRegion::BASE, "CSND:SharedMemory"); @@ -173,7 +173,7 @@ void CSND_SND::Reset(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_CSND, "(STUBBED) called"); } -CSND_SND::CSND_SND() : ServiceFramework("csnd:SND", 4) { +CSND_SND::CSND_SND(Core::System& system) : ServiceFramework("csnd:SND", 4), system(system) { static const FunctionInfo functions[] = { // clang-format off {0x00010140, &CSND_SND::Initialize, "Initialize"}, @@ -196,7 +196,7 @@ CSND_SND::CSND_SND() : ServiceFramework("csnd:SND", 4) { void InstallInterfaces(Core::System& system) { auto& service_manager = system.ServiceManager(); - std::make_shared()->InstallAsService(service_manager); + std::make_shared(system)->InstallAsService(service_manager); } } // namespace Service::CSND diff --git a/src/core/hle/service/csnd/csnd_snd.h b/src/core/hle/service/csnd/csnd_snd.h index 909034e6d..94b1bf3bb 100644 --- a/src/core/hle/service/csnd/csnd_snd.h +++ b/src/core/hle/service/csnd/csnd_snd.h @@ -16,7 +16,7 @@ namespace Service::CSND { class CSND_SND final : public ServiceFramework { public: - CSND_SND(); + explicit CSND_SND(Core::System& system); ~CSND_SND() = default; private: @@ -174,6 +174,8 @@ private: }; static_assert(sizeof(Type0Command) == 0x20, "Type0Command structure size is wrong"); + Core::System& system; + Kernel::SharedPtr mutex = nullptr; Kernel::SharedPtr shared_memory = nullptr;