2020-12-02 18:08:35 -08:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
2018-02-18 11:58:40 -08:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
// This file references various implementation details from Atmosphere, an open-source firmware for
|
|
|
|
// the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX.
|
|
|
|
|
2018-02-18 11:58:40 -08:00
|
|
|
#pragma once
|
|
|
|
|
2019-12-07 19:09:20 -08:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
2020-02-14 07:44:31 -08:00
|
|
|
#include <mutex>
|
2018-02-18 11:58:40 -08:00
|
|
|
#include <vector>
|
2019-12-07 19:09:20 -08:00
|
|
|
|
2018-02-18 11:58:40 -08:00
|
|
|
#include "common/common_types.h"
|
2019-03-15 21:30:15 -07:00
|
|
|
#include "common/multi_level_queue.h"
|
2020-12-02 18:08:35 -08:00
|
|
|
#include "common/scope_exit.h"
|
2020-02-24 18:04:12 -08:00
|
|
|
#include "common/spin_lock.h"
|
2020-12-02 18:08:35 -08:00
|
|
|
#include "core/core_timing.h"
|
2020-02-11 15:56:24 -08:00
|
|
|
#include "core/hardware_properties.h"
|
2020-12-02 18:08:35 -08:00
|
|
|
#include "core/hle/kernel/k_priority_queue.h"
|
|
|
|
#include "core/hle/kernel/k_scheduler_lock.h"
|
2018-02-18 11:58:40 -08:00
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
|
2020-03-06 05:52:24 -08:00
|
|
|
namespace Common {
|
2020-03-08 09:51:24 -07:00
|
|
|
class Fiber;
|
2020-03-06 05:52:24 -08:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:43:32 -07:00
|
|
|
namespace Core {
|
2018-07-31 05:06:09 -07:00
|
|
|
class ARM_Interface;
|
2019-03-04 13:02:59 -08:00
|
|
|
class System;
|
|
|
|
} // namespace Core
|
2018-07-31 05:06:09 -07:00
|
|
|
|
2018-02-18 11:58:40 -08:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-02-13 18:04:10 -08:00
|
|
|
class KernelCore;
|
2018-10-25 15:42:50 -07:00
|
|
|
class Process;
|
2020-02-14 07:44:31 -08:00
|
|
|
class SchedulerLock;
|
2018-10-25 15:42:50 -07:00
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
using KSchedulerPriorityQueue =
|
|
|
|
KPriorityQueue<Thread, Core::Hardware::NUM_CPU_CORES, THREADPRIO_LOWEST, THREADPRIO_HIGHEST>;
|
|
|
|
static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
|
|
|
|
|
|
|
|
class GlobalSchedulerContext final {
|
|
|
|
friend class KScheduler;
|
|
|
|
|
2018-02-18 11:58:40 -08:00
|
|
|
public:
|
2020-12-02 18:08:35 -08:00
|
|
|
explicit GlobalSchedulerContext(KernelCore& kernel);
|
|
|
|
~GlobalSchedulerContext();
|
2019-10-27 19:01:45 -07:00
|
|
|
|
2018-02-18 11:58:40 -08:00
|
|
|
/// Adds a new thread to the scheduler
|
2019-11-24 17:15:51 -08:00
|
|
|
void AddThread(std::shared_ptr<Thread> thread);
|
2018-02-18 11:58:40 -08:00
|
|
|
|
|
|
|
/// Removes a thread from the scheduler
|
2019-11-24 17:15:51 -08:00
|
|
|
void RemoveThread(std::shared_ptr<Thread> thread);
|
2018-02-18 11:58:40 -08:00
|
|
|
|
2019-03-29 14:01:17 -07:00
|
|
|
/// Returns a list of all threads managed by the scheduler
|
2019-11-24 17:15:51 -08:00
|
|
|
const std::vector<std::shared_ptr<Thread>>& GetThreadList() const {
|
2019-03-29 14:01:17 -07:00
|
|
|
return thread_list;
|
|
|
|
}
|
2018-02-18 11:58:40 -08:00
|
|
|
|
2019-10-27 19:01:45 -07:00
|
|
|
/**
|
|
|
|
* Rotates the scheduling queues of threads at a preemption priority and then does
|
|
|
|
* some core rebalancing. Preemption priorities can be found in the array
|
|
|
|
* 'preemption_priorities'.
|
|
|
|
*
|
|
|
|
* @note This operation happens every 10ms.
|
2019-10-12 07:13:25 -07:00
|
|
|
*/
|
2019-09-10 08:04:40 -07:00
|
|
|
void PreemptThreads();
|
|
|
|
|
2019-03-29 14:01:17 -07:00
|
|
|
u32 CpuCoresCount() const {
|
2020-02-11 15:56:24 -08:00
|
|
|
return Core::Hardware::NUM_CPU_CORES;
|
2019-03-29 14:01:17 -07:00
|
|
|
}
|
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
bool IsLocked() const;
|
2019-10-12 05:21:51 -07:00
|
|
|
|
2018-02-18 11:58:40 -08:00
|
|
|
private:
|
2020-02-14 07:44:31 -08:00
|
|
|
friend class SchedulerLock;
|
|
|
|
|
|
|
|
/// Lock the scheduler to the current thread.
|
|
|
|
void Lock();
|
|
|
|
|
|
|
|
/// Unlocks the scheduler, reselects threads, interrupts cores for rescheduling
|
|
|
|
/// and reschedules current core if needed.
|
|
|
|
void Unlock();
|
2020-02-24 18:04:12 -08:00
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
using LockType = KAbstractSchedulerLock<KScheduler>;
|
2019-09-10 08:04:40 -07:00
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
KernelCore& kernel;
|
2020-02-14 07:44:31 -08:00
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
std::atomic_bool scheduler_update_needed{};
|
|
|
|
KSchedulerPriorityQueue priority_queue;
|
|
|
|
LockType scheduler_lock;
|
2020-02-24 18:04:12 -08:00
|
|
|
|
2019-03-29 14:01:17 -07:00
|
|
|
/// Lists all thread ids that aren't deleted/etc.
|
2019-11-24 17:15:51 -08:00
|
|
|
std::vector<std::shared_ptr<Thread>> thread_list;
|
2020-12-02 18:08:35 -08:00
|
|
|
Common::SpinLock global_list_guard{};
|
2019-03-29 14:01:17 -07:00
|
|
|
};
|
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
class KScheduler final {
|
2019-03-29 14:01:17 -07:00
|
|
|
public:
|
2020-12-02 18:08:35 -08:00
|
|
|
explicit KScheduler(Core::System& system, std::size_t core_id);
|
|
|
|
~KScheduler();
|
2019-03-29 14:01:17 -07:00
|
|
|
|
|
|
|
/// Reschedules to the next available thread (call after current thread is suspended)
|
2020-12-02 18:08:35 -08:00
|
|
|
void RescheduleCurrentCore();
|
|
|
|
|
|
|
|
/// Reschedules cores pending reschedule, to be called on EnableScheduling.
|
|
|
|
static void RescheduleCores(KernelCore& kernel, u64 cores_pending_reschedule,
|
|
|
|
Core::EmuThreadHandle global_thread);
|
2019-03-29 14:01:17 -07:00
|
|
|
|
2020-03-10 08:50:33 -07:00
|
|
|
/// The next two are for SingleCore Only.
|
|
|
|
/// Unload current thread before preempting core.
|
2020-11-18 16:52:47 -08:00
|
|
|
void Unload(Thread* thread);
|
2020-12-02 18:08:35 -08:00
|
|
|
|
2020-03-10 08:50:33 -07:00
|
|
|
/// Reload current thread after core preemption.
|
2020-11-18 16:52:47 -08:00
|
|
|
void Reload(Thread* thread);
|
2020-03-10 08:50:33 -07:00
|
|
|
|
2019-03-29 14:01:17 -07:00
|
|
|
/// Gets the current running thread
|
|
|
|
Thread* GetCurrentThread() const;
|
|
|
|
|
|
|
|
/// Gets the timestamp for the last context switch in ticks.
|
|
|
|
u64 GetLastContextSwitchTicks() const;
|
|
|
|
|
|
|
|
bool ContextSwitchPending() const {
|
2020-12-02 18:08:35 -08:00
|
|
|
return this->state.needs_scheduling;
|
2019-03-29 14:01:17 -07:00
|
|
|
}
|
2018-02-18 11:58:40 -08:00
|
|
|
|
2020-02-24 18:04:12 -08:00
|
|
|
void Initialize();
|
|
|
|
|
|
|
|
void OnThreadStart();
|
|
|
|
|
2020-05-13 11:17:34 -07:00
|
|
|
std::shared_ptr<Common::Fiber>& ControlContext() {
|
2020-03-10 08:50:33 -07:00
|
|
|
return switch_fiber;
|
|
|
|
}
|
|
|
|
|
2020-06-27 15:20:06 -07:00
|
|
|
const std::shared_ptr<Common::Fiber>& ControlContext() const {
|
|
|
|
return switch_fiber;
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
std::size_t CurrentCoreId() const {
|
|
|
|
return core_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 UpdateHighestPriorityThread(Thread* highest_thread);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a thread and moves it to the back of the it's priority list.
|
|
|
|
*
|
|
|
|
* @note This operation can be redundant and no scheduling is changed if marked as so.
|
|
|
|
*/
|
|
|
|
void YieldWithoutCoreMigration();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @note This operation can be redundant and no scheduling is changed if marked as so.
|
|
|
|
*/
|
|
|
|
void YieldWithCoreMigration();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a thread and moves it out of the scheduling queue.
|
|
|
|
* and into the suggested queue. If no thread can be scheduled afterwards in that core,
|
|
|
|
* a suggested thread is obtained instead.
|
|
|
|
*
|
|
|
|
* @note This operation can be redundant and no scheduling is changed if marked as so.
|
|
|
|
*/
|
|
|
|
void YieldToAnyThread();
|
|
|
|
|
|
|
|
/// Notify the scheduler a thread's status has changed.
|
|
|
|
static void OnThreadStateChanged(KernelCore& kernel, Thread* thread, u32 old_state);
|
|
|
|
|
|
|
|
/// Notify the scheduler a thread's priority has changed.
|
|
|
|
static void OnThreadPriorityChanged(KernelCore& kernel, Thread* thread, Thread* current_thread,
|
|
|
|
u32 old_priority);
|
|
|
|
|
|
|
|
/// Notify the scheduler a thread's core and/or affinity mask has changed.
|
|
|
|
static void OnThreadAffinityMaskChanged(KernelCore& kernel, Thread* thread,
|
|
|
|
const KAffinityMask& old_affinity, s32 old_core);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Takes care of selecting the new scheduled threads in three steps:
|
|
|
|
*
|
|
|
|
* 1. 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.
|
|
|
|
*
|
|
|
|
* 2. 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.
|
|
|
|
*
|
|
|
|
* 3. 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.
|
|
|
|
*
|
|
|
|
* returns the cores needing scheduling.
|
|
|
|
*/
|
|
|
|
static u64 UpdateHighestPriorityThreadsImpl(KernelCore& kernel);
|
|
|
|
|
|
|
|
void RotateScheduledQueue(s32 core_id, s32 priority);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static bool CanSchedule(KernelCore& kernel);
|
|
|
|
static bool IsSchedulerUpdateNeeded(const KernelCore& kernel);
|
|
|
|
static void SetSchedulerUpdateNeeded(KernelCore& kernel);
|
|
|
|
static void ClearSchedulerUpdateNeeded(KernelCore& kernel);
|
|
|
|
static void DisableScheduling(KernelCore& kernel);
|
|
|
|
static void EnableScheduling(KernelCore& kernel, u64 cores_needing_scheduling,
|
|
|
|
Core::EmuThreadHandle global_thread);
|
|
|
|
static u64 UpdateHighestPriorityThreads(KernelCore& kernel);
|
|
|
|
|
2019-03-29 14:01:17 -07:00
|
|
|
private:
|
2020-12-02 18:08:35 -08:00
|
|
|
friend class GlobalSchedulerContext;
|
|
|
|
|
|
|
|
static KSchedulerPriorityQueue& GetPriorityQueue(KernelCore& kernel);
|
|
|
|
|
|
|
|
void Schedule() {
|
|
|
|
ASSERT(GetCurrentThread()->GetDisableDispatchCount() == 1);
|
|
|
|
this->ScheduleImpl();
|
|
|
|
}
|
2019-10-27 19:01:45 -07:00
|
|
|
|
|
|
|
/// Switches the CPU's active thread context to that of the specified thread
|
2020-12-02 18:08:35 -08:00
|
|
|
void ScheduleImpl();
|
|
|
|
void SwitchThread(Thread* next_thread);
|
2018-02-18 11:58:40 -08:00
|
|
|
|
2020-02-24 18:04:12 -08:00
|
|
|
/// When a thread wakes up, it must run this through it's new scheduler
|
|
|
|
void SwitchContextStep2();
|
|
|
|
|
2018-10-25 15:42:50 -07: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);
|
|
|
|
|
2020-03-06 05:52:24 -08:00
|
|
|
static void OnSwitch(void* this_scheduler);
|
|
|
|
void SwitchToCurrent();
|
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
private:
|
|
|
|
Thread* current_thread{};
|
|
|
|
Thread* idle_thread{};
|
|
|
|
|
|
|
|
std::shared_ptr<Common::Fiber> switch_fiber{};
|
2018-02-18 11:58:40 -08:00
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
struct SchedulingState {
|
|
|
|
std::atomic<bool> needs_scheduling;
|
|
|
|
bool interrupt_task_thread_runnable{};
|
|
|
|
bool should_count_idle{};
|
|
|
|
u64 idle_count{};
|
|
|
|
Thread* highest_priority_thread{};
|
|
|
|
void* idle_thread_stack{};
|
|
|
|
};
|
|
|
|
|
|
|
|
SchedulingState state;
|
2020-03-06 05:52:24 -08:00
|
|
|
|
2019-03-29 14:01:17 -07:00
|
|
|
Core::System& system;
|
2020-12-02 18:08:35 -08:00
|
|
|
u64 last_context_switch_time{};
|
2019-11-12 00:32:53 -08:00
|
|
|
const std::size_t core_id;
|
2018-05-07 19:12:45 -07:00
|
|
|
|
2020-02-24 18:04:12 -08:00
|
|
|
Common::SpinLock guard{};
|
2018-02-18 11:58:40 -08:00
|
|
|
};
|
|
|
|
|
2020-02-14 07:44:31 -08:00
|
|
|
class SchedulerLock {
|
|
|
|
public:
|
2020-08-02 17:15:20 -07:00
|
|
|
[[nodiscard]] explicit SchedulerLock(KernelCore& kernel);
|
2020-02-14 07:44:31 -08:00
|
|
|
~SchedulerLock();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
KernelCore& kernel;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SchedulerLockAndSleep : public SchedulerLock {
|
|
|
|
public:
|
2020-02-22 06:27:40 -08:00
|
|
|
explicit SchedulerLockAndSleep(KernelCore& kernel, Handle& event_handle, Thread* time_task,
|
|
|
|
s64 nanoseconds);
|
2020-02-14 07:44:31 -08:00
|
|
|
~SchedulerLockAndSleep();
|
|
|
|
|
|
|
|
void CancelSleep() {
|
|
|
|
sleep_cancelled = true;
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:43:28 -08:00
|
|
|
void Release();
|
|
|
|
|
2020-02-14 07:44:31 -08:00
|
|
|
private:
|
|
|
|
Handle& event_handle;
|
|
|
|
Thread* time_task;
|
|
|
|
s64 nanoseconds;
|
|
|
|
bool sleep_cancelled{};
|
|
|
|
};
|
|
|
|
|
2018-02-18 11:58:40 -08:00
|
|
|
} // namespace Kernel
|