From 69769d40107e50abddd35521d08c8245c295a738 Mon Sep 17 00:00:00 2001 From: inspuration Date: Wed, 11 Jun 2014 14:39:22 -0400 Subject: [PATCH] Finished primary work on semaphores, implemented mutexing --- src/core/hle/kernel/mutex.cpp | 8 +++++++- src/core/hle/kernel/semaphore.cpp | 27 +++++++++++++++++++++------ src/core/hle/svc.cpp | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index 1ccf1eb73..b2199ffe5 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp @@ -34,7 +34,13 @@ public: */ Result SyncRequest(bool* wait) { // TODO(bunnei): ImplementMe - locked = true; + if (!locked) { + locked = true; + *wait = false; + } + else { + *wait = true; + } return 0; } diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 49ab5fbd3..16a0f0f48 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -34,6 +34,13 @@ public: */ Result SyncRequest(bool* wait) { // TODO(bravia): ImplementMe + if (count > 0) { + count--; + *wait = false; + } + else{ + *wait = true; + } return 0; } @@ -43,7 +50,10 @@ public: * @return Result of operation, 0 on success, otherwise error code */ Result WaitSynchronization(bool* wait) { - // TODO(bravia): ImplementMe + *wait = (count == 0); + if (count == 0) { + Kernel::WaitCurrentThread(WAITTYPE_SEMA); + } return 0; } }; @@ -52,17 +62,22 @@ public: /** * Releases a semaphore - * @param unknown + * @param the previous count * @param handle Handle to mutex to release - * @param unknown + * @param the number of increments to be made */ Result ReleaseSemaphore(s32 * count, Handle handle, s32 release_count) { Semaphore* sem = Kernel::g_object_pool.GetFast(handle); - _assert_msg_(KERNEL, (sem != nullptr), "ReleaseSemaphore tried to release a nullptr sem!"); - //TODO(bravia): ImplementMe - return 0; + *count = sem->count; + if (sem->count + release_count <= sem->max_count) { + sem->count += release_count; + return 0; + } + else { + return -1; + } } /** diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index b20dec748..6ad405e74 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -284,7 +284,7 @@ Result CreateSemaphore(Handle* sem, s32 initial_count, s32 max_count) { return 0; } -/// Release a mutex +/// Release a Semaphore Result ReleaseSemaphore(s32* count, Handle handle, s32 releaseCount) { DEBUG_LOG(SVC, "called handle=0x%08X", handle); _assert_msg_(KERNEL, (handle != 0), "called, but handle is nullptr!");