summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/thread.cpp
diff options
context:
space:
mode:
authorGravatar Subv2018-04-20 20:15:16 -0500
committerGravatar Subv2018-04-23 11:23:44 -0500
commit46572d027dc9620ed2b2a50277e6afd2a115ab81 (patch)
tree72562a37575252e8f4c0160a3067b415027fdf4b /src/core/hle/kernel/thread.cpp
parentKernel: Use 0x2C as default main thread priority for homebrew and lone NRO/NSOs (diff)
downloadyuzu-46572d027dc9620ed2b2a50277e6afd2a115ab81.tar.gz
yuzu-46572d027dc9620ed2b2a50277e6afd2a115ab81.tar.xz
yuzu-46572d027dc9620ed2b2a50277e6afd2a115ab81.zip
Kernel: Implemented mutex priority inheritance.
Verified with a hwtest and implemented based on reverse engineering. Thread A's priority will get bumped to the highest priority among all the threads that are waiting for a mutex that A holds. Once A releases the mutex and ownership is transferred to B, A's priority will return to normal and B's priority will be bumped.
Diffstat (limited to 'src/core/hle/kernel/thread.cpp')
-rw-r--r--src/core/hle/kernel/thread.cpp41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 16d9b9e36..36222d45f 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -129,6 +129,11 @@ static void ThreadWakeupCallback(u64 thread_handle, int cycles_late) {
129 thread->mutex_wait_address = 0; 129 thread->mutex_wait_address = 0;
130 thread->condvar_wait_address = 0; 130 thread->condvar_wait_address = 0;
131 thread->wait_handle = 0; 131 thread->wait_handle = 0;
132
133 auto lock_owner = thread->lock_owner;
134 // Threads waking up by timeout from WaitProcessWideKey do not perform priority inheritance
135 // and don't have a lock owner.
136 ASSERT(lock_owner == nullptr);
132 } 137 }
133 138
134 if (resume) 139 if (resume)
@@ -325,8 +330,8 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
325void Thread::SetPriority(u32 priority) { 330void Thread::SetPriority(u32 priority) {
326 ASSERT_MSG(priority <= THREADPRIO_LOWEST && priority >= THREADPRIO_HIGHEST, 331 ASSERT_MSG(priority <= THREADPRIO_LOWEST && priority >= THREADPRIO_HIGHEST,
327 "Invalid priority value."); 332 "Invalid priority value.");
328 Core::System::GetInstance().Scheduler().SetThreadPriority(this, priority); 333 nominal_priority = priority;
329 nominal_priority = current_priority = priority; 334 UpdatePriority();
330} 335}
331 336
332void Thread::BoostPriority(u32 priority) { 337void Thread::BoostPriority(u32 priority) {
@@ -376,6 +381,38 @@ VAddr Thread::GetCommandBufferAddress() const {
376 return GetTLSAddress() + CommandHeaderOffset; 381 return GetTLSAddress() + CommandHeaderOffset;
377} 382}
378 383
384void Thread::AddMutexWaiter(SharedPtr<Thread> thread) {
385 thread->lock_owner = this;
386 wait_mutex_threads.emplace_back(std::move(thread));
387 UpdatePriority();
388}
389
390void Thread::RemoveMutexWaiter(SharedPtr<Thread> thread) {
391 boost::remove_erase(wait_mutex_threads, thread);
392 thread->lock_owner = nullptr;
393 UpdatePriority();
394}
395
396void Thread::UpdatePriority() {
397 // Find the highest priority among all the threads that are waiting for this thread's lock
398 u32 new_priority = nominal_priority;
399 for (const auto& thread : wait_mutex_threads) {
400 if (thread->nominal_priority < new_priority)
401 new_priority = thread->nominal_priority;
402 }
403
404 if (new_priority == current_priority)
405 return;
406
407 Core::System::GetInstance().Scheduler().SetThreadPriority(this, new_priority);
408
409 current_priority = new_priority;
410
411 // Recursively update the priority of the thread that depends on the priority of this one.
412 if (lock_owner)
413 lock_owner->UpdatePriority();
414}
415
379//////////////////////////////////////////////////////////////////////////////////////////////////// 416////////////////////////////////////////////////////////////////////////////////////////////////////
380 417
381/** 418/**