scheduler.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <utility>
  6. #include "common/assert.h"
  7. #include "common/logging/log.h"
  8. #include "core/arm/arm_interface.h"
  9. #include "core/core.h"
  10. #include "core/core_cpu.h"
  11. #include "core/core_timing.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/kernel/process.h"
  14. #include "core/hle/kernel/scheduler.h"
  15. namespace Kernel {
  16. std::mutex Scheduler::scheduler_mutex;
  17. Scheduler::Scheduler(Core::System& system, Core::ARM_Interface& cpu_core)
  18. : cpu_core{cpu_core}, system{system} {}
  19. Scheduler::~Scheduler() {
  20. for (auto& thread : thread_list) {
  21. thread->Stop();
  22. }
  23. }
  24. bool Scheduler::HaveReadyThreads() const {
  25. std::lock_guard lock{scheduler_mutex};
  26. return !ready_queue.empty();
  27. }
  28. Thread* Scheduler::GetCurrentThread() const {
  29. return current_thread.get();
  30. }
  31. u64 Scheduler::GetLastContextSwitchTicks() const {
  32. return last_context_switch_time;
  33. }
  34. Thread* Scheduler::PopNextReadyThread() {
  35. Thread* next = nullptr;
  36. Thread* thread = GetCurrentThread();
  37. if (thread && thread->GetStatus() == ThreadStatus::Running) {
  38. if (ready_queue.empty()) {
  39. return thread;
  40. }
  41. // We have to do better than the current thread.
  42. // This call returns null when that's not possible.
  43. next = ready_queue.front();
  44. if (next == nullptr || next->GetPriority() >= thread->GetPriority()) {
  45. next = thread;
  46. }
  47. } else {
  48. if (ready_queue.empty()) {
  49. return nullptr;
  50. }
  51. next = ready_queue.front();
  52. }
  53. return next;
  54. }
  55. void Scheduler::SwitchContext(Thread* new_thread) {
  56. Thread* previous_thread = GetCurrentThread();
  57. Process* const previous_process = system.Kernel().CurrentProcess();
  58. UpdateLastContextSwitchTime(previous_thread, previous_process);
  59. // Save context for previous thread
  60. if (previous_thread) {
  61. cpu_core.SaveContext(previous_thread->GetContext());
  62. // Save the TPIDR_EL0 system register in case it was modified.
  63. previous_thread->SetTPIDR_EL0(cpu_core.GetTPIDR_EL0());
  64. if (previous_thread->GetStatus() == ThreadStatus::Running) {
  65. // This is only the case when a reschedule is triggered without the current thread
  66. // yielding execution (i.e. an event triggered, system core time-sliced, etc)
  67. ready_queue.add(previous_thread, previous_thread->GetPriority(), false);
  68. previous_thread->SetStatus(ThreadStatus::Ready);
  69. }
  70. }
  71. // Load context of new thread
  72. if (new_thread) {
  73. ASSERT_MSG(new_thread->GetStatus() == ThreadStatus::Ready,
  74. "Thread must be ready to become running.");
  75. // Cancel any outstanding wakeup events for this thread
  76. new_thread->CancelWakeupTimer();
  77. current_thread = new_thread;
  78. ready_queue.remove(new_thread, new_thread->GetPriority());
  79. new_thread->SetStatus(ThreadStatus::Running);
  80. auto* const thread_owner_process = current_thread->GetOwnerProcess();
  81. if (previous_process != thread_owner_process) {
  82. system.Kernel().MakeCurrentProcess(thread_owner_process);
  83. }
  84. cpu_core.LoadContext(new_thread->GetContext());
  85. cpu_core.SetTlsAddress(new_thread->GetTLSAddress());
  86. cpu_core.SetTPIDR_EL0(new_thread->GetTPIDR_EL0());
  87. cpu_core.ClearExclusiveState();
  88. } else {
  89. current_thread = nullptr;
  90. // Note: We do not reset the current process and current page table when idling because
  91. // technically we haven't changed processes, our threads are just paused.
  92. }
  93. }
  94. void Scheduler::UpdateLastContextSwitchTime(Thread* thread, Process* process) {
  95. const u64 prev_switch_ticks = last_context_switch_time;
  96. const u64 most_recent_switch_ticks = system.CoreTiming().GetTicks();
  97. const u64 update_ticks = most_recent_switch_ticks - prev_switch_ticks;
  98. if (thread != nullptr) {
  99. thread->UpdateCPUTimeTicks(update_ticks);
  100. }
  101. if (process != nullptr) {
  102. process->UpdateCPUTimeTicks(update_ticks);
  103. }
  104. last_context_switch_time = most_recent_switch_ticks;
  105. }
  106. void Scheduler::Reschedule() {
  107. std::lock_guard lock{scheduler_mutex};
  108. Thread* cur = GetCurrentThread();
  109. Thread* next = PopNextReadyThread();
  110. if (cur && next) {
  111. LOG_TRACE(Kernel, "context switch {} -> {}", cur->GetObjectId(), next->GetObjectId());
  112. } else if (cur) {
  113. LOG_TRACE(Kernel, "context switch {} -> idle", cur->GetObjectId());
  114. } else if (next) {
  115. LOG_TRACE(Kernel, "context switch idle -> {}", next->GetObjectId());
  116. }
  117. SwitchContext(next);
  118. }
  119. void Scheduler::AddThread(SharedPtr<Thread> thread) {
  120. std::lock_guard lock{scheduler_mutex};
  121. thread_list.push_back(std::move(thread));
  122. }
  123. void Scheduler::RemoveThread(Thread* thread) {
  124. std::lock_guard lock{scheduler_mutex};
  125. thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
  126. thread_list.end());
  127. }
  128. void Scheduler::ScheduleThread(Thread* thread, u32 priority) {
  129. std::lock_guard lock{scheduler_mutex};
  130. ASSERT(thread->GetStatus() == ThreadStatus::Ready);
  131. ready_queue.add(thread, priority);
  132. }
  133. void Scheduler::UnscheduleThread(Thread* thread, u32 priority) {
  134. std::lock_guard lock{scheduler_mutex};
  135. ASSERT(thread->GetStatus() == ThreadStatus::Ready);
  136. ready_queue.remove(thread, priority);
  137. }
  138. void Scheduler::SetThreadPriority(Thread* thread, u32 priority) {
  139. std::lock_guard lock{scheduler_mutex};
  140. if (thread->GetPriority() == priority) {
  141. return;
  142. }
  143. // If thread was ready, adjust queues
  144. if (thread->GetStatus() == ThreadStatus::Ready)
  145. ready_queue.adjust(thread, thread->GetPriority(), priority);
  146. }
  147. Thread* Scheduler::GetNextSuggestedThread(u32 core, u32 maximum_priority) const {
  148. std::lock_guard lock{scheduler_mutex};
  149. const u32 mask = 1U << core;
  150. for (auto* thread : ready_queue) {
  151. if ((thread->GetAffinityMask() & mask) != 0 && thread->GetPriority() < maximum_priority) {
  152. return thread;
  153. }
  154. }
  155. return nullptr;
  156. }
  157. void Scheduler::YieldWithoutLoadBalancing(Thread* thread) {
  158. ASSERT(thread != nullptr);
  159. // Avoid yielding if the thread isn't even running.
  160. ASSERT(thread->GetStatus() == ThreadStatus::Running);
  161. // Sanity check that the priority is valid
  162. ASSERT(thread->GetPriority() < THREADPRIO_COUNT);
  163. // Yield this thread -- sleep for zero time and force reschedule to different thread
  164. GetCurrentThread()->Sleep(0);
  165. }
  166. void Scheduler::YieldWithLoadBalancing(Thread* thread) {
  167. ASSERT(thread != nullptr);
  168. const auto priority = thread->GetPriority();
  169. const auto core = static_cast<u32>(thread->GetProcessorID());
  170. // Avoid yielding if the thread isn't even running.
  171. ASSERT(thread->GetStatus() == ThreadStatus::Running);
  172. // Sanity check that the priority is valid
  173. ASSERT(priority < THREADPRIO_COUNT);
  174. // Sleep for zero time to be able to force reschedule to different thread
  175. GetCurrentThread()->Sleep(0);
  176. Thread* suggested_thread = nullptr;
  177. // Search through all of the cpu cores (except this one) for a suggested thread.
  178. // Take the first non-nullptr one
  179. for (unsigned cur_core = 0; cur_core < Core::NUM_CPU_CORES; ++cur_core) {
  180. const auto res =
  181. system.CpuCore(cur_core).Scheduler().GetNextSuggestedThread(core, priority);
  182. // If scheduler provides a suggested thread
  183. if (res != nullptr) {
  184. // And its better than the current suggested thread (or is the first valid one)
  185. if (suggested_thread == nullptr ||
  186. suggested_thread->GetPriority() > res->GetPriority()) {
  187. suggested_thread = res;
  188. }
  189. }
  190. }
  191. // If a suggested thread was found, queue that for this core
  192. if (suggested_thread != nullptr)
  193. suggested_thread->ChangeCore(core, suggested_thread->GetAffinityMask());
  194. }
  195. void Scheduler::YieldAndWaitForLoadBalancing(Thread* thread) {
  196. UNIMPLEMENTED_MSG("Wait for load balancing thread yield type is not implemented!");
  197. }
  198. } // namespace Kernel