k_scheduler.h 6.7 KB

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