summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbunnei <bunneidev@gmail.com>2018-07-22 21:26:03 +0200
committerGitHub <noreply@github.com>2018-07-22 21:26:03 +0200
commita4b2af73829b5ce5c0416cde47e3edddc20f59a4 (patch)
tree585976e23fd8ca03c3269532f028b958cb08ccd9
parentMerge pull request #773 from Subv/gl_ext_check (diff)
parentKernel/SVC: Perform atomic accesses in SignalProcessWideKey as per the real kernel. (diff)
downloadyuzu-a4b2af73829b5ce5c0416cde47e3edddc20f59a4.tar
yuzu-a4b2af73829b5ce5c0416cde47e3edddc20f59a4.tar.gz
yuzu-a4b2af73829b5ce5c0416cde47e3edddc20f59a4.tar.bz2
yuzu-a4b2af73829b5ce5c0416cde47e3edddc20f59a4.tar.lz
yuzu-a4b2af73829b5ce5c0416cde47e3edddc20f59a4.tar.xz
yuzu-a4b2af73829b5ce5c0416cde47e3edddc20f59a4.tar.zst
yuzu-a4b2af73829b5ce5c0416cde47e3edddc20f59a4.zip
-rw-r--r--src/core/hle/kernel/svc.cpp38
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
ASSERT(thread->condvar_wait_address == condition_variable_addr);
- // If the mutex is not yet acquired, acquire it.
- u32 mutex_val = Memory::Read32(thread->mutex_wait_address);
+ size_t current_core = Core::System::GetInstance().CurrentCoreIndex();
+
+ auto& monitor = Core::System::GetInstance().Monitor();
+
+ // Atomically read the value of the mutex.
+ u32 mutex_val = 0;
+ do {
+ monitor.SetExclusive(current_core, thread->mutex_wait_address);
+
+ // If the mutex is not yet acquired, acquire it.
+ mutex_val = Memory::Read32(thread->mutex_wait_address);
+
+ if (mutex_val != 0) {
+ monitor.ClearExclusive();
+ break;
+ }
+ } while (!monitor.ExclusiveWrite32(current_core, thread->mutex_wait_address,
+ thread->wait_handle));
if (mutex_val == 0) {
// We were able to acquire the mutex, resume this thread.
- Memory::Write32(thread->mutex_wait_address, thread->wait_handle);
ASSERT(thread->status == ThreadStatus::WaitMutex);
thread->ResumeFromWait();
@@ -668,7 +683,19 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target
thread->condvar_wait_address = 0;
thread->wait_handle = 0;
} else {
- // Couldn't acquire the mutex, block the thread.
+ // Atomically signal that the mutex now has a waiting thread.
+ do {
+ monitor.SetExclusive(current_core, thread->mutex_wait_address);
+
+ // Ensure that the mutex value is still what we expect.
+ u32 value = Memory::Read32(thread->mutex_wait_address);
+ // TODO(Subv): When this happens, the kernel just clears the exclusive state and
+ // retries the initial read for this thread.
+ ASSERT_MSG(mutex_val == value, "Unhandled synchronization primitive case");
+ } while (!monitor.ExclusiveWrite32(current_core, thread->mutex_wait_address,
+ mutex_val | Mutex::MutexHasWaitersFlag));
+
+ // The mutex is already owned by some other thread, make this thread wait on it.
Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
auto owner = g_handle_table.Get<Thread>(owner_handle);
ASSERT(owner);
@@ -676,9 +703,6 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target
thread->status = ThreadStatus::WaitMutex;
thread->wakeup_callback = nullptr;
- // Signal that the mutex now has a waiting thread.
- Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag);
-
owner->AddMutexWaiter(thread);
Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();