k_scheduler.h 6.5 KB

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