summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Lioncash2019-03-14 00:29:54 -0400
committerGravatar Lioncash2019-03-14 20:55:52 -0400
commit555cd26ec2b848634370a14d41b7e79b6c6beecd (patch)
tree4ec6b8245280f94b44969356c9f2a44b22abd00e /src
parentMerge pull request #2230 from lioncash/global (diff)
downloadyuzu-555cd26ec2b848634370a14d41b7e79b6c6beecd.tar.gz
yuzu-555cd26ec2b848634370a14d41b7e79b6c6beecd.tar.xz
yuzu-555cd26ec2b848634370a14d41b7e79b6c6beecd.zip
core/hle/kernel: Make Mutex a per-process class.
Makes it an instantiable class like it is in the actual kernel. This will also allow removing reliance on global accessors in a following change, now that we can encapsulate a reference to the system instance in the class.
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/kernel/mutex.cpp9
-rw-r--r--src/core/hle/kernel/mutex.h20
-rw-r--r--src/core/hle/kernel/process.cpp3
-rw-r--r--src/core/hle/kernel/process.h16
-rw-r--r--src/core/hle/kernel/svc.cpp17
5 files changed, 47 insertions, 18 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp
index 0743670ad..260e25fde 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -2,7 +2,6 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <map>
6#include <utility> 5#include <utility>
7#include <vector> 6#include <vector>
8 7
@@ -10,8 +9,10 @@
10#include "core/core.h" 9#include "core/core.h"
11#include "core/hle/kernel/errors.h" 10#include "core/hle/kernel/errors.h"
12#include "core/hle/kernel/handle_table.h" 11#include "core/hle/kernel/handle_table.h"
12#include "core/hle/kernel/kernel.h"
13#include "core/hle/kernel/mutex.h" 13#include "core/hle/kernel/mutex.h"
14#include "core/hle/kernel/object.h" 14#include "core/hle/kernel/object.h"
15#include "core/hle/kernel/process.h"
15#include "core/hle/kernel/thread.h" 16#include "core/hle/kernel/thread.h"
16#include "core/hle/result.h" 17#include "core/hle/result.h"
17#include "core/memory.h" 18#include "core/memory.h"
@@ -57,13 +58,17 @@ static void TransferMutexOwnership(VAddr mutex_addr, SharedPtr<Thread> current_t
57 } 58 }
58} 59}
59 60
60ResultCode Mutex::TryAcquire(HandleTable& handle_table, VAddr address, Handle holding_thread_handle, 61Mutex::Mutex(Core::System& system) : system{system} {}
62Mutex::~Mutex() = default;
63
64ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
61 Handle requesting_thread_handle) { 65 Handle requesting_thread_handle) {
62 // The mutex address must be 4-byte aligned 66 // The mutex address must be 4-byte aligned
63 if ((address % sizeof(u32)) != 0) { 67 if ((address % sizeof(u32)) != 0) {
64 return ERR_INVALID_ADDRESS; 68 return ERR_INVALID_ADDRESS;
65 } 69 }
66 70
71 const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
67 SharedPtr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle); 72 SharedPtr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle);
68 SharedPtr<Thread> requesting_thread = handle_table.Get<Thread>(requesting_thread_handle); 73 SharedPtr<Thread> requesting_thread = handle_table.Get<Thread>(requesting_thread_handle);
69 74
diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h
index 81e62d497..b904de2e8 100644
--- a/src/core/hle/kernel/mutex.h
+++ b/src/core/hle/kernel/mutex.h
@@ -5,32 +5,34 @@
5#pragma once 5#pragma once
6 6
7#include "common/common_types.h" 7#include "common/common_types.h"
8#include "core/hle/kernel/object.h"
9 8
10union ResultCode; 9union ResultCode;
11 10
12namespace Kernel { 11namespace Core {
12class System;
13}
13 14
14class HandleTable; 15namespace Kernel {
15class Thread;
16 16
17class Mutex final { 17class Mutex final {
18public: 18public:
19 explicit Mutex(Core::System& system);
20 ~Mutex();
21
19 /// Flag that indicates that a mutex still has threads waiting for it. 22 /// Flag that indicates that a mutex still has threads waiting for it.
20 static constexpr u32 MutexHasWaitersFlag = 0x40000000; 23 static constexpr u32 MutexHasWaitersFlag = 0x40000000;
21 /// Mask of the bits in a mutex address value that contain the mutex owner. 24 /// Mask of the bits in a mutex address value that contain the mutex owner.
22 static constexpr u32 MutexOwnerMask = 0xBFFFFFFF; 25 static constexpr u32 MutexOwnerMask = 0xBFFFFFFF;
23 26
24 /// Attempts to acquire a mutex at the specified address. 27 /// Attempts to acquire a mutex at the specified address.
25 static ResultCode TryAcquire(HandleTable& handle_table, VAddr address, 28 ResultCode TryAcquire(VAddr address, Handle holding_thread_handle,
26 Handle holding_thread_handle, Handle requesting_thread_handle); 29 Handle requesting_thread_handle);
27 30
28 /// Releases the mutex at the specified address. 31 /// Releases the mutex at the specified address.
29 static ResultCode Release(VAddr address); 32 ResultCode Release(VAddr address);
30 33
31private: 34private:
32 Mutex() = default; 35 Core::System& system;
33 ~Mutex() = default;
34}; 36};
35 37
36} // namespace Kernel 38} // namespace Kernel
diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp
index 49fced7b1..406808ce9 100644
--- a/src/core/hle/kernel/process.cpp
+++ b/src/core/hle/kernel/process.cpp
@@ -231,7 +231,8 @@ void Process::LoadModule(CodeSet module_, VAddr base_addr) {
231} 231}
232 232
233Process::Process(Core::System& system) 233Process::Process(Core::System& system)
234 : WaitObject{system.Kernel()}, address_arbiter{system}, system{system} {} 234 : WaitObject{system.Kernel()}, address_arbiter{system}, mutex{system}, system{system} {}
235
235Process::~Process() = default; 236Process::~Process() = default;
236 237
237void Process::Acquire(Thread* thread) { 238void Process::Acquire(Thread* thread) {
diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h
index 47ffd4ad3..5e72300b6 100644
--- a/src/core/hle/kernel/process.h
+++ b/src/core/hle/kernel/process.h
@@ -14,6 +14,7 @@
14#include "common/common_types.h" 14#include "common/common_types.h"
15#include "core/hle/kernel/address_arbiter.h" 15#include "core/hle/kernel/address_arbiter.h"
16#include "core/hle/kernel/handle_table.h" 16#include "core/hle/kernel/handle_table.h"
17#include "core/hle/kernel/mutex.h"
17#include "core/hle/kernel/process_capability.h" 18#include "core/hle/kernel/process_capability.h"
18#include "core/hle/kernel/vm_manager.h" 19#include "core/hle/kernel/vm_manager.h"
19#include "core/hle/kernel/wait_object.h" 20#include "core/hle/kernel/wait_object.h"
@@ -165,6 +166,16 @@ public:
165 return address_arbiter; 166 return address_arbiter;
166 } 167 }
167 168
169 /// Gets a reference to the process' mutex lock.
170 Mutex& GetMutex() {
171 return mutex;
172 }
173
174 /// Gets a const reference to the process' mutex lock
175 const Mutex& GetMutex() const {
176 return mutex;
177 }
178
168 /// Gets the current status of the process 179 /// Gets the current status of the process
169 ProcessStatus GetStatus() const { 180 ProcessStatus GetStatus() const {
170 return status; 181 return status;
@@ -327,6 +338,11 @@ private:
327 /// Per-process address arbiter. 338 /// Per-process address arbiter.
328 AddressArbiter address_arbiter; 339 AddressArbiter address_arbiter;
329 340
341 /// The per-process mutex lock instance used for handling various
342 /// forms of services, such as lock arbitration, and condition
343 /// variable related facilities.
344 Mutex mutex;
345
330 /// Random values for svcGetInfo RandomEntropy 346 /// Random values for svcGetInfo RandomEntropy
331 std::array<u64, RANDOM_ENTROPY_SIZE> random_entropy; 347 std::array<u64, RANDOM_ENTROPY_SIZE> random_entropy;
332 348
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index 77d0e3d96..599609944 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -551,9 +551,9 @@ static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr,
551 return ERR_INVALID_ADDRESS; 551 return ERR_INVALID_ADDRESS;
552 } 552 }
553 553
554 auto& handle_table = Core::CurrentProcess()->GetHandleTable(); 554 auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess();
555 return Mutex::TryAcquire(handle_table, mutex_addr, holding_thread_handle, 555 return current_process->GetMutex().TryAcquire(mutex_addr, holding_thread_handle,
556 requesting_thread_handle); 556 requesting_thread_handle);
557} 557}
558 558
559/// Unlock a mutex 559/// Unlock a mutex
@@ -571,7 +571,8 @@ static ResultCode ArbitrateUnlock(VAddr mutex_addr) {
571 return ERR_INVALID_ADDRESS; 571 return ERR_INVALID_ADDRESS;
572 } 572 }
573 573
574 return Mutex::Release(mutex_addr); 574 auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess();
575 return current_process->GetMutex().Release(mutex_addr);
575} 576}
576 577
577enum class BreakType : u32 { 578enum class BreakType : u32 {
@@ -1336,11 +1337,15 @@ static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_var
1336 "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}", 1337 "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}",
1337 mutex_addr, condition_variable_addr, thread_handle, nano_seconds); 1338 mutex_addr, condition_variable_addr, thread_handle, nano_seconds);
1338 1339
1339 const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); 1340 auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess();
1341 const auto& handle_table = current_process->GetHandleTable();
1340 SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); 1342 SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
1341 ASSERT(thread); 1343 ASSERT(thread);
1342 1344
1343 CASCADE_CODE(Mutex::Release(mutex_addr)); 1345 const auto release_result = current_process->GetMutex().Release(mutex_addr);
1346 if (release_result.IsError()) {
1347 return release_result;
1348 }
1344 1349
1345 SharedPtr<Thread> current_thread = GetCurrentThread(); 1350 SharedPtr<Thread> current_thread = GetCurrentThread();
1346 current_thread->SetCondVarWaitAddress(condition_variable_addr); 1351 current_thread->SetCondVarWaitAddress(condition_variable_addr);