2014-05-10 03:11:18 +01:00
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 08:49:13 +00:00
|
|
|
// Refer to the license.txt file included.
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-06-06 03:35:36 +01:00
|
|
|
#include <algorithm>
|
2014-08-18 04:03:22 +01:00
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
2014-05-10 03:11:18 +01:00
|
|
|
|
|
|
|
#include "common/common.h"
|
2014-05-15 23:27:08 +01:00
|
|
|
#include "common/thread_queue_list.h"
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-12-22 06:30:09 +00:00
|
|
|
#include "core/arm/arm_interface.h"
|
2014-05-14 03:00:11 +01:00
|
|
|
#include "core/core.h"
|
2015-01-07 15:10:58 +00:00
|
|
|
#include "core/core_timing.h"
|
2014-05-15 01:50:30 +01:00
|
|
|
#include "core/hle/hle.h"
|
2014-05-10 03:11:18 +01:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
2014-12-07 20:44:21 +00:00
|
|
|
#include "core/hle/kernel/mutex.h"
|
2014-10-23 04:20:01 +01:00
|
|
|
#include "core/hle/result.h"
|
|
|
|
#include "core/mem_map.h"
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
namespace Kernel {
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2015-01-20 23:16:45 +00:00
|
|
|
bool Thread::ShouldWait() {
|
|
|
|
return status != THREADSTATUS_DORMANT;
|
2014-12-22 06:32:03 +00:00
|
|
|
}
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2015-01-20 23:16:45 +00:00
|
|
|
void Thread::Acquire() {
|
|
|
|
_assert_msg_(Kernel, !ShouldWait(), "object unavailable!");
|
2015-01-18 03:23:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 03:00:11 +01:00
|
|
|
// Lists all thread ids that aren't deleted/etc.
|
2014-12-29 12:55:30 +00:00
|
|
|
static std::vector<SharedPtr<Thread>> thread_list;
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
// Lists only ready thread ids.
|
2014-12-22 13:07:22 +00:00
|
|
|
static Common::ThreadQueueList<Thread*, THREADPRIO_LOWEST+1> thread_ready_queue;
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-11-18 13:48:11 +00:00
|
|
|
static Thread* current_thread;
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-12-04 13:13:53 +00:00
|
|
|
static const u32 INITIAL_THREAD_ID = 1; ///< The first available thread id at startup
|
|
|
|
static u32 next_thread_id; ///< The next available thread id
|
|
|
|
|
2015-01-31 21:22:40 +00:00
|
|
|
Thread::Thread() {
|
|
|
|
}
|
|
|
|
|
2014-12-20 05:04:36 +00:00
|
|
|
Thread* GetCurrentThread() {
|
2014-11-18 13:48:11 +00:00
|
|
|
return current_thread;
|
2014-05-15 23:27:08 +01:00
|
|
|
}
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Resets a thread
|
2014-12-22 13:07:22 +00:00
|
|
|
static void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
|
2014-12-22 06:30:09 +00:00
|
|
|
memset(&t->context, 0, sizeof(Core::ThreadContext));
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-22 23:50:36 +01:00
|
|
|
t->context.cpu_registers[0] = arg;
|
2015-02-01 02:44:35 +00:00
|
|
|
t->context.pc = t->entry_point;
|
2014-05-15 23:27:08 +01:00
|
|
|
t->context.sp = t->stack_top;
|
2014-05-22 23:50:36 +01:00
|
|
|
t->context.cpsr = 0x1F; // Usermode
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-08-27 04:58:03 +01:00
|
|
|
// TODO(bunnei): This instructs the CPU core to start the execution as if it is "resuming" a
|
|
|
|
// thread. This is somewhat Sky-Eye specific, and should be re-architected in the future to be
|
|
|
|
// agnostic of the CPU core.
|
|
|
|
t->context.mode = 8;
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
if (t->current_priority < lowest_priority) {
|
|
|
|
t->current_priority = t->initial_priority;
|
2014-05-14 03:29:31 +01:00
|
|
|
}
|
2015-01-18 18:25:51 +00:00
|
|
|
|
2015-01-15 04:41:33 +00:00
|
|
|
t->wait_objects.clear();
|
2014-12-03 05:46:34 +00:00
|
|
|
t->wait_address = 0;
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Change a thread to "ready" state
|
2014-12-22 13:07:22 +00:00
|
|
|
static void ChangeReadyState(Thread* t, bool ready) {
|
2014-05-15 23:27:08 +01:00
|
|
|
if (t->IsReady()) {
|
|
|
|
if (!ready) {
|
2014-12-22 13:07:22 +00:00
|
|
|
thread_ready_queue.remove(t->current_priority, t);
|
2014-05-15 23:27:08 +01:00
|
|
|
}
|
|
|
|
} else if (ready) {
|
|
|
|
if (t->IsRunning()) {
|
2014-12-22 13:07:22 +00:00
|
|
|
thread_ready_queue.push_front(t->current_priority, t);
|
2014-05-14 03:00:11 +01:00
|
|
|
} else {
|
2014-12-22 13:07:22 +00:00
|
|
|
thread_ready_queue.push_back(t->current_priority, t);
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
2014-05-15 23:27:08 +01:00
|
|
|
t->status = THREADSTATUS_READY;
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 18:56:40 +00:00
|
|
|
/// Check if a thread is waiting on a the specified wait object
|
|
|
|
static bool CheckWait_WaitObject(const Thread* thread, WaitObject* wait_object) {
|
2015-01-20 23:40:01 +00:00
|
|
|
auto itr = std::find(thread->wait_objects.begin(), thread->wait_objects.end(), wait_object);
|
|
|
|
|
|
|
|
if (itr != thread->wait_objects.end())
|
|
|
|
return thread->IsWaiting();
|
|
|
|
|
2015-01-17 07:03:44 +00:00
|
|
|
return false;
|
2014-12-20 07:32:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 18:56:40 +00:00
|
|
|
/// Check if the specified thread is waiting on the specified address to be arbitrated
|
|
|
|
static bool CheckWait_AddressArbiter(const Thread* thread, VAddr wait_address) {
|
|
|
|
return thread->IsWaiting() && thread->wait_objects.empty() && wait_address == thread->wait_address;
|
2014-12-03 05:46:34 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 03:35:36 +01:00
|
|
|
/// Stops the current thread
|
2014-12-22 13:07:22 +00:00
|
|
|
void Thread::Stop(const char* reason) {
|
2014-12-07 20:44:21 +00:00
|
|
|
// Release all the mutexes that this thread holds
|
2015-01-20 23:33:23 +00:00
|
|
|
ReleaseThreadMutexes(this);
|
2014-12-20 07:32:19 +00:00
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
ChangeReadyState(this, false);
|
|
|
|
status = THREADSTATUS_DORMANT;
|
2015-01-20 23:20:47 +00:00
|
|
|
WakeupAllWaitingThreads();
|
2014-06-10 03:14:03 +01:00
|
|
|
|
|
|
|
// Stopped threads are never waiting.
|
2015-01-15 04:41:33 +00:00
|
|
|
wait_objects.clear();
|
2014-12-22 13:07:22 +00:00
|
|
|
wait_address = 0;
|
2014-06-06 03:35:36 +01:00
|
|
|
}
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Changes a threads state
|
2014-12-22 13:07:22 +00:00
|
|
|
static void ChangeThreadState(Thread* t, ThreadStatus new_status) {
|
2014-05-15 23:27:08 +01:00
|
|
|
if (!t || t->status == new_status) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-23 00:06:12 +01:00
|
|
|
ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
|
2014-05-15 23:27:08 +01:00
|
|
|
t->status = new_status;
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 03:48:19 +01:00
|
|
|
/// Arbitrate the highest priority thread that is waiting
|
2015-01-18 18:56:40 +00:00
|
|
|
Thread* ArbitrateHighestPriorityThread(u32 address) {
|
2014-12-22 13:07:22 +00:00
|
|
|
Thread* highest_priority_thread = nullptr;
|
2014-07-07 03:48:19 +01:00
|
|
|
s32 priority = THREADPRIO_LOWEST;
|
|
|
|
|
|
|
|
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
2014-12-29 12:55:30 +00:00
|
|
|
for (auto& thread : thread_list) {
|
2015-01-18 18:56:40 +00:00
|
|
|
if (!CheckWait_AddressArbiter(thread.get(), address))
|
2014-07-07 03:48:19 +01:00
|
|
|
continue;
|
|
|
|
|
2014-10-23 04:20:01 +01:00
|
|
|
if (thread == nullptr)
|
2015-01-18 18:56:40 +00:00
|
|
|
continue;
|
2014-12-03 05:46:34 +00:00
|
|
|
|
2014-07-07 03:48:19 +01:00
|
|
|
if(thread->current_priority <= priority) {
|
2014-12-29 12:55:30 +00:00
|
|
|
highest_priority_thread = thread.get();
|
2014-07-07 03:48:19 +01:00
|
|
|
priority = thread->current_priority;
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 13:07:22 +00:00
|
|
|
|
2014-07-07 03:48:19 +01:00
|
|
|
// If a thread was arbitrated, resume it
|
2014-12-22 13:07:22 +00:00
|
|
|
if (nullptr != highest_priority_thread) {
|
2015-01-18 18:56:40 +00:00
|
|
|
highest_priority_thread->ResumeFromWait();
|
2014-12-22 13:07:22 +00:00
|
|
|
}
|
2014-07-07 03:48:19 +01:00
|
|
|
|
|
|
|
return highest_priority_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Arbitrate all threads currently waiting
|
2015-01-18 18:56:40 +00:00
|
|
|
void ArbitrateAllThreads(u32 address) {
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-07-07 03:48:19 +01:00
|
|
|
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
2014-12-29 12:55:30 +00:00
|
|
|
for (auto& thread : thread_list) {
|
2015-01-18 18:56:40 +00:00
|
|
|
if (CheckWait_AddressArbiter(thread.get(), address))
|
|
|
|
thread->ResumeFromWait();
|
2014-07-07 03:48:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 05:56:00 +01:00
|
|
|
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
|
2014-12-22 13:07:22 +00:00
|
|
|
static void CallThread(Thread* t) {
|
2014-05-17 05:56:00 +01:00
|
|
|
// Stop waiting
|
2014-05-23 00:06:12 +01:00
|
|
|
ChangeThreadState(t, THREADSTATUS_READY);
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Switches CPU context to that of the specified thread
|
2014-12-22 13:07:22 +00:00
|
|
|
static void SwitchContext(Thread* t) {
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
// Save context for current thread
|
|
|
|
if (cur) {
|
2014-12-22 13:07:22 +00:00
|
|
|
Core::g_app_core->SaveContext(cur->context);
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
if (cur->IsRunning()) {
|
2014-05-23 00:06:12 +01:00
|
|
|
ChangeReadyState(cur, true);
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load context of new thread
|
|
|
|
if (t) {
|
2014-12-22 13:07:22 +00:00
|
|
|
current_thread = t;
|
2014-05-23 00:06:12 +01:00
|
|
|
ChangeReadyState(t, false);
|
2014-05-21 00:37:46 +01:00
|
|
|
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
2014-12-22 13:07:22 +00:00
|
|
|
Core::g_app_core->LoadContext(t->context);
|
2014-05-21 00:37:46 +01:00
|
|
|
} else {
|
2014-12-22 13:07:22 +00:00
|
|
|
current_thread = nullptr;
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the next thread that is ready to be run by priority
|
2014-12-22 13:07:22 +00:00
|
|
|
static Thread* NextThread() {
|
|
|
|
Thread* next;
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
if (cur && cur->IsRunning()) {
|
2014-11-18 13:48:11 +00:00
|
|
|
next = thread_ready_queue.pop_first_better(cur->current_priority);
|
2014-05-21 00:37:46 +01:00
|
|
|
} else {
|
2014-11-18 13:48:11 +00:00
|
|
|
next = thread_ready_queue.pop_first();
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
2014-05-22 02:42:18 +01:00
|
|
|
if (next == 0) {
|
2014-06-06 05:35:49 +01:00
|
|
|
return nullptr;
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
2014-12-22 13:07:22 +00:00
|
|
|
return next;
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-18 18:56:40 +00:00
|
|
|
void WaitCurrentThread_Sleep() {
|
2015-01-17 07:03:44 +00:00
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
|
|
|
|
}
|
|
|
|
|
2015-01-21 01:53:52 +00:00
|
|
|
void WaitCurrentThread_WaitSynchronization(SharedPtr<WaitObject> wait_object, bool wait_set_output, bool wait_all) {
|
2014-06-10 03:14:03 +01:00
|
|
|
Thread* thread = GetCurrentThread();
|
2015-01-21 01:53:52 +00:00
|
|
|
thread->wait_set_output = wait_set_output;
|
2015-01-18 18:25:51 +00:00
|
|
|
thread->wait_all = wait_all;
|
2015-01-21 01:53:52 +00:00
|
|
|
|
|
|
|
// It's possible to call WaitSynchronizationN without any objects passed in...
|
|
|
|
if (wait_object != nullptr)
|
|
|
|
thread->wait_objects.push_back(wait_object);
|
|
|
|
|
2014-06-10 03:14:03 +01:00
|
|
|
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
|
2014-05-21 02:00:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-18 18:56:40 +00:00
|
|
|
void WaitCurrentThread_ArbitrateAddress(VAddr wait_address) {
|
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
thread->wait_address = wait_address;
|
|
|
|
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
|
2014-12-03 05:46:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 21:40:08 +00:00
|
|
|
/// Event type for the thread wake up event
|
|
|
|
static int ThreadWakeupEventType = -1;
|
2015-01-31 16:23:09 +00:00
|
|
|
// TODO(yuriks): This can be removed if Thread objects are explicitly pooled in the future, allowing
|
|
|
|
// us to simply use a pool index or similar.
|
|
|
|
static Kernel::HandleTable wakeup_callback_handle_table;
|
2015-01-07 21:40:08 +00:00
|
|
|
|
|
|
|
/// Callback that will wake up the thread it was scheduled for
|
2015-01-31 16:23:09 +00:00
|
|
|
static void ThreadWakeupCallback(u64 thread_handle, int cycles_late) {
|
|
|
|
SharedPtr<Thread> thread = wakeup_callback_handle_table.Get<Thread>((Handle)thread_handle);
|
2015-01-07 21:40:08 +00:00
|
|
|
if (thread == nullptr) {
|
2015-01-31 16:23:09 +00:00
|
|
|
LOG_CRITICAL(Kernel, "Callback fired for invalid thread %08X", thread_handle);
|
2015-01-07 21:40:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-21 01:53:52 +00:00
|
|
|
thread->SetWaitSynchronizationResult(ResultCode(ErrorDescription::Timeout, ErrorModule::OS,
|
|
|
|
ErrorSummary::StatusChanged, ErrorLevel::Info));
|
|
|
|
|
|
|
|
if (thread->wait_set_output)
|
|
|
|
thread->SetWaitSynchronizationOutput(-1);
|
2015-01-17 07:03:44 +00:00
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
thread->ResumeFromWait();
|
2015-01-07 21:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-31 01:07:54 +00:00
|
|
|
void Thread::WakeAfterDelay(s64 nanoseconds) {
|
2015-01-07 21:40:08 +00:00
|
|
|
// Don't schedule a wakeup if the thread wants to wait forever
|
|
|
|
if (nanoseconds == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
u64 microseconds = nanoseconds / 1000;
|
2015-01-31 16:23:09 +00:00
|
|
|
CoreTiming::ScheduleEvent(usToCycles(microseconds), ThreadWakeupEventType, callback_handle);
|
2015-01-07 21:40:08 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 18:56:40 +00:00
|
|
|
void Thread::ReleaseWaitObject(WaitObject* wait_object) {
|
2015-01-17 07:03:44 +00:00
|
|
|
if (wait_objects.empty()) {
|
|
|
|
LOG_CRITICAL(Kernel, "thread is not waiting on any objects!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-18 06:27:46 +00:00
|
|
|
// Remove this thread from the waiting object's thread list
|
2015-01-17 07:03:44 +00:00
|
|
|
wait_object->RemoveWaitingThread(this);
|
|
|
|
|
2015-01-18 06:27:46 +00:00
|
|
|
unsigned index = 0;
|
|
|
|
bool wait_all_failed = false; // Will be set to true if any object is unavailable
|
2015-01-17 07:03:44 +00:00
|
|
|
|
2015-01-18 06:27:46 +00:00
|
|
|
// Iterate through all waiting objects to check availability...
|
|
|
|
for (auto itr = wait_objects.begin(); itr != wait_objects.end(); ++itr) {
|
2015-01-20 23:16:45 +00:00
|
|
|
if ((*itr)->ShouldWait())
|
2015-01-18 06:27:46 +00:00
|
|
|
wait_all_failed = true;
|
|
|
|
|
|
|
|
// The output should be the last index of wait_object
|
|
|
|
if (*itr == wait_object)
|
|
|
|
index = itr - wait_objects.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are waiting on all objects...
|
|
|
|
if (wait_all) {
|
|
|
|
// Resume the thread only if all are available...
|
|
|
|
if (!wait_all_failed) {
|
2015-01-21 01:53:52 +00:00
|
|
|
SetWaitSynchronizationResult(RESULT_SUCCESS);
|
|
|
|
SetWaitSynchronizationOutput(-1);
|
|
|
|
|
2015-01-17 07:03:44 +00:00
|
|
|
ResumeFromWait();
|
|
|
|
}
|
2015-01-18 06:27:46 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, resume
|
2015-01-21 01:53:52 +00:00
|
|
|
SetWaitSynchronizationResult(RESULT_SUCCESS);
|
|
|
|
|
|
|
|
if (wait_set_output)
|
|
|
|
SetWaitSynchronizationOutput(index);
|
|
|
|
|
2015-01-18 06:27:46 +00:00
|
|
|
ResumeFromWait();
|
2015-01-17 07:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
void Thread::ResumeFromWait() {
|
2015-01-11 18:18:52 +00:00
|
|
|
// Cancel any outstanding wakeup events
|
2015-01-31 16:23:09 +00:00
|
|
|
CoreTiming::UnscheduleEvent(ThreadWakeupEventType, callback_handle);
|
2015-01-11 18:18:52 +00:00
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
status &= ~THREADSTATUS_WAIT;
|
2015-01-15 04:41:33 +00:00
|
|
|
|
|
|
|
// Remove this thread from all other WaitObjects
|
|
|
|
for (auto wait_object : wait_objects)
|
2015-01-18 06:27:46 +00:00
|
|
|
wait_object->RemoveWaitingThread(this);
|
2015-01-15 04:41:33 +00:00
|
|
|
|
|
|
|
wait_objects.clear();
|
2015-01-21 01:53:52 +00:00
|
|
|
wait_set_output = false;
|
2015-01-17 07:03:44 +00:00
|
|
|
wait_all = false;
|
2015-01-18 18:56:40 +00:00
|
|
|
wait_address = 0;
|
2015-01-18 18:25:51 +00:00
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
|
|
|
|
ChangeReadyState(this, true);
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:35:36 +01:00
|
|
|
/// Prints the thread queue for debugging purposes
|
2014-12-22 13:07:22 +00:00
|
|
|
static void DebugThreadQueue() {
|
2014-06-06 03:35:36 +01:00
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
if (!thread) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-31 16:55:40 +00:00
|
|
|
LOG_DEBUG(Kernel, "0x%02X %u (current)", thread->current_priority, GetCurrentThread()->GetObjectId());
|
2014-12-29 12:55:30 +00:00
|
|
|
for (auto& t : thread_list) {
|
|
|
|
s32 priority = thread_ready_queue.contains(t.get());
|
2014-06-06 03:35:36 +01:00
|
|
|
if (priority != -1) {
|
2015-01-31 16:55:40 +00:00
|
|
|
LOG_DEBUG(Kernel, "0x%02X %u", priority, t->GetObjectId());
|
2014-06-06 03:35:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 12:55:30 +00:00
|
|
|
ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, s32 priority,
|
|
|
|
u32 arg, s32 processor_id, VAddr stack_top, u32 stack_size) {
|
|
|
|
if (stack_size < 0x200) {
|
|
|
|
LOG_ERROR(Kernel, "(name=%s): invalid stack_size=0x%08X", name.c_str(), stack_size);
|
2014-12-22 13:07:22 +00:00
|
|
|
// TODO: Verify error
|
|
|
|
return ResultCode(ErrorDescription::InvalidSize, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
|
|
|
|
s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
|
|
|
|
LOG_WARNING(Kernel_SVC, "(name=%s): invalid priority=%d, clamping to %d",
|
2014-12-29 12:55:30 +00:00
|
|
|
name.c_str(), priority, new_priority);
|
2014-12-22 13:07:22 +00:00
|
|
|
// TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
|
|
|
|
// validity of this
|
|
|
|
priority = new_priority;
|
|
|
|
}
|
2014-05-17 05:56:00 +01:00
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
if (!Memory::GetPointer(entry_point)) {
|
2014-12-29 12:55:30 +00:00
|
|
|
LOG_ERROR(Kernel_SVC, "(name=%s): invalid entry %08x", name.c_str(), entry_point);
|
2014-12-22 13:07:22 +00:00
|
|
|
// TODO: Verify error
|
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
|
|
|
}
|
2014-05-21 02:02:35 +01:00
|
|
|
|
2014-12-29 12:55:30 +00:00
|
|
|
SharedPtr<Thread> thread(new Thread);
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
// TODO(yuriks): Thread requires a handle to be inserted into the various scheduling queues for
|
|
|
|
// the time being. Create a handle here, it will be copied to the handle field in
|
|
|
|
// the object and use by the rest of the code. This should be removed when other
|
|
|
|
// code doesn't rely on the handle anymore.
|
|
|
|
ResultVal<Handle> handle = Kernel::g_handle_table.Create(thread);
|
|
|
|
if (handle.Failed())
|
|
|
|
return handle.Code();
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-12-29 13:32:05 +00:00
|
|
|
thread_list.push_back(thread);
|
2014-11-18 13:48:11 +00:00
|
|
|
thread_ready_queue.prepare(priority);
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-12-04 19:59:56 +00:00
|
|
|
thread->thread_id = next_thread_id++;
|
2014-06-10 03:14:03 +01:00
|
|
|
thread->status = THREADSTATUS_DORMANT;
|
|
|
|
thread->entry_point = entry_point;
|
|
|
|
thread->stack_top = stack_top;
|
|
|
|
thread->stack_size = stack_size;
|
|
|
|
thread->initial_priority = thread->current_priority = priority;
|
|
|
|
thread->processor_id = processor_id;
|
2015-01-21 01:53:52 +00:00
|
|
|
thread->wait_set_output = false;
|
2015-01-17 07:03:44 +00:00
|
|
|
thread->wait_all = false;
|
2015-01-15 04:41:33 +00:00
|
|
|
thread->wait_objects.clear();
|
2014-12-03 05:46:34 +00:00
|
|
|
thread->wait_address = 0;
|
2014-12-29 12:55:30 +00:00
|
|
|
thread->name = std::move(name);
|
2015-01-31 16:23:09 +00:00
|
|
|
thread->callback_handle = wakeup_callback_handle_table.Create(thread).MoveFrom();
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-12-29 12:55:30 +00:00
|
|
|
ResetThread(thread.get(), arg, 0);
|
|
|
|
CallThread(thread.get());
|
2014-06-02 02:42:50 +01:00
|
|
|
|
2014-12-29 12:55:30 +00:00
|
|
|
return MakeResult<SharedPtr<Thread>>(std::move(thread));
|
2014-06-02 03:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the priority of the thread specified by handle
|
2014-12-22 13:07:22 +00:00
|
|
|
void Thread::SetPriority(s32 priority) {
|
2014-06-02 03:12:54 +01:00
|
|
|
// If priority is invalid, clamp to valid range
|
|
|
|
if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
|
|
|
|
s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
|
2014-12-06 01:53:49 +00:00
|
|
|
LOG_WARNING(Kernel_SVC, "invalid priority=%d, clamping to %d", priority, new_priority);
|
2014-06-02 03:12:54 +01:00
|
|
|
// TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
|
|
|
|
// validity of this
|
|
|
|
priority = new_priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change thread priority
|
2014-12-22 13:07:22 +00:00
|
|
|
s32 old = current_priority;
|
|
|
|
thread_ready_queue.remove(old, this);
|
|
|
|
current_priority = priority;
|
|
|
|
thread_ready_queue.prepare(current_priority);
|
2014-06-02 03:12:54 +01:00
|
|
|
|
|
|
|
// Change thread status to "ready" and push to ready queue
|
2014-12-22 13:07:22 +00:00
|
|
|
if (IsRunning()) {
|
|
|
|
status = (status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY;
|
2014-06-02 03:12:54 +01:00
|
|
|
}
|
2014-12-22 13:07:22 +00:00
|
|
|
if (IsReady()) {
|
|
|
|
thread_ready_queue.push_back(current_priority, this);
|
2014-06-02 03:12:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 01:07:54 +00:00
|
|
|
SharedPtr<Thread> SetupIdleThread() {
|
2014-12-22 13:07:22 +00:00
|
|
|
// We need to pass a few valid values to get around parameter checking in Thread::Create.
|
2015-01-31 16:23:09 +00:00
|
|
|
auto thread = Thread::Create("idle", Memory::KERNEL_MEMORY_VADDR, THREADPRIO_LOWEST, 0,
|
|
|
|
THREADPROCESSORID_0, 0, Kernel::DEFAULT_STACK_SIZE).MoveFrom();
|
2014-12-22 13:07:22 +00:00
|
|
|
|
2015-01-07 15:10:58 +00:00
|
|
|
thread->idle = true;
|
2014-12-29 12:55:30 +00:00
|
|
|
CallThread(thread.get());
|
2015-01-31 01:07:54 +00:00
|
|
|
return thread;
|
2015-01-07 15:10:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 12:55:30 +00:00
|
|
|
SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size) {
|
2014-05-15 23:27:08 +01:00
|
|
|
// Initialize new "main" thread
|
2014-12-29 12:55:30 +00:00
|
|
|
auto thread_res = Thread::Create("main", Core::g_app_core->GetPC(), priority, 0,
|
|
|
|
THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
|
2014-12-22 13:07:22 +00:00
|
|
|
// TODO(yuriks): Propagate error
|
|
|
|
_dbg_assert_(Kernel, thread_res.Succeeded());
|
2014-12-29 12:55:30 +00:00
|
|
|
SharedPtr<Thread> thread = std::move(*thread_res);
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
// If running another thread already, set it to "ready" state
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-05-15 23:27:08 +01:00
|
|
|
if (cur && cur->IsRunning()) {
|
2014-05-23 00:06:12 +01:00
|
|
|
ChangeReadyState(cur, true);
|
2014-05-15 01:50:30 +01:00
|
|
|
}
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
// Run new "main" thread
|
2014-12-29 12:55:30 +00:00
|
|
|
current_thread = thread.get();
|
2014-06-10 03:14:03 +01:00
|
|
|
thread->status = THREADSTATUS_RUNNING;
|
2014-12-22 13:07:22 +00:00
|
|
|
Core::g_app_core->LoadContext(thread->context);
|
2014-05-15 01:50:30 +01:00
|
|
|
|
2014-12-22 13:07:22 +00:00
|
|
|
return thread;
|
2014-05-15 23:27:08 +01:00
|
|
|
}
|
2014-05-15 01:50:30 +01:00
|
|
|
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Reschedules to the next available thread (call after current thread is suspended)
|
2014-05-23 00:32:45 +01:00
|
|
|
void Reschedule() {
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* prev = GetCurrentThread();
|
|
|
|
Thread* next = NextThread();
|
2014-06-06 03:35:36 +01:00
|
|
|
HLE::g_reschedule = false;
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-12-20 07:32:19 +00:00
|
|
|
if (next != nullptr) {
|
2015-01-31 16:55:40 +00:00
|
|
|
LOG_TRACE(Kernel, "context switch %u -> %u", prev->GetObjectId(), next->GetObjectId());
|
2014-05-23 00:32:45 +01:00
|
|
|
SwitchContext(next);
|
2014-12-20 07:32:19 +00:00
|
|
|
} else {
|
2015-01-31 16:55:40 +00:00
|
|
|
LOG_TRACE(Kernel, "cannot context switch from %u, no higher priority thread!", prev->GetObjectId());
|
2014-05-15 23:27:08 +01:00
|
|
|
|
2014-12-29 12:55:30 +00:00
|
|
|
for (auto& thread : thread_list) {
|
2015-01-31 16:55:40 +00:00
|
|
|
LOG_TRACE(Kernel, "\tid=%u prio=0x%02X, status=0x%08X", thread->GetObjectId(),
|
2015-01-18 18:25:51 +00:00
|
|
|
thread->current_priority, thread->status);
|
2014-06-06 03:35:36 +01:00
|
|
|
}
|
2014-05-22 23:50:36 +01:00
|
|
|
}
|
2014-05-17 05:56:00 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 01:53:52 +00:00
|
|
|
void Thread::SetWaitSynchronizationResult(ResultCode result) {
|
|
|
|
context.cpu_registers[0] = result.raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::SetWaitSynchronizationOutput(s32 output) {
|
|
|
|
context.cpu_registers[1] = output;
|
2015-01-17 07:03:44 +00:00
|
|
|
}
|
|
|
|
|
2014-05-17 05:56:00 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-05-15 23:27:08 +01:00
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
void ThreadingInit() {
|
2014-12-04 13:13:53 +00:00
|
|
|
next_thread_id = INITIAL_THREAD_ID;
|
2015-01-07 21:40:08 +00:00
|
|
|
ThreadWakeupEventType = CoreTiming::RegisterEvent("ThreadWakeupCallback", ThreadWakeupCallback);
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
void ThreadingShutdown() {
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
2014-05-21 00:37:46 +01:00
|
|
|
|
|
|
|
} // namespace
|