2014-05-10 07:41:18 +05:30
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
2014-12-17 11:08:14 +05:30
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 14:19:13 +05:30
|
|
|
// Refer to the license.txt file included.
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-06-06 08:05:36 +05:30
|
|
|
#include <algorithm>
|
2014-08-18 08:33:22 +05:30
|
|
|
#include <list>
|
2014-05-10 07:41:18 +05:30
|
|
|
#include <map>
|
2014-08-18 08:33:22 +05:30
|
|
|
#include <vector>
|
2014-05-10 07:41:18 +05:30
|
|
|
|
|
|
|
#include "common/common.h"
|
2014-05-16 03:57:08 +05:30
|
|
|
#include "common/thread_queue_list.h"
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-12-22 12:00:09 +05:30
|
|
|
#include "core/arm/arm_interface.h"
|
2014-05-14 07:30:11 +05:30
|
|
|
#include "core/core.h"
|
2015-01-07 20:40:58 +05:30
|
|
|
#include "core/core_timing.h"
|
2014-05-15 06:20:30 +05:30
|
|
|
#include "core/hle/hle.h"
|
2014-05-10 07:41:18 +05:30
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
2014-12-08 02:14:21 +05:30
|
|
|
#include "core/hle/kernel/mutex.h"
|
2014-10-23 08:50:01 +05:30
|
|
|
#include "core/hle/result.h"
|
|
|
|
#include "core/mem_map.h"
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-21 05:07:46 +05:30
|
|
|
namespace Kernel {
|
2014-05-14 07:30:11 +05:30
|
|
|
|
2014-12-22 12:02:03 +05:30
|
|
|
ResultVal<bool> Thread::WaitSynchronization() {
|
|
|
|
const bool wait = status != THREADSTATUS_DORMANT;
|
|
|
|
if (wait) {
|
2015-01-15 05:52:50 +05:30
|
|
|
AddWaitingThread(GetCurrentThread());
|
2014-12-22 18:37:22 +05:30
|
|
|
WaitCurrentThread(WAITTYPE_THREADEND, this);
|
2014-05-27 06:31:27 +05:30
|
|
|
}
|
|
|
|
|
2014-12-22 12:02:03 +05:30
|
|
|
return MakeResult<bool>(wait);
|
|
|
|
}
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-14 07:30:11 +05:30
|
|
|
// Lists all thread ids that aren't deleted/etc.
|
2014-12-29 18:25:30 +05:30
|
|
|
static std::vector<SharedPtr<Thread>> thread_list;
|
2014-05-14 07:30:11 +05:30
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
// Lists only ready thread ids.
|
2014-12-22 18:37:22 +05:30
|
|
|
static Common::ThreadQueueList<Thread*, THREADPRIO_LOWEST+1> thread_ready_queue;
|
2014-05-14 07:30:11 +05:30
|
|
|
|
2014-11-18 19:18:11 +05:30
|
|
|
static Thread* current_thread;
|
2014-05-14 07:30:11 +05:30
|
|
|
|
2014-12-04 18:43:53 +05:30
|
|
|
static const u32 INITIAL_THREAD_ID = 1; ///< The first available thread id at startup
|
|
|
|
static u32 next_thread_id; ///< The next available thread id
|
|
|
|
|
2014-12-20 10:34:36 +05:30
|
|
|
Thread* GetCurrentThread() {
|
2014-11-18 19:18:11 +05:30
|
|
|
return current_thread;
|
2014-05-16 03:57:08 +05:30
|
|
|
}
|
2014-05-14 07:30:11 +05:30
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
/// Resets a thread
|
2014-12-22 18:37:22 +05:30
|
|
|
static void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
|
2014-12-22 12:00:09 +05:30
|
|
|
memset(&t->context, 0, sizeof(Core::ThreadContext));
|
2014-05-14 07:30:11 +05:30
|
|
|
|
2014-05-23 04:20:36 +05:30
|
|
|
t->context.cpu_registers[0] = arg;
|
2014-06-06 08:49:55 +05:30
|
|
|
t->context.pc = t->context.reg_15 = t->entry_point;
|
2014-05-16 03:57:08 +05:30
|
|
|
t->context.sp = t->stack_top;
|
2014-05-23 04:20:36 +05:30
|
|
|
t->context.cpsr = 0x1F; // Usermode
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-08-27 09:28:03 +05:30
|
|
|
// 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-16 03:57:08 +05:30
|
|
|
if (t->current_priority < lowest_priority) {
|
|
|
|
t->current_priority = t->initial_priority;
|
2014-05-14 07:59:31 +05:30
|
|
|
}
|
2014-05-16 03:57:08 +05:30
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2015-01-15 10:11:33 +05:30
|
|
|
t->wait_objects.clear();
|
2014-12-03 11:16:34 +05:30
|
|
|
t->wait_address = 0;
|
2014-05-14 07:30:11 +05:30
|
|
|
}
|
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
/// Change a thread to "ready" state
|
2014-12-22 18:37:22 +05:30
|
|
|
static void ChangeReadyState(Thread* t, bool ready) {
|
2014-05-16 03:57:08 +05:30
|
|
|
if (t->IsReady()) {
|
|
|
|
if (!ready) {
|
2014-12-22 18:37:22 +05:30
|
|
|
thread_ready_queue.remove(t->current_priority, t);
|
2014-05-16 03:57:08 +05:30
|
|
|
}
|
|
|
|
} else if (ready) {
|
|
|
|
if (t->IsRunning()) {
|
2014-12-22 18:37:22 +05:30
|
|
|
thread_ready_queue.push_front(t->current_priority, t);
|
2014-05-14 07:30:11 +05:30
|
|
|
} else {
|
2014-12-22 18:37:22 +05:30
|
|
|
thread_ready_queue.push_back(t->current_priority, t);
|
2014-05-14 07:30:11 +05:30
|
|
|
}
|
2014-05-16 03:57:08 +05:30
|
|
|
t->status = THREADSTATUS_READY;
|
2014-05-14 07:30:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 13:02:19 +05:30
|
|
|
/// Check if a thread is blocking on a specified wait type
|
|
|
|
static bool CheckWaitType(const Thread* thread, WaitType type) {
|
|
|
|
return (type == thread->wait_type) && (thread->IsWaiting());
|
2014-06-06 08:05:36 +05:30
|
|
|
}
|
|
|
|
|
2014-12-20 13:02:19 +05:30
|
|
|
/// Check if a thread is blocking on a specified wait type with a specified handle
|
2014-12-22 18:37:22 +05:30
|
|
|
static bool CheckWaitType(const Thread* thread, WaitType type, Object* wait_object) {
|
2015-01-15 10:11:33 +05:30
|
|
|
auto itr = std::find(thread->wait_objects.begin(), thread->wait_objects.end(), wait_object);
|
|
|
|
if (itr == thread->wait_objects.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return CheckWaitType(thread, type);
|
2014-12-20 13:02:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a thread is blocking on a specified wait type with a specified handle and address
|
2014-12-22 18:37:22 +05:30
|
|
|
static bool CheckWaitType(const Thread* thread, WaitType type, Object* wait_object, VAddr wait_address) {
|
|
|
|
return CheckWaitType(thread, type, wait_object) && (wait_address == thread->wait_address);
|
2014-12-03 11:16:34 +05:30
|
|
|
}
|
|
|
|
|
2014-06-06 08:05:36 +05:30
|
|
|
/// Stops the current thread
|
2014-12-22 18:37:22 +05:30
|
|
|
void Thread::Stop(const char* reason) {
|
2014-12-08 02:14:21 +05:30
|
|
|
// Release all the mutexes that this thread holds
|
2014-12-22 18:37:22 +05:30
|
|
|
ReleaseThreadMutexes(GetHandle());
|
2014-12-20 13:02:19 +05:30
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
ChangeReadyState(this, false);
|
|
|
|
status = THREADSTATUS_DORMANT;
|
2015-01-15 05:52:50 +05:30
|
|
|
ResumeAllWaitingThreads();
|
2014-06-10 07:44:03 +05:30
|
|
|
|
|
|
|
// Stopped threads are never waiting.
|
2014-12-22 18:37:22 +05:30
|
|
|
wait_type = WAITTYPE_NONE;
|
2015-01-15 10:11:33 +05:30
|
|
|
wait_objects.clear();
|
2014-12-22 18:37:22 +05:30
|
|
|
wait_address = 0;
|
2014-06-06 08:05:36 +05:30
|
|
|
}
|
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
/// Changes a threads state
|
2014-12-22 18:37:22 +05:30
|
|
|
static void ChangeThreadState(Thread* t, ThreadStatus new_status) {
|
2014-05-16 03:57:08 +05:30
|
|
|
if (!t || t->status == new_status) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-23 04:36:12 +05:30
|
|
|
ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
|
2014-05-16 03:57:08 +05:30
|
|
|
t->status = new_status;
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
if (new_status == THREADSTATUS_WAIT) {
|
|
|
|
if (t->wait_type == WAITTYPE_NONE) {
|
2014-12-06 07:23:49 +05:30
|
|
|
LOG_ERROR(Kernel, "Waittype none not allowed");
|
2014-05-16 03:57:08 +05:30
|
|
|
}
|
2014-05-14 07:30:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-07 08:18:19 +05:30
|
|
|
/// Arbitrate the highest priority thread that is waiting
|
2014-12-22 18:37:22 +05:30
|
|
|
Thread* ArbitrateHighestPriorityThread(Object* arbiter, u32 address) {
|
|
|
|
Thread* highest_priority_thread = nullptr;
|
2014-07-07 08:18:19 +05:30
|
|
|
s32 priority = THREADPRIO_LOWEST;
|
|
|
|
|
|
|
|
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
2014-12-29 18:25:30 +05:30
|
|
|
for (auto& thread : thread_list) {
|
|
|
|
if (!CheckWaitType(thread.get(), WAITTYPE_ARB, arbiter, address))
|
2014-07-07 08:18:19 +05:30
|
|
|
continue;
|
|
|
|
|
2014-10-23 08:50:01 +05:30
|
|
|
if (thread == nullptr)
|
|
|
|
continue; // TODO(yuriks): Thread handle will hang around forever. Should clean up.
|
2014-12-03 11:16:34 +05:30
|
|
|
|
2014-07-07 08:18:19 +05:30
|
|
|
if(thread->current_priority <= priority) {
|
2014-12-29 18:25:30 +05:30
|
|
|
highest_priority_thread = thread.get();
|
2014-07-07 08:18:19 +05:30
|
|
|
priority = thread->current_priority;
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 18:37:22 +05:30
|
|
|
|
2014-07-07 08:18:19 +05:30
|
|
|
// If a thread was arbitrated, resume it
|
2014-12-22 18:37:22 +05:30
|
|
|
if (nullptr != highest_priority_thread) {
|
|
|
|
highest_priority_thread->ResumeFromWait();
|
|
|
|
}
|
2014-07-07 08:18:19 +05:30
|
|
|
|
|
|
|
return highest_priority_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Arbitrate all threads currently waiting
|
2014-12-22 18:37:22 +05:30
|
|
|
void ArbitrateAllThreads(Object* arbiter, u32 address) {
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-07-07 08:18:19 +05:30
|
|
|
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
2014-12-29 18:25:30 +05:30
|
|
|
for (auto& thread : thread_list) {
|
|
|
|
if (CheckWaitType(thread.get(), WAITTYPE_ARB, arbiter, address))
|
2014-12-22 18:37:22 +05:30
|
|
|
thread->ResumeFromWait();
|
2014-07-07 08:18:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 10:26:00 +05:30
|
|
|
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
|
2014-12-22 18:37:22 +05:30
|
|
|
static void CallThread(Thread* t) {
|
2014-05-17 10:26:00 +05:30
|
|
|
// Stop waiting
|
|
|
|
if (t->wait_type != WAITTYPE_NONE) {
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
}
|
2014-05-23 04:36:12 +05:30
|
|
|
ChangeThreadState(t, THREADSTATUS_READY);
|
2014-05-21 05:07:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/// Switches CPU context to that of the specified thread
|
2014-12-22 18:37:22 +05:30
|
|
|
static void SwitchContext(Thread* t) {
|
2014-05-23 04:36:12 +05:30
|
|
|
Thread* cur = GetCurrentThread();
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-21 05:07:46 +05:30
|
|
|
// Save context for current thread
|
|
|
|
if (cur) {
|
2014-12-22 18:37:22 +05:30
|
|
|
Core::g_app_core->SaveContext(cur->context);
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-21 05:07:46 +05:30
|
|
|
if (cur->IsRunning()) {
|
2014-05-23 04:36:12 +05:30
|
|
|
ChangeReadyState(cur, true);
|
2014-05-21 05:07:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load context of new thread
|
|
|
|
if (t) {
|
2014-12-22 18:37:22 +05:30
|
|
|
current_thread = t;
|
2014-05-23 04:36:12 +05:30
|
|
|
ChangeReadyState(t, false);
|
2014-05-21 05:07:46 +05:30
|
|
|
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-12-22 18:37:22 +05:30
|
|
|
Core::g_app_core->LoadContext(t->context);
|
2014-05-21 05:07:46 +05:30
|
|
|
} else {
|
2014-12-22 18:37:22 +05:30
|
|
|
current_thread = nullptr;
|
2014-05-21 05:07:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the next thread that is ready to be run by priority
|
2014-12-22 18:37:22 +05:30
|
|
|
static Thread* NextThread() {
|
|
|
|
Thread* next;
|
2014-05-23 04:36:12 +05:30
|
|
|
Thread* cur = GetCurrentThread();
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-21 05:07:46 +05:30
|
|
|
if (cur && cur->IsRunning()) {
|
2014-11-18 19:18:11 +05:30
|
|
|
next = thread_ready_queue.pop_first_better(cur->current_priority);
|
2014-05-21 05:07:46 +05:30
|
|
|
} else {
|
2014-11-18 19:18:11 +05:30
|
|
|
next = thread_ready_queue.pop_first();
|
2014-05-21 05:07:46 +05:30
|
|
|
}
|
2014-05-22 07:12:18 +05:30
|
|
|
if (next == 0) {
|
2014-06-06 10:05:49 +05:30
|
|
|
return nullptr;
|
2014-05-21 05:07:46 +05:30
|
|
|
}
|
2014-12-22 18:37:22 +05:30
|
|
|
return next;
|
2014-05-21 05:07:46 +05:30
|
|
|
}
|
|
|
|
|
2015-01-15 10:11:33 +05:30
|
|
|
void WaitCurrentThread(WaitType wait_type, WaitObject* wait_object) {
|
2014-06-10 07:44:03 +05:30
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
thread->wait_type = wait_type;
|
2015-01-15 10:11:33 +05:30
|
|
|
|
|
|
|
auto res = std::find(thread->wait_objects.begin(), thread->wait_objects.end(), wait_object);
|
|
|
|
if (res == thread->wait_objects.end()) {
|
|
|
|
thread->wait_objects.push_back(wait_object);
|
|
|
|
}
|
2014-06-10 07:44:03 +05:30
|
|
|
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
|
2014-05-21 06:30:10 +05:30
|
|
|
}
|
|
|
|
|
2015-01-15 10:11:33 +05:30
|
|
|
void WaitCurrentThread(WaitType wait_type, WaitObject* wait_object, VAddr wait_address) {
|
2014-12-22 18:37:22 +05:30
|
|
|
WaitCurrentThread(wait_type, wait_object);
|
2014-12-03 11:16:34 +05:30
|
|
|
GetCurrentThread()->wait_address = wait_address;
|
|
|
|
}
|
|
|
|
|
2015-01-08 03:10:08 +05:30
|
|
|
/// Event type for the thread wake up event
|
|
|
|
static int ThreadWakeupEventType = -1;
|
|
|
|
|
|
|
|
/// Callback that will wake up the thread it was scheduled for
|
|
|
|
static void ThreadWakeupCallback(u64 parameter, int cycles_late) {
|
|
|
|
Handle handle = static_cast<Handle>(parameter);
|
2014-12-29 18:25:30 +05:30
|
|
|
SharedPtr<Thread> thread = Kernel::g_handle_table.Get<Thread>(handle);
|
2015-01-08 03:10:08 +05:30
|
|
|
if (thread == nullptr) {
|
|
|
|
LOG_ERROR(Kernel, "Thread doesn't exist %u", handle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
thread->ResumeFromWait();
|
2015-01-08 03:10:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds) {
|
2015-01-08 03:10:08 +05:30
|
|
|
// Don't schedule a wakeup if the thread wants to wait forever
|
|
|
|
if (nanoseconds == -1)
|
|
|
|
return;
|
2014-12-22 18:37:22 +05:30
|
|
|
_dbg_assert_(Kernel, thread != nullptr);
|
2015-01-08 03:10:08 +05:30
|
|
|
|
|
|
|
u64 microseconds = nanoseconds / 1000;
|
2014-12-22 18:37:22 +05:30
|
|
|
CoreTiming::ScheduleEvent(usToCycles(microseconds), ThreadWakeupEventType, thread->GetHandle());
|
2015-01-08 03:10:08 +05:30
|
|
|
}
|
|
|
|
|
2014-05-21 05:07:46 +05:30
|
|
|
/// Resumes a thread from waiting by marking it as "ready"
|
2014-12-22 18:37:22 +05:30
|
|
|
void Thread::ResumeFromWait() {
|
2015-01-11 23:48:52 +05:30
|
|
|
// Cancel any outstanding wakeup events
|
|
|
|
CoreTiming::UnscheduleEvent(ThreadWakeupEventType, GetHandle());
|
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
status &= ~THREADSTATUS_WAIT;
|
2015-01-15 10:11:33 +05:30
|
|
|
|
|
|
|
// Remove this thread from all other WaitObjects
|
|
|
|
for (auto wait_object : wait_objects)
|
|
|
|
wait_object->RemoveWaitingThread(this);
|
|
|
|
|
|
|
|
wait_objects.clear();
|
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
wait_type = WAITTYPE_NONE;
|
|
|
|
if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
|
|
|
|
ChangeReadyState(this, true);
|
2014-05-21 05:07:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 08:05:36 +05:30
|
|
|
/// Prints the thread queue for debugging purposes
|
2014-12-22 18:37:22 +05:30
|
|
|
static void DebugThreadQueue() {
|
2014-06-06 08:05:36 +05:30
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
if (!thread) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-22 18:37:22 +05:30
|
|
|
LOG_DEBUG(Kernel, "0x%02X 0x%08X (current)", thread->current_priority, GetCurrentThread()->GetHandle());
|
2014-12-29 18:25:30 +05:30
|
|
|
for (auto& t : thread_list) {
|
|
|
|
s32 priority = thread_ready_queue.contains(t.get());
|
2014-06-06 08:05:36 +05:30
|
|
|
if (priority != -1) {
|
2014-12-22 18:37:22 +05:30
|
|
|
LOG_DEBUG(Kernel, "0x%02X 0x%08X", priority, t->GetHandle());
|
2014-06-06 08:05:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:25:30 +05:30
|
|
|
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 18:37:22 +05:30
|
|
|
// 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 18:25:30 +05:30
|
|
|
name.c_str(), priority, new_priority);
|
2014-12-22 18:37:22 +05:30
|
|
|
// TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
|
|
|
|
// validity of this
|
|
|
|
priority = new_priority;
|
|
|
|
}
|
2014-05-17 10:26:00 +05:30
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
if (!Memory::GetPointer(entry_point)) {
|
2014-12-29 18:25:30 +05:30
|
|
|
LOG_ERROR(Kernel_SVC, "(name=%s): invalid entry %08x", name.c_str(), entry_point);
|
2014-12-22 18:37:22 +05:30
|
|
|
// TODO: Verify error
|
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
|
|
|
}
|
2014-05-21 06:32:35 +05:30
|
|
|
|
2014-12-29 18:25:30 +05:30
|
|
|
SharedPtr<Thread> thread(new Thread);
|
2014-06-06 08:05:36 +05:30
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
// 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 08:05:36 +05:30
|
|
|
|
2014-12-29 19:02:05 +05:30
|
|
|
thread_list.push_back(thread);
|
2014-11-18 19:18:11 +05:30
|
|
|
thread_ready_queue.prepare(priority);
|
2014-06-06 08:05:36 +05:30
|
|
|
|
2014-12-05 01:29:56 +05:30
|
|
|
thread->thread_id = next_thread_id++;
|
2014-06-10 07:44:03 +05:30
|
|
|
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;
|
|
|
|
thread->wait_type = WAITTYPE_NONE;
|
2015-01-15 10:11:33 +05:30
|
|
|
thread->wait_objects.clear();
|
2014-12-03 11:16:34 +05:30
|
|
|
thread->wait_address = 0;
|
2014-12-29 18:25:30 +05:30
|
|
|
thread->name = std::move(name);
|
2014-06-06 08:05:36 +05:30
|
|
|
|
2014-12-29 18:25:30 +05:30
|
|
|
ResetThread(thread.get(), arg, 0);
|
|
|
|
CallThread(thread.get());
|
2014-06-02 07:12:50 +05:30
|
|
|
|
2014-12-29 18:25:30 +05:30
|
|
|
return MakeResult<SharedPtr<Thread>>(std::move(thread));
|
2014-06-02 07:42:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the priority of the thread specified by handle
|
2014-12-22 18:37:22 +05:30
|
|
|
void Thread::SetPriority(s32 priority) {
|
2014-06-02 07:42:54 +05:30
|
|
|
// 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 07:23:49 +05:30
|
|
|
LOG_WARNING(Kernel_SVC, "invalid priority=%d, clamping to %d", priority, new_priority);
|
2014-06-02 07:42:54 +05:30
|
|
|
// 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 18:37:22 +05:30
|
|
|
s32 old = current_priority;
|
|
|
|
thread_ready_queue.remove(old, this);
|
|
|
|
current_priority = priority;
|
|
|
|
thread_ready_queue.prepare(current_priority);
|
2014-06-02 07:42:54 +05:30
|
|
|
|
|
|
|
// Change thread status to "ready" and push to ready queue
|
2014-12-22 18:37:22 +05:30
|
|
|
if (IsRunning()) {
|
|
|
|
status = (status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY;
|
2014-06-02 07:42:54 +05:30
|
|
|
}
|
2014-12-22 18:37:22 +05:30
|
|
|
if (IsReady()) {
|
|
|
|
thread_ready_queue.push_back(current_priority, this);
|
2014-06-02 07:42:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 20:40:58 +05:30
|
|
|
Handle SetupIdleThread() {
|
2014-12-22 18:37:22 +05:30
|
|
|
// We need to pass a few valid values to get around parameter checking in Thread::Create.
|
|
|
|
auto thread_res = Thread::Create("idle", Memory::KERNEL_MEMORY_VADDR, THREADPRIO_LOWEST, 0,
|
|
|
|
THREADPROCESSORID_0, 0, Kernel::DEFAULT_STACK_SIZE);
|
|
|
|
_dbg_assert_(Kernel, thread_res.Succeeded());
|
2014-12-29 18:25:30 +05:30
|
|
|
SharedPtr<Thread> thread = std::move(*thread_res);
|
2014-12-22 18:37:22 +05:30
|
|
|
|
2015-01-07 20:40:58 +05:30
|
|
|
thread->idle = true;
|
2014-12-29 18:25:30 +05:30
|
|
|
CallThread(thread.get());
|
2014-12-22 18:37:22 +05:30
|
|
|
return thread->GetHandle();
|
2015-01-07 20:40:58 +05:30
|
|
|
}
|
|
|
|
|
2014-12-29 18:25:30 +05:30
|
|
|
SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size) {
|
2014-05-16 03:57:08 +05:30
|
|
|
// Initialize new "main" thread
|
2014-12-29 18:25:30 +05:30
|
|
|
auto thread_res = Thread::Create("main", Core::g_app_core->GetPC(), priority, 0,
|
|
|
|
THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
|
2014-12-22 18:37:22 +05:30
|
|
|
// TODO(yuriks): Propagate error
|
|
|
|
_dbg_assert_(Kernel, thread_res.Succeeded());
|
2014-12-29 18:25:30 +05:30
|
|
|
SharedPtr<Thread> thread = std::move(*thread_res);
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
// If running another thread already, set it to "ready" state
|
2014-05-23 04:36:12 +05:30
|
|
|
Thread* cur = GetCurrentThread();
|
2014-05-16 03:57:08 +05:30
|
|
|
if (cur && cur->IsRunning()) {
|
2014-05-23 04:36:12 +05:30
|
|
|
ChangeReadyState(cur, true);
|
2014-05-15 06:20:30 +05:30
|
|
|
}
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
// Run new "main" thread
|
2014-12-29 18:25:30 +05:30
|
|
|
current_thread = thread.get();
|
2014-06-10 07:44:03 +05:30
|
|
|
thread->status = THREADSTATUS_RUNNING;
|
2014-12-22 18:37:22 +05:30
|
|
|
Core::g_app_core->LoadContext(thread->context);
|
2014-05-15 06:20:30 +05:30
|
|
|
|
2014-12-22 18:37:22 +05:30
|
|
|
return thread;
|
2014-05-16 03:57:08 +05:30
|
|
|
}
|
2014-05-15 06:20:30 +05:30
|
|
|
|
2014-06-06 08:05:36 +05:30
|
|
|
|
2014-05-16 03:57:08 +05:30
|
|
|
/// Reschedules to the next available thread (call after current thread is suspended)
|
2014-05-23 05:02:45 +05:30
|
|
|
void Reschedule() {
|
2014-05-23 04:36:12 +05:30
|
|
|
Thread* prev = GetCurrentThread();
|
|
|
|
Thread* next = NextThread();
|
2014-06-06 08:05:36 +05:30
|
|
|
HLE::g_reschedule = false;
|
2014-11-19 14:19:13 +05:30
|
|
|
|
2014-12-20 13:02:19 +05:30
|
|
|
if (next != nullptr) {
|
|
|
|
LOG_TRACE(Kernel, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle());
|
2014-05-23 05:02:45 +05:30
|
|
|
SwitchContext(next);
|
2014-12-20 13:02:19 +05:30
|
|
|
} else {
|
|
|
|
LOG_TRACE(Kernel, "cannot context switch from 0x%08X, no higher priority thread!", prev->GetHandle());
|
2014-05-16 03:57:08 +05:30
|
|
|
|
2014-12-29 18:25:30 +05:30
|
|
|
for (auto& thread : thread_list) {
|
2015-01-15 10:11:33 +05:30
|
|
|
LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X wait_type=0x%08X",
|
|
|
|
thread->GetHandle(), thread->current_priority, thread->status, thread->wait_type);
|
2014-06-06 08:05:36 +05:30
|
|
|
}
|
2014-05-23 04:20:36 +05:30
|
|
|
}
|
2014-05-17 10:26:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-05-16 03:57:08 +05:30
|
|
|
|
2014-05-21 05:07:46 +05:30
|
|
|
void ThreadingInit() {
|
2014-12-04 18:43:53 +05:30
|
|
|
next_thread_id = INITIAL_THREAD_ID;
|
2015-01-08 03:10:08 +05:30
|
|
|
ThreadWakeupEventType = CoreTiming::RegisterEvent("ThreadWakeupCallback", ThreadWakeupCallback);
|
2014-05-14 07:30:11 +05:30
|
|
|
}
|
2014-05-10 07:41:18 +05:30
|
|
|
|
2014-05-21 05:07:46 +05:30
|
|
|
void ThreadingShutdown() {
|
2014-05-14 07:30:11 +05:30
|
|
|
}
|
2014-05-21 05:07:46 +05:30
|
|
|
|
|
|
|
} // namespace
|