From 4e33b4b42f404ff6250df15f1a48ed96ce839b77 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 7 Jan 2018 16:52:23 -0500 Subject: [PATCH] semaphore: More changes for Switch. --- src/core/hle/kernel/semaphore.cpp | 20 +++++++++++++------- src/core/hle/kernel/semaphore.h | 8 ++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 3f364661b..9c58aa42f 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -14,7 +14,8 @@ namespace Kernel { Semaphore::Semaphore() {} Semaphore::~Semaphore() {} -ResultVal> Semaphore::Create(VAddr guest_addr, VAddr mutex_addr, std::string name) { +ResultVal> Semaphore::Create(VAddr guest_addr, VAddr mutex_addr, + std::string name) { SharedPtr semaphore(new Semaphore); // When the semaphore is created, some slots are reserved for other threads, @@ -37,23 +38,28 @@ bool Semaphore::ShouldWait(Thread* thread) const { void Semaphore::Acquire(Thread* thread) { if (available_count <= 0) return; + --available_count; UpdateGuestState(); } -ResultVal Semaphore::Release(s32 release_count) { - s32 previous_count = available_count; - available_count += release_count; +ResultCode Semaphore::Release(s32 target) { + ++available_count; UpdateGuestState(); - WakeupAllWaitingThreads(); + if (target == -1) { + // When -1, wake up all waiting threads + WakeupAllWaitingThreads(); + } else { + // Otherwise, wake up just a single thread + WakeupWaitingThread(GetHighestPriorityReadyThread()); + } - return MakeResult(previous_count); + return RESULT_SUCCESS; } void Semaphore::UpdateGuestState() { Memory::Write32(guest_addr, available_count); } - } // namespace Kernel diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h index 9cad4450a..e80230cac 100644 --- a/src/core/hle/kernel/semaphore.h +++ b/src/core/hle/kernel/semaphore.h @@ -50,11 +50,11 @@ public: void Acquire(Thread* thread) override; /** - * Releases a certain number of slots from a semaphore. - * @param release_count The number of slots to release - * @return The number of free slots the semaphore had before this call + * Releases a slot from a semaphore. + * @param target The number of threads to wakeup, -1 is all. + * @return ResultCode indicating if the operation succeeded. */ - ResultVal Release(s32 release_count); + ResultCode Release(s32 target); private: Semaphore();