From 82c84883a5d10bd6c9a3516fe16b996c5333360e Mon Sep 17 00:00:00 2001 From: Subv Date: Wed, 3 Dec 2014 18:49:51 -0500 Subject: SVC: Implemented svcCreateSemaphore ToDo: Implement svcReleaseSemaphore * Some testing against hardware needed --- src/core/CMakeLists.txt | 2 ++ src/core/hle/function_wrappers.h | 7 ++++ src/core/hle/kernel/semaphore.cpp | 76 +++++++++++++++++++++++++++++++++++++++ src/core/hle/kernel/semaphore.h | 22 ++++++++++++ src/core/hle/svc.cpp | 11 +++++- 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/core/hle/kernel/semaphore.cpp create mode 100644 src/core/hle/kernel/semaphore.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8f6792791..567d7454e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -28,6 +28,7 @@ set(SRCS hle/kernel/event.cpp hle/kernel/kernel.cpp hle/kernel/mutex.cpp + hle/kernel/semaphore.cpp hle/kernel/shared_memory.cpp hle/kernel/thread.cpp hle/service/ac_u.cpp @@ -106,6 +107,7 @@ set(HEADERS hle/kernel/event.h hle/kernel/kernel.h hle/kernel/mutex.h + hle/kernel/semaphore.h hle/kernel/shared_memory.h hle/kernel/thread.h hle/service/ac_u.h diff --git a/src/core/hle/function_wrappers.h b/src/core/hle/function_wrappers.h index 3dbe25037..dc3668624 100644 --- a/src/core/hle/function_wrappers.h +++ b/src/core/hle/function_wrappers.h @@ -114,6 +114,13 @@ template void Wrap() { FuncReturn(retval); } +template void Wrap() { + u32 param_1 = 0; + u32 retval = func(¶m_1, PARAM(1), PARAM(2)); + Core::g_app_core->SetReg(1, param_1); + FuncReturn(retval); +} + //////////////////////////////////////////////////////////////////////////////////////////////////// // Function wrappers that return type u32 diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp new file mode 100644 index 000000000..73ffbe3cf --- /dev/null +++ b/src/core/hle/kernel/semaphore.cpp @@ -0,0 +1,76 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include +#include + +#include "common/common.h" + +#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/semaphore.h" +#include "core/hle/kernel/thread.h" + +namespace Kernel { + +class Semaphore : public Object { +public: + std::string GetTypeName() const override { return "Semaphore"; } + std::string GetName() const override { return name; } + + static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Semaphore; } + Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Semaphore; } + + u32 initial_count; ///< Number of reserved entries + u32 max_count; ///< Maximum number of simultaneous holders the semaphore can have + u32 current_usage; ///< Number of currently used entries in the semaphore + std::vector waiting_threads; ///< Threads that are waiting for the semaphore + std::string name; ///< Name of semaphore (optional) + + ResultVal SyncRequest() override { + // TODO(Subv): ImplementMe + return MakeResult(false); + } + + ResultVal WaitSynchronization() override { + bool wait = current_usage == max_count; + + if (wait) { + Kernel::WaitCurrentThread(WAITTYPE_SEMA, GetHandle()); + waiting_threads.push_back(GetCurrentThreadHandle()); + } else { + ++current_usage; + } + + return MakeResult(wait); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/** + * Creates a semaphore + * @param handle Reference to handle for the newly created semaphore + * @param initial_count initial amount of times the semaphore is held + * @param max_count maximum number of holders the semaphore can have + * @param name Optional name of semaphore + * @return Pointer to new Semaphore object + */ +Semaphore* CreateSemaphore(Handle& handle, u32 initial_count, u32 max_count, const std::string& name) { + Semaphore* semaphore = new Semaphore; + handle = Kernel::g_object_pool.Create(semaphore); + + semaphore->initial_count = semaphore->current_usage = initial_count; + semaphore->max_count = max_count; + semaphore->name = name; + + return semaphore; +} + +Handle CreateSemaphore(u32 initial_count, u32 max_count, const std::string& name) { + Handle handle; + Semaphore* semaphore = CreateSemaphore(handle, initial_count, max_count, name); + return handle; +} + +} // namespace diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h new file mode 100644 index 000000000..6a686db2e --- /dev/null +++ b/src/core/hle/kernel/semaphore.h @@ -0,0 +1,22 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +namespace Kernel { + +/** + * Creates a semaphore + * @param initial_count number of reserved entries in the semaphore + * @param max_count maximum number of holders the semaphore can have + * @param name Optional name of semaphore + * @return Handle to newly created object + */ +Handle CreateSemaphore(u32 initial_count, u32 max_count, const std::string& name = "Unknown"); + +} // namespace diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index db0c42e74..107d12156 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -12,6 +12,7 @@ #include "core/hle/kernel/address_arbiter.h" #include "core/hle/kernel/event.h" #include "core/hle/kernel/mutex.h" +#include "core/hle/kernel/semaphore.h" #include "core/hle/kernel/shared_memory.h" #include "core/hle/kernel/thread.h" @@ -288,6 +289,14 @@ static Result GetThreadId(u32* thread_id, Handle handle) { return result.raw; } +/// Creates a semaphore +static Result CreateSemaphore(Handle* semaphore, s32 initial_count, s32 max_count) { + *semaphore = Kernel::CreateSemaphore(initial_count, max_count); + DEBUG_LOG(SVC, "called initial_count=%d, max_count=%d, created handle=0x%08X", + initial_count, max_count, *semaphore); + return 0; +} + /// Query memory static Result QueryMemory(void* info, void* out, u32 addr) { LOG_ERROR(Kernel_SVC, "(UNIMPLEMENTED) called addr=0x%08X", addr); @@ -366,7 +375,7 @@ const HLE::FunctionDef SVC_Table[] = { {0x12, nullptr, "Run"}, {0x13, HLE::Wrap, "CreateMutex"}, {0x14, HLE::Wrap, "ReleaseMutex"}, - {0x15, nullptr, "CreateSemaphore"}, + {0x15, HLE::Wrap, "CreateSemaphore"}, {0x16, nullptr, "ReleaseSemaphore"}, {0x17, HLE::Wrap, "CreateEvent"}, {0x18, HLE::Wrap, "SignalEvent"}, -- cgit v1.2.3 From 49b31badba5672bae3a5950abe3d45c883879c0d Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 4 Dec 2014 11:40:36 -0500 Subject: SVC: Implemented ReleaseSemaphore. This behavior was tested on hardware, however i'm still not sure what use the "initial_count" parameter has --- src/core/hle/function_wrappers.h | 7 +++++ src/core/hle/kernel/semaphore.cpp | 65 +++++++++++++++++++++++++++++++-------- src/core/hle/kernel/semaphore.h | 15 +++++++-- src/core/hle/svc.cpp | 13 ++++++-- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/core/hle/function_wrappers.h b/src/core/hle/function_wrappers.h index dc3668624..b44479b2f 100644 --- a/src/core/hle/function_wrappers.h +++ b/src/core/hle/function_wrappers.h @@ -121,6 +121,13 @@ template void Wrap() { FuncReturn(retval); } +template void Wrap() { + s32 param_1 = 0; + u32 retval = func(¶m_1, PARAM(1), PARAM(2)); + Core::g_app_core->SetReg(1, param_1); + FuncReturn(retval); +} + //////////////////////////////////////////////////////////////////////////////////////////////////// // Function wrappers that return type u32 diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 73ffbe3cf..674b727d5 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -2,8 +2,7 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. -#include -#include +#include #include "common/common.h" @@ -21,12 +20,20 @@ public: static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Semaphore; } Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Semaphore; } - u32 initial_count; ///< Number of reserved entries + u32 initial_count; ///< Number of reserved entries TODO(Subv): Make use of this u32 max_count; ///< Maximum number of simultaneous holders the semaphore can have u32 current_usage; ///< Number of currently used entries in the semaphore - std::vector waiting_threads; ///< Threads that are waiting for the semaphore + std::queue waiting_threads; ///< Threads that are waiting for the semaphore std::string name; ///< Name of semaphore (optional) + /** + * Tests whether a semaphore is at its peak capacity + * @return Whether the semaphore is full + */ + bool IsFull() const { + return current_usage == max_count; + } + ResultVal SyncRequest() override { // TODO(Subv): ImplementMe return MakeResult(false); @@ -37,7 +44,7 @@ public: if (wait) { Kernel::WaitCurrentThread(WAITTYPE_SEMA, GetHandle()); - waiting_threads.push_back(GetCurrentThreadHandle()); + waiting_threads.push(GetCurrentThreadHandle()); } else { ++current_usage; } @@ -56,21 +63,53 @@ public: * @param name Optional name of semaphore * @return Pointer to new Semaphore object */ -Semaphore* CreateSemaphore(Handle& handle, u32 initial_count, u32 max_count, const std::string& name) { +Semaphore* CreateSemaphore(Handle& handle, u32 initial_count, + u32 max_count, const std::string& name) { + Semaphore* semaphore = new Semaphore; - handle = Kernel::g_object_pool.Create(semaphore); + handle = g_object_pool.Create(semaphore); - semaphore->initial_count = semaphore->current_usage = initial_count; - semaphore->max_count = max_count; + semaphore->initial_count = initial_count; + // When the semaphore is created, all slots are used by the creator thread + semaphore->max_count = semaphore->current_usage = max_count; semaphore->name = name; return semaphore; } -Handle CreateSemaphore(u32 initial_count, u32 max_count, const std::string& name) { - Handle handle; - Semaphore* semaphore = CreateSemaphore(handle, initial_count, max_count, name); - return handle; +ResultCode CreateSemaphore(Handle* handle, u32 initial_count, + u32 max_count, const std::string& name) { + + if (initial_count > max_count) + return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::Kernel, + ErrorSummary::WrongArgument, ErrorLevel::Permanent); + Semaphore* semaphore = CreateSemaphore(*handle, initial_count, max_count, name); + + return RESULT_SUCCESS; +} + +ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) { + + Semaphore* semaphore = g_object_pool.Get(handle); + if (semaphore == nullptr) + return InvalidHandle(ErrorModule::Kernel); + + if (semaphore->current_usage < release_count) + return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Kernel, + ErrorSummary::InvalidArgument, ErrorLevel::Permanent); + + *count = semaphore->max_count - semaphore->current_usage; + semaphore->current_usage = semaphore->current_usage - release_count; + + // Notify some of the threads that the semaphore has been released + // stop once the semaphore is full again or there are no more waiting threads + while (!semaphore->waiting_threads.empty() && !semaphore->IsFull()) { + Kernel::ResumeThreadFromWait(semaphore->waiting_threads.front()); + semaphore->waiting_threads.pop(); + semaphore->current_usage++; + } + + return RESULT_SUCCESS; } } // namespace diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h index 6a686db2e..854831ecf 100644 --- a/src/core/hle/kernel/semaphore.h +++ b/src/core/hle/kernel/semaphore.h @@ -12,11 +12,20 @@ namespace Kernel { /** * Creates a semaphore + * @param handle Pointer to the handle of the newly created object * @param initial_count number of reserved entries in the semaphore * @param max_count maximum number of holders the semaphore can have - * @param name Optional name of semaphore - * @return Handle to newly created object + * @param name Optional name of semaphore + * @return ResultCode of the error */ -Handle CreateSemaphore(u32 initial_count, u32 max_count, const std::string& name = "Unknown"); +ResultCode CreateSemaphore(Handle* handle, u32 initial_count, u32 max_count, const std::string& name = "Unknown"); +/** + * Releases a certain number of slots from a semaphore + * @param count The number of free slots the semaphore had before this call + * @param handle The handle of the semaphore to release + * @param release_count The number of slots to release + * @return ResultCode of the error + */ +ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count); } // namespace diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index 107d12156..2846bb482 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -291,10 +291,17 @@ static Result GetThreadId(u32* thread_id, Handle handle) { /// Creates a semaphore static Result CreateSemaphore(Handle* semaphore, s32 initial_count, s32 max_count) { - *semaphore = Kernel::CreateSemaphore(initial_count, max_count); + ResultCode res = Kernel::CreateSemaphore(semaphore, initial_count, max_count); DEBUG_LOG(SVC, "called initial_count=%d, max_count=%d, created handle=0x%08X", initial_count, max_count, *semaphore); - return 0; + return res.raw; +} + +/// Releases a certain number of slots in a semaphore +static Result ReleaseSemaphore(s32* count, Handle semaphore, s32 release_count) { + DEBUG_LOG(SVC, "called release_count=%d, handle=0x%08X", release_count, semaphore); + ResultCode res = Kernel::ReleaseSemaphore(count, semaphore, release_count); + return res.raw; } /// Query memory @@ -376,7 +383,7 @@ const HLE::FunctionDef SVC_Table[] = { {0x13, HLE::Wrap, "CreateMutex"}, {0x14, HLE::Wrap, "ReleaseMutex"}, {0x15, HLE::Wrap, "CreateSemaphore"}, - {0x16, nullptr, "ReleaseSemaphore"}, + {0x16, HLE::Wrap, "ReleaseSemaphore"}, {0x17, HLE::Wrap, "CreateEvent"}, {0x18, HLE::Wrap, "SignalEvent"}, {0x19, HLE::Wrap, "ClearEvent"}, -- cgit v1.2.3 From abff4a7ee23a04baa9d264417c9c814309ef294b Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 4 Dec 2014 11:55:13 -0500 Subject: Semaphore: Implemented the initial_count parameter. --- src/core/hle/kernel/semaphore.cpp | 8 +++++--- src/core/hle/kernel/semaphore.h | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 674b727d5..c5c1fbeb3 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -20,7 +20,7 @@ public: static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Semaphore; } Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Semaphore; } - u32 initial_count; ///< Number of reserved entries TODO(Subv): Make use of this + u32 initial_count; ///< Number of entries reserved for other threads u32 max_count; ///< Maximum number of simultaneous holders the semaphore can have u32 current_usage; ///< Number of currently used entries in the semaphore std::queue waiting_threads; ///< Threads that are waiting for the semaphore @@ -58,7 +58,7 @@ public: /** * Creates a semaphore * @param handle Reference to handle for the newly created semaphore - * @param initial_count initial amount of times the semaphore is held + * @param initial_count number of slots reserved for other threads * @param max_count maximum number of holders the semaphore can have * @param name Optional name of semaphore * @return Pointer to new Semaphore object @@ -70,8 +70,10 @@ Semaphore* CreateSemaphore(Handle& handle, u32 initial_count, handle = g_object_pool.Create(semaphore); semaphore->initial_count = initial_count; - // When the semaphore is created, all slots are used by the creator thread + // When the semaphore is created, some slots are reserved for other threads, + // and the rest is reserved for the caller thread semaphore->max_count = semaphore->current_usage = max_count; + semaphore->current_usage -= initial_count; semaphore->name = name; return semaphore; diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h index 854831ecf..b29812e1d 100644 --- a/src/core/hle/kernel/semaphore.h +++ b/src/core/hle/kernel/semaphore.h @@ -13,8 +13,8 @@ namespace Kernel { /** * Creates a semaphore * @param handle Pointer to the handle of the newly created object - * @param initial_count number of reserved entries in the semaphore - * @param max_count maximum number of holders the semaphore can have + * @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 ResultCode of the error */ -- cgit v1.2.3 From 61434651d82b8ecfe7ed43b72841dfb7325e4ef4 Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 4 Dec 2014 15:03:39 -0500 Subject: Semaphores: Addressed some style issues --- src/core/hle/kernel/semaphore.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index c5c1fbeb3..c7afe49fc 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -57,17 +57,16 @@ public: /** * Creates a semaphore - * @param handle Reference to handle for the newly created semaphore * @param initial_count number of slots reserved for other threads * @param max_count maximum number of holders the semaphore can have * @param name Optional name of semaphore - * @return Pointer to new Semaphore object + * @return Handle for the newly created semaphore */ -Semaphore* CreateSemaphore(Handle& handle, u32 initial_count, +Handle CreateSemaphore(u32 initial_count, u32 max_count, const std::string& name) { Semaphore* semaphore = new Semaphore; - handle = g_object_pool.Create(semaphore); + Handle handle = g_object_pool.Create(semaphore); semaphore->initial_count = initial_count; // When the semaphore is created, some slots are reserved for other threads, @@ -76,7 +75,7 @@ Semaphore* CreateSemaphore(Handle& handle, u32 initial_count, semaphore->current_usage -= initial_count; semaphore->name = name; - return semaphore; + return handle; } ResultCode CreateSemaphore(Handle* handle, u32 initial_count, @@ -85,7 +84,7 @@ ResultCode CreateSemaphore(Handle* handle, u32 initial_count, if (initial_count > max_count) return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::Kernel, ErrorSummary::WrongArgument, ErrorLevel::Permanent); - Semaphore* semaphore = CreateSemaphore(*handle, initial_count, max_count, name); + *handle = CreateSemaphore(initial_count, max_count, name); return RESULT_SUCCESS; } -- cgit v1.2.3 From cc81a510e3ab61786f83df1cb2e55a0b29b7eefb Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 6 Dec 2014 00:22:44 -0500 Subject: Semaphore: Removed an unneeded function --- src/core/hle/kernel/semaphore.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index c7afe49fc..216c97835 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -34,11 +34,6 @@ public: return current_usage == max_count; } - ResultVal SyncRequest() override { - // TODO(Subv): ImplementMe - return MakeResult(false); - } - ResultVal WaitSynchronization() override { bool wait = current_usage == max_count; -- cgit v1.2.3 From 5e259862352eaef61567ee33b7d68f2f268344b5 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 12 Dec 2014 20:46:52 -0500 Subject: Kernel/Semaphores: Addressed some issues. --- src/core/hle/kernel/semaphore.cpp | 41 +++++++++++++-------------------------- src/core/hle/kernel/semaphore.h | 9 +++++---- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 216c97835..f7a895c3f 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -27,11 +27,11 @@ public: std::string name; ///< Name of semaphore (optional) /** - * Tests whether a semaphore is at its peak capacity - * @return Whether the semaphore is full + * Tests whether a semaphore still has free slots + * @return Whether the semaphore is available */ - bool IsFull() const { - return current_usage == max_count; + bool IsAvailable() const { + return current_usage < max_count; } ResultVal WaitSynchronization() override { @@ -50,42 +50,27 @@ public: //////////////////////////////////////////////////////////////////////////////////////////////////// -/** - * Creates a semaphore - * @param initial_count number of slots reserved for other threads - * @param max_count maximum number of holders the semaphore can have - * @param name Optional name of semaphore - * @return Handle for the newly created semaphore - */ -Handle CreateSemaphore(u32 initial_count, +ResultCode CreateSemaphore(Handle* handle, u32 initial_count, u32 max_count, const std::string& name) { + if (initial_count > max_count) + return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::Kernel, + ErrorSummary::WrongArgument, ErrorLevel::Permanent); + Semaphore* semaphore = new Semaphore; - Handle handle = g_object_pool.Create(semaphore); + *handle = g_object_pool.Create(semaphore); semaphore->initial_count = initial_count; // When the semaphore is created, some slots are reserved for other threads, // and the rest is reserved for the caller thread - semaphore->max_count = semaphore->current_usage = max_count; - semaphore->current_usage -= initial_count; + semaphore->max_count = max_count; + semaphore->current_usage = max_count - initial_count; semaphore->name = name; - return handle; -} - -ResultCode CreateSemaphore(Handle* handle, u32 initial_count, - u32 max_count, const std::string& name) { - - if (initial_count > max_count) - return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::Kernel, - ErrorSummary::WrongArgument, ErrorLevel::Permanent); - *handle = CreateSemaphore(initial_count, max_count, name); - return RESULT_SUCCESS; } ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) { - Semaphore* semaphore = g_object_pool.Get(handle); if (semaphore == nullptr) return InvalidHandle(ErrorModule::Kernel); @@ -99,7 +84,7 @@ ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) { // Notify some of the threads that the semaphore has been released // stop once the semaphore is full again or there are no more waiting threads - while (!semaphore->waiting_threads.empty() && !semaphore->IsFull()) { + while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) { Kernel::ResumeThreadFromWait(semaphore->waiting_threads.front()); semaphore->waiting_threads.pop(); semaphore->current_usage++; diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h index b29812e1d..f0075fdb8 100644 --- a/src/core/hle/kernel/semaphore.h +++ b/src/core/hle/kernel/semaphore.h @@ -11,21 +11,22 @@ namespace Kernel { /** - * Creates a semaphore + * Creates a semaphore. * @param handle Pointer to the handle of the newly created object - * @param initial_count number of slots reserved for other threads - * @param max_count maximum number of slots the semaphore can have + * @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 ResultCode of the error */ ResultCode CreateSemaphore(Handle* handle, u32 initial_count, u32 max_count, const std::string& name = "Unknown"); /** - * Releases a certain number of slots from a semaphore + * Releases a certain number of slots from a semaphore. * @param count The number of free slots the semaphore had before this call * @param handle The handle of the semaphore to release * @param release_count The number of slots to release * @return ResultCode of the error */ ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count); + } // namespace -- cgit v1.2.3 From effb18188848477e98a97102f358d7d4a38bd566 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 12 Dec 2014 22:22:11 -0500 Subject: Kernel/Semaphores: Invert the available count checking. Same semantics, idea by @yuriks --- src/core/hle/kernel/semaphore.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index f7a895c3f..331d32069 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -20,9 +20,8 @@ public: static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Semaphore; } Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Semaphore; } - u32 initial_count; ///< Number of entries reserved for other threads u32 max_count; ///< Maximum number of simultaneous holders the semaphore can have - u32 current_usage; ///< Number of currently used entries in the semaphore + u32 available_count; ///< Number of free slots left in the semaphore std::queue waiting_threads; ///< Threads that are waiting for the semaphore std::string name; ///< Name of semaphore (optional) @@ -31,17 +30,17 @@ public: * @return Whether the semaphore is available */ bool IsAvailable() const { - return current_usage < max_count; + return available_count > 0; } ResultVal WaitSynchronization() override { - bool wait = current_usage == max_count; + bool wait = available_count == 0; if (wait) { Kernel::WaitCurrentThread(WAITTYPE_SEMA, GetHandle()); waiting_threads.push(GetCurrentThreadHandle()); } else { - ++current_usage; + --available_count; } return MakeResult(wait); @@ -60,11 +59,10 @@ ResultCode CreateSemaphore(Handle* handle, u32 initial_count, Semaphore* semaphore = new Semaphore; *handle = g_object_pool.Create(semaphore); - semaphore->initial_count = initial_count; // When the semaphore is created, some slots are reserved for other threads, // and the rest is reserved for the caller thread semaphore->max_count = max_count; - semaphore->current_usage = max_count - initial_count; + semaphore->available_count = initial_count; semaphore->name = name; return RESULT_SUCCESS; @@ -75,19 +73,19 @@ ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) { if (semaphore == nullptr) return InvalidHandle(ErrorModule::Kernel); - if (semaphore->current_usage < release_count) + if (semaphore->max_count - semaphore->available_count < release_count) return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Kernel, ErrorSummary::InvalidArgument, ErrorLevel::Permanent); - *count = semaphore->max_count - semaphore->current_usage; - semaphore->current_usage = semaphore->current_usage - release_count; + *count = semaphore->available_count; + semaphore->available_count += release_count; // Notify some of the threads that the semaphore has been released // stop once the semaphore is full again or there are no more waiting threads while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) { Kernel::ResumeThreadFromWait(semaphore->waiting_threads.front()); semaphore->waiting_threads.pop(); - semaphore->current_usage++; + --semaphore->available_count; } return RESULT_SUCCESS; -- cgit v1.2.3 From ea958764318d7446618b838f24a5dc8099a76e3b Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 13 Dec 2014 10:29:11 -0500 Subject: Kernel/Semaphore: Small style change --- src/core/hle/kernel/semaphore.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 331d32069..6f56da8a9 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -34,7 +34,7 @@ public: } ResultVal WaitSynchronization() override { - bool wait = available_count == 0; + bool wait = !IsAvailable(); if (wait) { Kernel::WaitCurrentThread(WAITTYPE_SEMA, GetHandle()); -- cgit v1.2.3 From 1051795c329345ac6e08ac1d19024f56568b762d Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 13 Dec 2014 13:43:01 -0500 Subject: Kernel/Semaphores: Fixed build --- src/core/hle/svc.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index 2846bb482..f3595096e 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -292,14 +292,14 @@ static Result GetThreadId(u32* thread_id, Handle handle) { /// Creates a semaphore static Result CreateSemaphore(Handle* semaphore, s32 initial_count, s32 max_count) { ResultCode res = Kernel::CreateSemaphore(semaphore, initial_count, max_count); - DEBUG_LOG(SVC, "called initial_count=%d, max_count=%d, created handle=0x%08X", + LOG_TRACE(Kernel_SVC, "called initial_count=%d, max_count=%d, created handle=0x%08X", initial_count, max_count, *semaphore); return res.raw; } /// Releases a certain number of slots in a semaphore static Result ReleaseSemaphore(s32* count, Handle semaphore, s32 release_count) { - DEBUG_LOG(SVC, "called release_count=%d, handle=0x%08X", release_count, semaphore); + LOG_TRACE(Kernel_SVC, "called release_count=%d, handle=0x%08X", release_count, semaphore); ResultCode res = Kernel::ReleaseSemaphore(count, semaphore, release_count); return res.raw; } -- cgit v1.2.3