summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/kernel/scheduler.cpp55
-rw-r--r--src/core/hle/kernel/scheduler.h109
2 files changed, 73 insertions, 91 deletions
diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp
index e6dcb9639..0e2dbf13e 100644
--- a/src/core/hle/kernel/scheduler.cpp
+++ b/src/core/hle/kernel/scheduler.cpp
@@ -22,9 +22,9 @@
22 22
23namespace Kernel { 23namespace Kernel {
24 24
25GlobalScheduler::GlobalScheduler(Core::System& system) : system{system} { 25GlobalScheduler::GlobalScheduler(Core::System& system) : system{system} {}
26 is_reselection_pending = false; 26
27} 27GlobalScheduler::~GlobalScheduler() = default;
28 28
29void GlobalScheduler::AddThread(SharedPtr<Thread> thread) { 29void GlobalScheduler::AddThread(SharedPtr<Thread> thread) {
30 thread_list.push_back(std::move(thread)); 30 thread_list.push_back(std::move(thread));
@@ -35,24 +35,11 @@ void GlobalScheduler::RemoveThread(const Thread* thread) {
35 thread_list.end()); 35 thread_list.end());
36} 36}
37 37
38/*
39 * UnloadThread selects a core and forces it to unload its current thread's context
40 */
41void GlobalScheduler::UnloadThread(s32 core) { 38void GlobalScheduler::UnloadThread(s32 core) {
42 Scheduler& sched = system.Scheduler(core); 39 Scheduler& sched = system.Scheduler(core);
43 sched.UnloadThread(); 40 sched.UnloadThread();
44} 41}
45 42
46/*
47 * SelectThread takes care of selecting the new scheduled thread.
48 * It does it in 3 steps:
49 * - First a thread is selected from the top of the priority queue. If no thread
50 * is obtained then we move to step two, else we are done.
51 * - Second we try to get a suggested thread that's not assigned to any core or
52 * that is not the top thread in that core.
53 * - Third is no suggested thread is found, we do a second pass and pick a running
54 * thread in another core and swap it with its current thread.
55 */
56void GlobalScheduler::SelectThread(u32 core) { 43void GlobalScheduler::SelectThread(u32 core) {
57 const auto update_thread = [](Thread* thread, Scheduler& sched) { 44 const auto update_thread = [](Thread* thread, Scheduler& sched) {
58 if (thread != sched.selected_thread) { 45 if (thread != sched.selected_thread) {
@@ -114,30 +101,19 @@ void GlobalScheduler::SelectThread(u32 core) {
114 update_thread(current_thread, sched); 101 update_thread(current_thread, sched);
115} 102}
116 103
117/*
118 * YieldThread takes a thread and moves it to the back of the it's priority list
119 * This operation can be redundant and no scheduling is changed if marked as so.
120 */
121bool GlobalScheduler::YieldThread(Thread* yielding_thread) { 104bool GlobalScheduler::YieldThread(Thread* yielding_thread) {
122 // Note: caller should use critical section, etc. 105 // Note: caller should use critical section, etc.
123 const u32 core_id = static_cast<u32>(yielding_thread->GetProcessorID()); 106 const u32 core_id = static_cast<u32>(yielding_thread->GetProcessorID());
124 const u32 priority = yielding_thread->GetPriority(); 107 const u32 priority = yielding_thread->GetPriority();
125 108
126 // Yield the thread 109 // Yield the thread
127 ASSERT_MSG(yielding_thread == scheduled_queue[core_id].front(priority), 110 const Thread* const winner = scheduled_queue[core_id].front(priority);
128 "Thread yielding without being in front"); 111 ASSERT_MSG(yielding_thread == winner, "Thread yielding without being in front");
129 scheduled_queue[core_id].yield(priority); 112 scheduled_queue[core_id].yield(priority);
130 113
131 Thread* winner = scheduled_queue[core_id].front(priority);
132 return AskForReselectionOrMarkRedundant(yielding_thread, winner); 114 return AskForReselectionOrMarkRedundant(yielding_thread, winner);
133} 115}
134 116
135/*
136 * YieldThreadAndBalanceLoad takes a thread and moves it to the back of the it's priority list.
137 * Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
138 * a better priority than the next thread in the core.
139 * This operation can be redundant and no scheduling is changed if marked as so.
140 */
141bool GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) { 117bool GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) {
142 // Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section, 118 // Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
143 // etc. 119 // etc.
@@ -189,12 +165,6 @@ bool GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) {
189 return AskForReselectionOrMarkRedundant(yielding_thread, winner); 165 return AskForReselectionOrMarkRedundant(yielding_thread, winner);
190} 166}
191 167
192/*
193 * YieldThreadAndWaitForLoadBalancing takes a thread and moves it out of the scheduling queue
194 * and into the suggested queue. If no thread can be squeduled afterwards in that core,
195 * a suggested thread is obtained instead.
196 * This operation can be redundant and no scheduling is changed if marked as so.
197 */
198bool GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread) { 168bool GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread) {
199 // Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section, 169 // Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
200 // etc. 170 // etc.
@@ -280,7 +250,7 @@ void GlobalScheduler::PreemptThreads() {
280 if (winner->IsRunning()) { 250 if (winner->IsRunning()) {
281 UnloadThread(winner->GetProcessorID()); 251 UnloadThread(winner->GetProcessorID());
282 } 252 }
283 TransferToCore(winner->GetPriority(), core_id, winner); 253 TransferToCore(winner->GetPriority(), s32(core_id), winner);
284 current_thread = 254 current_thread =
285 winner->GetPriority() <= current_thread->GetPriority() ? winner : current_thread; 255 winner->GetPriority() <= current_thread->GetPriority() ? winner : current_thread;
286 } 256 }
@@ -313,7 +283,7 @@ void GlobalScheduler::PreemptThreads() {
313 if (winner->IsRunning()) { 283 if (winner->IsRunning()) {
314 UnloadThread(winner->GetProcessorID()); 284 UnloadThread(winner->GetProcessorID());
315 } 285 }
316 TransferToCore(winner->GetPriority(), core_id, winner); 286 TransferToCore(winner->GetPriority(), s32(core_id), winner);
317 current_thread = winner; 287 current_thread = winner;
318 } 288 }
319 } 289 }
@@ -331,12 +301,12 @@ void GlobalScheduler::Unsuggest(u32 priority, u32 core, Thread* thread) {
331} 301}
332 302
333void GlobalScheduler::Schedule(u32 priority, u32 core, Thread* thread) { 303void GlobalScheduler::Schedule(u32 priority, u32 core, Thread* thread) {
334 ASSERT_MSG(thread->GetProcessorID() == core, "Thread must be assigned to this core."); 304 ASSERT_MSG(thread->GetProcessorID() == s32(core), "Thread must be assigned to this core.");
335 scheduled_queue[core].add(thread, priority); 305 scheduled_queue[core].add(thread, priority);
336} 306}
337 307
338void GlobalScheduler::SchedulePrepend(u32 priority, u32 core, Thread* thread) { 308void GlobalScheduler::SchedulePrepend(u32 priority, u32 core, Thread* thread) {
339 ASSERT_MSG(thread->GetProcessorID() == core, "Thread must be assigned to this core."); 309 ASSERT_MSG(thread->GetProcessorID() == s32(core), "Thread must be assigned to this core.");
340 scheduled_queue[core].add(thread, priority, false); 310 scheduled_queue[core].add(thread, priority, false);
341} 311}
342 312
@@ -368,7 +338,8 @@ void GlobalScheduler::TransferToCore(u32 priority, s32 destination_core, Thread*
368 } 338 }
369} 339}
370 340
371bool GlobalScheduler::AskForReselectionOrMarkRedundant(Thread* current_thread, Thread* winner) { 341bool GlobalScheduler::AskForReselectionOrMarkRedundant(Thread* current_thread,
342 const Thread* winner) {
372 if (current_thread == winner) { 343 if (current_thread == winner) {
373 current_thread->IncrementYieldCount(); 344 current_thread->IncrementYieldCount();
374 return true; 345 return true;
@@ -386,8 +357,6 @@ void GlobalScheduler::Shutdown() {
386 thread_list.clear(); 357 thread_list.clear();
387} 358}
388 359
389GlobalScheduler::~GlobalScheduler() = default;
390
391Scheduler::Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, u32 core_id) 360Scheduler::Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, u32 core_id)
392 : system(system), cpu_core(cpu_core), core_id(core_id) {} 361 : system(system), cpu_core(cpu_core), core_id(core_id) {}
393 362
@@ -470,7 +439,7 @@ void Scheduler::SwitchContext() {
470 439
471 // Load context of new thread 440 // Load context of new thread
472 if (new_thread) { 441 if (new_thread) {
473 ASSERT_MSG(new_thread->GetProcessorID() == this->core_id, 442 ASSERT_MSG(new_thread->GetProcessorID() == s32(this->core_id),
474 "Thread must be assigned to this core."); 443 "Thread must be assigned to this core.");
475 ASSERT_MSG(new_thread->GetStatus() == ThreadStatus::Ready, 444 ASSERT_MSG(new_thread->GetStatus() == ThreadStatus::Ready,
476 "Thread must be ready to become running."); 445 "Thread must be ready to become running.");
diff --git a/src/core/hle/kernel/scheduler.h b/src/core/hle/kernel/scheduler.h
index fcae28e0a..f2d6311b8 100644
--- a/src/core/hle/kernel/scheduler.h
+++ b/src/core/hle/kernel/scheduler.h
@@ -26,6 +26,7 @@ public:
26 26
27 explicit GlobalScheduler(Core::System& system); 27 explicit GlobalScheduler(Core::System& system);
28 ~GlobalScheduler(); 28 ~GlobalScheduler();
29
29 /// Adds a new thread to the scheduler 30 /// Adds a new thread to the scheduler
30 void AddThread(SharedPtr<Thread> thread); 31 void AddThread(SharedPtr<Thread> thread);
31 32
@@ -37,47 +38,57 @@ public:
37 return thread_list; 38 return thread_list;
38 } 39 }
39 40
40 // Add a thread to the suggested queue of a cpu core. Suggested threads may be 41 /**
41 // picked if no thread is scheduled to run on the core. 42 * Add a thread to the suggested queue of a cpu core. Suggested threads may be
43 * picked if no thread is scheduled to run on the core.
44 */
42 void Suggest(u32 priority, u32 core, Thread* thread); 45 void Suggest(u32 priority, u32 core, Thread* thread);
43 46
44 // Remove a thread to the suggested queue of a cpu core. Suggested threads may be 47 /**
45 // picked if no thread is scheduled to run on the core. 48 * Remove a thread to the suggested queue of a cpu core. Suggested threads may be
49 * picked if no thread is scheduled to run on the core.
50 */
46 void Unsuggest(u32 priority, u32 core, Thread* thread); 51 void Unsuggest(u32 priority, u32 core, Thread* thread);
47 52
48 // Add a thread to the scheduling queue of a cpu core. The thread is added at the 53 /**
49 // back the queue in its priority level 54 * Add a thread to the scheduling queue of a cpu core. The thread is added at the
55 * back the queue in its priority level.
56 */
50 void Schedule(u32 priority, u32 core, Thread* thread); 57 void Schedule(u32 priority, u32 core, Thread* thread);
51 58
52 // Add a thread to the scheduling queue of a cpu core. The thread is added at the 59 /**
53 // front the queue in its priority level 60 * Add a thread to the scheduling queue of a cpu core. The thread is added at the
61 * front the queue in its priority level.
62 */
54 void SchedulePrepend(u32 priority, u32 core, Thread* thread); 63 void SchedulePrepend(u32 priority, u32 core, Thread* thread);
55 64
56 // Reschedule an already scheduled thread based on a new priority 65 /// Reschedule an already scheduled thread based on a new priority
57 void Reschedule(u32 priority, u32 core, Thread* thread); 66 void Reschedule(u32 priority, u32 core, Thread* thread);
58 67
59 // Unschedule a thread. 68 /// Unschedules a thread.
60 void Unschedule(u32 priority, u32 core, Thread* thread); 69 void Unschedule(u32 priority, u32 core, Thread* thread);
61 70
62 // Transfers a thread into an specific core. If the destination_core is -1 71 /**
63 // it will be unscheduled from its source code and added into its suggested 72 * Transfers a thread into an specific core. If the destination_core is -1
64 // queue. 73 * it will be unscheduled from its source code and added into its suggested
74 * queue.
75 */
65 void TransferToCore(u32 priority, s32 destination_core, Thread* thread); 76 void TransferToCore(u32 priority, s32 destination_core, Thread* thread);
66 77
67 /* 78 /// Selects a core and forces it to unload its current thread's context
68 * UnloadThread selects a core and forces it to unload its current thread's context
69 */
70 void UnloadThread(s32 core); 79 void UnloadThread(s32 core);
71 80
72 /* 81 /**
73 * SelectThread takes care of selecting the new scheduled thread. 82 * Takes care of selecting the new scheduled thread in three steps:
74 * It does it in 3 steps: 83 *
75 * - First a thread is selected from the top of the priority queue. If no thread 84 * 1. First a thread is selected from the top of the priority queue. If no thread
76 * is obtained then we move to step two, else we are done. 85 * is obtained then we move to step two, else we are done.
77 * - Second we try to get a suggested thread that's not assigned to any core or 86 *
78 * that is not the top thread in that core. 87 * 2. Second we try to get a suggested thread that's not assigned to any core or
79 * - Third is no suggested thread is found, we do a second pass and pick a running 88 * that is not the top thread in that core.
80 * thread in another core and swap it with its current thread. 89 *
90 * 3. Third is no suggested thread is found, we do a second pass and pick a running
91 * thread in another core and swap it with its current thread.
81 */ 92 */
82 void SelectThread(u32 core); 93 void SelectThread(u32 core);
83 94
@@ -85,33 +96,37 @@ public:
85 return !scheduled_queue[core_id].empty(); 96 return !scheduled_queue[core_id].empty();
86 } 97 }
87 98
88 /* 99 /**
89 * YieldThread takes a thread and moves it to the back of the it's priority list 100 * Takes a thread and moves it to the back of the it's priority list.
90 * This operation can be redundant and no scheduling is changed if marked as so. 101 *
102 * @note This operation can be redundant and no scheduling is changed if marked as so.
91 */ 103 */
92 bool YieldThread(Thread* thread); 104 bool YieldThread(Thread* thread);
93 105
94 /* 106 /**
95 * YieldThreadAndBalanceLoad takes a thread and moves it to the back of the it's priority list. 107 * Takes a thread and moves it to the back of the it's priority list.
96 * Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or 108 * Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
97 * a better priority than the next thread in the core. 109 * a better priority than the next thread in the core.
98 * This operation can be redundant and no scheduling is changed if marked as so. 110 *
111 * @note This operation can be redundant and no scheduling is changed if marked as so.
99 */ 112 */
100 bool YieldThreadAndBalanceLoad(Thread* thread); 113 bool YieldThreadAndBalanceLoad(Thread* thread);
101 114
102 /* 115 /**
103 * YieldThreadAndWaitForLoadBalancing takes a thread and moves it out of the scheduling queue 116 * Takes a thread and moves it out of the scheduling queue.
104 * and into the suggested queue. If no thread can be squeduled afterwards in that core, 117 * and into the suggested queue. If no thread can be scheduled afterwards in that core,
105 * a suggested thread is obtained instead. 118 * a suggested thread is obtained instead.
106 * This operation can be redundant and no scheduling is changed if marked as so. 119 *
120 * @note This operation can be redundant and no scheduling is changed if marked as so.
107 */ 121 */
108 bool YieldThreadAndWaitForLoadBalancing(Thread* thread); 122 bool YieldThreadAndWaitForLoadBalancing(Thread* thread);
109 123
110 /* 124 /**
111 * PreemptThreads this operation rotates the scheduling queues of threads at 125 * Rotates the scheduling queues of threads at a preemption priority and then does
112 * a preemption priority and then does some core rebalancing. Preemption priorities 126 * some core rebalancing. Preemption priorities can be found in the array
113 * can be found in the array 'preemption_priorities'. This operation happens 127 * 'preemption_priorities'.
114 * every 10ms. 128 *
129 * @note This operation happens every 10ms.
115 */ 130 */
116 void PreemptThreads(); 131 void PreemptThreads();
117 132
@@ -130,15 +145,15 @@ public:
130 void Shutdown(); 145 void Shutdown();
131 146
132private: 147private:
133 bool AskForReselectionOrMarkRedundant(Thread* current_thread, Thread* winner); 148 bool AskForReselectionOrMarkRedundant(Thread* current_thread, const Thread* winner);
134 149
135 static constexpr u32 min_regular_priority = 2; 150 static constexpr u32 min_regular_priority = 2;
136 std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> scheduled_queue; 151 std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> scheduled_queue;
137 std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> suggested_queue; 152 std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> suggested_queue;
138 std::atomic<bool> is_reselection_pending; 153 std::atomic<bool> is_reselection_pending{false};
139 154
140 // `preemption_priorities` are the priority levels at which the global scheduler 155 // The priority levels at which the global scheduler preempts threads every 10 ms. They are
141 // preempts threads every 10 ms. They are ordered from Core 0 to Core 3 156 // ordered from Core 0 to Core 3.
142 std::array<u32, NUM_CPU_CORES> preemption_priorities = {59, 59, 59, 62}; 157 std::array<u32, NUM_CPU_CORES> preemption_priorities = {59, 59, 59, 62};
143 158
144 /// Lists all thread ids that aren't deleted/etc. 159 /// Lists all thread ids that aren't deleted/etc.
@@ -181,10 +196,8 @@ public:
181 196
182private: 197private:
183 friend class GlobalScheduler; 198 friend class GlobalScheduler;
184 /** 199
185 * Switches the CPU's active thread context to that of the specified thread 200 /// Switches the CPU's active thread context to that of the specified thread
186 * @param new_thread The thread to switch to
187 */
188 void SwitchContext(); 201 void SwitchContext();
189 202
190 /** 203 /**