2020-12-04 01:43:18 +01:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/spin_lock.h"
|
|
|
|
#include "core/hardware_properties.h"
|
|
|
|
#include "core/hle/kernel/k_priority_queue.h"
|
|
|
|
#include "core/hle/kernel/k_scheduler_lock.h"
|
2020-12-31 08:01:08 +01:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2021-01-01 11:06:06 +01:00
|
|
|
#include "core/hle/kernel/svc_types.h"
|
2020-12-04 01:43:18 +01:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class KernelCore;
|
|
|
|
class SchedulerLock;
|
|
|
|
|
|
|
|
using KSchedulerPriorityQueue =
|
2021-01-20 06:08:35 +01:00
|
|
|
KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, Svc::LowestThreadPriority,
|
2021-01-01 11:06:06 +01:00
|
|
|
Svc::HighestThreadPriority>;
|
|
|
|
|
|
|
|
static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
|
|
|
|
static_assert(Svc::LowestThreadPriority >= HighestCoreMigrationAllowedPriority);
|
|
|
|
static_assert(Svc::HighestThreadPriority <= HighestCoreMigrationAllowedPriority);
|
2020-12-04 01:43:18 +01:00
|
|
|
|
|
|
|
class GlobalSchedulerContext final {
|
|
|
|
friend class KScheduler;
|
|
|
|
|
|
|
|
public:
|
2020-12-04 07:26:42 +01:00
|
|
|
using LockType = KAbstractSchedulerLock<KScheduler>;
|
|
|
|
|
2021-05-16 07:46:30 +02:00
|
|
|
explicit GlobalSchedulerContext(KernelCore& kernel_);
|
2020-12-04 01:43:18 +01:00
|
|
|
~GlobalSchedulerContext();
|
|
|
|
|
|
|
|
/// Adds a new thread to the scheduler
|
2021-04-03 03:02:10 +02:00
|
|
|
void AddThread(KThread* thread);
|
2020-12-04 01:43:18 +01:00
|
|
|
|
|
|
|
/// Removes a thread from the scheduler
|
2021-04-03 03:02:10 +02:00
|
|
|
void RemoveThread(KThread* thread);
|
2020-12-04 01:43:18 +01:00
|
|
|
|
|
|
|
/// Returns a list of all threads managed by the scheduler
|
2021-04-03 03:02:10 +02:00
|
|
|
[[nodiscard]] const std::vector<KThread*>& GetThreadList() const {
|
2020-12-04 01:43:18 +01:00
|
|
|
return thread_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
void PreemptThreads();
|
|
|
|
|
|
|
|
/// Returns true if the global scheduler lock is acquired
|
|
|
|
bool IsLocked() const;
|
|
|
|
|
2020-12-05 08:47:59 +01:00
|
|
|
[[nodiscard]] LockType& SchedulerLock() {
|
2020-12-04 07:26:42 +01:00
|
|
|
return scheduler_lock;
|
|
|
|
}
|
|
|
|
|
2020-12-05 08:47:59 +01:00
|
|
|
[[nodiscard]] const LockType& SchedulerLock() const {
|
2020-12-04 07:26:42 +01:00
|
|
|
return scheduler_lock;
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:43:18 +01:00
|
|
|
private:
|
2020-12-04 07:26:42 +01:00
|
|
|
friend class KScopedSchedulerLock;
|
2020-12-04 06:56:02 +01:00
|
|
|
friend class KScopedSchedulerLockAndSleep;
|
2020-12-04 01:43:18 +01:00
|
|
|
|
|
|
|
KernelCore& kernel;
|
|
|
|
|
|
|
|
std::atomic_bool scheduler_update_needed{};
|
|
|
|
KSchedulerPriorityQueue priority_queue;
|
|
|
|
LockType scheduler_lock;
|
|
|
|
|
|
|
|
/// Lists all thread ids that aren't deleted/etc.
|
2021-04-03 03:02:10 +02:00
|
|
|
std::vector<KThread*> thread_list;
|
2020-12-04 01:43:18 +01:00
|
|
|
Common::SpinLock global_list_guard{};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|