2018-02-18 20:58:40 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
2019-06-19 15:11:18 +02:00
|
|
|
//
|
|
|
|
// SelectThreads, Yield functions originally by TuxSH.
|
|
|
|
// licensed under GPLv2 or later under exception provided by the author.
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2018-07-31 14:06:09 +02:00
|
|
|
#include <algorithm>
|
2019-03-29 22:01:17 +01:00
|
|
|
#include <set>
|
|
|
|
#include <unordered_set>
|
2018-07-19 01:02:47 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2018-07-31 14:06:09 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/arm/arm_interface.h"
|
2018-03-13 22:49:59 +01:00
|
|
|
#include "core/core.h"
|
2018-10-26 00:42:50 +02:00
|
|
|
#include "core/core_timing.h"
|
2018-10-10 06:42:10 +02:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2018-02-18 20:58:40 +01:00
|
|
|
#include "core/hle/kernel/process.h"
|
|
|
|
#include "core/hle/kernel/scheduler.h"
|
2020-02-14 16:44:31 +01:00
|
|
|
#include "core/hle/kernel/time_manager.h"
|
2018-02-18 20:58:40 +01:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2020-02-14 03:04:10 +01:00
|
|
|
GlobalScheduler::GlobalScheduler(KernelCore& kernel) : kernel{kernel} {}
|
2019-10-28 03:13:51 +01:00
|
|
|
|
|
|
|
GlobalScheduler::~GlobalScheduler() = default;
|
2019-04-02 14:03:44 +02:00
|
|
|
|
2019-11-25 02:15:51 +01:00
|
|
|
void GlobalScheduler::AddThread(std::shared_ptr<Thread> thread) {
|
2019-03-29 22:01:17 +01:00
|
|
|
thread_list.push_back(std::move(thread));
|
|
|
|
}
|
|
|
|
|
2019-11-25 02:15:51 +01:00
|
|
|
void GlobalScheduler::RemoveThread(std::shared_ptr<Thread> thread) {
|
2019-03-29 22:01:17 +01:00
|
|
|
thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
|
|
|
|
thread_list.end());
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::UnloadThread(std::size_t core) {
|
2020-02-14 03:04:10 +01:00
|
|
|
Scheduler& sched = kernel.Scheduler(core);
|
2019-03-29 22:01:17 +01:00
|
|
|
sched.UnloadThread();
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::SelectThread(std::size_t core) {
|
2019-06-19 15:11:18 +02:00
|
|
|
const auto update_thread = [](Thread* thread, Scheduler& sched) {
|
2019-11-25 02:15:51 +01:00
|
|
|
if (thread != sched.selected_thread.get()) {
|
2019-03-29 22:01:17 +01:00
|
|
|
if (thread == nullptr) {
|
|
|
|
++sched.idle_selection_count;
|
|
|
|
}
|
2019-11-25 02:15:51 +01:00
|
|
|
sched.selected_thread = SharedFrom(thread);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-10-12 16:13:25 +02:00
|
|
|
sched.is_context_switch_pending = sched.selected_thread != sched.current_thread;
|
2019-03-29 22:01:17 +01:00
|
|
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
|
|
|
};
|
2020-02-14 03:04:10 +01:00
|
|
|
Scheduler& sched = kernel.Scheduler(core);
|
2019-03-29 22:01:17 +01:00
|
|
|
Thread* current_thread = nullptr;
|
2019-04-02 14:03:44 +02:00
|
|
|
// Step 1: Get top thread in schedule queue.
|
2019-03-29 22:01:17 +01:00
|
|
|
current_thread = scheduled_queue[core].empty() ? nullptr : scheduled_queue[core].front();
|
2019-04-02 14:03:44 +02:00
|
|
|
if (current_thread) {
|
|
|
|
update_thread(current_thread, sched);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Step 2: Try selecting a suggested thread.
|
|
|
|
Thread* winner = nullptr;
|
|
|
|
std::set<s32> sug_cores;
|
|
|
|
for (auto thread : suggested_queue[core]) {
|
|
|
|
s32 this_core = thread->GetProcessorID();
|
|
|
|
Thread* thread_on_core = nullptr;
|
|
|
|
if (this_core >= 0) {
|
|
|
|
thread_on_core = scheduled_queue[this_core].front();
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-04-02 14:03:44 +02:00
|
|
|
if (this_core < 0 || thread != thread_on_core) {
|
|
|
|
winner = thread;
|
|
|
|
break;
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-04-02 14:03:44 +02:00
|
|
|
sug_cores.insert(this_core);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-04-02 14:03:44 +02:00
|
|
|
// if we got a suggested thread, select it, else do a second pass.
|
|
|
|
if (winner && winner->GetPriority() > 2) {
|
|
|
|
if (winner->IsRunning()) {
|
2019-11-12 09:32:53 +01:00
|
|
|
UnloadThread(static_cast<u32>(winner->GetProcessorID()));
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-11-12 09:32:53 +01:00
|
|
|
TransferToCore(winner->GetPriority(), static_cast<s32>(core), winner);
|
2019-04-02 14:03:44 +02:00
|
|
|
update_thread(winner, sched);
|
|
|
|
return;
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-04-02 14:03:44 +02:00
|
|
|
// Step 3: Select a suggested thread from another core
|
|
|
|
for (auto& src_core : sug_cores) {
|
|
|
|
auto it = scheduled_queue[src_core].begin();
|
|
|
|
it++;
|
|
|
|
if (it != scheduled_queue[src_core].end()) {
|
|
|
|
Thread* thread_on_core = scheduled_queue[src_core].front();
|
|
|
|
Thread* to_change = *it;
|
|
|
|
if (thread_on_core->IsRunning() || to_change->IsRunning()) {
|
2019-11-12 09:32:53 +01:00
|
|
|
UnloadThread(static_cast<u32>(src_core));
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-11-12 09:32:53 +01:00
|
|
|
TransferToCore(thread_on_core->GetPriority(), static_cast<s32>(core), thread_on_core);
|
2019-04-02 14:03:44 +02:00
|
|
|
current_thread = thread_on_core;
|
|
|
|
break;
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
2019-04-02 14:03:44 +02:00
|
|
|
update_thread(current_thread, sched);
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:23:43 +02:00
|
|
|
bool GlobalScheduler::YieldThread(Thread* yielding_thread) {
|
2019-03-29 22:01:17 +01:00
|
|
|
// Note: caller should use critical section, etc.
|
2019-06-19 15:11:18 +02:00
|
|
|
const u32 core_id = static_cast<u32>(yielding_thread->GetProcessorID());
|
|
|
|
const u32 priority = yielding_thread->GetPriority();
|
2019-03-29 22:01:17 +01:00
|
|
|
|
|
|
|
// Yield the thread
|
2019-10-28 03:39:20 +01:00
|
|
|
const Thread* const winner = scheduled_queue[core_id].front(priority);
|
|
|
|
ASSERT_MSG(yielding_thread == winner, "Thread yielding without being in front");
|
2019-03-29 22:01:17 +01:00
|
|
|
scheduled_queue[core_id].yield(priority);
|
|
|
|
|
2019-09-10 16:23:43 +02:00
|
|
|
return AskForReselectionOrMarkRedundant(yielding_thread, winner);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:23:43 +02:00
|
|
|
bool GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) {
|
2019-03-29 22:01:17 +01:00
|
|
|
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
|
|
|
// etc.
|
2019-06-19 15:11:18 +02:00
|
|
|
const u32 core_id = static_cast<u32>(yielding_thread->GetProcessorID());
|
|
|
|
const u32 priority = yielding_thread->GetPriority();
|
2019-03-29 22:01:17 +01:00
|
|
|
|
|
|
|
// Yield the thread
|
|
|
|
ASSERT_MSG(yielding_thread == scheduled_queue[core_id].front(priority),
|
|
|
|
"Thread yielding without being in front");
|
|
|
|
scheduled_queue[core_id].yield(priority);
|
|
|
|
|
2020-02-12 00:56:24 +01:00
|
|
|
std::array<Thread*, Core::Hardware::NUM_CPU_CORES> current_threads;
|
2020-02-13 22:01:44 +01:00
|
|
|
for (std::size_t i = 0; i < current_threads.size(); i++) {
|
2019-03-29 22:01:17 +01:00
|
|
|
current_threads[i] = scheduled_queue[i].empty() ? nullptr : scheduled_queue[i].front();
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread* next_thread = scheduled_queue[core_id].front(priority);
|
|
|
|
Thread* winner = nullptr;
|
|
|
|
for (auto& thread : suggested_queue[core_id]) {
|
2019-06-19 15:11:18 +02:00
|
|
|
const s32 source_core = thread->GetProcessorID();
|
2019-03-29 22:01:17 +01:00
|
|
|
if (source_core >= 0) {
|
|
|
|
if (current_threads[source_core] != nullptr) {
|
|
|
|
if (thread == current_threads[source_core] ||
|
2019-06-19 15:11:18 +02:00
|
|
|
current_threads[source_core]->GetPriority() < min_regular_priority) {
|
2019-03-29 22:01:17 +01:00
|
|
|
continue;
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2019-09-10 21:26:24 +02:00
|
|
|
}
|
|
|
|
if (next_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks() ||
|
|
|
|
next_thread->GetPriority() < thread->GetPriority()) {
|
|
|
|
if (thread->GetPriority() <= priority) {
|
|
|
|
winner = thread;
|
|
|
|
break;
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (winner != nullptr) {
|
|
|
|
if (winner != yielding_thread) {
|
2019-06-19 15:11:18 +02:00
|
|
|
if (winner->IsRunning()) {
|
2019-11-12 09:32:53 +01:00
|
|
|
UnloadThread(static_cast<u32>(winner->GetProcessorID()));
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-11-12 09:32:53 +01:00
|
|
|
TransferToCore(winner->GetPriority(), s32(core_id), winner);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
winner = next_thread;
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:23:43 +02:00
|
|
|
return AskForReselectionOrMarkRedundant(yielding_thread, winner);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:23:43 +02:00
|
|
|
bool GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread) {
|
2019-03-29 22:01:17 +01:00
|
|
|
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
|
|
|
// etc.
|
|
|
|
Thread* winner = nullptr;
|
2019-06-19 15:11:18 +02:00
|
|
|
const u32 core_id = static_cast<u32>(yielding_thread->GetProcessorID());
|
2019-03-29 22:01:17 +01:00
|
|
|
|
|
|
|
// Remove the thread from its scheduled mlq, put it on the corresponding "suggested" one instead
|
|
|
|
TransferToCore(yielding_thread->GetPriority(), -1, yielding_thread);
|
|
|
|
|
|
|
|
// If the core is idle, perform load balancing, excluding the threads that have just used this
|
|
|
|
// function...
|
|
|
|
if (scheduled_queue[core_id].empty()) {
|
|
|
|
// Here, "current_threads" is calculated after the ""yield"", unlike yield -1
|
2020-02-12 00:56:24 +01:00
|
|
|
std::array<Thread*, Core::Hardware::NUM_CPU_CORES> current_threads;
|
2020-02-13 22:01:44 +01:00
|
|
|
for (std::size_t i = 0; i < current_threads.size(); i++) {
|
2019-03-29 22:01:17 +01:00
|
|
|
current_threads[i] = scheduled_queue[i].empty() ? nullptr : scheduled_queue[i].front();
|
|
|
|
}
|
|
|
|
for (auto& thread : suggested_queue[core_id]) {
|
2019-06-19 15:11:18 +02:00
|
|
|
const s32 source_core = thread->GetProcessorID();
|
|
|
|
if (source_core < 0 || thread == current_threads[source_core]) {
|
2019-03-29 22:01:17 +01:00
|
|
|
continue;
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
if (current_threads[source_core] == nullptr ||
|
|
|
|
current_threads[source_core]->GetPriority() >= min_regular_priority) {
|
|
|
|
winner = thread;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (winner != nullptr) {
|
|
|
|
if (winner != yielding_thread) {
|
2019-06-19 15:11:18 +02:00
|
|
|
if (winner->IsRunning()) {
|
2019-11-12 09:32:53 +01:00
|
|
|
UnloadThread(static_cast<u32>(winner->GetProcessorID()));
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-11-12 09:32:53 +01:00
|
|
|
TransferToCore(winner->GetPriority(), static_cast<s32>(core_id), winner);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
winner = yielding_thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:23:43 +02:00
|
|
|
return AskForReselectionOrMarkRedundant(yielding_thread, winner);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 17:04:40 +02:00
|
|
|
void GlobalScheduler::PreemptThreads() {
|
2020-02-12 00:56:24 +01:00
|
|
|
for (std::size_t core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) {
|
2019-09-10 21:26:24 +02:00
|
|
|
const u32 priority = preemption_priorities[core_id];
|
2019-09-11 18:14:37 +02:00
|
|
|
|
|
|
|
if (scheduled_queue[core_id].size(priority) > 0) {
|
|
|
|
scheduled_queue[core_id].front(priority)->IncrementYieldCount();
|
2019-09-10 17:04:40 +02:00
|
|
|
scheduled_queue[core_id].yield(priority);
|
2019-09-11 18:14:37 +02:00
|
|
|
if (scheduled_queue[core_id].size(priority) > 1) {
|
|
|
|
scheduled_queue[core_id].front(priority)->IncrementYieldCount();
|
|
|
|
}
|
2019-09-10 17:04:40 +02:00
|
|
|
}
|
2019-09-11 18:14:37 +02:00
|
|
|
|
|
|
|
Thread* current_thread =
|
|
|
|
scheduled_queue[core_id].empty() ? nullptr : scheduled_queue[core_id].front();
|
|
|
|
Thread* winner = nullptr;
|
|
|
|
for (auto& thread : suggested_queue[core_id]) {
|
|
|
|
const s32 source_core = thread->GetProcessorID();
|
|
|
|
if (thread->GetPriority() != priority) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (source_core >= 0) {
|
|
|
|
Thread* next_thread = scheduled_queue[source_core].empty()
|
|
|
|
? nullptr
|
|
|
|
: scheduled_queue[source_core].front();
|
|
|
|
if (next_thread != nullptr && next_thread->GetPriority() < 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next_thread == thread) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current_thread != nullptr &&
|
|
|
|
current_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks()) {
|
|
|
|
winner = thread;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (winner != nullptr) {
|
|
|
|
if (winner->IsRunning()) {
|
2019-11-12 09:32:53 +01:00
|
|
|
UnloadThread(static_cast<u32>(winner->GetProcessorID()));
|
2019-09-11 18:14:37 +02:00
|
|
|
}
|
2019-10-28 03:34:28 +01:00
|
|
|
TransferToCore(winner->GetPriority(), s32(core_id), winner);
|
2019-10-01 02:50:59 +02:00
|
|
|
current_thread =
|
|
|
|
winner->GetPriority() <= current_thread->GetPriority() ? winner : current_thread;
|
2019-09-11 18:14:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current_thread != nullptr && current_thread->GetPriority() > priority) {
|
|
|
|
for (auto& thread : suggested_queue[core_id]) {
|
|
|
|
const s32 source_core = thread->GetProcessorID();
|
2019-09-11 18:47:37 +02:00
|
|
|
if (thread->GetPriority() < priority) {
|
2019-09-11 18:14:37 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (source_core >= 0) {
|
|
|
|
Thread* next_thread = scheduled_queue[source_core].empty()
|
|
|
|
? nullptr
|
|
|
|
: scheduled_queue[source_core].front();
|
|
|
|
if (next_thread != nullptr && next_thread->GetPriority() < 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next_thread == thread) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current_thread != nullptr &&
|
|
|
|
current_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks()) {
|
|
|
|
winner = thread;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (winner != nullptr) {
|
|
|
|
if (winner->IsRunning()) {
|
2019-11-12 09:32:53 +01:00
|
|
|
UnloadThread(static_cast<u32>(winner->GetProcessorID()));
|
2019-09-11 18:14:37 +02:00
|
|
|
}
|
2019-10-28 03:34:28 +01:00
|
|
|
TransferToCore(winner->GetPriority(), s32(core_id), winner);
|
2019-09-11 18:14:37 +02:00
|
|
|
current_thread = winner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 16:13:25 +02:00
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
2019-09-10 17:04:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::Suggest(u32 priority, std::size_t core, Thread* thread) {
|
2019-10-12 16:13:25 +02:00
|
|
|
suggested_queue[core].add(thread, priority);
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::Unsuggest(u32 priority, std::size_t core, Thread* thread) {
|
2019-10-12 16:13:25 +02:00
|
|
|
suggested_queue[core].remove(thread, priority);
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::Schedule(u32 priority, std::size_t core, Thread* thread) {
|
2019-10-28 03:34:28 +01:00
|
|
|
ASSERT_MSG(thread->GetProcessorID() == s32(core), "Thread must be assigned to this core.");
|
2019-06-19 15:11:18 +02:00
|
|
|
scheduled_queue[core].add(thread, priority);
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::SchedulePrepend(u32 priority, std::size_t core, Thread* thread) {
|
2019-10-28 03:34:28 +01:00
|
|
|
ASSERT_MSG(thread->GetProcessorID() == s32(core), "Thread must be assigned to this core.");
|
2019-06-19 15:11:18 +02:00
|
|
|
scheduled_queue[core].add(thread, priority, false);
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::Reschedule(u32 priority, std::size_t core, Thread* thread) {
|
2019-10-12 16:13:25 +02:00
|
|
|
scheduled_queue[core].remove(thread, priority);
|
|
|
|
scheduled_queue[core].add(thread, priority);
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::Unschedule(u32 priority, std::size_t core, Thread* thread) {
|
2019-10-12 16:13:25 +02:00
|
|
|
scheduled_queue[core].remove(thread, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalScheduler::TransferToCore(u32 priority, s32 destination_core, Thread* thread) {
|
|
|
|
const bool schedulable = thread->GetPriority() < THREADPRIO_COUNT;
|
|
|
|
const s32 source_core = thread->GetProcessorID();
|
|
|
|
if (source_core == destination_core || !schedulable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
thread->SetProcessorID(destination_core);
|
|
|
|
if (source_core >= 0) {
|
2019-11-12 09:32:53 +01:00
|
|
|
Unschedule(priority, static_cast<u32>(source_core), thread);
|
2019-10-12 16:13:25 +02:00
|
|
|
}
|
|
|
|
if (destination_core >= 0) {
|
2019-11-12 09:32:53 +01:00
|
|
|
Unsuggest(priority, static_cast<u32>(destination_core), thread);
|
|
|
|
Schedule(priority, static_cast<u32>(destination_core), thread);
|
2019-10-12 16:13:25 +02:00
|
|
|
}
|
|
|
|
if (source_core >= 0) {
|
2019-11-12 09:32:53 +01:00
|
|
|
Suggest(priority, static_cast<u32>(source_core), thread);
|
2019-10-12 16:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 03:39:20 +01:00
|
|
|
bool GlobalScheduler::AskForReselectionOrMarkRedundant(Thread* current_thread,
|
|
|
|
const Thread* winner) {
|
2019-03-29 22:01:17 +01:00
|
|
|
if (current_thread == winner) {
|
2019-09-11 18:14:37 +02:00
|
|
|
current_thread->IncrementYieldCount();
|
2019-09-10 16:23:43 +02:00
|
|
|
return true;
|
2019-03-29 22:01:17 +01:00
|
|
|
} else {
|
2019-10-12 16:13:25 +02:00
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
2019-09-10 16:23:43 +02:00
|
|
|
return false;
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 14:21:51 +02:00
|
|
|
void GlobalScheduler::Shutdown() {
|
2020-02-12 00:56:24 +01:00
|
|
|
for (std::size_t core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
2019-10-12 14:21:51 +02:00
|
|
|
scheduled_queue[core].clear();
|
|
|
|
suggested_queue[core].clear();
|
|
|
|
}
|
|
|
|
thread_list.clear();
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:44:31 +01:00
|
|
|
void GlobalScheduler::Lock() {
|
2020-02-22 15:27:40 +01:00
|
|
|
Core::EmuThreadHandle current_thread = kernel.GetCurrentEmuThreadID();
|
2020-02-14 16:44:31 +01:00
|
|
|
if (current_thread == current_owner) {
|
|
|
|
++scope_lock;
|
|
|
|
} else {
|
|
|
|
inner_lock.lock();
|
|
|
|
current_owner = current_thread;
|
2020-02-22 15:27:40 +01:00
|
|
|
ASSERT(current_owner != Core::EmuThreadHandle::InvalidHandle());
|
2020-02-14 16:44:31 +01:00
|
|
|
scope_lock = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalScheduler::Unlock() {
|
2020-02-22 15:27:40 +01:00
|
|
|
if (--scope_lock != 0) {
|
|
|
|
ASSERT(scope_lock > 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
|
|
|
|
SelectThread(i);
|
2020-02-14 16:44:31 +01:00
|
|
|
}
|
2020-02-22 15:27:40 +01:00
|
|
|
current_owner = Core::EmuThreadHandle::InvalidHandle();
|
|
|
|
scope_lock = 1;
|
|
|
|
inner_lock.unlock();
|
|
|
|
// TODO(Blinkhawk): Setup the interrupts and change context on current core.
|
2020-02-14 16:44:31 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
Scheduler::Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, std::size_t core_id)
|
2019-06-19 15:11:18 +02:00
|
|
|
: system(system), cpu_core(cpu_core), core_id(core_id) {}
|
2019-03-29 22:01:17 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
Scheduler::~Scheduler() = default;
|
2019-03-29 22:01:17 +01:00
|
|
|
|
2018-08-12 18:55:56 +02:00
|
|
|
bool Scheduler::HaveReadyThreads() const {
|
2019-06-19 15:11:18 +02:00
|
|
|
return system.GlobalScheduler().HaveReadyThreads(core_id);
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread* Scheduler::GetCurrentThread() const {
|
|
|
|
return current_thread.get();
|
|
|
|
}
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
Thread* Scheduler::GetSelectedThread() const {
|
|
|
|
return selected_thread.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::SelectThreads() {
|
2019-06-19 15:11:18 +02:00
|
|
|
system.GlobalScheduler().SelectThread(core_id);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
2018-10-26 00:42:50 +02:00
|
|
|
u64 Scheduler::GetLastContextSwitchTicks() const {
|
|
|
|
return last_context_switch_time;
|
|
|
|
}
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
void Scheduler::TryDoContextSwitch() {
|
2019-10-12 16:28:44 +02:00
|
|
|
if (is_context_switch_pending) {
|
2019-03-29 22:01:17 +01:00
|
|
|
SwitchContext();
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::UnloadThread() {
|
|
|
|
Thread* const previous_thread = GetCurrentThread();
|
2019-06-19 15:11:18 +02:00
|
|
|
Process* const previous_process = system.Kernel().CurrentProcess();
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
UpdateLastContextSwitchTime(previous_thread, previous_process);
|
|
|
|
|
|
|
|
// Save context for previous thread
|
|
|
|
if (previous_thread) {
|
|
|
|
cpu_core.SaveContext(previous_thread->GetContext());
|
|
|
|
// Save the TPIDR_EL0 system register in case it was modified.
|
|
|
|
previous_thread->SetTPIDR_EL0(cpu_core.GetTPIDR_EL0());
|
|
|
|
|
|
|
|
if (previous_thread->GetStatus() == ThreadStatus::Running) {
|
|
|
|
// 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)
|
|
|
|
previous_thread->SetStatus(ThreadStatus::Ready);
|
2019-03-20 03:20:15 +01:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
previous_thread->SetIsRunning(false);
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
current_thread = nullptr;
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
void Scheduler::SwitchContext() {
|
|
|
|
Thread* const previous_thread = GetCurrentThread();
|
|
|
|
Thread* const new_thread = GetSelectedThread();
|
|
|
|
|
2019-10-12 16:13:25 +02:00
|
|
|
is_context_switch_pending = false;
|
2019-06-19 15:11:18 +02:00
|
|
|
if (new_thread == previous_thread) {
|
2019-03-29 22:01:17 +01:00
|
|
|
return;
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
Process* const previous_process = system.Kernel().CurrentProcess();
|
2018-10-26 00:42:50 +02:00
|
|
|
|
|
|
|
UpdateLastContextSwitchTime(previous_thread, previous_process);
|
2018-02-18 20:58:40 +01:00
|
|
|
|
|
|
|
// Save context for previous thread
|
|
|
|
if (previous_thread) {
|
2018-10-04 00:47:57 +02:00
|
|
|
cpu_core.SaveContext(previous_thread->GetContext());
|
2018-07-21 02:57:45 +02:00
|
|
|
// Save the TPIDR_EL0 system register in case it was modified.
|
2018-10-04 00:47:57 +02:00
|
|
|
previous_thread->SetTPIDR_EL0(cpu_core.GetTPIDR_EL0());
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2018-10-04 00:47:57 +02:00
|
|
|
if (previous_thread->GetStatus() == ThreadStatus::Running) {
|
2018-02-18 20:58:40 +01: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-04 00:47:57 +02:00
|
|
|
previous_thread->SetStatus(ThreadStatus::Ready);
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
previous_thread->SetIsRunning(false);
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load context of new thread
|
|
|
|
if (new_thread) {
|
2019-10-28 03:34:28 +01:00
|
|
|
ASSERT_MSG(new_thread->GetProcessorID() == s32(this->core_id),
|
2019-03-29 22:01:17 +01:00
|
|
|
"Thread must be assigned to this core.");
|
2018-10-04 00:47:57 +02:00
|
|
|
ASSERT_MSG(new_thread->GetStatus() == ThreadStatus::Ready,
|
2018-02-18 20:58:40 +01:00
|
|
|
"Thread must be ready to become running.");
|
|
|
|
|
|
|
|
// Cancel any outstanding wakeup events for this thread
|
|
|
|
new_thread->CancelWakeupTimer();
|
2019-11-25 02:15:51 +01:00
|
|
|
current_thread = SharedFrom(new_thread);
|
2018-10-04 00:47:57 +02:00
|
|
|
new_thread->SetStatus(ThreadStatus::Running);
|
2019-03-29 22:01:17 +01:00
|
|
|
new_thread->SetIsRunning(true);
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2018-10-10 06:42:10 +02:00
|
|
|
auto* const thread_owner_process = current_thread->GetOwnerProcess();
|
2018-10-04 00:47:57 +02:00
|
|
|
if (previous_process != thread_owner_process) {
|
2019-03-04 22:02:59 +01:00
|
|
|
system.Kernel().MakeCurrentProcess(thread_owner_process);
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:47:57 +02:00
|
|
|
cpu_core.LoadContext(new_thread->GetContext());
|
2018-09-25 22:00:14 +02:00
|
|
|
cpu_core.SetTlsAddress(new_thread->GetTLSAddress());
|
|
|
|
cpu_core.SetTPIDR_EL0(new_thread->GetTPIDR_EL0());
|
2018-02-18 20:58:40 +01: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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 00:42:50 +02:00
|
|
|
void Scheduler::UpdateLastContextSwitchTime(Thread* thread, Process* process) {
|
|
|
|
const u64 prev_switch_ticks = last_context_switch_time;
|
2019-06-19 15:11:18 +02:00
|
|
|
const u64 most_recent_switch_ticks = system.CoreTiming().GetTicks();
|
2018-10-26 00:42:50 +02:00
|
|
|
const u64 update_ticks = most_recent_switch_ticks - prev_switch_ticks;
|
|
|
|
|
|
|
|
if (thread != nullptr) {
|
|
|
|
thread->UpdateCPUTimeTicks(update_ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process != nullptr) {
|
|
|
|
process->UpdateCPUTimeTicks(update_ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_context_switch_time = most_recent_switch_ticks;
|
|
|
|
}
|
|
|
|
|
2019-10-12 16:13:25 +02:00
|
|
|
void Scheduler::Shutdown() {
|
|
|
|
current_thread = nullptr;
|
|
|
|
selected_thread = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:44:31 +01:00
|
|
|
SchedulerLock::SchedulerLock(KernelCore& kernel) : kernel{kernel} {
|
2020-02-22 17:39:17 +01:00
|
|
|
kernel.GlobalScheduler().Lock();
|
2020-02-14 16:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SchedulerLock::~SchedulerLock() {
|
2020-02-22 17:39:17 +01:00
|
|
|
kernel.GlobalScheduler().Unlock();
|
2020-02-14 16:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SchedulerLockAndSleep::SchedulerLockAndSleep(KernelCore& kernel, Handle& event_handle,
|
|
|
|
Thread* time_task, s64 nanoseconds)
|
|
|
|
: SchedulerLock{kernel}, event_handle{event_handle}, time_task{time_task}, nanoseconds{
|
|
|
|
nanoseconds} {
|
|
|
|
event_handle = InvalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
SchedulerLockAndSleep::~SchedulerLockAndSleep() {
|
2020-02-22 16:51:03 +01:00
|
|
|
if (sleep_cancelled) {
|
|
|
|
return;
|
2020-02-14 16:44:31 +01:00
|
|
|
}
|
2020-02-22 16:51:03 +01:00
|
|
|
auto& time_manager = kernel.TimeManager();
|
|
|
|
time_manager.ScheduleTimeEvent(event_handle, time_task, nanoseconds);
|
2020-02-14 16:44:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 20:58:40 +01:00
|
|
|
} // namespace Kernel
|