summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/common/thread_queue_list.h18
-rw-r--r--src/core/hle/function_wrappers.h7
-rw-r--r--src/core/hle/kernel/thread.cpp48
-rw-r--r--src/core/hle/kernel/thread.h25
-rw-r--r--src/core/hle/svc.cpp22
5 files changed, 92 insertions, 28 deletions
diff --git a/src/common/thread_queue_list.h b/src/common/thread_queue_list.h
index 444abf115..4f27fc899 100644
--- a/src/common/thread_queue_list.h
+++ b/src/common/thread_queue_list.h
@@ -40,6 +40,18 @@ struct ThreadQueueList {
40 return -1; 40 return -1;
41 } 41 }
42 42
43 T get_first() {
44 Queue *cur = first;
45 while (cur != nullptr) {
46 if (!cur->data.empty()) {
47 return cur->data.front();
48 }
49 cur = cur->next_nonempty;
50 }
51
52 return T();
53 }
54
43 T pop_first() { 55 T pop_first() {
44 Queue *cur = first; 56 Queue *cur = first;
45 while (cur != nullptr) { 57 while (cur != nullptr) {
@@ -79,6 +91,12 @@ struct ThreadQueueList {
79 cur->data.push_back(thread_id); 91 cur->data.push_back(thread_id);
80 } 92 }
81 93
94 void move(const T& thread_id, Priority old_priority, Priority new_priority) {
95 remove(old_priority, thread_id);
96 prepare(new_priority);
97 push_back(new_priority, thread_id);
98 }
99
82 void remove(Priority priority, const T& thread_id) { 100 void remove(Priority priority, const T& thread_id) {
83 Queue *cur = &queues[priority]; 101 Queue *cur = &queues[priority];
84 boost::remove_erase(cur->data, thread_id); 102 boost::remove_erase(cur->data, thread_id);
diff --git a/src/core/hle/function_wrappers.h b/src/core/hle/function_wrappers.h
index 0b6b6f518..be2626eef 100644
--- a/src/core/hle/function_wrappers.h
+++ b/src/core/hle/function_wrappers.h
@@ -46,6 +46,13 @@ template<ResultCode func(u32*, u32, u32, u32, u32, u32)> void Wrap(){
46 FuncReturn(retval); 46 FuncReturn(retval);
47} 47}
48 48
49template<ResultCode func(u32*, s32, u32, u32, u32, s32)> void Wrap() {
50 u32 param_1 = 0;
51 u32 retval = func(&param_1, PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4)).raw;
52 Core::g_app_core->SetReg(1, param_1);
53 FuncReturn(retval);
54}
55
49template<ResultCode func(s32*, u32*, s32, bool, s64)> void Wrap() { 56template<ResultCode func(s32*, u32*, s32, bool, s64)> void Wrap() {
50 s32 param_1 = 0; 57 s32 param_1 = 0;
51 s32 retval = func(&param_1, (Handle*)Memory::GetPointer(PARAM(1)), (s32)PARAM(2), 58 s32 retval = func(&param_1, (Handle*)Memory::GetPointer(PARAM(1)), (s32)PARAM(2),
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index be1aed615..3a1e15ac6 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -140,6 +140,29 @@ void ArbitrateAllThreads(u32 address) {
140 } 140 }
141} 141}
142 142
143/// Boost low priority threads (temporarily) that have been starved
144static void PriorityBoostStarvedThreads() {
145 u64 current_ticks = CoreTiming::GetTicks();
146
147 for (auto& thread : thread_list) {
148 // TODO(bunnei): Threads that have been waiting to be scheduled for `boost_ticks` (or
149 // longer) will have their priority temporarily adjusted to 1 higher than the highest
150 // priority thread to prevent thread starvation. This general behavior has been verified
151 // on hardware. However, this is almost certainly not perfect, and the real CTR OS scheduler
152 // should probably be reversed to verify this.
153
154 const u64 boost_timeout = 2000000; // Boost threads that have been ready for > this long
155
156 u64 delta = current_ticks - thread->last_running_ticks;
157
158 if (thread->status == THREADSTATUS_READY && delta > boost_timeout && !thread->idle) {
159 const s32 boost_priority = std::max(ready_queue.get_first()->current_priority - 1, 0);
160 ready_queue.move(thread, thread->current_priority, boost_priority);
161 thread->current_priority = boost_priority;
162 }
163 }
164}
165
143/** 166/**
144 * Switches the CPU's active thread context to that of the specified thread 167 * Switches the CPU's active thread context to that of the specified thread
145 * @param new_thread The thread to switch to 168 * @param new_thread The thread to switch to
@@ -151,6 +174,7 @@ static void SwitchContext(Thread* new_thread) {
151 174
152 // Save context for previous thread 175 // Save context for previous thread
153 if (previous_thread) { 176 if (previous_thread) {
177 previous_thread->last_running_ticks = CoreTiming::GetTicks();
154 Core::g_app_core->SaveContext(previous_thread->context); 178 Core::g_app_core->SaveContext(previous_thread->context);
155 179
156 if (previous_thread->status == THREADSTATUS_RUNNING) { 180 if (previous_thread->status == THREADSTATUS_RUNNING) {
@@ -168,6 +192,9 @@ static void SwitchContext(Thread* new_thread) {
168 ready_queue.remove(new_thread->current_priority, new_thread); 192 ready_queue.remove(new_thread->current_priority, new_thread);
169 new_thread->status = THREADSTATUS_RUNNING; 193 new_thread->status = THREADSTATUS_RUNNING;
170 194
195 // Restores thread to its nominal priority if it has been temporarily changed
196 new_thread->current_priority = new_thread->nominal_priority;
197
171 Core::g_app_core->LoadContext(new_thread->context); 198 Core::g_app_core->LoadContext(new_thread->context);
172 } else { 199 } else {
173 current_thread = nullptr; 200 current_thread = nullptr;
@@ -364,7 +391,8 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
364 thread->status = THREADSTATUS_DORMANT; 391 thread->status = THREADSTATUS_DORMANT;
365 thread->entry_point = entry_point; 392 thread->entry_point = entry_point;
366 thread->stack_top = stack_top; 393 thread->stack_top = stack_top;
367 thread->initial_priority = thread->current_priority = priority; 394 thread->nominal_priority = thread->current_priority = priority;
395 thread->last_running_ticks = CoreTiming::GetTicks();
368 thread->processor_id = processor_id; 396 thread->processor_id = processor_id;
369 thread->wait_set_output = false; 397 thread->wait_set_output = false;
370 thread->wait_all = false; 398 thread->wait_all = false;
@@ -400,18 +428,11 @@ static void ClampPriority(const Thread* thread, s32* priority) {
400void Thread::SetPriority(s32 priority) { 428void Thread::SetPriority(s32 priority) {
401 ClampPriority(this, &priority); 429 ClampPriority(this, &priority);
402 430
403 if (current_priority == priority) { 431 // If thread was ready, adjust queues
404 return; 432 if (status == THREADSTATUS_READY)
405 } 433 ready_queue.move(this, current_priority, priority);
406 434
407 if (status == THREADSTATUS_READY) { 435 nominal_priority = current_priority = priority;
408 // If thread was ready, adjust queues
409 ready_queue.remove(current_priority, this);
410 ready_queue.prepare(priority);
411 ready_queue.push_back(priority, this);
412 }
413
414 current_priority = priority;
415} 436}
416 437
417SharedPtr<Thread> SetupIdleThread() { 438SharedPtr<Thread> SetupIdleThread() {
@@ -440,6 +461,9 @@ SharedPtr<Thread> SetupMainThread(u32 stack_size, u32 entry_point, s32 priority)
440 461
441void Reschedule() { 462void Reschedule() {
442 Thread* prev = GetCurrentThread(); 463 Thread* prev = GetCurrentThread();
464
465 PriorityBoostStarvedThreads();
466
443 Thread* next = PopNextReadyThread(); 467 Thread* next = PopNextReadyThread();
444 HLE::g_reschedule = false; 468 HLE::g_reschedule = false;
445 469
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index bde4eecf2..92f2c423b 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -17,16 +17,19 @@
17#include "core/hle/kernel/kernel.h" 17#include "core/hle/kernel/kernel.h"
18#include "core/hle/result.h" 18#include "core/hle/result.h"
19 19
20enum ThreadPriority { 20enum ThreadPriority : s32{
21 THREADPRIO_HIGHEST = 0x0, ///< Highest thread priority 21 THREADPRIO_HIGHEST = 0, ///< Highest thread priority
22 THREADPRIO_DEFAULT = 0x30, ///< Default thread priority for userland apps 22 THREADPRIO_USERLAND_MAX = 24, ///< Highest thread priority for userland apps
23 THREADPRIO_LOWEST = 0x3F, ///< Lowest thread priority 23 THREADPRIO_DEFAULT = 48, ///< Default thread priority for userland apps
24 THREADPRIO_LOWEST = 63, ///< Lowest thread priority
24}; 25};
25 26
26enum ThreadProcessorId { 27enum ThreadProcessorId : s32 {
27 THREADPROCESSORID_0 = 0xFFFFFFFE, ///< Enables core appcode 28 THREADPROCESSORID_DEFAULT = -2, ///< Run thread on default core specified by exheader
28 THREADPROCESSORID_1 = 0xFFFFFFFD, ///< Enables core syscore 29 THREADPROCESSORID_ALL = -1, ///< Run thread on either core
29 THREADPROCESSORID_ALL = 0xFFFFFFFC, ///< Enables both cores 30 THREADPROCESSORID_0 = 0, ///< Run thread on core 0 (AppCore)
31 THREADPROCESSORID_1 = 1, ///< Run thread on core 1 (SysCore)
32 THREADPROCESSORID_MAX = 2, ///< Processor ID must be less than this
30}; 33};
31 34
32enum ThreadStatus { 35enum ThreadStatus {
@@ -134,8 +137,10 @@ public:
134 u32 entry_point; 137 u32 entry_point;
135 u32 stack_top; 138 u32 stack_top;
136 139
137 s32 initial_priority; 140 s32 nominal_priority; ///< Nominal thread priority, as set by the emulated application
138 s32 current_priority; 141 s32 current_priority; ///< Current thread priority, can be temporarily changed
142
143 u64 last_running_ticks; ///< CPU tick when thread was last running
139 144
140 s32 processor_id; 145 s32 processor_id;
141 146
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index e89e97238..82e187466 100644
--- a/src/core/hle/svc.cpp
+++ b/src/core/hle/svc.cpp
@@ -312,7 +312,7 @@ static ResultCode GetResourceLimitCurrentValues(s64* values, Handle resource_lim
312} 312}
313 313
314/// Creates a new thread 314/// Creates a new thread
315static ResultCode CreateThread(u32* out_handle, u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 processor_id) { 315static ResultCode CreateThread(Handle* out_handle, s32 priority, u32 entry_point, u32 arg, u32 stack_top, s32 processor_id) {
316 using Kernel::Thread; 316 using Kernel::Thread;
317 317
318 std::string name; 318 std::string name;
@@ -323,6 +323,21 @@ static ResultCode CreateThread(u32* out_handle, u32 priority, u32 entry_point, u
323 name = Common::StringFromFormat("unknown-%08x", entry_point); 323 name = Common::StringFromFormat("unknown-%08x", entry_point);
324 } 324 }
325 325
326 // TODO(bunnei): Implement resource limits to return an error code instead of the below assert.
327 // The error code should be: Description::NotAuthorized, Module::OS, Summary::WrongArgument,
328 // Level::Permanent
329 ASSERT_MSG(priority >= THREADPRIO_USERLAND_MAX, "Unexpected thread priority!");
330
331 if (priority > THREADPRIO_LOWEST) {
332 return ResultCode(ErrorDescription::OutOfRange, ErrorModule::OS,
333 ErrorSummary::InvalidArgument, ErrorLevel::Usage);
334 }
335
336 if (processor_id > THREADPROCESSORID_MAX) {
337 return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Kernel,
338 ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
339 }
340
326 CASCADE_RESULT(SharedPtr<Thread> thread, Kernel::Thread::Create( 341 CASCADE_RESULT(SharedPtr<Thread> thread, Kernel::Thread::Create(
327 name, entry_point, priority, arg, processor_id, stack_top)); 342 name, entry_point, priority, arg, processor_id, stack_top));
328 CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(thread))); 343 CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(thread)));
@@ -331,11 +346,6 @@ static ResultCode CreateThread(u32* out_handle, u32 priority, u32 entry_point, u
331 "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point, 346 "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point,
332 name.c_str(), arg, stack_top, priority, processor_id, *out_handle); 347 name.c_str(), arg, stack_top, priority, processor_id, *out_handle);
333 348
334 if (THREADPROCESSORID_1 == processor_id) {
335 LOG_WARNING(Kernel_SVC,
336 "thread designated for system CPU core (UNIMPLEMENTED) will be run with app core scheduling");
337 }
338
339 HLE::Reschedule(__func__); 349 HLE::Reschedule(__func__);
340 350
341 return RESULT_SUCCESS; 351 return RESULT_SUCCESS;