k_scheduler.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 KThread;
  25. class KScheduler final {
  26. public:
  27. explicit KScheduler(Core::System& system, s32 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. /// The next two are for SingleCore Only.
  34. /// Unload current thread before preempting core.
  35. void Unload(KThread* thread);
  36. /// Reload current thread after core preemption.
  37. void Reload(KThread* thread);
  38. /// Gets the current running thread
  39. [[nodiscard]] KThread* GetCurrentThread() const;
  40. /// Returns true if the scheduler is idle
  41. [[nodiscard]] bool IsIdle() const {
  42. return GetCurrentThread() == idle_thread;
  43. }
  44. /// Gets the timestamp for the last context switch in ticks.
  45. [[nodiscard]] u64 GetLastContextSwitchTicks() const;
  46. [[nodiscard]] bool ContextSwitchPending() const {
  47. return state.needs_scheduling.load(std::memory_order_relaxed);
  48. }
  49. void Initialize();
  50. void OnThreadStart();
  51. [[nodiscard]] std::shared_ptr<Common::Fiber>& ControlContext() {
  52. return switch_fiber;
  53. }
  54. [[nodiscard]] const std::shared_ptr<Common::Fiber>& ControlContext() const {
  55. return switch_fiber;
  56. }
  57. [[nodiscard]] u64 UpdateHighestPriorityThread(KThread* highest_thread);
  58. /**
  59. * Takes a thread and moves it to the back of the it's priority list.
  60. *
  61. * @note This operation can be redundant and no scheduling is changed if marked as so.
  62. */
  63. static void YieldWithoutCoreMigration(KernelCore& kernel);
  64. /**
  65. * Takes a thread and moves it to the back of the it's priority list.
  66. * Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
  67. * a better priority than the next thread in the core.
  68. *
  69. * @note This operation can be redundant and no scheduling is changed if marked as so.
  70. */
  71. static void YieldWithCoreMigration(KernelCore& kernel);
  72. /**
  73. * Takes a thread and moves it out of the scheduling queue.
  74. * and into the suggested queue. If no thread can be scheduled afterwards in that core,
  75. * a suggested thread is obtained instead.
  76. *
  77. * @note This operation can be redundant and no scheduling is changed if marked as so.
  78. */
  79. static void YieldToAnyThread(KernelCore& kernel);
  80. static void ClearPreviousThread(KernelCore& kernel, KThread* thread);
  81. /// Notify the scheduler a thread's status has changed.
  82. static void OnThreadStateChanged(KernelCore& kernel, KThread* thread, ThreadState old_state);
  83. /// Notify the scheduler a thread's priority has changed.
  84. static void OnThreadPriorityChanged(KernelCore& kernel, KThread* thread, s32 old_priority);
  85. /// Notify the scheduler a thread's core and/or affinity mask has changed.
  86. static void OnThreadAffinityMaskChanged(KernelCore& kernel, KThread* thread,
  87. const KAffinityMask& old_affinity, s32 old_core);
  88. static bool CanSchedule(KernelCore& kernel);
  89. static bool IsSchedulerUpdateNeeded(const KernelCore& kernel);
  90. static void SetSchedulerUpdateNeeded(KernelCore& kernel);
  91. static void ClearSchedulerUpdateNeeded(KernelCore& kernel);
  92. static void DisableScheduling(KernelCore& kernel);
  93. static void EnableScheduling(KernelCore& kernel, u64 cores_needing_scheduling);
  94. [[nodiscard]] static u64 UpdateHighestPriorityThreads(KernelCore& kernel);
  95. private:
  96. friend class GlobalSchedulerContext;
  97. /**
  98. * Takes care of selecting the new scheduled threads in three steps:
  99. *
  100. * 1. First a thread is selected from the top of the priority queue. If no thread
  101. * is obtained then we move to step two, else we are done.
  102. *
  103. * 2. Second we try to get a suggested thread that's not assigned to any core or
  104. * that is not the top thread in that core.
  105. *
  106. * 3. Third is no suggested thread is found, we do a second pass and pick a running
  107. * thread in another core and swap it with its current thread.
  108. *
  109. * returns the cores needing scheduling.
  110. */
  111. [[nodiscard]] static u64 UpdateHighestPriorityThreadsImpl(KernelCore& kernel);
  112. [[nodiscard]] static KSchedulerPriorityQueue& GetPriorityQueue(KernelCore& kernel);
  113. void RotateScheduledQueue(s32 core_id, s32 priority);
  114. void Schedule() {
  115. ASSERT(GetCurrentThread()->GetDisableDispatchCount() == 1);
  116. this->ScheduleImpl();
  117. }
  118. /// Switches the CPU's active thread context to that of the specified thread
  119. void ScheduleImpl();
  120. /// When a thread wakes up, it must run this through it's new scheduler
  121. void SwitchContextStep2();
  122. /**
  123. * Called on every context switch to update the internal timestamp
  124. * This also updates the running time ticks for the given thread and
  125. * process using the following difference:
  126. *
  127. * ticks += most_recent_ticks - last_context_switch_ticks
  128. *
  129. * The internal tick timestamp for the scheduler is simply the
  130. * most recent tick count retrieved. No special arithmetic is
  131. * applied to it.
  132. */
  133. void UpdateLastContextSwitchTime(KThread* thread, Process* process);
  134. static void OnSwitch(void* this_scheduler);
  135. void SwitchToCurrent();
  136. KThread* prev_thread{};
  137. std::atomic<KThread*> current_thread{};
  138. KThread* idle_thread;
  139. std::shared_ptr<Common::Fiber> switch_fiber{};
  140. struct SchedulingState {
  141. std::atomic<bool> needs_scheduling;
  142. bool interrupt_task_thread_runnable{};
  143. bool should_count_idle{};
  144. u64 idle_count{};
  145. KThread* highest_priority_thread{};
  146. void* idle_thread_stack{};
  147. };
  148. SchedulingState state;
  149. Core::System& system;
  150. u64 last_context_switch_time{};
  151. const s32 core_id;
  152. Common::SpinLock guard{};
  153. };
  154. class KScopedSchedulerLock : KScopedLock<GlobalSchedulerContext::LockType> {
  155. public:
  156. explicit KScopedSchedulerLock(KernelCore& kernel);
  157. ~KScopedSchedulerLock();
  158. };
  159. } // namespace Kernel