scheduler.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. Memory::SetCurrentPageTable(&thread_owner_process->VMManager().page_table);
  84. }
  85. cpu_core.LoadContext(new_thread->GetContext());
  86. cpu_core.SetTlsAddress(new_thread->GetTLSAddress());
  87. cpu_core.SetTPIDR_EL0(new_thread->GetTPIDR_EL0());
  88. cpu_core.ClearExclusiveState();
  89. } else {
  90. current_thread = nullptr;
  91. // Note: We do not reset the current process and current page table when idling because
  92. // technically we haven't changed processes, our threads are just paused.
  93. }
  94. }
  95. void Scheduler::UpdateLastContextSwitchTime(Thread* thread, Process* process) {
  96. const u64 prev_switch_ticks = last_context_switch_time;
  97. const u64 most_recent_switch_ticks = system.CoreTiming().GetTicks();
  98. const u64 update_ticks = most_recent_switch_ticks - prev_switch_ticks;
  99. if (thread != nullptr) {
  100. thread->UpdateCPUTimeTicks(update_ticks);
  101. }
  102. if (process != nullptr) {
  103. process->UpdateCPUTimeTicks(update_ticks);
  104. }
  105. last_context_switch_time = most_recent_switch_ticks;
  106. }
  107. void Scheduler::Reschedule() {
  108. std::lock_guard lock{scheduler_mutex};
  109. Thread* cur = GetCurrentThread();
  110. Thread* next = PopNextReadyThread();
  111. if (cur && next) {
  112. LOG_TRACE(Kernel, "context switch {} -> {}", cur->GetObjectId(), next->GetObjectId());
  113. } else if (cur) {
  114. LOG_TRACE(Kernel, "context switch {} -> idle", cur->GetObjectId());
  115. } else if (next) {
  116. LOG_TRACE(Kernel, "context switch idle -> {}", next->GetObjectId());
  117. }
  118. SwitchContext(next);
  119. }
  120. void Scheduler::AddThread(SharedPtr<Thread> thread) {
  121. std::lock_guard lock{scheduler_mutex};
  122. thread_list.push_back(std::move(thread));
  123. }
  124. void Scheduler::RemoveThread(Thread* thread) {
  125. std::lock_guard lock{scheduler_mutex};
  126. thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
  127. thread_list.end());
  128. }
  129. void Scheduler::ScheduleThread(Thread* thread, u32 priority) {
  130. std::lock_guard lock{scheduler_mutex};
  131. ASSERT(thread->GetStatus() == ThreadStatus::Ready);
  132. ready_queue.add(thread, priority);
  133. }
  134. void Scheduler::UnscheduleThread(Thread* thread, u32 priority) {
  135. std::lock_guard lock{scheduler_mutex};
  136. ASSERT(thread->GetStatus() == ThreadStatus::Ready);
  137. ready_queue.remove(thread, priority);
  138. }
  139. void Scheduler::SetThreadPriority(Thread* thread, u32 priority) {
  140. std::lock_guard lock{scheduler_mutex};
  141. if (thread->GetPriority() == priority) {
  142. return;
  143. }
  144. // If thread was ready, adjust queues
  145. if (thread->GetStatus() == ThreadStatus::Ready)
  146. ready_queue.adjust(thread, thread->GetPriority(), priority);
  147. }
  148. Thread* Scheduler::GetNextSuggestedThread(u32 core, u32 maximum_priority) const {
  149. std::lock_guard lock{scheduler_mutex};
  150. const u32 mask = 1U << core;
  151. for (auto* thread : ready_queue) {
  152. if ((thread->GetAffinityMask() & mask) != 0 && thread->GetPriority() < maximum_priority) {
  153. return thread;
  154. }
  155. }
  156. return nullptr;
  157. }
  158. void Scheduler::YieldWithoutLoadBalancing(Thread* thread) {
  159. ASSERT(thread != nullptr);
  160. // Avoid yielding if the thread isn't even running.
  161. ASSERT(thread->GetStatus() == ThreadStatus::Running);
  162. // Sanity check that the priority is valid
  163. ASSERT(thread->GetPriority() < THREADPRIO_COUNT);
  164. // Yield this thread -- sleep for zero time and force reschedule to different thread
  165. GetCurrentThread()->Sleep(0);
  166. }
  167. void Scheduler::YieldWithLoadBalancing(Thread* thread) {
  168. ASSERT(thread != nullptr);
  169. const auto priority = thread->GetPriority();
  170. const auto core = static_cast<u32>(thread->GetProcessorID());
  171. // Avoid yielding if the thread isn't even running.
  172. ASSERT(thread->GetStatus() == ThreadStatus::Running);
  173. // Sanity check that the priority is valid
  174. ASSERT(priority < THREADPRIO_COUNT);
  175. // Sleep for zero time to be able to force reschedule to different thread
  176. GetCurrentThread()->Sleep(0);
  177. Thread* suggested_thread = nullptr;
  178. // Search through all of the cpu cores (except this one) for a suggested thread.
  179. // Take the first non-nullptr one
  180. for (unsigned cur_core = 0; cur_core < Core::NUM_CPU_CORES; ++cur_core) {
  181. const auto res =
  182. system.CpuCore(cur_core).Scheduler().GetNextSuggestedThread(core, priority);
  183. // If scheduler provides a suggested thread
  184. if (res != nullptr) {
  185. // And its better than the current suggested thread (or is the first valid one)
  186. if (suggested_thread == nullptr ||
  187. suggested_thread->GetPriority() > res->GetPriority()) {
  188. suggested_thread = res;
  189. }
  190. }
  191. }
  192. // If a suggested thread was found, queue that for this core
  193. if (suggested_thread != nullptr)
  194. suggested_thread->ChangeCore(core, suggested_thread->GetAffinityMask());
  195. }
  196. void Scheduler::YieldAndWaitForLoadBalancing(Thread* thread) {
  197. UNIMPLEMENTED_MSG("Wait for load balancing thread yield type is not implemented!");
  198. }
  199. } // namespace Kernel