2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-12-04 01:43:18 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
2022-10-23 11:24:38 +02:00
|
|
|
#include <set>
|
2020-12-04 01:43:18 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/common_types.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>;
|
|
|
|
|
2023-03-07 01:45:40 +01: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
|
2023-03-07 01:45:40 +01:00
|
|
|
/// This is only safe to iterate while holding the scheduler lock
|
2021-04-03 03:02:10 +02:00
|
|
|
[[nodiscard]] const std::vector<KThread*>& GetThreadList() const {
|
2023-03-07 01:45:40 +01:00
|
|
|
return m_thread_list;
|
2020-12-04 01:43:18 +01: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.
|
|
|
|
*/
|
|
|
|
void PreemptThreads();
|
|
|
|
|
|
|
|
/// Returns true if the global scheduler lock is acquired
|
|
|
|
bool IsLocked() const;
|
|
|
|
|
2022-10-23 11:24:38 +02:00
|
|
|
void UnregisterDummyThreadForWakeup(KThread* thread);
|
|
|
|
void RegisterDummyThreadForWakeup(KThread* thread);
|
|
|
|
void WakeupWaitingDummyThreads();
|
|
|
|
|
2020-12-05 08:47:59 +01:00
|
|
|
[[nodiscard]] LockType& SchedulerLock() {
|
2023-03-07 01:45:40 +01:00
|
|
|
return m_scheduler_lock;
|
2020-12-04 07:26:42 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-03-07 01:45:40 +01:00
|
|
|
KernelCore& m_kernel;
|
2020-12-04 01:43:18 +01:00
|
|
|
|
2023-03-07 01:45:40 +01:00
|
|
|
std::atomic_bool m_scheduler_update_needed{};
|
|
|
|
KSchedulerPriorityQueue m_priority_queue;
|
|
|
|
LockType m_scheduler_lock;
|
2020-12-04 01:43:18 +01:00
|
|
|
|
2022-10-23 11:24:38 +02:00
|
|
|
/// Lists dummy threads pending wakeup on lock release
|
2023-03-07 01:45:40 +01:00
|
|
|
std::set<KThread*> m_woken_dummy_threads;
|
2022-10-23 11:24:38 +02:00
|
|
|
|
2020-12-04 01:43:18 +01:00
|
|
|
/// Lists all thread ids that aren't deleted/etc.
|
2023-03-07 01:45:40 +01:00
|
|
|
std::vector<KThread*> m_thread_list;
|
|
|
|
std::mutex m_global_list_guard;
|
2020-12-04 01:43:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|