k_scheduler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, s32 old_priority);
  81. /// Notify the scheduler a thread's core and/or affinity mask has changed.
  82. static void OnThreadAffinityMaskChanged(KernelCore& kernel, Thread* thread,
  83. const KAffinityMask& old_affinity, s32 old_core);
  84. static bool CanSchedule(KernelCore& kernel);
  85. static bool IsSchedulerUpdateNeeded(const KernelCore& kernel);
  86. static void SetSchedulerUpdateNeeded(KernelCore& kernel);
  87. static void ClearSchedulerUpdateNeeded(KernelCore& kernel);
  88. static void DisableScheduling(KernelCore& kernel);
  89. static void EnableScheduling(KernelCore& kernel, u64 cores_needing_scheduling,
  90. Core::EmuThreadHandle global_thread);
  91. [[nodiscard]] static u64 UpdateHighestPriorityThreads(KernelCore& kernel);
  92. private:
  93. friend class GlobalSchedulerContext;
  94. /**
  95. * Takes care of selecting the new scheduled threads in three steps:
  96. *
  97. * 1. First a thread is selected from the top of the priority queue. If no thread
  98. * is obtained then we move to step two, else we are done.
  99. *
  100. * 2. Second we try to get a suggested thread that's not assigned to any core or
  101. * that is not the top thread in that core.
  102. *
  103. * 3. Third is no suggested thread is found, we do a second pass and pick a running
  104. * thread in another core and swap it with its current thread.
  105. *
  106. * returns the cores needing scheduling.
  107. */
  108. [[nodiscard]] static u64 UpdateHighestPriorityThreadsImpl(KernelCore& kernel);
  109. [[nodiscard]] static KSchedulerPriorityQueue& GetPriorityQueue(KernelCore& kernel);
  110. void RotateScheduledQueue(s32 core_id, s32 priority);
  111. void Schedule() {
  112. ASSERT(GetCurrentThread()->GetDisableDispatchCount() == 1);
  113. this->ScheduleImpl();
  114. }
  115. /// Switches the CPU's active thread context to that of the specified thread
  116. void ScheduleImpl();
  117. /// When a thread wakes up, it must run this through it's new scheduler
  118. void SwitchContextStep2();
  119. /**
  120. * Called on every context switch to update the internal timestamp
  121. * This also updates the running time ticks for the given thread and
  122. * process using the following difference:
  123. *
  124. * ticks += most_recent_ticks - last_context_switch_ticks
  125. *
  126. * The internal tick timestamp for the scheduler is simply the
  127. * most recent tick count retrieved. No special arithmetic is
  128. * applied to it.
  129. */
  130. void UpdateLastContextSwitchTime(Thread* thread, Process* process);
  131. static void OnSwitch(void* this_scheduler);
  132. void SwitchToCurrent();
  133. Thread* current_thread{};
  134. Thread* idle_thread{};
  135. std::shared_ptr<Common::Fiber> switch_fiber{};
  136. struct SchedulingState {
  137. std::atomic<bool> needs_scheduling;
  138. bool interrupt_task_thread_runnable{};
  139. bool should_count_idle{};
  140. u64 idle_count{};
  141. Thread* highest_priority_thread{};
  142. void* idle_thread_stack{};
  143. };
  144. SchedulingState state;
  145. Core::System& system;
  146. u64 last_context_switch_time{};
  147. const std::size_t core_id;
  148. Common::SpinLock guard{};
  149. };
  150. class KScopedSchedulerLock : KScopedLock<GlobalSchedulerContext::LockType> {
  151. public:
  152. explicit KScopedSchedulerLock(KernelCore& kernel);
  153. ~KScopedSchedulerLock();
  154. };
  155. } // namespace Kernel