Compare commits
3 Commits
civa
...
static-vec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc4d7559d5 | ||
|
|
ac5671b154 | ||
|
|
98f02f0ebb |
@@ -1,7 +1,11 @@
|
|||||||
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "core/core.h"
|
#include "core/core.h"
|
||||||
@@ -17,57 +21,61 @@ GlobalSchedulerContext::GlobalSchedulerContext(KernelCore& kernel)
|
|||||||
|
|
||||||
GlobalSchedulerContext::~GlobalSchedulerContext() = default;
|
GlobalSchedulerContext::~GlobalSchedulerContext() = default;
|
||||||
|
|
||||||
void GlobalSchedulerContext::AddThread(KThread* thread) {
|
/// @brief Adds a new thread to the scheduler
|
||||||
|
void GlobalSchedulerContext::AddThread(KThread* thread) noexcept {
|
||||||
std::scoped_lock lock{m_global_list_guard};
|
std::scoped_lock lock{m_global_list_guard};
|
||||||
m_thread_list.push_back(thread);
|
m_thread_list.push_back(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::RemoveThread(KThread* thread) {
|
/// @brief Removes a thread from the scheduler
|
||||||
|
void GlobalSchedulerContext::RemoveThread(KThread* thread) noexcept {
|
||||||
std::scoped_lock lock{m_global_list_guard};
|
std::scoped_lock lock{m_global_list_guard};
|
||||||
std::erase(m_thread_list, thread);
|
m_thread_list.erase(std::ranges::find(m_thread_list, thread));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::PreemptThreads() {
|
/// @brief 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 GlobalSchedulerContext::PreemptThreads() noexcept {
|
||||||
// The priority levels at which the global scheduler preempts threads every 10 ms. They are
|
// The priority levels at which the global scheduler preempts threads every 10 ms. They are
|
||||||
// ordered from Core 0 to Core 3.
|
// ordered from Core 0 to Core 3.
|
||||||
static constexpr std::array<u32, Core::Hardware::NUM_CPU_CORES> preemption_priorities{
|
static constexpr std::array<u32, Core::Hardware::NUM_CPU_CORES> per_core{
|
||||||
59,
|
59,
|
||||||
59,
|
59,
|
||||||
59,
|
59,
|
||||||
63,
|
63,
|
||||||
};
|
};
|
||||||
|
|
||||||
ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
|
ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
|
||||||
for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) {
|
for (u32 core_id = 0; core_id < per_core.size(); core_id++)
|
||||||
const u32 priority = preemption_priorities[core_id];
|
KScheduler::RotateScheduledQueue(m_kernel, core_id, per_core[core_id]);
|
||||||
KScheduler::RotateScheduledQueue(m_kernel, core_id, priority);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GlobalSchedulerContext::IsLocked() const {
|
/// @brief Returns true if the global scheduler lock is acquired
|
||||||
|
bool GlobalSchedulerContext::IsLocked() const noexcept {
|
||||||
return m_scheduler_lock.IsLockedByCurrentThread();
|
return m_scheduler_lock.IsLockedByCurrentThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::RegisterDummyThreadForWakeup(KThread* thread) {
|
void GlobalSchedulerContext::RegisterDummyThreadForWakeup(KThread* thread) noexcept {
|
||||||
ASSERT(this->IsLocked());
|
ASSERT(this->IsLocked());
|
||||||
|
m_woken_dummy_threads.push_back(thread);
|
||||||
m_woken_dummy_threads.insert(thread);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::UnregisterDummyThreadForWakeup(KThread* thread) {
|
void GlobalSchedulerContext::UnregisterDummyThreadForWakeup(KThread* thread) noexcept {
|
||||||
ASSERT(this->IsLocked());
|
ASSERT(this->IsLocked());
|
||||||
|
if(auto it = std::ranges::find(m_woken_dummy_threads, thread); it != m_woken_dummy_threads.end()) {
|
||||||
m_woken_dummy_threads.erase(thread);
|
*it = m_woken_dummy_threads.back();
|
||||||
}
|
m_woken_dummy_threads.pop_back();
|
||||||
|
|
||||||
void GlobalSchedulerContext::WakeupWaitingDummyThreads() {
|
|
||||||
ASSERT(this->IsLocked());
|
|
||||||
|
|
||||||
for (auto* thread : m_woken_dummy_threads) {
|
|
||||||
thread->DummyThreadEndWait();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_woken_dummy_threads.clear();
|
void GlobalSchedulerContext::WakeupWaitingDummyThreads() noexcept {
|
||||||
|
ASSERT(this->IsLocked());
|
||||||
|
if (m_woken_dummy_threads.size() > 0) {
|
||||||
|
for (auto* thread : m_woken_dummy_threads)
|
||||||
|
thread->DummyThreadEndWait();
|
||||||
|
m_woken_dummy_threads.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <set>
|
#include <boost/container/small_vector.hpp>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
#include "core/hardware_properties.h"
|
#include "core/hardware_properties.h"
|
||||||
@@ -31,59 +33,42 @@ class GlobalSchedulerContext final {
|
|||||||
friend class KScheduler;
|
friend class KScheduler;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static constexpr size_t MAX_THREADS = 256;
|
||||||
using LockType = KAbstractSchedulerLock<KScheduler>;
|
using LockType = KAbstractSchedulerLock<KScheduler>;
|
||||||
|
using ThreadList = boost::container::small_vector<KThread*, MAX_THREADS>;
|
||||||
|
|
||||||
explicit GlobalSchedulerContext(KernelCore& kernel);
|
explicit GlobalSchedulerContext(KernelCore& kernel);
|
||||||
~GlobalSchedulerContext();
|
~GlobalSchedulerContext();
|
||||||
|
|
||||||
/// Adds a new thread to the scheduler
|
/// @brief Returns a list of all threads managed by the scheduler
|
||||||
void AddThread(KThread* thread);
|
|
||||||
|
|
||||||
/// Removes a thread from the scheduler
|
|
||||||
void RemoveThread(KThread* thread);
|
|
||||||
|
|
||||||
/// Returns a list of all threads managed by the scheduler
|
|
||||||
/// This is only safe to iterate while holding the scheduler lock
|
/// This is only safe to iterate while holding the scheduler lock
|
||||||
const std::vector<KThread*>& GetThreadList() const {
|
ThreadList const& GetThreadList() const noexcept {
|
||||||
return m_thread_list;
|
return m_thread_list;
|
||||||
}
|
}
|
||||||
|
LockType& SchedulerLock() noexcept {
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
void UnregisterDummyThreadForWakeup(KThread* thread);
|
|
||||||
void RegisterDummyThreadForWakeup(KThread* thread);
|
|
||||||
void WakeupWaitingDummyThreads();
|
|
||||||
|
|
||||||
LockType& SchedulerLock() {
|
|
||||||
return m_scheduler_lock;
|
return m_scheduler_lock;
|
||||||
}
|
}
|
||||||
|
void AddThread(KThread* thread) noexcept;
|
||||||
|
void RemoveThread(KThread* thread) noexcept;
|
||||||
|
void PreemptThreads() noexcept;
|
||||||
|
bool IsLocked() const noexcept;
|
||||||
|
void UnregisterDummyThreadForWakeup(KThread* thread) noexcept;
|
||||||
|
void RegisterDummyThreadForWakeup(KThread* thread) noexcept;
|
||||||
|
void WakeupWaitingDummyThreads() noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class KScopedSchedulerLock;
|
friend class KScopedSchedulerLock;
|
||||||
friend class KScopedSchedulerLockAndSleep;
|
friend class KScopedSchedulerLockAndSleep;
|
||||||
|
|
||||||
KernelCore& m_kernel;
|
KernelCore& m_kernel;
|
||||||
|
|
||||||
std::atomic_bool m_scheduler_update_needed{};
|
std::atomic_bool m_scheduler_update_needed{};
|
||||||
KSchedulerPriorityQueue m_priority_queue;
|
KSchedulerPriorityQueue m_priority_queue;
|
||||||
LockType m_scheduler_lock;
|
LockType m_scheduler_lock;
|
||||||
|
|
||||||
/// Lists dummy threads pending wakeup on lock release
|
|
||||||
std::set<KThread*> m_woken_dummy_threads;
|
|
||||||
|
|
||||||
/// Lists all thread ids that aren't deleted/etc.
|
|
||||||
std::vector<KThread*> m_thread_list;
|
|
||||||
std::mutex m_global_list_guard;
|
std::mutex m_global_list_guard;
|
||||||
|
/// Lists dummy threads pending wakeup on lock release
|
||||||
|
ThreadList m_woken_dummy_threads;
|
||||||
|
/// Lists all thread ids that aren't deleted/etc.
|
||||||
|
ThreadList m_thread_list;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|||||||
@@ -527,35 +527,27 @@ void KScheduler::ClearPreviousThread(KernelCore& kernel, KThread* thread) {
|
|||||||
|
|
||||||
void KScheduler::OnThreadStateChanged(KernelCore& kernel, KThread* thread, ThreadState old_state) {
|
void KScheduler::OnThreadStateChanged(KernelCore& kernel, KThread* thread, ThreadState old_state) {
|
||||||
ASSERT(IsSchedulerLockedByCurrentThread(kernel));
|
ASSERT(IsSchedulerLockedByCurrentThread(kernel));
|
||||||
|
|
||||||
// Check if the state has changed, because if it hasn't there's nothing to do.
|
// Check if the state has changed, because if it hasn't there's nothing to do.
|
||||||
const ThreadState cur_state = thread->GetRawState();
|
if (const ThreadState cur_state = thread->GetRawState(); cur_state != old_state) {
|
||||||
if (cur_state == old_state) {
|
// Update the priority queues.
|
||||||
return;
|
if (old_state == ThreadState::Runnable) {
|
||||||
}
|
// If we were previously runnable, then we're not runnable now, and we should remove.
|
||||||
|
GetPriorityQueue(kernel).Remove(thread);
|
||||||
// Update the priority queues.
|
IncrementScheduledCount(thread);
|
||||||
if (old_state == ThreadState::Runnable) {
|
SetSchedulerUpdateNeeded(kernel);
|
||||||
// If we were previously runnable, then we're not runnable now, and we should remove.
|
|
||||||
GetPriorityQueue(kernel).Remove(thread);
|
|
||||||
IncrementScheduledCount(thread);
|
|
||||||
SetSchedulerUpdateNeeded(kernel);
|
|
||||||
|
|
||||||
if (thread->IsDummyThread()) {
|
|
||||||
// HACK: if this is a dummy thread, it should no longer wake up when the
|
// HACK: if this is a dummy thread, it should no longer wake up when the
|
||||||
// scheduler lock is released.
|
// scheduler lock is released.
|
||||||
kernel.GlobalSchedulerContext().UnregisterDummyThreadForWakeup(thread);
|
if (thread->IsDummyThread())
|
||||||
}
|
kernel.GlobalSchedulerContext().UnregisterDummyThreadForWakeup(thread);
|
||||||
} else if (cur_state == ThreadState::Runnable) {
|
} else if (cur_state == ThreadState::Runnable) {
|
||||||
// If we're now runnable, then we weren't previously, and we should add.
|
// If we're now runnable, then we weren't previously, and we should add.
|
||||||
GetPriorityQueue(kernel).PushBack(thread);
|
GetPriorityQueue(kernel).PushBack(thread);
|
||||||
IncrementScheduledCount(thread);
|
IncrementScheduledCount(thread);
|
||||||
SetSchedulerUpdateNeeded(kernel);
|
SetSchedulerUpdateNeeded(kernel);
|
||||||
|
|
||||||
if (thread->IsDummyThread()) {
|
|
||||||
// HACK: if this is a dummy thread, it should wake up when the scheduler
|
// HACK: if this is a dummy thread, it should wake up when the scheduler
|
||||||
// lock is released.
|
// lock is released.
|
||||||
kernel.GlobalSchedulerContext().RegisterDummyThreadForWakeup(thread);
|
if (thread->IsDummyThread())
|
||||||
|
kernel.GlobalSchedulerContext().RegisterDummyThreadForWakeup(thread);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user