diff options
Diffstat (limited to 'src/core/hle/kernel/scheduler.cpp')
| -rw-r--r-- | src/core/hle/kernel/scheduler.cpp | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp index cc189cc64..6d0f13ecf 100644 --- a/src/core/hle/kernel/scheduler.cpp +++ b/src/core/hle/kernel/scheduler.cpp | |||
| @@ -30,7 +30,7 @@ Scheduler::~Scheduler() { | |||
| 30 | 30 | ||
| 31 | bool Scheduler::HaveReadyThreads() const { | 31 | bool Scheduler::HaveReadyThreads() const { |
| 32 | std::lock_guard<std::mutex> lock(scheduler_mutex); | 32 | std::lock_guard<std::mutex> lock(scheduler_mutex); |
| 33 | return ready_queue.get_first() != nullptr; | 33 | return !ready_queue.empty(); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | Thread* Scheduler::GetCurrentThread() const { | 36 | Thread* Scheduler::GetCurrentThread() const { |
| @@ -46,22 +46,27 @@ Thread* Scheduler::PopNextReadyThread() { | |||
| 46 | Thread* thread = GetCurrentThread(); | 46 | Thread* thread = GetCurrentThread(); |
| 47 | 47 | ||
| 48 | if (thread && thread->GetStatus() == ThreadStatus::Running) { | 48 | if (thread && thread->GetStatus() == ThreadStatus::Running) { |
| 49 | if (ready_queue.empty()) { | ||
| 50 | return thread; | ||
| 51 | } | ||
| 49 | // We have to do better than the current thread. | 52 | // We have to do better than the current thread. |
| 50 | // This call returns null when that's not possible. | 53 | // This call returns null when that's not possible. |
| 51 | next = ready_queue.pop_first_better(thread->GetPriority()); | 54 | next = ready_queue.front(); |
| 52 | if (!next) { | 55 | if (next == nullptr || next->GetPriority() >= thread->GetPriority()) { |
| 53 | // Otherwise just keep going with the current thread | ||
| 54 | next = thread; | 56 | next = thread; |
| 55 | } | 57 | } |
| 56 | } else { | 58 | } else { |
| 57 | next = ready_queue.pop_first(); | 59 | if (ready_queue.empty()) { |
| 60 | return nullptr; | ||
| 61 | } | ||
| 62 | next = ready_queue.front(); | ||
| 58 | } | 63 | } |
| 59 | 64 | ||
| 60 | return next; | 65 | return next; |
| 61 | } | 66 | } |
| 62 | 67 | ||
| 63 | void Scheduler::SwitchContext(Thread* new_thread) { | 68 | void Scheduler::SwitchContext(Thread* new_thread) { |
| 64 | Thread* const previous_thread = GetCurrentThread(); | 69 | Thread* previous_thread = GetCurrentThread(); |
| 65 | Process* const previous_process = system.Kernel().CurrentProcess(); | 70 | Process* const previous_process = system.Kernel().CurrentProcess(); |
| 66 | 71 | ||
| 67 | UpdateLastContextSwitchTime(previous_thread, previous_process); | 72 | UpdateLastContextSwitchTime(previous_thread, previous_process); |
| @@ -75,7 +80,7 @@ void Scheduler::SwitchContext(Thread* new_thread) { | |||
| 75 | if (previous_thread->GetStatus() == ThreadStatus::Running) { | 80 | if (previous_thread->GetStatus() == ThreadStatus::Running) { |
| 76 | // This is only the case when a reschedule is triggered without the current thread | 81 | // This is only the case when a reschedule is triggered without the current thread |
| 77 | // yielding execution (i.e. an event triggered, system core time-sliced, etc) | 82 | // yielding execution (i.e. an event triggered, system core time-sliced, etc) |
| 78 | ready_queue.push_front(previous_thread->GetPriority(), previous_thread); | 83 | ready_queue.add(previous_thread, previous_thread->GetPriority(), false); |
| 79 | previous_thread->SetStatus(ThreadStatus::Ready); | 84 | previous_thread->SetStatus(ThreadStatus::Ready); |
| 80 | } | 85 | } |
| 81 | } | 86 | } |
| @@ -90,7 +95,7 @@ void Scheduler::SwitchContext(Thread* new_thread) { | |||
| 90 | 95 | ||
| 91 | current_thread = new_thread; | 96 | current_thread = new_thread; |
| 92 | 97 | ||
| 93 | ready_queue.remove(new_thread->GetPriority(), new_thread); | 98 | ready_queue.remove(new_thread, new_thread->GetPriority()); |
| 94 | new_thread->SetStatus(ThreadStatus::Running); | 99 | new_thread->SetStatus(ThreadStatus::Running); |
| 95 | 100 | ||
| 96 | auto* const thread_owner_process = current_thread->GetOwnerProcess(); | 101 | auto* const thread_owner_process = current_thread->GetOwnerProcess(); |
| @@ -147,7 +152,6 @@ void Scheduler::AddThread(SharedPtr<Thread> thread, u32 priority) { | |||
| 147 | std::lock_guard<std::mutex> lock(scheduler_mutex); | 152 | std::lock_guard<std::mutex> lock(scheduler_mutex); |
| 148 | 153 | ||
| 149 | thread_list.push_back(std::move(thread)); | 154 | thread_list.push_back(std::move(thread)); |
| 150 | ready_queue.prepare(priority); | ||
| 151 | } | 155 | } |
| 152 | 156 | ||
| 153 | void Scheduler::RemoveThread(Thread* thread) { | 157 | void Scheduler::RemoveThread(Thread* thread) { |
| @@ -161,33 +165,37 @@ void Scheduler::ScheduleThread(Thread* thread, u32 priority) { | |||
| 161 | std::lock_guard<std::mutex> lock(scheduler_mutex); | 165 | std::lock_guard<std::mutex> lock(scheduler_mutex); |
| 162 | 166 | ||
| 163 | ASSERT(thread->GetStatus() == ThreadStatus::Ready); | 167 | ASSERT(thread->GetStatus() == ThreadStatus::Ready); |
| 164 | ready_queue.push_back(priority, thread); | 168 | ready_queue.add(thread, priority); |
| 165 | } | 169 | } |
| 166 | 170 | ||
| 167 | void Scheduler::UnscheduleThread(Thread* thread, u32 priority) { | 171 | void Scheduler::UnscheduleThread(Thread* thread, u32 priority) { |
| 168 | std::lock_guard<std::mutex> lock(scheduler_mutex); | 172 | std::lock_guard<std::mutex> lock(scheduler_mutex); |
| 169 | 173 | ||
| 170 | ASSERT(thread->GetStatus() == ThreadStatus::Ready); | 174 | ASSERT(thread->GetStatus() == ThreadStatus::Ready); |
| 171 | ready_queue.remove(priority, thread); | 175 | ready_queue.remove(thread, priority); |
| 172 | } | 176 | } |
| 173 | 177 | ||
| 174 | void Scheduler::SetThreadPriority(Thread* thread, u32 priority) { | 178 | void Scheduler::SetThreadPriority(Thread* thread, u32 priority) { |
| 175 | std::lock_guard<std::mutex> lock(scheduler_mutex); | 179 | std::lock_guard<std::mutex> lock(scheduler_mutex); |
| 180 | if (thread->GetPriority() == priority) { | ||
| 181 | return; | ||
| 182 | } | ||
| 176 | 183 | ||
| 177 | // If thread was ready, adjust queues | 184 | // If thread was ready, adjust queues |
| 178 | if (thread->GetStatus() == ThreadStatus::Ready) | 185 | if (thread->GetStatus() == ThreadStatus::Ready) |
| 179 | ready_queue.move(thread, thread->GetPriority(), priority); | 186 | ready_queue.adjust(thread, thread->GetPriority(), priority); |
| 180 | else | ||
| 181 | ready_queue.prepare(priority); | ||
| 182 | } | 187 | } |
| 183 | 188 | ||
| 184 | Thread* Scheduler::GetNextSuggestedThread(u32 core, u32 maximum_priority) const { | 189 | Thread* Scheduler::GetNextSuggestedThread(u32 core, u32 maximum_priority) const { |
| 185 | std::lock_guard<std::mutex> lock(scheduler_mutex); | 190 | std::lock_guard<std::mutex> lock(scheduler_mutex); |
| 186 | 191 | ||
| 187 | const u32 mask = 1U << core; | 192 | const u32 mask = 1U << core; |
| 188 | return ready_queue.get_first_filter([mask, maximum_priority](Thread const* thread) { | 193 | for (auto* thread : ready_queue) { |
| 189 | return (thread->GetAffinityMask() & mask) != 0 && thread->GetPriority() < maximum_priority; | 194 | if ((thread->GetAffinityMask() & mask) != 0 && thread->GetPriority() < maximum_priority) { |
| 190 | }); | 195 | return thread; |
| 196 | } | ||
| 197 | } | ||
| 198 | return nullptr; | ||
| 191 | } | 199 | } |
| 192 | 200 | ||
| 193 | void Scheduler::YieldWithoutLoadBalancing(Thread* thread) { | 201 | void Scheduler::YieldWithoutLoadBalancing(Thread* thread) { |