summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/os/mutex.cpp
diff options
context:
space:
mode:
authorLiam <byteslice@airmail.cc>2024-02-07 05:09:43 +0100
committerLiam <byteslice@airmail.cc>2024-02-07 18:14:46 +0100
commit9404633bfd1e4ea23ccf8ef526b2b4c564ba512d (patch)
treefe37bb0acf2383ecc625f3c278000ba5869ccbaa /src/core/hle/service/os/mutex.cpp
parentMerge pull request #12883 from FernandoS27/memory_manager_mem (diff)
downloadyuzu-9404633bfd1e4ea23ccf8ef526b2b4c564ba512d.tar
yuzu-9404633bfd1e4ea23ccf8ef526b2b4c564ba512d.tar.gz
yuzu-9404633bfd1e4ea23ccf8ef526b2b4c564ba512d.tar.bz2
yuzu-9404633bfd1e4ea23ccf8ef526b2b4c564ba512d.tar.lz
yuzu-9404633bfd1e4ea23ccf8ef526b2b4c564ba512d.tar.xz
yuzu-9404633bfd1e4ea23ccf8ef526b2b4c564ba512d.tar.zst
yuzu-9404633bfd1e4ea23ccf8ef526b2b4c564ba512d.zip
Diffstat (limited to 'src/core/hle/service/os/mutex.cpp')
-rw-r--r--src/core/hle/service/os/mutex.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/core/hle/service/os/mutex.cpp b/src/core/hle/service/os/mutex.cpp
new file mode 100644
index 000000000..6009f4866
--- /dev/null
+++ b/src/core/hle/service/os/mutex.cpp
@@ -0,0 +1,46 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/kernel/k_synchronization_object.h"
+#include "core/hle/service/os/mutex.h"
+
+namespace Service {
+
+Mutex::Mutex(Core::System& system) : m_system(system) {
+ m_event = Kernel::KEvent::Create(system.Kernel());
+ m_event->Initialize(nullptr);
+
+ // Register the event.
+ Kernel::KEvent::Register(system.Kernel(), m_event);
+
+ ASSERT(R_SUCCEEDED(m_event->Signal()));
+}
+
+Mutex::~Mutex() {
+ m_event->GetReadableEvent().Close();
+ m_event->Close();
+}
+
+void Mutex::lock() {
+ // Infinitely retry until we successfully clear the event.
+ while (R_FAILED(m_event->GetReadableEvent().Reset())) {
+ s32 index;
+ Kernel::KSynchronizationObject* obj = &m_event->GetReadableEvent();
+
+ // The event was already cleared!
+ // Wait for it to become signaled again.
+ ASSERT(R_SUCCEEDED(
+ Kernel::KSynchronizationObject::Wait(m_system.Kernel(), &index, &obj, 1, -1)));
+ }
+
+ // We successfully cleared the event, and now have exclusive ownership.
+}
+
+void Mutex::unlock() {
+ // Unlock.
+ ASSERT(R_SUCCEEDED(m_event->Signal()));
+}
+
+} // namespace Service