Improved the algorithm for GetHighestPriorityReadyThread.

pull/8/head
Subv 9 years ago
parent bdad00c73f
commit 1f286b72a1

@ -40,24 +40,23 @@ SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
if (waiting_threads.empty()) if (waiting_threads.empty())
return nullptr; return nullptr;
auto candidate_threads = waiting_threads; SharedPtr<Thread> candidate = nullptr;
s32 candidate_priority = THREADPRIO_LOWEST + 1;
// Eliminate all threads that are waiting on more than one object, and not all of said objects are ready for (const auto& thread : waiting_threads) {
candidate_threads.erase(std::remove_if(candidate_threads.begin(), candidate_threads.end(), [](const SharedPtr<Thread>& thread) -> bool { if (thread->current_priority >= candidate_priority)
return std::any_of(thread->wait_objects.begin(), thread->wait_objects.end(), [](const SharedPtr<WaitObject>& object) -> bool { continue;
return object->ShouldWait();
});
}), candidate_threads.end());
// Return the thread with the lowest priority value (The one with the highest priority) bool ready_to_run = std::none_of(thread->wait_objects.begin(), thread->wait_objects.end(), [](const SharedPtr<WaitObject>& object) {
auto thread_itr = std::min_element(candidate_threads.begin(), candidate_threads.end(), [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) { return object->ShouldWait();
return lhs->current_priority < rhs->current_priority;
}); });
if (ready_to_run) {
candidate = thread;
candidate_priority = thread->current_priority;
}
}
if (thread_itr == candidate_threads.end()) return candidate;
return nullptr;
return *thread_itr;
} }
void WaitObject::WakeupAllWaitingThreads() { void WaitObject::WakeupAllWaitingThreads() {

Loading…
Cancel
Save