diff options
| author | 2019-03-21 22:18:36 -0400 | |
|---|---|---|
| committer | 2019-03-21 22:18:36 -0400 | |
| commit | 7b6d516faa788c25e26c79e2e5d19915f73983a5 (patch) | |
| tree | d477c1598d78ad60360f2ab08d3b201fff9f38bd /src/core/hle/kernel/mutex.cpp | |
| parent | Merge pull request #2274 from lioncash/include (diff) | |
| parent | core/hle/kernel/mutex: Remove usages of global system accessors (diff) | |
| download | yuzu-7b6d516faa788c25e26c79e2e5d19915f73983a5.tar.gz yuzu-7b6d516faa788c25e26c79e2e5d19915f73983a5.tar.xz yuzu-7b6d516faa788c25e26c79e2e5d19915f73983a5.zip | |
Merge pull request #2234 from lioncash/mutex
core/hle/kernel: Make Mutex a per-process class.
Diffstat (limited to 'src/core/hle/kernel/mutex.cpp')
| -rw-r--r-- | src/core/hle/kernel/mutex.cpp | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index 0743670ad..98e87313b 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,11 @@ | |||
| 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" | ||
| 16 | #include "core/hle/kernel/scheduler.h" | ||
| 15 | #include "core/hle/kernel/thread.h" | 17 | #include "core/hle/kernel/thread.h" |
| 16 | #include "core/hle/result.h" | 18 | #include "core/hle/result.h" |
| 17 | #include "core/memory.h" | 19 | #include "core/memory.h" |
| @@ -57,41 +59,47 @@ static void TransferMutexOwnership(VAddr mutex_addr, SharedPtr<Thread> current_t | |||
| 57 | } | 59 | } |
| 58 | } | 60 | } |
| 59 | 61 | ||
| 60 | ResultCode Mutex::TryAcquire(HandleTable& handle_table, VAddr address, Handle holding_thread_handle, | 62 | Mutex::Mutex(Core::System& system) : system{system} {} |
| 63 | Mutex::~Mutex() = default; | ||
| 64 | |||
| 65 | ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle, | ||
| 61 | Handle requesting_thread_handle) { | 66 | Handle requesting_thread_handle) { |
| 62 | // The mutex address must be 4-byte aligned | 67 | // The mutex address must be 4-byte aligned |
| 63 | if ((address % sizeof(u32)) != 0) { | 68 | if ((address % sizeof(u32)) != 0) { |
| 64 | return ERR_INVALID_ADDRESS; | 69 | return ERR_INVALID_ADDRESS; |
| 65 | } | 70 | } |
| 66 | 71 | ||
| 72 | const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); | ||
| 73 | Thread* const current_thread = system.CurrentScheduler().GetCurrentThread(); | ||
| 67 | SharedPtr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle); | 74 | SharedPtr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle); |
| 68 | SharedPtr<Thread> requesting_thread = handle_table.Get<Thread>(requesting_thread_handle); | 75 | SharedPtr<Thread> requesting_thread = handle_table.Get<Thread>(requesting_thread_handle); |
| 69 | 76 | ||
| 70 | // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of another | 77 | // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of another |
| 71 | // thread. | 78 | // thread. |
| 72 | ASSERT(requesting_thread == GetCurrentThread()); | 79 | ASSERT(requesting_thread == current_thread); |
| 73 | 80 | ||
| 74 | u32 addr_value = Memory::Read32(address); | 81 | const u32 addr_value = Memory::Read32(address); |
| 75 | 82 | ||
| 76 | // If the mutex isn't being held, just return success. | 83 | // If the mutex isn't being held, just return success. |
| 77 | if (addr_value != (holding_thread_handle | Mutex::MutexHasWaitersFlag)) { | 84 | if (addr_value != (holding_thread_handle | Mutex::MutexHasWaitersFlag)) { |
| 78 | return RESULT_SUCCESS; | 85 | return RESULT_SUCCESS; |
| 79 | } | 86 | } |
| 80 | 87 | ||
| 81 | if (holding_thread == nullptr) | 88 | if (holding_thread == nullptr) { |
| 82 | return ERR_INVALID_HANDLE; | 89 | return ERR_INVALID_HANDLE; |
| 90 | } | ||
| 83 | 91 | ||
| 84 | // Wait until the mutex is released | 92 | // Wait until the mutex is released |
| 85 | GetCurrentThread()->SetMutexWaitAddress(address); | 93 | current_thread->SetMutexWaitAddress(address); |
| 86 | GetCurrentThread()->SetWaitHandle(requesting_thread_handle); | 94 | current_thread->SetWaitHandle(requesting_thread_handle); |
| 87 | 95 | ||
| 88 | GetCurrentThread()->SetStatus(ThreadStatus::WaitMutex); | 96 | current_thread->SetStatus(ThreadStatus::WaitMutex); |
| 89 | GetCurrentThread()->InvalidateWakeupCallback(); | 97 | current_thread->InvalidateWakeupCallback(); |
| 90 | 98 | ||
| 91 | // Update the lock holder thread's priority to prevent priority inversion. | 99 | // Update the lock holder thread's priority to prevent priority inversion. |
| 92 | holding_thread->AddMutexWaiter(GetCurrentThread()); | 100 | holding_thread->AddMutexWaiter(current_thread); |
| 93 | 101 | ||
| 94 | Core::System::GetInstance().PrepareReschedule(); | 102 | system.PrepareReschedule(); |
| 95 | 103 | ||
| 96 | return RESULT_SUCCESS; | 104 | return RESULT_SUCCESS; |
| 97 | } | 105 | } |
| @@ -102,7 +110,8 @@ ResultCode Mutex::Release(VAddr address) { | |||
| 102 | return ERR_INVALID_ADDRESS; | 110 | return ERR_INVALID_ADDRESS; |
| 103 | } | 111 | } |
| 104 | 112 | ||
| 105 | auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(GetCurrentThread(), address); | 113 | auto* const current_thread = system.CurrentScheduler().GetCurrentThread(); |
| 114 | auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(current_thread, address); | ||
| 106 | 115 | ||
| 107 | // There are no more threads waiting for the mutex, release it completely. | 116 | // There are no more threads waiting for the mutex, release it completely. |
| 108 | if (thread == nullptr) { | 117 | if (thread == nullptr) { |
| @@ -111,7 +120,7 @@ ResultCode Mutex::Release(VAddr address) { | |||
| 111 | } | 120 | } |
| 112 | 121 | ||
| 113 | // Transfer the ownership of the mutex from the previous owner to the new one. | 122 | // Transfer the ownership of the mutex from the previous owner to the new one. |
| 114 | TransferMutexOwnership(address, GetCurrentThread(), thread); | 123 | TransferMutexOwnership(address, current_thread, thread); |
| 115 | 124 | ||
| 116 | u32 mutex_value = thread->GetWaitHandle(); | 125 | u32 mutex_value = thread->GetWaitHandle(); |
| 117 | 126 | ||