k_scheduler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /// Reschedules to the next available thread (call after current thread is suspended)
  28. void RescheduleCurrentCore();
  29. /// Reschedules cores pending reschedule, to be called on EnableScheduling.
  30. static void RescheduleCores(KernelCore& kernel, u64 cores_pending_reschedule);
  31. /// The next two are for SingleCore Only.
  32. /// Unload current thread before preempting core.
  33. void Unload(KThread* thread);
  34. /// Reload current thread after core preemption.
  35. void Reload(KThread* thread);
  36. /// Gets the current running thread
  37. [[nodiscard]] KThread* GetCurrentThread() const;
  38. /// Returns true if the scheduler is idle
  39. [[nodiscard]] bool IsIdle() const {
  40. return GetCurrentThread() == idle_thread;
  41. }
  42. /// Gets the timestamp for the last context switch in ticks.
  43. [[nodiscard]] u64 GetLastContextSwitchTicks() const;
  44. [[nodiscard]] bool ContextSwitchPending() const {
  45. return state.needs_scheduling.load(std::memory_order_relaxed);
  46. }
  47. void Initialize();
  48. void OnThreadStart();
  49. [[nodiscard]] std::shared_ptr<Common::Fiber>& ControlContext() {
  50. return switch_fiber;
  51. }
  52. [[nodiscard]] const std::shared_ptr<Common::Fiber>& ControlContext() const {
  53. return switch_fiber;
  54. }
  55. [[nodiscard]] u64 UpdateHighestPriorityThread(KThread* highest_thread);
  56. /**
  57. * Takes a thread and moves it to the back of the it's priority list.
  58. *
  59. * @note This operation can be redundant and no scheduling is changed if marked as so.
  60. */
  61. static void YieldWithoutCoreMigration(KernelCore& kernel);
  62. /**
  63. * Takes a thread and moves it to the back of the it's priority list.
  64. * Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
  65. * a better priority than the next thread in the core.
  66. *
  67. * @note This operation can be redundant and no scheduling is changed if marked as so.
  68. */
  69. static void YieldWithCoreMigration(KernelCore& kernel);
  70. /**
  71. * Takes a thread and moves it out of the scheduling queue.
  72. * and into the suggested queue. If no thread can be scheduled afterwards in that core,
  73. * a suggested thread is obtained instead.
  74. *
  75. * @note This operation can be redundant and no scheduling is changed if marked as so.
  76. */
  77. static void YieldToAnyThread(KernelCore& kernel);
  78. static void ClearPreviousThread(KernelCore& kernel, KThread* thread);
  79. /// Notify the scheduler a thread's status has changed.
  80. static void OnThreadStateChanged(KernelCore& kernel, KThread* thread, ThreadState old_state);
  81. /// Notify the scheduler a thread's priority has changed.
  82. static void OnThreadPriorityChanged(KernelCore& kernel, KThread* thread, s32 old_priority);
  83. /// Notify the scheduler a thread's core and/or affinity mask has changed.
  84. static void OnThreadAffinityMaskChanged(KernelCore& kernel, KThread* thread,
  85. const KAffinityMask& old_affinity, s32 old_core);
  86. static bool CanSchedule(KernelCore& kernel);
  87. static bool IsSchedulerUpdateNeeded(const KernelCore& kernel);
  88. static void SetSchedulerUpdateNeeded(KernelCore& kernel);
  89. static void ClearSchedulerUpdateNeeded(KernelCore& kernel);
  90. static void DisableScheduling(KernelCore& kernel);
  91. static void EnableScheduling(KernelCore& kernel, u64 cores_needing_scheduling);
  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 cpu_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(KThread* thread, KProcess* process);
  132. static void OnSwitch(void* this_scheduler);
  133. void SwitchToCurrent();
  134. KThread* prev_thread{};
  135. std::atomic<KThread*> current_thread{};
  136. KThread* idle_thread{};
  137. std::shared_ptr<Common::Fiber> switch_fiber{};
  138. struct SchedulingState {
  139. std::atomic<bool> needs_scheduling{};
  140. bool interrupt_task_thread_runnable{};
  141. bool should_count_idle{};
  142. u64 idle_count{};
  143. KThread* highest_priority_thread{};
  144. void* idle_thread_stack{};
  145. };
  146. SchedulingState state;
  147. Core::System& system;
  148. u64 last_context_switch_time{};
  149. const s32 core_id;
  150. KSpinLock guard{};
  151. };
  152. class [[nodiscard]] KScopedSchedulerLock : KScopedLock<GlobalSchedulerContext::LockType> {
  153. public:
  154. explicit KScopedSchedulerLock(KernelCore & kernel);
  155. ~KScopedSchedulerLock();
  156. };
  157. } // namespace Kernel