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 +++++++++++++++++++++++++++++++++++++++ src/core/hle/kernel/semaphore.h | 22 ++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/core/hle/kernel/semaphore.cpp create mode 100644 src/core/hle/kernel/semaphore.h (limited to 'src/core/hle/kernel') 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 -- cgit v1.2.3