scheduler.cpp 8.3 KB

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