summaryrefslogtreecommitdiff
path: root/src/core/hle/service/mutex.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/service/mutex.cpp')
-rw-r--r--src/core/hle/service/mutex.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/core/hle/service/mutex.cpp b/src/core/hle/service/mutex.cpp
new file mode 100644
index 000000000..07589a0f0
--- /dev/null
+++ b/src/core/hle/service/mutex.cpp
@@ -0,0 +1,43 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include "core/core.h"
5#include "core/hle/kernel/k_event.h"
6#include "core/hle/kernel/k_synchronization_object.h"
7#include "core/hle/service/mutex.h"
8
9namespace Service {
10
11Mutex::Mutex(Core::System& system) : m_system(system) {
12 m_event = Kernel::KEvent::Create(system.Kernel());
13 m_event->Initialize(nullptr);
14
15 ASSERT(R_SUCCEEDED(m_event->Signal()));
16}
17
18Mutex::~Mutex() {
19 m_event->GetReadableEvent().Close();
20 m_event->Close();
21}
22
23void Mutex::lock() {
24 // Infinitely retry until we successfully clear the event.
25 while (R_FAILED(m_event->GetReadableEvent().Reset())) {
26 s32 index;
27 Kernel::KSynchronizationObject* obj = &m_event->GetReadableEvent();
28
29 // The event was already cleared!
30 // Wait for it to become signaled again.
31 ASSERT(R_SUCCEEDED(
32 Kernel::KSynchronizationObject::Wait(m_system.Kernel(), &index, &obj, 1, -1)));
33 }
34
35 // We successfully cleared the event, and now have exclusive ownership.
36}
37
38void Mutex::unlock() {
39 // Unlock.
40 ASSERT(R_SUCCEEDED(m_event->Signal()));
41}
42
43} // namespace Service