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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-05-08 04:12:45 +02:00
|
|
|
#include <mutex>
|
2018-02-18 20:58:40 +01:00
|
|
|
#include <vector>
|
|
|
|
#include "common/common_types.h"
|
2019-03-16 05:30:15 +01:00
|
|
|
#include "common/multi_level_queue.h"
|
2018-08-02 04:40:00 +02:00
|
|
|
#include "core/hle/kernel/object.h"
|
2018-02-18 20:58:40 +01:00
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
|
2018-08-25 03:43:32 +02:00
|
|
|
namespace Core {
|
2018-07-31 14:06:09 +02:00
|
|
|
class ARM_Interface;
|
2019-03-04 22:02:59 +01:00
|
|
|
class System;
|
|
|
|
} // namespace Core
|
2018-07-31 14:06:09 +02:00
|
|
|
|
2018-02-18 20:58:40 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2018-10-26 00:42:50 +02:00
|
|
|
class Process;
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
class GlobalScheduler final {
|
2018-02-18 20:58:40 +01:00
|
|
|
public:
|
2019-03-29 22:01:17 +01:00
|
|
|
static constexpr u32 NUM_CPU_CORES = 4;
|
2018-10-26 00:42:50 +02:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
explicit GlobalScheduler(Core::System& system);
|
2019-03-29 22:01:17 +01:00
|
|
|
~GlobalScheduler();
|
2018-02-18 20:58:40 +01:00
|
|
|
/// Adds a new thread to the scheduler
|
2019-03-30 10:26:38 +01:00
|
|
|
void AddThread(SharedPtr<Thread> thread);
|
2018-02-18 20:58:40 +01:00
|
|
|
|
|
|
|
/// Removes a thread from the scheduler
|
2019-06-19 15:11:18 +02:00
|
|
|
void RemoveThread(const Thread* thread);
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
/// Returns a list of all threads managed by the scheduler
|
|
|
|
const std::vector<SharedPtr<Thread>>& GetThreadList() const {
|
|
|
|
return thread_list;
|
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
// Add a thread to the suggested queue of a cpu core. Suggested threads may be
|
|
|
|
// picked if no thread is scheduled to run on the core.
|
2019-03-29 22:01:17 +01:00
|
|
|
void Suggest(u32 priority, u32 core, Thread* thread) {
|
|
|
|
suggested_queue[core].add(thread, priority);
|
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
// Remove a thread to the suggested queue of a cpu core. Suggested threads may be
|
|
|
|
// picked if no thread is scheduled to run on the core.
|
2019-03-29 22:01:17 +01:00
|
|
|
void Unsuggest(u32 priority, u32 core, Thread* thread) {
|
|
|
|
suggested_queue[core].remove(thread, priority);
|
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
// Add a thread to the scheduling queue of a cpu core. The thread is added at the
|
|
|
|
// back the queue in its priority level
|
|
|
|
void Schedule(u32 priority, u32 core, Thread* thread);
|
2018-11-22 06:33:53 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
// Add a thread to the scheduling queue of a cpu core. The thread is added at the
|
|
|
|
// front the queue in its priority level
|
|
|
|
void SchedulePrepend(u32 priority, u32 core, Thread* thread);
|
2018-11-22 06:33:53 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
// Reschedule an already scheduled thread based on a new priority
|
2019-03-29 22:01:17 +01:00
|
|
|
void Reschedule(u32 priority, u32 core, Thread* thread) {
|
|
|
|
scheduled_queue[core].remove(thread, priority);
|
|
|
|
scheduled_queue[core].add(thread, priority);
|
|
|
|
}
|
2018-11-22 06:33:53 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
// Unschedule a thread.
|
2019-03-29 22:01:17 +01:00
|
|
|
void Unschedule(u32 priority, u32 core, Thread* thread) {
|
|
|
|
scheduled_queue[core].remove(thread, priority);
|
|
|
|
}
|
2018-11-19 05:44:19 +01:00
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
// Transfers a thread into an specific core. If the destination_core is -1
|
|
|
|
// it will be unscheduled from its source code and added into its suggested
|
|
|
|
// queue.
|
2019-03-29 22:01:17 +01:00
|
|
|
void TransferToCore(u32 priority, s32 destination_core, Thread* thread) {
|
2019-06-19 15:11:18 +02:00
|
|
|
const bool schedulable = thread->GetPriority() < THREADPRIO_COUNT;
|
|
|
|
const s32 source_core = thread->GetProcessorID();
|
|
|
|
if (source_core == destination_core || !schedulable) {
|
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
|
|
|
thread->SetProcessorID(destination_core);
|
2019-06-19 15:11:18 +02:00
|
|
|
if (source_core >= 0) {
|
2019-03-29 22:01:17 +01:00
|
|
|
Unschedule(priority, source_core, thread);
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
if (destination_core >= 0) {
|
|
|
|
Unsuggest(priority, destination_core, thread);
|
|
|
|
Schedule(priority, destination_core, thread);
|
|
|
|
}
|
2019-06-19 15:11:18 +02:00
|
|
|
if (source_core >= 0) {
|
2019-03-29 22:01:17 +01:00
|
|
|
Suggest(priority, source_core, thread);
|
2019-06-19 15:11:18 +02:00
|
|
|
}
|
2019-03-29 22:01:17 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:03:44 +02:00
|
|
|
/*
|
|
|
|
* UnloadThread selects a core and forces it to unload its current thread's context
|
|
|
|
*/
|
2019-03-29 22:01:17 +01:00
|
|
|
void UnloadThread(s32 core);
|
|
|
|
|
2019-04-02 14:03:44 +02:00
|
|
|
/*
|
|
|
|
* SelectThread takes care of selecting the new scheduled thread.
|
|
|
|
* It does it in 3 steps:
|
|
|
|
* - First a thread is selected from the top of the priority queue. If no thread
|
|
|
|
* is obtained then we move to step two, else we are done.
|
|
|
|
* - Second we try to get a suggested thread that's not assigned to any core or
|
|
|
|
* that is not the top thread in that core.
|
|
|
|
* - Third is no suggested thread is found, we do a second pass and pick a running
|
|
|
|
* thread in another core and swap it with its current thread.
|
|
|
|
*/
|
2019-03-29 22:01:17 +01:00
|
|
|
void SelectThread(u32 core);
|
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
bool HaveReadyThreads(u32 core_id) const {
|
2019-03-29 22:01:17 +01:00
|
|
|
return !scheduled_queue[core_id].empty();
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:03:44 +02:00
|
|
|
/*
|
|
|
|
* YieldThread takes a thread and moves it to the back of the it's priority list
|
|
|
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
|
|
|
*/
|
2019-09-10 16:23:43 +02:00
|
|
|
bool YieldThread(Thread* thread);
|
2019-04-02 14:03:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* YieldThreadAndBalanceLoad takes a thread and moves it to the back of the it's priority list.
|
|
|
|
* Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
|
|
|
|
* a better priority than the next thread in the core.
|
|
|
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
|
|
|
*/
|
2019-09-10 16:23:43 +02:00
|
|
|
bool YieldThreadAndBalanceLoad(Thread* thread);
|
2019-04-02 14:03:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* YieldThreadAndWaitForLoadBalancing takes a thread and moves it out of the scheduling queue
|
|
|
|
* and into the suggested queue. If no thread can be squeduled afterwards in that core,
|
|
|
|
* a suggested thread is obtained instead.
|
|
|
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
|
|
|
*/
|
2019-09-10 16:23:43 +02:00
|
|
|
bool YieldThreadAndWaitForLoadBalancing(Thread* thread);
|
2019-03-29 22:01:17 +01:00
|
|
|
|
2019-09-10 17:04:40 +02:00
|
|
|
void PreemptThreads();
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
u32 CpuCoresCount() const {
|
|
|
|
return NUM_CPU_CORES;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetReselectionPending() {
|
|
|
|
reselection_pending.store(true, std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:11:18 +02:00
|
|
|
bool IsReselectionPending() const {
|
|
|
|
return reselection_pending.load();
|
2018-02-18 20:58:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-12 14:21:51 +02:00
|
|
|
void Shutdown();
|
|
|
|
|
2018-02-18 20:58:40 +01:00
|
|
|
private:
|
2019-09-10 16:23:43 +02:00
|
|
|
bool AskForReselectionOrMarkRedundant(Thread* current_thread, Thread* winner);
|
2019-03-29 22:01:17 +01:00
|
|
|
|
|
|
|
static constexpr u32 min_regular_priority = 2;
|
|
|
|
std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> scheduled_queue;
|
|
|
|
std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> suggested_queue;
|
|
|
|
std::atomic<bool> reselection_pending;
|
|
|
|
|
2019-09-10 21:26:24 +02:00
|
|
|
std::array<u32, NUM_CPU_CORES> preemption_priorities = {59, 59, 59, 62};
|
2019-09-10 17:04:40 +02:00
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
/// Lists all thread ids that aren't deleted/etc.
|
|
|
|
std::vector<SharedPtr<Thread>> thread_list;
|
2019-06-19 15:11:18 +02:00
|
|
|
Core::System& system;
|
2019-03-29 22:01:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Scheduler final {
|
|
|
|
public:
|
2019-06-19 15:11:18 +02:00
|
|
|
explicit Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, const u32 core_id);
|
2019-03-29 22:01:17 +01:00
|
|
|
~Scheduler();
|
|
|
|
|
|
|
|
/// Returns whether there are any threads that are ready to run.
|
|
|
|
bool HaveReadyThreads() const;
|
|
|
|
|
|
|
|
/// Reschedules to the next available thread (call after current thread is suspended)
|
|
|
|
void TryDoContextSwitch();
|
|
|
|
|
|
|
|
void UnloadThread();
|
|
|
|
|
|
|
|
void SelectThreads();
|
|
|
|
|
|
|
|
/// Gets the current running thread
|
|
|
|
Thread* GetCurrentThread() const;
|
|
|
|
|
|
|
|
Thread* GetSelectedThread() const;
|
|
|
|
|
|
|
|
/// Gets the timestamp for the last context switch in ticks.
|
|
|
|
u64 GetLastContextSwitchTicks() const;
|
|
|
|
|
|
|
|
bool ContextSwitchPending() const {
|
|
|
|
return context_switch_pending;
|
|
|
|
}
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2019-10-12 14:21:51 +02:00
|
|
|
void Shutdown() {
|
|
|
|
current_thread = nullptr;
|
|
|
|
selected_thread = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
private:
|
|
|
|
friend class GlobalScheduler;
|
2018-02-18 20:58:40 +01:00
|
|
|
/**
|
|
|
|
* Switches the CPU's active thread context to that of the specified thread
|
|
|
|
* @param new_thread The thread to switch to
|
|
|
|
*/
|
2019-03-29 22:01:17 +01:00
|
|
|
void SwitchContext();
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2018-10-26 00:42:50 +02:00
|
|
|
/**
|
|
|
|
* Called on every context switch to update the internal timestamp
|
|
|
|
* This also updates the running time ticks for the given thread and
|
|
|
|
* process using the following difference:
|
|
|
|
*
|
|
|
|
* ticks += most_recent_ticks - last_context_switch_ticks
|
|
|
|
*
|
|
|
|
* The internal tick timestamp for the scheduler is simply the
|
|
|
|
* most recent tick count retrieved. No special arithmetic is
|
|
|
|
* applied to it.
|
|
|
|
*/
|
|
|
|
void UpdateLastContextSwitchTime(Thread* thread, Process* process);
|
|
|
|
|
2018-02-18 20:58:40 +01:00
|
|
|
SharedPtr<Thread> current_thread = nullptr;
|
2019-03-29 22:01:17 +01:00
|
|
|
SharedPtr<Thread> selected_thread = nullptr;
|
2018-02-18 20:58:40 +01:00
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
Core::System& system;
|
2018-09-25 22:00:14 +02:00
|
|
|
Core::ARM_Interface& cpu_core;
|
2018-10-26 00:42:50 +02:00
|
|
|
u64 last_context_switch_time = 0;
|
2019-03-29 22:01:17 +01:00
|
|
|
u64 idle_selection_count = 0;
|
2019-06-19 15:11:18 +02:00
|
|
|
const u32 core_id;
|
2018-05-08 04:12:45 +02:00
|
|
|
|
2019-03-29 22:01:17 +01:00
|
|
|
bool context_switch_pending = false;
|
2018-02-18 20:58:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|