k_scheduler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. // This file references various implementation details from Atmosphere, an open-source firmware for
  5. // the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX.
  6. #pragma once
  7. #include <atomic>
  8. #include "common/common_types.h"
  9. #include "common/spin_lock.h"
  10. #include "core/hle/kernel/global_scheduler_context.h"
  11. #include "core/hle/kernel/k_priority_queue.h"
  12. #include "core/hle/kernel/k_scheduler_lock.h"
  13. #include "core/hle/kernel/k_scoped_lock.h"
  14. namespace Common {
  15. class Fiber;
  16. }
  17. namespace Core {
  18. class System;
  19. }
  20. namespace Kernel {
  21. class KernelCore;
  22. class Process;
  23. class SchedulerLock;
  24. class Thread;
  25. class KScheduler final {
  26. public:
  27. explicit KScheduler(Core::System& system, std::size_t core_id);
  28. ~KScheduler();
  29. /// Reschedules to the next available thread (call after current thread is suspended)
  30. void RescheduleCurrentCore();
  31. /// Reschedules cores pending reschedule, to be called on EnableScheduling.
  32. static void RescheduleCores(KernelCore& kernel, u64 cores_pending_reschedule,
  33. Core::EmuThreadHandle global_thread);
  34. /// The next two are for SingleCore Only.
  35. /// Unload current thread before preempting core.
  36. void Unload(Thread* thread);
  37. /// Reload current thread after core preemption.
  38. void Reload(Thread* thread);
  39. /// Gets the current running thread
  40. [[nodiscard]] Thread* GetCurrentThread() const;
  41. /// Gets the timestamp for the last context switch in ticks.
  42. [[nodiscard]] u64 GetLastContextSwitchTicks() const;
  43. [[nodiscard]] bool ContextSwitchPending() const {
  44. return state.needs_scheduling.load(std::memory_order_relaxed);
  45. }
  46. void Initialize();
  47. void OnThreadStart();
  48. [[nodiscard]] std::shared_ptr<Common::Fiber>& ControlContext() {
  49. return switch_fiber;
  50. }
  51. [[nodiscard]] const std::shared_ptr<Common::Fiber>& ControlContext() const {
  52. return switch_fiber;
  53. }
  54. [[nodiscard]] u64 UpdateHighestPriorityThread(Thread* highest_thread);
  55. /**
  56. * Takes a thread and moves it to the back of the it's priority list.
  57. *
  58. * @note This operation can be redundant and no scheduling is changed if marked as so.
  59. */
  60. void YieldWithoutCoreMigration();
  61. /**
  62. * Takes a thread and moves it to the back of the it's priority list.
  63. * Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
  64. * a better priority than the next thread in the core.
  65. *
  66. * @note This operation can be redundant and no scheduling is changed if marked as so.
  67. */
  68. void YieldWithCoreMigration();
  69. /**
  70. * Takes a thread and moves it out of the scheduling queue.
  71. * and into the suggested queue. If no thread can be scheduled afterwards in that core,
  72. * a suggested thread is obtained instead.
  73. *
  74. * @note This operation can be redundant and no scheduling is changed if marked as so.
  75. */
  76. void YieldToAnyThread();
  77. /// Notify the scheduler a thread's status has changed.
  78. static void OnThreadStateChanged(KernelCore& kernel, Thread* thread, ThreadState old_state);
  79. /// Notify the scheduler a thread's priority has changed.
  80. static void OnThreadPriorityChanged(KernelCore& kernel, Thread* thread, Thread* current_thread,
  81. s32 old_priority);
  82. /// Notify the scheduler a thread's core and/or affinity mask has changed.
  83. static void OnThreadAffinityMaskChanged(KernelCore& kernel, Thread* thread,
  84. const KAffinityMask& old_affinity, s32 old_core);
  85. static bool CanSchedule(KernelCore& kernel);
  86. static bool IsSchedulerUpdateNeeded(const KernelCore& kernel);
  87. static void SetSchedulerUpdateNeeded(KernelCore& kernel);
  88. static void ClearSchedulerUpdateNeeded(KernelCore& kernel);
  89. static void DisableScheduling(KernelCore& kernel);
  90. static void EnableScheduling(KernelCore& kernel, u64 cores_needing_scheduling,
  91. Core::EmuThreadHandle global_thread);
  92. [[nodiscard]] static u64 UpdateHighestPriorityThreads(KernelCore& kernel);
  93. private:
  94. friend class GlobalSchedulerContext;
  95. /**
  96. * Takes care of selecting the new scheduled threads in three steps:
  97. *
  98. * 1. First a thread is selected from the top of the priority queue. If no thread
  99. * is obtained then we move to step two, else we are done.
  100. *
  101. * 2. Second we try to get a suggested thread that's not assigned to any core or
  102. * that is not the top thread in that core.
  103. *
  104. * 3. Third is no suggested thread is found, we do a second pass and pick a running
  105. * thread in another core and swap it with its current thread.
  106. *
  107. * returns the cores needing scheduling.
  108. */
  109. [[nodiscard]] static u64 UpdateHighestPriorityThreadsImpl(KernelCore& kernel);
  110. [[nodiscard]] static KSchedulerPriorityQueue& GetPriorityQueue(KernelCore& kernel);
  111. void RotateScheduledQueue(s32 core_id, s32 priority);
  112. void Schedule() {
  113. ASSERT(GetCurrentThread()->GetDisableDispatchCount() == 1);
  114. this->ScheduleImpl();
  115. }
  116. /// Switches the CPU's active thread context to that of the specified thread
  117. void ScheduleImpl();
  118. /// When a thread wakes up, it must run this through it's new scheduler
  119. void SwitchContextStep2();
  120. /**
  121. * Called on every context switch to update the internal timestamp
  122. * This also updates the running time ticks for the given thread and
  123. * process using the following difference:
  124. *
  125. * ticks += most_recent_ticks - last_context_switch_ticks
  126. *
  127. * The internal tick timestamp for the scheduler is simply the
  128. * most recent tick count retrieved. No special arithmetic is
  129. * applied to it.
  130. */
  131. void UpdateLastContextSwitchTime(Thread* thread, Process* process);
  132. static void OnSwitch(void* this_scheduler);
  133. void SwitchToCurrent();
  134. Thread* current_thread{};
  135. Thread* idle_thread{};
  136. std::shared_ptr<Common::Fiber> switch_fiber{};
  137. struct SchedulingState {
  138. std::atomic<bool> needs_scheduling;
  139. bool interrupt_task_thread_runnable{};
  140. bool should_count_idle{};
  141. u64 idle_count{};
  142. Thread* highest_priority_thread{};
  143. void* idle_thread_stack{};
  144. };
  145. SchedulingState state;
  146. Core::System& system;
  147. u64 last_context_switch_time{};
  148. const std::size_t core_id;
  149. Common::SpinLock guard{};
  150. };
  151. class KScopedSchedulerLock : KScopedLock<GlobalSchedulerContext::LockType> {
  152. public:
  153. explicit KScopedSchedulerLock(KernelCore& kernel);
  154. ~KScopedSchedulerLock();
  155. };
  156. } // namespace Kernel