From 247249d5d3a4974a5bf7d8e712fe23048925bbdf Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 12 Oct 2018 16:12:08 -0400 Subject: [PATCH] Kernel: pass ref to sempahore --- src/core/hle/kernel/kernel.h | 11 +++++++++++ src/core/hle/kernel/semaphore.cpp | 8 ++++---- src/core/hle/kernel/semaphore.h | 14 +++----------- src/core/hle/kernel/svc.cpp | 3 ++- src/core/hle/service/sm/srv.cpp | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index aca8a5fab..beff0c31a 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -17,6 +17,7 @@ class Mutex; class CodeSet; class Process; class Thread; +class Semaphore; enum class ResetType { OneShot, @@ -73,6 +74,16 @@ public: ResultVal> CreateThread(std::string name, VAddr entry_point, u32 priority, u32 arg, s32 processor_id, VAddr stack_top, SharedPtr owner_process); + + /** + * Creates a semaphore. + * @param initial_count Number of slots reserved for other threads + * @param max_count Maximum number of slots the semaphore can have + * @param name Optional name of semaphore + * @return The created semaphore + */ + ResultVal> CreateSemaphore(s32 initial_count, s32 max_count, + std::string name = "Unknown"); }; } // namespace Kernel diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 4ec1669ba..afe770a64 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -10,16 +10,16 @@ namespace Kernel { -Semaphore::Semaphore() {} +Semaphore::Semaphore(KernelSystem& kernel) {} Semaphore::~Semaphore() {} -ResultVal> Semaphore::Create(s32 initial_count, s32 max_count, - std::string name) { +ResultVal> KernelSystem::CreateSemaphore(s32 initial_count, s32 max_count, + std::string name) { if (initial_count > max_count) return ERR_INVALID_COMBINATION_KERNEL; - SharedPtr semaphore(new Semaphore); + SharedPtr semaphore(new Semaphore(*this)); // When the semaphore is created, some slots are reserved for other threads, // and the rest is reserved for the caller thread diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h index 6019b09a2..7303ee7d1 100644 --- a/src/core/hle/kernel/semaphore.h +++ b/src/core/hle/kernel/semaphore.h @@ -15,16 +15,6 @@ namespace Kernel { class Semaphore final : public WaitObject { public: - /** - * Creates a semaphore. - * @param initial_count Number of slots reserved for other threads - * @param max_count Maximum number of slots the semaphore can have - * @param name Optional name of semaphore - * @return The created semaphore - */ - static ResultVal> Create(s32 initial_count, s32 max_count, - std::string name = "Unknown"); - std::string GetTypeName() const override { return "Semaphore"; } @@ -52,8 +42,10 @@ public: ResultVal Release(s32 release_count); private: - Semaphore(); + explicit Semaphore(KernelSystem& kernel); ~Semaphore() override; + + friend class KernelSystem; }; } // namespace Kernel diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index e61bf53b0..228e12ee8 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -891,7 +891,8 @@ static ResultCode GetThreadId(u32* thread_id, Handle handle) { /// Creates a semaphore static ResultCode CreateSemaphore(Handle* out_handle, s32 initial_count, s32 max_count) { - CASCADE_RESULT(SharedPtr semaphore, Semaphore::Create(initial_count, max_count)); + CASCADE_RESULT(SharedPtr semaphore, + Core::System::GetInstance().Kernel().CreateSemaphore(initial_count, max_count)); semaphore->name = fmt::format("semaphore-{:08x}", Core::CPU().GetReg(14)); CASCADE_RESULT(*out_handle, g_handle_table.Create(std::move(semaphore))); diff --git a/src/core/hle/service/sm/srv.cpp b/src/core/hle/service/sm/srv.cpp index 83fb94418..07b47ea9b 100644 --- a/src/core/hle/service/sm/srv.cpp +++ b/src/core/hle/service/sm/srv.cpp @@ -63,7 +63,7 @@ void SRV::EnableNotification(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp(ctx, 0x2, 0, 0); notification_semaphore = - Kernel::Semaphore::Create(0, MAX_PENDING_NOTIFICATIONS, "SRV:Notification").Unwrap(); + system.Kernel().CreateSemaphore(0, MAX_PENDING_NOTIFICATIONS, "SRV:Notification").Unwrap(); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(RESULT_SUCCESS);