aboutsummaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/kernel.cpp
diff options
context:
space:
mode:
authorSubv <subv2112@gmail.com>2016-12-03 22:38:14 -0500
committerSubv <subv2112@gmail.com>2016-12-03 22:38:14 -0500
commit8634b8cb83755b6c6554faa11c0e488d2ad21f90 (patch)
tree93c2e91659ccd2925210dcffb559213edbd2a64a /src/core/hle/kernel/kernel.cpp
parent0423a38ab55fd2ed7eb9853e9c867d31afd71649 (diff)
Threading: Reworked the way our scheduler works.
Threads will now be awakened when the objects they're waiting on are signaled, instead of repeating the WaitSynchronization call every now and then. The scheduler is now called once after every SVC call, and once after a thread is awakened from sleep by its timeout callback. This new implementation is based off reverse-engineering of the real kernel. See https://gist.github.com/Subv/02f29bd9f1e5deb7aceea1e8f019c8f4 for a more detailed description of how the real kernel handles rescheduling.
Diffstat (limited to 'src/core/hle/kernel/kernel.cpp')
-rw-r--r--src/core/hle/kernel/kernel.cpp59
1 files changed, 54 insertions, 5 deletions
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 0c8752670..be7a5a6d8 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -31,13 +31,62 @@ void WaitObject::RemoveWaitingThread(Thread* thread) {
waiting_threads.erase(itr);
}
-void WaitObject::WakeupAllWaitingThreads() {
- for (auto thread : waiting_threads)
- thread->ResumeFromWait();
+SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
+ // Remove the threads that are ready or already running from our waitlist
+ waiting_threads.erase(std::remove_if(waiting_threads.begin(), waiting_threads.end(), [](SharedPtr<Thread> thread) -> bool {
+ return thread->status == THREADSTATUS_RUNNING || thread->status == THREADSTATUS_READY;
+ }), waiting_threads.end());
+
+ if (waiting_threads.empty())
+ return nullptr;
- waiting_threads.clear();
+ auto candidate_threads = waiting_threads;
- HLE::Reschedule(__func__);
+ // Eliminate all threads that are waiting on more than one object, and not all of them are ready
+ candidate_threads.erase(std::remove_if(candidate_threads.begin(), candidate_threads.end(), [](SharedPtr<Thread> thread) -> bool {
+ for (auto object : thread->wait_objects)
+ if (object->ShouldWait())
+ return true;
+ return false;
+ }), candidate_threads.end());
+
+ // Return the thread with the lowest priority value (The one with the highest priority)
+ auto thread_itr = std::min_element(candidate_threads.begin(), candidate_threads.end(), [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
+ return lhs->current_priority < rhs->current_priority;
+ });
+
+ if (thread_itr == candidate_threads.end())
+ return nullptr;
+
+ return *thread_itr;
+}
+
+void WaitObject::WakeupAllWaitingThreads() {
+ // Wake up all threads that can be awoken, in priority order
+ while (auto thread = GetHighestPriorityReadyThread()) {
+ if (thread->wait_objects.empty()) {
+ Acquire();
+ // Set the output index of the WaitSynchronizationN call to the index of this object.
+ if (thread->wait_set_output) {
+ thread->SetWaitSynchronizationOutput(thread->GetWaitObjectIndex(this));
+ thread->wait_set_output = false;
+ }
+ } else {
+ for (auto object : thread->wait_objects) {
+ object->Acquire();
+ // Remove the thread from the object's waitlist
+ object->RemoveWaitingThread(thread.get());
+ }
+ // Note: This case doesn't update the output index of WaitSynchronizationN.
+ // Clear the thread's waitlist
+ thread->wait_objects.clear();
+ }
+
+ // Set the result of the call to WaitSynchronization to RESULT_SUCCESS
+ thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
+ thread->ResumeFromWait();
+ // Note: Removing the thread from the object's waitlist will be done by GetHighestPriorityReadyThread
+ }
}
const std::vector<SharedPtr<Thread>>& WaitObject::GetWaitingThreads() const {