global_scheduler_context.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <mutex>
  4. #include "common/assert.h"
  5. #include "core/core.h"
  6. #include "core/hle/kernel/global_scheduler_context.h"
  7. #include "core/hle/kernel/k_scheduler.h"
  8. #include "core/hle/kernel/kernel.h"
  9. #include "core/hle/kernel/physical_core.h"
  10. namespace Kernel {
  11. GlobalSchedulerContext::GlobalSchedulerContext(KernelCore& kernel)
  12. : m_kernel{kernel}, m_scheduler_lock{kernel} {}
  13. GlobalSchedulerContext::~GlobalSchedulerContext() = default;
  14. void GlobalSchedulerContext::AddThread(KThread* thread) {
  15. std::scoped_lock lock{m_global_list_guard};
  16. m_thread_list.push_back(thread);
  17. }
  18. void GlobalSchedulerContext::RemoveThread(KThread* thread) {
  19. std::scoped_lock lock{m_global_list_guard};
  20. std::erase(m_thread_list, thread);
  21. }
  22. void GlobalSchedulerContext::PreemptThreads() {
  23. // The priority levels at which the global scheduler preempts threads every 10 ms. They are
  24. // ordered from Core 0 to Core 3.
  25. static constexpr std::array<u32, Core::Hardware::NUM_CPU_CORES> preemption_priorities{
  26. 59,
  27. 59,
  28. 59,
  29. 63,
  30. };
  31. ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
  32. for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) {
  33. const u32 priority = preemption_priorities[core_id];
  34. KScheduler::RotateScheduledQueue(m_kernel, core_id, priority);
  35. }
  36. }
  37. bool GlobalSchedulerContext::IsLocked() const {
  38. return m_scheduler_lock.IsLockedByCurrentThread();
  39. }
  40. void GlobalSchedulerContext::RegisterDummyThreadForWakeup(KThread* thread) {
  41. ASSERT(this->IsLocked());
  42. m_woken_dummy_threads.insert(thread);
  43. }
  44. void GlobalSchedulerContext::UnregisterDummyThreadForWakeup(KThread* thread) {
  45. ASSERT(this->IsLocked());
  46. m_woken_dummy_threads.erase(thread);
  47. }
  48. void GlobalSchedulerContext::WakeupWaitingDummyThreads() {
  49. ASSERT(this->IsLocked());
  50. for (auto* thread : m_woken_dummy_threads) {
  51. thread->DummyThreadEndWait();
  52. }
  53. m_woken_dummy_threads.clear();
  54. }
  55. } // namespace Kernel