diff options
| -rw-r--r-- | src/core/hle/kernel/svc.cpp | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 6b2995fe2..7b41c9cfd 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp | |||
| @@ -650,12 +650,27 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target | |||
| 650 | 650 | ||
| 651 | ASSERT(thread->condvar_wait_address == condition_variable_addr); | 651 | ASSERT(thread->condvar_wait_address == condition_variable_addr); |
| 652 | 652 | ||
| 653 | // If the mutex is not yet acquired, acquire it. | 653 | size_t current_core = Core::System::GetInstance().CurrentCoreIndex(); |
| 654 | u32 mutex_val = Memory::Read32(thread->mutex_wait_address); | 654 | |
| 655 | auto& monitor = Core::System::GetInstance().Monitor(); | ||
| 656 | |||
| 657 | // Atomically read the value of the mutex. | ||
| 658 | u32 mutex_val = 0; | ||
| 659 | do { | ||
| 660 | monitor.SetExclusive(current_core, thread->mutex_wait_address); | ||
| 661 | |||
| 662 | // If the mutex is not yet acquired, acquire it. | ||
| 663 | mutex_val = Memory::Read32(thread->mutex_wait_address); | ||
| 664 | |||
| 665 | if (mutex_val != 0) { | ||
| 666 | monitor.ClearExclusive(); | ||
| 667 | break; | ||
| 668 | } | ||
| 669 | } while (!monitor.ExclusiveWrite32(current_core, thread->mutex_wait_address, | ||
| 670 | thread->wait_handle)); | ||
| 655 | 671 | ||
| 656 | if (mutex_val == 0) { | 672 | if (mutex_val == 0) { |
| 657 | // We were able to acquire the mutex, resume this thread. | 673 | // We were able to acquire the mutex, resume this thread. |
| 658 | Memory::Write32(thread->mutex_wait_address, thread->wait_handle); | ||
| 659 | ASSERT(thread->status == ThreadStatus::WaitMutex); | 674 | ASSERT(thread->status == ThreadStatus::WaitMutex); |
| 660 | thread->ResumeFromWait(); | 675 | thread->ResumeFromWait(); |
| 661 | 676 | ||
| @@ -668,7 +683,19 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target | |||
| 668 | thread->condvar_wait_address = 0; | 683 | thread->condvar_wait_address = 0; |
| 669 | thread->wait_handle = 0; | 684 | thread->wait_handle = 0; |
| 670 | } else { | 685 | } else { |
| 671 | // Couldn't acquire the mutex, block the thread. | 686 | // Atomically signal that the mutex now has a waiting thread. |
| 687 | do { | ||
| 688 | monitor.SetExclusive(current_core, thread->mutex_wait_address); | ||
| 689 | |||
| 690 | // Ensure that the mutex value is still what we expect. | ||
| 691 | u32 value = Memory::Read32(thread->mutex_wait_address); | ||
| 692 | // TODO(Subv): When this happens, the kernel just clears the exclusive state and | ||
| 693 | // retries the initial read for this thread. | ||
| 694 | ASSERT_MSG(mutex_val == value, "Unhandled synchronization primitive case"); | ||
| 695 | } while (!monitor.ExclusiveWrite32(current_core, thread->mutex_wait_address, | ||
| 696 | mutex_val | Mutex::MutexHasWaitersFlag)); | ||
| 697 | |||
| 698 | // The mutex is already owned by some other thread, make this thread wait on it. | ||
| 672 | Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask); | 699 | Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask); |
| 673 | auto owner = g_handle_table.Get<Thread>(owner_handle); | 700 | auto owner = g_handle_table.Get<Thread>(owner_handle); |
| 674 | ASSERT(owner); | 701 | ASSERT(owner); |
| @@ -676,9 +703,6 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target | |||
| 676 | thread->status = ThreadStatus::WaitMutex; | 703 | thread->status = ThreadStatus::WaitMutex; |
| 677 | thread->wakeup_callback = nullptr; | 704 | thread->wakeup_callback = nullptr; |
| 678 | 705 | ||
| 679 | // Signal that the mutex now has a waiting thread. | ||
| 680 | Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag); | ||
| 681 | |||
| 682 | owner->AddMutexWaiter(thread); | 706 | owner->AddMutexWaiter(thread); |
| 683 | 707 | ||
| 684 | Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); | 708 | Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); |