diff options
author | Fernando Sahmkow <fsahmkow27@gmail.com> | 2020-02-25 22:37:12 +0100 |
---|---|---|
committer | Fernando Sahmkow <fsahmkow27@gmail.com> | 2020-06-27 17:35:14 +0200 |
commit | 203e706302c24f278eec7d0bd2362ce73b0e2612 (patch) | |
tree | 2a725cf13ed569fd846f7ab603475ca1cd06302f | |
parent | SVC: Correct SignalEvent, ClearEvent, ResetSignal, WaitSynchronization, CancelSynchronization, ArbitrateLock (diff) | |
download | yuzu-203e706302c24f278eec7d0bd2362ce73b0e2612.tar yuzu-203e706302c24f278eec7d0bd2362ce73b0e2612.tar.gz yuzu-203e706302c24f278eec7d0bd2362ce73b0e2612.tar.bz2 yuzu-203e706302c24f278eec7d0bd2362ce73b0e2612.tar.lz yuzu-203e706302c24f278eec7d0bd2362ce73b0e2612.tar.xz yuzu-203e706302c24f278eec7d0bd2362ce73b0e2612.tar.zst yuzu-203e706302c24f278eec7d0bd2362ce73b0e2612.zip |
-rw-r--r-- | src/core/hle/kernel/mutex.cpp | 65 | ||||
-rw-r--r-- | src/core/hle/kernel/mutex.h | 3 | ||||
-rw-r--r-- | src/core/hle/kernel/thread.cpp | 2 |
3 files changed, 37 insertions, 33 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index 3520c5e49..18325db57 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp @@ -84,10 +84,11 @@ ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle, const auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); std::shared_ptr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle); - std::shared_ptr<Thread> requesting_thread = handle_table.Get<Thread>(requesting_thread_handle); + std::shared_ptr<Thread> requesting_thread = + handle_table.Get<Thread>(requesting_thread_handle); - // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of another - // thread. + // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of + // another thread. ASSERT(requesting_thread == current_thread); current_thread->SetSynchronizationResults(nullptr, RESULT_SUCCESS); @@ -123,47 +124,47 @@ ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle, return current_thread->GetSignalingResult(); } -ResultCode Mutex::Release(VAddr address) { - // The mutex address must be 4-byte aligned - if ((address % sizeof(u32)) != 0) { - LOG_ERROR(Kernel, "Address is not 4-byte aligned! address={:016X}", address); - return ERR_INVALID_ADDRESS; - } - - std::shared_ptr<Thread> current_thread = - SharedFrom(system.CurrentScheduler().GetCurrentThread()); - auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(current_thread, address); +std::pair<ResultCode, std::shared_ptr<Thread>> Mutex::Unlock(std::shared_ptr<Thread> owner, + VAddr address) { + // The mutex address must be 4-byte aligned + if ((address % sizeof(u32)) != 0) { + LOG_ERROR(Kernel, "Address is not 4-byte aligned! address={:016X}", address); + return {ERR_INVALID_ADDRESS, nullptr}; + } - // There are no more threads waiting for the mutex, release it completely. - if (thread == nullptr) { + auto [new_owner, num_waiters] = GetHighestPriorityMutexWaitingThread(owner, address); + if (new_owner == nullptr) { system.Memory().Write32(address, 0); - return RESULT_SUCCESS; + return {RESULT_SUCCESS, nullptr}; } - // Transfer the ownership of the mutex from the previous owner to the new one. - TransferMutexOwnership(address, current_thread, thread); - - u32 mutex_value = thread->GetWaitHandle(); - + TransferMutexOwnership(address, owner, new_owner); + u32 mutex_value = new_owner->GetWaitHandle(); if (num_waiters >= 2) { // Notify the guest that there are still some threads waiting for the mutex mutex_value |= Mutex::MutexHasWaitersFlag; } - - // Grant the mutex to the next waiting thread and resume it. + new_owner->SetSynchronizationResults(nullptr, RESULT_SUCCESS); + new_owner->ResumeFromWait(); + new_owner->SetLockOwner(nullptr); system.Memory().Write32(address, mutex_value); + return {RESULT_SUCCESS, new_owner}; +} + +ResultCode Mutex::Release(VAddr address) { + auto& kernel = system.Kernel(); + SchedulerLock lock(kernel); - ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex); - thread->ResumeFromWait(); + std::shared_ptr<Thread> current_thread = + SharedFrom(kernel.CurrentScheduler().GetCurrentThread()); - thread->SetLockOwner(nullptr); - thread->SetCondVarWaitAddress(0); - thread->SetMutexWaitAddress(0); - thread->SetWaitHandle(0); - thread->SetWaitSynchronizationResult(RESULT_SUCCESS); + auto [result, new_owner] = Unlock(current_thread, address); - system.PrepareReschedule(); + if (result != RESULT_SUCCESS && new_owner != nullptr) { + new_owner->SetSynchronizationResults(nullptr, result); + } - return RESULT_SUCCESS; + return result; } + } // namespace Kernel diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h index b904de2e8..bce06ecea 100644 --- a/src/core/hle/kernel/mutex.h +++ b/src/core/hle/kernel/mutex.h @@ -28,6 +28,9 @@ public: ResultCode TryAcquire(VAddr address, Handle holding_thread_handle, Handle requesting_thread_handle); + /// Unlocks a mutex for owner at address + std::pair<ResultCode, std::shared_ptr<Thread>> Unlock(std::shared_ptr<Thread> owner, VAddr address); + /// Releases the mutex at the specified address. ResultCode Release(VAddr address); diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 5fef3945b..f100ffc70 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -80,7 +80,7 @@ void Thread::CancelWakeupTimer() { void Thread::ResumeFromWait() { ASSERT_MSG(wait_objects.empty(), "Thread is waking up while waiting for objects"); - + SchedulerLock lock(kernel); switch (status) { case ThreadStatus::Paused: case ThreadStatus::WaitSynch: |