yuzu-emu
/
yuzu
Archived
1
0
Fork 0

SVC: Correct ArbitrateUnlock

This commit is contained in:
Fernando Sahmkow 2020-02-25 17:37:12 -04:00
parent 3b5b950c89
commit 203e706302
3 changed files with 41 additions and 37 deletions

View File

@ -84,10 +84,11 @@ ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
const auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); const auto& handle_table = kernel.CurrentProcess()->GetHandleTable();
std::shared_ptr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle); 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 // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of
// thread. // another thread.
ASSERT(requesting_thread == current_thread); ASSERT(requesting_thread == current_thread);
current_thread->SetSynchronizationResults(nullptr, RESULT_SUCCESS); current_thread->SetSynchronizationResults(nullptr, RESULT_SUCCESS);
@ -123,47 +124,47 @@ ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
return current_thread->GetSignalingResult(); return current_thread->GetSignalingResult();
} }
ResultCode Mutex::Release(VAddr address) { std::pair<ResultCode, std::shared_ptr<Thread>> Mutex::Unlock(std::shared_ptr<Thread> owner,
// The mutex address must be 4-byte aligned VAddr address) {
if ((address % sizeof(u32)) != 0) { // The mutex address must be 4-byte aligned
LOG_ERROR(Kernel, "Address is not 4-byte aligned! address={:016X}", address); if ((address % sizeof(u32)) != 0) {
return ERR_INVALID_ADDRESS; LOG_ERROR(Kernel, "Address is not 4-byte aligned! address={:016X}", address);
} return {ERR_INVALID_ADDRESS, nullptr};
}
std::shared_ptr<Thread> current_thread = auto [new_owner, num_waiters] = GetHighestPriorityMutexWaitingThread(owner, address);
SharedFrom(system.CurrentScheduler().GetCurrentThread()); if (new_owner == nullptr) {
auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(current_thread, address);
// There are no more threads waiting for the mutex, release it completely.
if (thread == nullptr) {
system.Memory().Write32(address, 0); 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. // Transfer the ownership of the mutex from the previous owner to the new one.
TransferMutexOwnership(address, current_thread, thread); TransferMutexOwnership(address, owner, new_owner);
u32 mutex_value = new_owner->GetWaitHandle();
u32 mutex_value = thread->GetWaitHandle();
if (num_waiters >= 2) { if (num_waiters >= 2) {
// Notify the guest that there are still some threads waiting for the mutex // Notify the guest that there are still some threads waiting for the mutex
mutex_value |= Mutex::MutexHasWaitersFlag; mutex_value |= Mutex::MutexHasWaitersFlag;
} }
new_owner->SetSynchronizationResults(nullptr, RESULT_SUCCESS);
// Grant the mutex to the next waiting thread and resume it. new_owner->ResumeFromWait();
new_owner->SetLockOwner(nullptr);
system.Memory().Write32(address, mutex_value); system.Memory().Write32(address, mutex_value);
return {RESULT_SUCCESS, new_owner};
ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
thread->ResumeFromWait();
thread->SetLockOwner(nullptr);
thread->SetCondVarWaitAddress(0);
thread->SetMutexWaitAddress(0);
thread->SetWaitHandle(0);
thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
system.PrepareReschedule();
return RESULT_SUCCESS;
} }
ResultCode Mutex::Release(VAddr address) {
auto& kernel = system.Kernel();
SchedulerLock lock(kernel);
std::shared_ptr<Thread> current_thread =
SharedFrom(kernel.CurrentScheduler().GetCurrentThread());
auto [result, new_owner] = Unlock(current_thread, address);
if (result != RESULT_SUCCESS && new_owner != nullptr) {
new_owner->SetSynchronizationResults(nullptr, result);
}
return result;
}
} // namespace Kernel } // namespace Kernel

View File

@ -28,6 +28,9 @@ public:
ResultCode TryAcquire(VAddr address, Handle holding_thread_handle, ResultCode TryAcquire(VAddr address, Handle holding_thread_handle,
Handle requesting_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. /// Releases the mutex at the specified address.
ResultCode Release(VAddr address); ResultCode Release(VAddr address);

View File

@ -80,7 +80,7 @@ void Thread::CancelWakeupTimer() {
void Thread::ResumeFromWait() { void Thread::ResumeFromWait() {
ASSERT_MSG(wait_objects.empty(), "Thread is waking up while waiting for objects"); ASSERT_MSG(wait_objects.empty(), "Thread is waking up while waiting for objects");
SchedulerLock lock(kernel);
switch (status) { switch (status) {
case ThreadStatus::Paused: case ThreadStatus::Paused:
case ThreadStatus::WaitSynch: case ThreadStatus::WaitSynch: