2018-02-18 19:58:40 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-31 13:06:09 +01:00
|
|
|
#include <algorithm>
|
2018-07-19 00:02:47 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2018-07-31 13:06:09 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/arm/arm_interface.h"
|
2018-03-13 21:49:59 +00:00
|
|
|
#include "core/core.h"
|
2018-02-18 19:58:40 +00:00
|
|
|
#include "core/core_timing.h"
|
|
|
|
#include "core/hle/kernel/process.h"
|
|
|
|
#include "core/hle/kernel/scheduler.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2018-05-08 03:12:45 +01:00
|
|
|
std::mutex Scheduler::scheduler_mutex;
|
|
|
|
|
2018-09-25 21:00:14 +01:00
|
|
|
Scheduler::Scheduler(Core::ARM_Interface& cpu_core) : cpu_core(cpu_core) {}
|
2018-02-18 19:58:40 +00:00
|
|
|
|
|
|
|
Scheduler::~Scheduler() {
|
|
|
|
for (auto& thread : thread_list) {
|
|
|
|
thread->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 17:55:56 +01:00
|
|
|
bool Scheduler::HaveReadyThreads() const {
|
2018-05-08 03:12:45 +01:00
|
|
|
std::lock_guard<std::mutex> lock(scheduler_mutex);
|
2018-02-18 19:58:40 +00:00
|
|
|
return ready_queue.get_first() != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread* Scheduler::GetCurrentThread() const {
|
|
|
|
return current_thread.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread* Scheduler::PopNextReadyThread() {
|
|
|
|
Thread* next = nullptr;
|
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
|
2018-10-03 23:47:57 +01:00
|
|
|
if (thread && thread->GetStatus() == ThreadStatus::Running) {
|
2018-02-18 19:58:40 +00:00
|
|
|
// We have to do better than the current thread.
|
|
|
|
// This call returns null when that's not possible.
|
2018-10-03 23:47:57 +01:00
|
|
|
next = ready_queue.pop_first_better(thread->GetPriority());
|
2018-02-18 19:58:40 +00:00
|
|
|
if (!next) {
|
|
|
|
// Otherwise just keep going with the current thread
|
|
|
|
next = thread;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
next = ready_queue.pop_first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::SwitchContext(Thread* new_thread) {
|
|
|
|
Thread* previous_thread = GetCurrentThread();
|
|
|
|
|
|
|
|
// Save context for previous thread
|
|
|
|
if (previous_thread) {
|
2018-10-03 23:47:57 +01:00
|
|
|
cpu_core.SaveContext(previous_thread->GetContext());
|
2018-07-21 01:57:45 +01:00
|
|
|
// Save the TPIDR_EL0 system register in case it was modified.
|
2018-10-03 23:47:57 +01:00
|
|
|
previous_thread->SetTPIDR_EL0(cpu_core.GetTPIDR_EL0());
|
2018-02-18 19:58:40 +00:00
|
|
|
|
2018-10-03 23:47:57 +01:00
|
|
|
if (previous_thread->GetStatus() == ThreadStatus::Running) {
|
2018-02-18 19:58:40 +00:00
|
|
|
// This is only the case when a reschedule is triggered without the current thread
|
|
|
|
// yielding execution (i.e. an event triggered, system core time-sliced, etc)
|
2018-10-03 23:47:57 +01:00
|
|
|
ready_queue.push_front(previous_thread->GetPriority(), previous_thread);
|
|
|
|
previous_thread->SetStatus(ThreadStatus::Ready);
|
2018-02-18 19:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load context of new thread
|
|
|
|
if (new_thread) {
|
2018-10-03 23:47:57 +01:00
|
|
|
ASSERT_MSG(new_thread->GetStatus() == ThreadStatus::Ready,
|
2018-02-18 19:58:40 +00:00
|
|
|
"Thread must be ready to become running.");
|
|
|
|
|
|
|
|
// Cancel any outstanding wakeup events for this thread
|
|
|
|
new_thread->CancelWakeupTimer();
|
|
|
|
|
2018-03-13 21:49:59 +00:00
|
|
|
auto previous_process = Core::CurrentProcess();
|
2018-02-18 19:58:40 +00:00
|
|
|
|
|
|
|
current_thread = new_thread;
|
|
|
|
|
2018-10-03 23:47:57 +01:00
|
|
|
ready_queue.remove(new_thread->GetPriority(), new_thread);
|
|
|
|
new_thread->SetStatus(ThreadStatus::Running);
|
2018-02-18 19:58:40 +00:00
|
|
|
|
2018-10-03 23:47:57 +01:00
|
|
|
const auto thread_owner_process = current_thread->GetOwnerProcess();
|
|
|
|
if (previous_process != thread_owner_process) {
|
|
|
|
Core::CurrentProcess() = thread_owner_process;
|
2018-09-29 23:47:00 +01:00
|
|
|
SetCurrentPageTable(&Core::CurrentProcess()->VMManager().page_table);
|
2018-02-18 19:58:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:47:57 +01:00
|
|
|
cpu_core.LoadContext(new_thread->GetContext());
|
2018-09-25 21:00:14 +01:00
|
|
|
cpu_core.SetTlsAddress(new_thread->GetTLSAddress());
|
|
|
|
cpu_core.SetTPIDR_EL0(new_thread->GetTPIDR_EL0());
|
|
|
|
cpu_core.ClearExclusiveState();
|
2018-02-18 19:58:40 +00:00
|
|
|
} else {
|
|
|
|
current_thread = nullptr;
|
|
|
|
// Note: We do not reset the current process and current page table when idling because
|
|
|
|
// technically we haven't changed processes, our threads are just paused.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::Reschedule() {
|
2018-05-08 03:12:45 +01:00
|
|
|
std::lock_guard<std::mutex> lock(scheduler_mutex);
|
|
|
|
|
2018-02-18 19:58:40 +00:00
|
|
|
Thread* cur = GetCurrentThread();
|
|
|
|
Thread* next = PopNextReadyThread();
|
|
|
|
|
|
|
|
if (cur && next) {
|
2018-07-02 17:13:26 +01:00
|
|
|
LOG_TRACE(Kernel, "context switch {} -> {}", cur->GetObjectId(), next->GetObjectId());
|
2018-02-18 19:58:40 +00:00
|
|
|
} else if (cur) {
|
2018-07-02 17:13:26 +01:00
|
|
|
LOG_TRACE(Kernel, "context switch {} -> idle", cur->GetObjectId());
|
2018-02-18 19:58:40 +00:00
|
|
|
} else if (next) {
|
2018-07-02 17:13:26 +01:00
|
|
|
LOG_TRACE(Kernel, "context switch idle -> {}", next->GetObjectId());
|
2018-02-18 19:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwitchContext(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::AddThread(SharedPtr<Thread> thread, u32 priority) {
|
2018-05-08 03:12:45 +01:00
|
|
|
std::lock_guard<std::mutex> lock(scheduler_mutex);
|
|
|
|
|
2018-07-19 00:02:47 +01:00
|
|
|
thread_list.push_back(std::move(thread));
|
2018-02-18 19:58:40 +00:00
|
|
|
ready_queue.prepare(priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::RemoveThread(Thread* thread) {
|
2018-05-08 03:12:45 +01:00
|
|
|
std::lock_guard<std::mutex> lock(scheduler_mutex);
|
|
|
|
|
2018-02-18 19:58:40 +00:00
|
|
|
thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
|
|
|
|
thread_list.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::ScheduleThread(Thread* thread, u32 priority) {
|
2018-05-08 03:12:45 +01:00
|
|
|
std::lock_guard<std::mutex> lock(scheduler_mutex);
|
|
|
|
|
2018-10-03 23:47:57 +01:00
|
|
|
ASSERT(thread->GetStatus() == ThreadStatus::Ready);
|
2018-02-18 19:58:40 +00:00
|
|
|
ready_queue.push_back(priority, thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::UnscheduleThread(Thread* thread, u32 priority) {
|
2018-05-08 03:12:45 +01:00
|
|
|
std::lock_guard<std::mutex> lock(scheduler_mutex);
|
|
|
|
|
2018-10-03 23:47:57 +01:00
|
|
|
ASSERT(thread->GetStatus() == ThreadStatus::Ready);
|
2018-02-18 19:58:40 +00:00
|
|
|
ready_queue.remove(priority, thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::SetThreadPriority(Thread* thread, u32 priority) {
|
2018-05-08 03:12:45 +01:00
|
|
|
std::lock_guard<std::mutex> lock(scheduler_mutex);
|
|
|
|
|
2018-02-18 19:58:40 +00:00
|
|
|
// If thread was ready, adjust queues
|
2018-10-03 23:47:57 +01:00
|
|
|
if (thread->GetStatus() == ThreadStatus::Ready)
|
|
|
|
ready_queue.move(thread, thread->GetPriority(), priority);
|
2018-02-18 19:58:40 +00:00
|
|
|
else
|
|
|
|
ready_queue.prepare(priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|