summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/svc.cpp
diff options
context:
space:
mode:
authorGravatar Subv2018-05-19 16:58:30 -0500
committerGravatar Subv2018-05-19 16:58:30 -0500
commit2a35a3625195087b4e7b0e420b91637e31ef3bca (patch)
treed451b368237da2be47a2122ade4fe6489ff17c0e /src/core/hle/kernel/svc.cpp
parentKernel/Threads: Reschedule the proper core when operating on that core's thre... (diff)
downloadyuzu-2a35a3625195087b4e7b0e420b91637e31ef3bca.tar.gz
yuzu-2a35a3625195087b4e7b0e420b91637e31ef3bca.tar.xz
yuzu-2a35a3625195087b4e7b0e420b91637e31ef3bca.zip
Kernel/SVC: Signal the highest priority threads first in svcSignalProcessWideKey.
Diffstat (limited to 'src/core/hle/kernel/svc.cpp')
-rw-r--r--src/core/hle/kernel/svc.cpp119
1 files changed, 68 insertions, 51 deletions
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index 810ef66aa..0811a16b8 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -608,61 +608,78 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target
608 NGLOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}", 608 NGLOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}",
609 condition_variable_addr, target); 609 condition_variable_addr, target);
610 610
611 u32 processed = 0; 611 auto RetrieveWaitingThreads =
612 [](size_t core_index, std::vector<SharedPtr<Thread>>& waiting_threads, VAddr condvar_addr) {
613 const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
614 auto& thread_list = scheduler->GetThreadList();
615
616 for (auto& thread : thread_list) {
617 if (thread->condvar_wait_address == condvar_addr)
618 waiting_threads.push_back(thread);
619 }
620 };
621
622 // Retrieve a list of all threads that are waiting for this condition variable.
623 std::vector<SharedPtr<Thread>> waiting_threads;
624 RetrieveWaitingThreads(0, waiting_threads, condition_variable_addr);
625 RetrieveWaitingThreads(1, waiting_threads, condition_variable_addr);
626 RetrieveWaitingThreads(2, waiting_threads, condition_variable_addr);
627 RetrieveWaitingThreads(3, waiting_threads, condition_variable_addr);
628 // Sort them by priority, such that the highest priority ones come first.
629 std::sort(waiting_threads.begin(), waiting_threads.end(),
630 [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
631 return lhs->current_priority < rhs->current_priority;
632 });
633
634 // Only process up to 'target' threads, unless 'target' is -1, in which case process
635 // them all.
636 size_t last = waiting_threads.size();
637 if (target != -1)
638 last = target;
639
640 // If there are no threads waiting on this condition variable, just exit
641 if (last > waiting_threads.size())
642 return RESULT_SUCCESS;
612 643
613 auto signal_process_wide_key = [&](size_t core_index) { 644 for (size_t index = 0; index < last; ++index) {
614 const auto& scheduler = Core::System::GetInstance().Scheduler(core_index); 645 auto& thread = waiting_threads[index];
615 for (auto& thread : scheduler->GetThreadList()) {
616 if (thread->condvar_wait_address != condition_variable_addr)
617 continue;
618 646
619 // Only process up to 'target' threads, unless 'target' is -1, in which case process 647 ASSERT(thread->condvar_wait_address == condition_variable_addr);
620 // them all.
621 if (target != -1 && processed >= target)
622 break;
623
624 // If the mutex is not yet acquired, acquire it.
625 u32 mutex_val = Memory::Read32(thread->mutex_wait_address);
626
627 if (mutex_val == 0) {
628 // We were able to acquire the mutex, resume this thread.
629 Memory::Write32(thread->mutex_wait_address, thread->wait_handle);
630 ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX);
631 thread->ResumeFromWait();
632
633 auto lock_owner = thread->lock_owner;
634 if (lock_owner)
635 lock_owner->RemoveMutexWaiter(thread);
636
637 thread->lock_owner = nullptr;
638 thread->mutex_wait_address = 0;
639 thread->condvar_wait_address = 0;
640 thread->wait_handle = 0;
641 } else {
642 // Couldn't acquire the mutex, block the thread.
643 Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
644 auto owner = g_handle_table.Get<Thread>(owner_handle);
645 ASSERT(owner);
646 ASSERT(thread->status != THREADSTATUS_RUNNING);
647 thread->status = THREADSTATUS_WAIT_MUTEX;
648 thread->wakeup_callback = nullptr;
649
650 // Signal that the mutex now has a waiting thread.
651 Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag);
652
653 owner->AddMutexWaiter(thread);
654
655 Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();
656 }
657 648
658 ++processed; 649 // If the mutex is not yet acquired, acquire it.
659 } 650 u32 mutex_val = Memory::Read32(thread->mutex_wait_address);
660 }; 651
652 if (mutex_val == 0) {
653 // We were able to acquire the mutex, resume this thread.
654 Memory::Write32(thread->mutex_wait_address, thread->wait_handle);
655 ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX);
656 thread->ResumeFromWait();
661 657
662 signal_process_wide_key(0); 658 auto lock_owner = thread->lock_owner;
663 signal_process_wide_key(1); 659 if (lock_owner)
664 signal_process_wide_key(2); 660 lock_owner->RemoveMutexWaiter(thread);
665 signal_process_wide_key(3); 661
662 thread->lock_owner = nullptr;
663 thread->mutex_wait_address = 0;
664 thread->condvar_wait_address = 0;
665 thread->wait_handle = 0;
666 } else {
667 // Couldn't acquire the mutex, block the thread.
668 Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
669 auto owner = g_handle_table.Get<Thread>(owner_handle);
670 ASSERT(owner);
671 ASSERT(thread->status != THREADSTATUS_RUNNING);
672 thread->status = THREADSTATUS_WAIT_MUTEX;
673 thread->wakeup_callback = nullptr;
674
675 // Signal that the mutex now has a waiting thread.
676 Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag);
677
678 owner->AddMutexWaiter(thread);
679
680 Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();
681 }
682 }
666 683
667 return RESULT_SUCCESS; 684 return RESULT_SUCCESS;
668} 685}