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>
|
2020-06-29 02:42:57 +02:00
|
|
|
#include <mutex>
|
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"
|
2020-02-25 03:04:12 +01:00
|
|
|
#include "common/bit_util.h"
|
|
|
|
#include "common/fiber.h"
|
2018-07-31 14:06:09 +02:00
|
|
|
#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"
|
2020-02-25 03:04:12 +01:00
|
|
|
#include "core/cpu_manager.h"
|
2018-10-10 06:42:10 +02:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2020-02-25 03:04:12 +01:00
|
|
|
#include "core/hle/kernel/physical_core.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) {
|
2020-06-29 02:42:57 +02:00
|
|
|
std::scoped_lock lock{global_list_guard};
|
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) {
|
2020-06-29 02:42:57 +02:00
|
|
|
std::scoped_lock lock{global_list_guard};
|
2019-03-29 22:01:17 +01:00
|
|
|
thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
|
|
|
|
thread_list.end());
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:04:12 +01:00
|
|
|
u32 GlobalScheduler::SelectThreads() {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2019-06-19 15:11:18 +02:00
|
|
|
const auto update_thread = [](Thread* thread, Scheduler& sched) {
|
2020-06-29 02:42:57 +02:00
|
|
|
std::scoped_lock lock{sched.guard};
|
2020-03-03 20:50:38 +01:00
|
|
|
if (thread != sched.selected_thread_set.get()) {
|
2019-03-29 22:01:17 +01:00
|
|
|
if (thread == nullptr) {
|
|
|
|
++sched.idle_selection_count;
|
|
|
|
}
|
2020-03-03 20:50:38 +01:00
|
|
|
sched.selected_thread_set = SharedFrom(thread);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2020-03-06 14:52:24 +01:00
|
|
|
const bool reschedule_pending =
|
|
|
|
sched.is_context_switch_pending || (sched.selected_thread_set != sched.current_thread);
|
2020-02-25 03:04:12 +01:00
|
|
|
sched.is_context_switch_pending = reschedule_pending;
|
2019-03-29 22:01:17 +01:00
|
|
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
2020-02-25 03:04:12 +01:00
|
|
|
return reschedule_pending;
|
2019-03-29 22:01:17 +01:00
|
|
|
};
|
2020-02-25 03:04:12 +01:00
|
|
|
if (!is_reselection_pending.load()) {
|
|
|
|
return 0;
|
2019-04-02 14:03:44 +02:00
|
|
|
}
|
2020-02-25 03:04:12 +01:00
|
|
|
std::array<Thread*, Core::Hardware::NUM_CPU_CORES> top_threads{};
|
|
|
|
|
|
|
|
u32 idle_cores{};
|
|
|
|
|
|
|
|
// Step 1: Get top thread in schedule queue.
|
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
Thread* top_thread =
|
|
|
|
scheduled_queue[core].empty() ? nullptr : scheduled_queue[core].front();
|
|
|
|
if (top_thread != nullptr) {
|
|
|
|
// TODO(Blinkhawk): Implement Thread Pinning
|
|
|
|
} else {
|
2020-10-13 14:10:50 +02:00
|
|
|
idle_cores |= (1U << core);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2020-02-25 03:04:12 +01:00
|
|
|
top_threads[core] = top_thread;
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2020-02-25 03:04:12 +01:00
|
|
|
|
|
|
|
while (idle_cores != 0) {
|
|
|
|
u32 core_id = Common::CountTrailingZeroes32(idle_cores);
|
|
|
|
|
|
|
|
if (!suggested_queue[core_id].empty()) {
|
|
|
|
std::array<s32, Core::Hardware::NUM_CPU_CORES> migration_candidates{};
|
|
|
|
std::size_t num_candidates = 0;
|
|
|
|
auto iter = suggested_queue[core_id].begin();
|
|
|
|
Thread* suggested = nullptr;
|
|
|
|
// Step 2: Try selecting a suggested thread.
|
|
|
|
while (iter != suggested_queue[core_id].end()) {
|
|
|
|
suggested = *iter;
|
|
|
|
iter++;
|
2020-10-21 04:07:39 +02:00
|
|
|
s32 suggested_core_id = suggested->GetProcessorID();
|
|
|
|
Thread* top_thread =
|
|
|
|
suggested_core_id >= 0 ? top_threads[suggested_core_id] : nullptr;
|
2020-02-25 03:04:12 +01:00
|
|
|
if (top_thread != suggested) {
|
|
|
|
if (top_thread != nullptr &&
|
|
|
|
top_thread->GetPriority() < THREADPRIO_MAX_CORE_MIGRATION) {
|
|
|
|
suggested = nullptr;
|
|
|
|
break;
|
|
|
|
// There's a too high thread to do core migration, cancel
|
|
|
|
}
|
|
|
|
TransferToCore(suggested->GetPriority(), static_cast<s32>(core_id), suggested);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-03 20:59:09 +01:00
|
|
|
suggested = nullptr;
|
2020-02-25 03:04:12 +01:00
|
|
|
migration_candidates[num_candidates++] = suggested_core_id;
|
|
|
|
}
|
|
|
|
// Step 3: Select a suggested thread from another core
|
|
|
|
if (suggested == nullptr) {
|
|
|
|
for (std::size_t i = 0; i < num_candidates; i++) {
|
2020-10-21 04:07:39 +02:00
|
|
|
s32 candidate_core = migration_candidates[i];
|
2020-02-25 03:04:12 +01:00
|
|
|
suggested = top_threads[candidate_core];
|
|
|
|
auto it = scheduled_queue[candidate_core].begin();
|
2020-10-21 04:07:39 +02:00
|
|
|
it++;
|
2020-02-25 03:04:12 +01:00
|
|
|
Thread* next = it != scheduled_queue[candidate_core].end() ? *it : nullptr;
|
|
|
|
if (next != nullptr) {
|
|
|
|
TransferToCore(suggested->GetPriority(), static_cast<s32>(core_id),
|
|
|
|
suggested);
|
|
|
|
top_threads[candidate_core] = next;
|
|
|
|
break;
|
2020-03-03 20:50:38 +01:00
|
|
|
} else {
|
|
|
|
suggested = nullptr;
|
2020-02-25 03:04:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
top_threads[core_id] = suggested;
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2020-02-25 03:04:12 +01:00
|
|
|
|
2020-10-13 14:10:50 +02:00
|
|
|
idle_cores &= ~(1U << core_id);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2020-10-21 04:07:39 +02:00
|
|
|
u32 cores_needing_context_switch{};
|
2020-02-25 03:04:12 +01:00
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
Scheduler& sched = kernel.Scheduler(core);
|
2020-08-05 21:12:29 +02:00
|
|
|
ASSERT(top_threads[core] == nullptr ||
|
|
|
|
static_cast<u32>(top_threads[core]->GetProcessorID()) == core);
|
2020-02-25 03:04:12 +01:00
|
|
|
if (update_thread(top_threads[core], sched)) {
|
2020-10-13 14:10:50 +02:00
|
|
|
cores_needing_context_switch |= (1U << core);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
2020-02-25 03:04:12 +01:00
|
|
|
return cores_needing_context_switch;
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:23:43 +02:00
|
|
|
bool GlobalScheduler::YieldThread(Thread* yielding_thread) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2019-03-29 22:01:17 +01:00
|
|
|
// Note: caller should use critical section, etc.
|
2020-03-13 00:53:54 +01:00
|
|
|
if (!yielding_thread->IsRunnable()) {
|
|
|
|
// Normally this case shouldn't happen except for SetThreadActivity.
|
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
|
|
|
return false;
|
|
|
|
}
|
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
|
2020-03-08 16:25:50 +01:00
|
|
|
Reschedule(priority, core_id, yielding_thread);
|
|
|
|
const Thread* const winner = scheduled_queue[core_id].front();
|
|
|
|
if (kernel.GetCurrentHostThreadID() != core_id) {
|
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
|
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) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2019-03-29 22:01:17 +01:00
|
|
|
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
|
|
|
// etc.
|
2020-03-13 00:53:54 +01:00
|
|
|
if (!yielding_thread->IsRunnable()) {
|
|
|
|
// Normally this case shouldn't happen except for SetThreadActivity.
|
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
|
|
|
return false;
|
|
|
|
}
|
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
|
2020-03-08 16:25:50 +01:00
|
|
|
Reschedule(priority, core_id, yielding_thread);
|
2019-03-29 22:01:17 +01:00
|
|
|
|
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) {
|
2020-10-21 04:07:39 +02:00
|
|
|
if (current_threads[source_core] != nullptr) {
|
|
|
|
if (thread == current_threads[source_core] ||
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:25:50 +01:00
|
|
|
if (kernel.GetCurrentHostThreadID() != core_id) {
|
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2019-03-29 22:01:17 +01:00
|
|
|
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
|
|
|
// etc.
|
2020-03-13 00:53:54 +01:00
|
|
|
if (!yielding_thread->IsRunnable()) {
|
|
|
|
// Normally this case shouldn't happen except for SetThreadActivity.
|
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
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();
|
2020-10-21 04:07:39 +02:00
|
|
|
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
|
|
|
}
|
2020-10-21 04:07:39 +02:00
|
|
|
if (current_threads[source_core] == nullptr ||
|
|
|
|
current_threads[source_core]->GetPriority() >= min_regular_priority) {
|
2019-03-29 22:01:17 +01:00
|
|
|
winner = thread;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (winner != nullptr) {
|
|
|
|
if (winner != yielding_thread) {
|
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;
|
|
|
|
}
|
2020-03-08 16:25:50 +01:00
|
|
|
} else {
|
2020-03-08 17:51:24 +01:00
|
|
|
winner = scheduled_queue[core_id].front();
|
2020-03-08 16:25:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (kernel.GetCurrentHostThreadID() != core_id) {
|
|
|
|
is_reselection_pending.store(true, std::memory_order_release);
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
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-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
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) {
|
2020-02-25 03:04:12 +01:00
|
|
|
if (scheduled_queue[core_id].size(priority) > 1) {
|
|
|
|
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) {
|
2020-10-21 04:07:39 +02:00
|
|
|
Thread* next_thread = scheduled_queue[source_core].empty()
|
2019-09-11 18:14:37 +02:00
|
|
|
? nullptr
|
2020-10-21 04:07:39 +02:00
|
|
|
: scheduled_queue[source_core].front();
|
2019-09-11 18:14:37 +02:00
|
|
|
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) {
|
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) {
|
2020-10-21 04:07:39 +02:00
|
|
|
Thread* next_thread = scheduled_queue[source_core].empty()
|
2019-09-11 18:14:37 +02:00
|
|
|
? nullptr
|
2020-10-21 04:07:39 +02:00
|
|
|
: scheduled_queue[source_core].front();
|
2019-09-11 18:14:37 +02:00
|
|
|
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) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:04:12 +01:00
|
|
|
void GlobalScheduler::EnableInterruptAndSchedule(u32 cores_pending_reschedule,
|
|
|
|
Core::EmuThreadHandle global_thread) {
|
2020-10-21 04:07:39 +02:00
|
|
|
u32 current_core = global_thread.host_handle;
|
2020-02-25 03:04:12 +01:00
|
|
|
bool must_context_switch = global_thread.guest_handle != InvalidHandle &&
|
|
|
|
(current_core < Core::Hardware::NUM_CPU_CORES);
|
|
|
|
while (cores_pending_reschedule != 0) {
|
2020-10-21 04:07:39 +02:00
|
|
|
u32 core = Common::CountTrailingZeroes32(cores_pending_reschedule);
|
2020-02-25 03:04:12 +01:00
|
|
|
ASSERT(core < Core::Hardware::NUM_CPU_CORES);
|
|
|
|
if (!must_context_switch || core != current_core) {
|
|
|
|
auto& phys_core = kernel.PhysicalCore(core);
|
|
|
|
phys_core.Interrupt();
|
|
|
|
} else {
|
|
|
|
must_context_switch = true;
|
|
|
|
}
|
2020-10-13 14:10:50 +02:00
|
|
|
cores_pending_reschedule &= ~(1U << core);
|
2020-02-25 03:04:12 +01:00
|
|
|
}
|
|
|
|
if (must_context_switch) {
|
|
|
|
auto& core_scheduler = kernel.CurrentScheduler();
|
2020-03-12 21:48:43 +01:00
|
|
|
kernel.ExitSVCProfile();
|
2020-02-25 03:04:12 +01:00
|
|
|
core_scheduler.TryDoContextSwitch();
|
2020-03-12 21:48:43 +01:00
|
|
|
kernel.EnterSVCProfile();
|
2020-02-25 03:04:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:32:53 +01:00
|
|
|
void GlobalScheduler::Suggest(u32 priority, std::size_t core, Thread* thread) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
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) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
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) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
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) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
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) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
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) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2019-10-12 16:13:25 +02:00
|
|
|
scheduled_queue[core].remove(thread, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalScheduler::TransferToCore(u32 priority, s32 destination_core, Thread* thread) {
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2019-10-12 16:13:25 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:04:12 +01:00
|
|
|
void GlobalScheduler::AdjustSchedulingOnStatus(Thread* thread, u32 old_flags) {
|
|
|
|
if (old_flags == thread->scheduling_state) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2020-02-25 03:04:12 +01:00
|
|
|
|
2020-03-07 17:44:35 +01:00
|
|
|
if (old_flags == static_cast<u32>(ThreadSchedStatus::Runnable)) {
|
2020-02-25 03:04:12 +01:00
|
|
|
// In this case the thread was running, now it's pausing/exitting
|
|
|
|
if (thread->processor_id >= 0) {
|
|
|
|
Unschedule(thread->current_priority, static_cast<u32>(thread->processor_id), thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
if (core != static_cast<u32>(thread->processor_id) &&
|
|
|
|
((thread->affinity_mask >> core) & 1) != 0) {
|
|
|
|
Unsuggest(thread->current_priority, core, thread);
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 17:44:35 +01:00
|
|
|
} else if (thread->scheduling_state == static_cast<u32>(ThreadSchedStatus::Runnable)) {
|
2020-02-25 03:04:12 +01:00
|
|
|
// The thread is now set to running from being stopped
|
|
|
|
if (thread->processor_id >= 0) {
|
|
|
|
Schedule(thread->current_priority, static_cast<u32>(thread->processor_id), thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
if (core != static_cast<u32>(thread->processor_id) &&
|
|
|
|
((thread->affinity_mask >> core) & 1) != 0) {
|
|
|
|
Suggest(thread->current_priority, core, thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetReselectionPending();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalScheduler::AdjustSchedulingOnPriority(Thread* thread, u32 old_priority) {
|
2020-03-07 17:44:35 +01:00
|
|
|
if (thread->scheduling_state != static_cast<u32>(ThreadSchedStatus::Runnable)) {
|
2020-02-25 03:04:12 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2020-02-25 03:04:12 +01:00
|
|
|
if (thread->processor_id >= 0) {
|
|
|
|
Unschedule(old_priority, static_cast<u32>(thread->processor_id), thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
if (core != static_cast<u32>(thread->processor_id) &&
|
|
|
|
((thread->affinity_mask >> core) & 1) != 0) {
|
|
|
|
Unsuggest(old_priority, core, thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread->processor_id >= 0) {
|
2020-03-07 18:27:27 +01:00
|
|
|
if (thread == kernel.CurrentScheduler().GetCurrentThread()) {
|
2020-02-25 03:04:12 +01:00
|
|
|
SchedulePrepend(thread->current_priority, static_cast<u32>(thread->processor_id),
|
|
|
|
thread);
|
|
|
|
} else {
|
|
|
|
Schedule(thread->current_priority, static_cast<u32>(thread->processor_id), thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
if (core != static_cast<u32>(thread->processor_id) &&
|
|
|
|
((thread->affinity_mask >> core) & 1) != 0) {
|
|
|
|
Suggest(thread->current_priority, core, thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread->IncrementYieldCount();
|
|
|
|
SetReselectionPending();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalScheduler::AdjustSchedulingOnAffinity(Thread* thread, u64 old_affinity_mask,
|
|
|
|
s32 old_core) {
|
2020-03-07 17:44:35 +01:00
|
|
|
if (thread->scheduling_state != static_cast<u32>(ThreadSchedStatus::Runnable) ||
|
2020-02-25 03:04:12 +01:00
|
|
|
thread->current_priority >= THREADPRIO_COUNT) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-28 14:42:06 +01:00
|
|
|
ASSERT(is_locked);
|
2020-02-25 03:04:12 +01:00
|
|
|
|
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
if (((old_affinity_mask >> core) & 1) != 0) {
|
|
|
|
if (core == static_cast<u32>(old_core)) {
|
|
|
|
Unschedule(thread->current_priority, core, thread);
|
|
|
|
} else {
|
|
|
|
Unsuggest(thread->current_priority, core, thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
if (((thread->affinity_mask >> core) & 1) != 0) {
|
|
|
|
if (core == static_cast<u32>(thread->processor_id)) {
|
|
|
|
Schedule(thread->current_priority, core, thread);
|
|
|
|
} else {
|
|
|
|
Suggest(thread->current_priority, core, thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->IncrementYieldCount();
|
|
|
|
SetReselectionPending();
|
|
|
|
}
|
|
|
|
|
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-28 14:42:06 +01:00
|
|
|
ASSERT(!current_thread.IsInvalid());
|
2020-02-14 16:44:31 +01:00
|
|
|
if (current_thread == current_owner) {
|
|
|
|
++scope_lock;
|
|
|
|
} else {
|
|
|
|
inner_lock.lock();
|
2020-02-28 14:42:06 +01:00
|
|
|
is_locked = true;
|
2020-02-14 16:44:31 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-02-25 03:04:12 +01:00
|
|
|
u32 cores_pending_reschedule = SelectThreads();
|
|
|
|
Core::EmuThreadHandle leaving_thread = current_owner;
|
2020-02-22 15:27:40 +01:00
|
|
|
current_owner = Core::EmuThreadHandle::InvalidHandle();
|
|
|
|
scope_lock = 1;
|
2020-02-28 14:42:06 +01:00
|
|
|
is_locked = false;
|
2020-02-22 15:27:40 +01:00
|
|
|
inner_lock.unlock();
|
2020-02-25 03:04:12 +01:00
|
|
|
EnableInterruptAndSchedule(cores_pending_reschedule, leaving_thread);
|
2020-02-14 16:44:31 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 19:56:05 +01:00
|
|
|
Scheduler::Scheduler(Core::System& system, std::size_t core_id) : system(system), core_id(core_id) {
|
2020-03-06 14:52:24 +01:00
|
|
|
switch_fiber = std::make_shared<Common::Fiber>(std::function<void(void*)>(OnSwitch), this);
|
|
|
|
}
|
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 {
|
2020-02-25 03:04:12 +01:00
|
|
|
if (current_thread) {
|
|
|
|
return current_thread.get();
|
|
|
|
}
|
|
|
|
return idle_thread.get();
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
Thread* Scheduler::GetSelectedThread() const {
|
|
|
|
return selected_thread.get();
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-02-25 03:04:12 +01:00
|
|
|
auto& phys_core = system.Kernel().CurrentPhysicalCore();
|
|
|
|
if (phys_core.IsInterrupted()) {
|
|
|
|
phys_core.ClearInterrupt();
|
|
|
|
}
|
|
|
|
guard.lock();
|
2019-10-12 16:28:44 +02:00
|
|
|
if (is_context_switch_pending) {
|
2019-03-29 22:01:17 +01:00
|
|
|
SwitchContext();
|
2020-02-25 03:04:12 +01:00
|
|
|
} else {
|
|
|
|
guard.unlock();
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 03:04:12 +01:00
|
|
|
void Scheduler::OnThreadStart() {
|
|
|
|
SwitchContextStep2();
|
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2020-03-10 16:50:33 +01:00
|
|
|
void Scheduler::Unload() {
|
|
|
|
Thread* thread = current_thread.get();
|
|
|
|
if (thread) {
|
|
|
|
thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
|
|
|
thread->SetIsRunning(false);
|
2020-11-13 20:11:12 +01:00
|
|
|
if (thread->IsContinuousOnSVC() && !thread->IsHLEThread()) {
|
|
|
|
system.ArmInterface(core_id).ExceptionalExit();
|
|
|
|
thread->SetContinuousOnSVC(false);
|
|
|
|
}
|
2020-03-10 18:24:52 +01:00
|
|
|
if (!thread->IsHLEThread() && !thread->HasExited()) {
|
2020-11-13 20:11:12 +01:00
|
|
|
Core::ARM_Interface& cpu_core = system.ArmInterface(core_id);
|
2020-03-10 16:50:33 +01:00
|
|
|
cpu_core.SaveContext(thread->GetContext32());
|
|
|
|
cpu_core.SaveContext(thread->GetContext64());
|
|
|
|
// Save the TPIDR_EL0 system register in case it was modified.
|
|
|
|
thread->SetTPIDR_EL0(cpu_core.GetTPIDR_EL0());
|
|
|
|
cpu_core.ClearExclusiveState();
|
|
|
|
}
|
|
|
|
thread->context_guard.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::Reload() {
|
|
|
|
Thread* thread = current_thread.get();
|
|
|
|
if (thread) {
|
|
|
|
ASSERT_MSG(thread->GetSchedulingStatus() == ThreadSchedStatus::Runnable,
|
|
|
|
"Thread must be runnable.");
|
|
|
|
|
|
|
|
// Cancel any outstanding wakeup events for this thread
|
|
|
|
thread->SetIsRunning(true);
|
2020-03-12 21:48:43 +01:00
|
|
|
thread->SetWasRunning(false);
|
2020-03-10 16:50:33 +01:00
|
|
|
thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
|
|
|
|
|
|
|
auto* const thread_owner_process = thread->GetOwnerProcess();
|
|
|
|
if (thread_owner_process != nullptr) {
|
|
|
|
system.Kernel().MakeCurrentProcess(thread_owner_process);
|
|
|
|
}
|
|
|
|
if (!thread->IsHLEThread()) {
|
2020-11-13 20:11:12 +01:00
|
|
|
Core::ARM_Interface& cpu_core = system.ArmInterface(core_id);
|
2020-03-10 16:50:33 +01:00
|
|
|
cpu_core.LoadContext(thread->GetContext32());
|
|
|
|
cpu_core.LoadContext(thread->GetContext64());
|
|
|
|
cpu_core.SetTlsAddress(thread->GetTLSAddress());
|
|
|
|
cpu_core.SetTPIDR_EL0(thread->GetTPIDR_EL0());
|
|
|
|
cpu_core.ClearExclusiveState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:04:12 +01:00
|
|
|
void Scheduler::SwitchContextStep2() {
|
|
|
|
// Load context of new thread
|
2020-08-05 21:18:51 +02:00
|
|
|
if (selected_thread) {
|
|
|
|
ASSERT_MSG(selected_thread->GetSchedulingStatus() == ThreadSchedStatus::Runnable,
|
2020-03-07 00:30:37 +01:00
|
|
|
"Thread must be runnable.");
|
2020-02-25 03:04:12 +01:00
|
|
|
|
|
|
|
// Cancel any outstanding wakeup events for this thread
|
2020-08-05 21:18:51 +02:00
|
|
|
selected_thread->SetIsRunning(true);
|
|
|
|
selected_thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
|
|
|
selected_thread->SetWasRunning(false);
|
2020-02-25 03:04:12 +01:00
|
|
|
|
|
|
|
auto* const thread_owner_process = current_thread->GetOwnerProcess();
|
2020-03-01 17:14:17 +01:00
|
|
|
if (thread_owner_process != nullptr) {
|
2020-02-25 03:04:12 +01:00
|
|
|
system.Kernel().MakeCurrentProcess(thread_owner_process);
|
2019-03-20 03:20:15 +01:00
|
|
|
}
|
2020-08-05 21:18:51 +02:00
|
|
|
if (!selected_thread->IsHLEThread()) {
|
2020-11-13 20:11:12 +01:00
|
|
|
Core::ARM_Interface& cpu_core = system.ArmInterface(core_id);
|
2020-08-05 21:18:51 +02:00
|
|
|
cpu_core.LoadContext(selected_thread->GetContext32());
|
|
|
|
cpu_core.LoadContext(selected_thread->GetContext64());
|
|
|
|
cpu_core.SetTlsAddress(selected_thread->GetTLSAddress());
|
|
|
|
cpu_core.SetTPIDR_EL0(selected_thread->GetTPIDR_EL0());
|
2020-02-28 00:12:41 +01:00
|
|
|
cpu_core.ClearExclusiveState();
|
2020-02-25 03:04:12 +01:00
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
2020-03-03 20:50:38 +01:00
|
|
|
|
|
|
|
TryDoContextSwitch();
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
void Scheduler::SwitchContext() {
|
2020-03-03 20:50:38 +01:00
|
|
|
current_thread_prev = current_thread;
|
|
|
|
selected_thread = selected_thread_set;
|
|
|
|
Thread* previous_thread = current_thread_prev.get();
|
2020-02-25 03:04:12 +01:00
|
|
|
Thread* new_thread = selected_thread.get();
|
2020-03-03 20:50:38 +01:00
|
|
|
current_thread = selected_thread;
|
2019-03-29 22:01:17 +01:00
|
|
|
|
2019-10-12 16:13:25 +02:00
|
|
|
is_context_switch_pending = false;
|
2020-03-06 14:52:24 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
if (new_thread == previous_thread) {
|
2020-03-06 14:52:24 +01:00
|
|
|
guard.unlock();
|
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) {
|
2020-03-12 21:48:43 +01:00
|
|
|
if (new_thread != nullptr && new_thread->IsSuspendThread()) {
|
|
|
|
previous_thread->SetWasRunning(true);
|
|
|
|
}
|
2020-03-09 02:13:18 +01:00
|
|
|
previous_thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
2020-03-01 17:14:17 +01:00
|
|
|
previous_thread->SetIsRunning(false);
|
2020-11-13 20:11:12 +01:00
|
|
|
if (previous_thread->IsContinuousOnSVC() && !previous_thread->IsHLEThread()) {
|
|
|
|
system.ArmInterface(core_id).ExceptionalExit();
|
|
|
|
previous_thread->SetContinuousOnSVC(false);
|
|
|
|
}
|
2020-03-07 19:16:25 +01:00
|
|
|
if (!previous_thread->IsHLEThread() && !previous_thread->HasExited()) {
|
2020-11-13 20:11:12 +01:00
|
|
|
Core::ARM_Interface& cpu_core = system.ArmInterface(core_id);
|
2020-02-25 03:04:12 +01:00
|
|
|
cpu_core.SaveContext(previous_thread->GetContext32());
|
|
|
|
cpu_core.SaveContext(previous_thread->GetContext64());
|
|
|
|
// Save the TPIDR_EL0 system register in case it was modified.
|
2020-03-06 19:56:05 +01:00
|
|
|
previous_thread->SetTPIDR_EL0(cpu_core.GetTPIDR_EL0());
|
2020-02-27 03:26:53 +01:00
|
|
|
cpu_core.ClearExclusiveState();
|
2020-02-25 03:04:12 +01:00
|
|
|
}
|
|
|
|
previous_thread->context_guard.unlock();
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 20:17:34 +02:00
|
|
|
std::shared_ptr<Common::Fiber>* old_context;
|
2020-02-25 03:04:12 +01:00
|
|
|
if (previous_thread != nullptr) {
|
2020-05-13 20:17:34 +02:00
|
|
|
old_context = &previous_thread->GetHostContext();
|
2020-02-25 03:04:12 +01:00
|
|
|
} else {
|
2020-05-13 20:17:34 +02:00
|
|
|
old_context = &idle_thread->GetHostContext();
|
2020-02-25 03:04:12 +01:00
|
|
|
}
|
2020-03-06 14:52:24 +01:00
|
|
|
guard.unlock();
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2020-05-13 20:17:34 +02:00
|
|
|
Common::Fiber::YieldTo(*old_context, switch_fiber);
|
2020-02-25 03:04:12 +01:00
|
|
|
/// When a thread wakes up, the scheduler may have changed to other in another core.
|
|
|
|
auto& next_scheduler = system.Kernel().CurrentScheduler();
|
|
|
|
next_scheduler.SwitchContextStep2();
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 14:52:24 +01:00
|
|
|
void Scheduler::OnSwitch(void* this_scheduler) {
|
|
|
|
Scheduler* sched = static_cast<Scheduler*>(this_scheduler);
|
|
|
|
sched->SwitchToCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::SwitchToCurrent() {
|
|
|
|
while (true) {
|
2020-06-29 02:42:57 +02:00
|
|
|
{
|
|
|
|
std::scoped_lock lock{guard};
|
|
|
|
selected_thread = selected_thread_set;
|
|
|
|
current_thread = selected_thread;
|
|
|
|
is_context_switch_pending = false;
|
|
|
|
}
|
2020-08-26 03:55:44 +02:00
|
|
|
const auto is_switch_pending = [this] {
|
|
|
|
std::scoped_lock lock{guard};
|
|
|
|
return is_context_switch_pending;
|
|
|
|
};
|
|
|
|
do {
|
2020-03-07 03:58:56 +01:00
|
|
|
if (current_thread != nullptr && !current_thread->IsHLEThread()) {
|
2020-03-07 01:36:05 +01:00
|
|
|
current_thread->context_guard.lock();
|
2020-03-08 16:25:50 +01:00
|
|
|
if (!current_thread->IsRunnable()) {
|
|
|
|
current_thread->context_guard.unlock();
|
|
|
|
break;
|
|
|
|
}
|
2020-10-13 14:10:50 +02:00
|
|
|
if (static_cast<u32>(current_thread->GetProcessorID()) != core_id) {
|
2020-03-07 01:36:05 +01:00
|
|
|
current_thread->context_guard.unlock();
|
|
|
|
break;
|
|
|
|
}
|
2020-03-07 01:20:36 +01:00
|
|
|
}
|
2020-05-13 20:17:34 +02:00
|
|
|
std::shared_ptr<Common::Fiber>* next_context;
|
2020-03-07 01:20:36 +01:00
|
|
|
if (current_thread != nullptr) {
|
2020-05-13 20:17:34 +02:00
|
|
|
next_context = ¤t_thread->GetHostContext();
|
2020-03-07 01:20:36 +01:00
|
|
|
} else {
|
2020-05-13 20:17:34 +02:00
|
|
|
next_context = &idle_thread->GetHostContext();
|
2020-03-07 01:20:36 +01:00
|
|
|
}
|
2020-05-13 20:17:34 +02:00
|
|
|
Common::Fiber::YieldTo(switch_fiber, *next_context);
|
2020-08-26 03:55:44 +02:00
|
|
|
} while (!is_switch_pending());
|
2020-03-06 14:52:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 00:42:50 +02:00
|
|
|
void Scheduler::UpdateLastContextSwitchTime(Thread* thread, Process* process) {
|
|
|
|
const u64 prev_switch_ticks = last_context_switch_time;
|
2020-02-25 03:04:12 +01:00
|
|
|
const u64 most_recent_switch_ticks = system.CoreTiming().GetCPUTicks();
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:04:12 +01:00
|
|
|
void Scheduler::Initialize() {
|
|
|
|
std::string name = "Idle Thread Id:" + std::to_string(core_id);
|
2020-07-16 19:28:10 +02:00
|
|
|
std::function<void(void*)> init_func = Core::CpuManager::GetIdleThreadStartFunc();
|
2020-02-25 03:04:12 +01:00
|
|
|
void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater();
|
2020-10-21 04:07:39 +02:00
|
|
|
ThreadType type = static_cast<ThreadType>(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_IDLE);
|
|
|
|
auto thread_res = Thread::Create(system, type, name, 0, 64, 0, static_cast<u32>(core_id), 0,
|
|
|
|
nullptr, std::move(init_func), init_func_parameter);
|
2020-02-25 03:04:12 +01:00
|
|
|
idle_thread = std::move(thread_res).Unwrap();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-26 00:43:28 +01:00
|
|
|
void SchedulerLockAndSleep::Release() {
|
|
|
|
if (sleep_cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto& time_manager = kernel.TimeManager();
|
|
|
|
time_manager.ScheduleTimeEvent(event_handle, time_task, nanoseconds);
|
|
|
|
sleep_cancelled = true;
|
|
|
|
}
|
|
|
|
|
2018-02-18 20:58:40 +01:00
|
|
|
} // namespace Kernel
|