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/hle/kernel/semaphore.cpp | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/core/hle/kernel/semaphore.cpp (limited to 'src/core/hle/kernel/semaphore.cpp') 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 -- 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/kernel/semaphore.cpp | 65 +++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 13 deletions(-) (limited to 'src/core/hle/kernel/semaphore.cpp') 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 -- 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 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/core/hle/kernel/semaphore.cpp') 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; -- 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(-) (limited to 'src/core/hle/kernel/semaphore.cpp') 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(-) (limited to 'src/core/hle/kernel/semaphore.cpp') 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 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) (limited to 'src/core/hle/kernel/semaphore.cpp') 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++; -- 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(-) (limited to 'src/core/hle/kernel/semaphore.cpp') 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(-) (limited to 'src/core/hle/kernel/semaphore.cpp') 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