aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/hle/kernel/semaphore.cpp
diff options
context:
space:
mode:
authorGravatar Subv <subv2112@gmail.com>2014-12-03 18:49:51 -0500
committerGravatar Subv <subv2112@gmail.com>2014-12-13 13:40:09 -0500
commit82c84883a5d10bd6c9a3516fe16b996c5333360e (patch)
tree79016ba73405e93affa092432c8c0b86bd917aac /src/core/hle/kernel/semaphore.cpp
parent9ce11b2d925b08352f9349ecbf228431581331f4 (diff)
SVC: Implemented svcCreateSemaphore
ToDo: Implement svcReleaseSemaphore * Some testing against hardware needed
Diffstat (limited to 'src/core/hle/kernel/semaphore.cpp')
-rw-r--r--src/core/hle/kernel/semaphore.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp
new file mode 100644
index 00000000..73ffbe3c
--- /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 <map>
+#include <vector>
+
+#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<Handle> waiting_threads; ///< Threads that are waiting for the semaphore
+ std::string name; ///< Name of semaphore (optional)
+
+ ResultVal<bool> SyncRequest() override {
+ // TODO(Subv): ImplementMe
+ return MakeResult<bool>(false);
+ }
+
+ ResultVal<bool> 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<bool>(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