2014-05-10 02:11:18 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "common/common.h"
|
2014-05-15 22:27:08 +00:00
|
|
|
#include "common/thread_queue_list.h"
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2014-05-14 02:00:11 +00:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/mem_map.h"
|
2014-05-15 00:50:30 +00:00
|
|
|
#include "core/hle/hle.h"
|
|
|
|
#include "core/hle/syscall.h"
|
2014-05-10 02:11:18 +00:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
// Enums
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
enum ThreadPriority {
|
|
|
|
THREADPRIO_HIGHEST = 0,
|
|
|
|
THREADPRIO_DEFAULT = 16,
|
|
|
|
THREADPRIO_LOWEST = 31,
|
|
|
|
};
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
enum ThreadStatus {
|
|
|
|
THREADSTATUS_RUNNING = 1,
|
|
|
|
THREADSTATUS_READY = 2,
|
|
|
|
THREADSTATUS_WAIT = 4,
|
|
|
|
THREADSTATUS_SUSPEND = 8,
|
|
|
|
THREADSTATUS_DORMANT = 16,
|
|
|
|
THREADSTATUS_DEAD = 32,
|
|
|
|
THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
|
2014-05-14 02:00:11 +00:00
|
|
|
};
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
enum WaitType {
|
|
|
|
WAITTYPE_NONE,
|
|
|
|
WAITTYPE_SLEEP,
|
|
|
|
WAITTYPE_SEMA,
|
|
|
|
WAITTYPE_EVENTFLAG,
|
|
|
|
WAITTYPE_THREADEND,
|
|
|
|
WAITTYPE_VBLANK,
|
|
|
|
WAITTYPE_MUTEX,
|
|
|
|
WAITTYPE_SYNCH,
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
NUM_WAITTYPES
|
2014-05-10 02:11:18 +00:00
|
|
|
};
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
typedef s32 Handle;
|
2014-05-10 02:11:18 +00:00
|
|
|
|
|
|
|
class Thread : public KernelObject {
|
|
|
|
public:
|
2014-05-15 22:27:08 +00:00
|
|
|
|
|
|
|
const char *GetName() { return name; }
|
2014-05-10 02:11:18 +00:00
|
|
|
const char *GetTypeName() { return "Thread"; }
|
2014-05-15 22:27:08 +00:00
|
|
|
|
2014-05-14 02:00:11 +00:00
|
|
|
static KernelIDType GetStaticIDType() { return KERNEL_ID_TYPE_THREAD; }
|
|
|
|
KernelIDType GetIDType() const { return KERNEL_ID_TYPE_THREAD; }
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
|
|
|
|
inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
|
|
|
|
inline bool IsReady() const { return (status & THREADSTATUS_READY) != 0; }
|
|
|
|
inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; }
|
|
|
|
inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
|
2014-05-10 02:11:18 +00:00
|
|
|
|
|
|
|
ThreadContext context;
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
u32 status;
|
|
|
|
u32 entry_point;
|
|
|
|
u32 stack_top;
|
|
|
|
u32 stack_size;
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
s32 initial_priority;
|
|
|
|
s32 current_priority;
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
s32 processor_id;
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
WaitType wait_type;
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
char name[KERNELOBJECT_MAX_NAME_LENGTH+1];
|
2014-05-10 02:11:18 +00:00
|
|
|
};
|
|
|
|
|
2014-05-14 02:00:11 +00:00
|
|
|
// Lists all thread ids that aren't deleted/etc.
|
2014-05-15 22:27:08 +00:00
|
|
|
std::vector<Handle> g_thread_queue;
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
// Lists only ready thread ids.
|
|
|
|
Common::ThreadQueueList<Handle> g_thread_ready_queue;
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
Handle g_current_thread_handle;
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
Thread* g_current_thread;
|
2014-05-14 02:00:11 +00:00
|
|
|
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
inline Thread *__GetCurrentThread() {
|
|
|
|
return g_current_thread;
|
|
|
|
}
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
inline void __SetCurrentThread(Thread *t) {
|
|
|
|
g_current_thread = t;
|
|
|
|
g_current_thread_handle = t->GetHandle();
|
|
|
|
}
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Saves the current CPU context
|
|
|
|
void __KernelSaveContext(ThreadContext &ctx) {
|
|
|
|
ctx.cpu_registers[0] = Core::g_app_core->GetReg(0);
|
|
|
|
ctx.cpu_registers[1] = Core::g_app_core->GetReg(1);
|
|
|
|
ctx.cpu_registers[2] = Core::g_app_core->GetReg(2);
|
|
|
|
ctx.cpu_registers[3] = Core::g_app_core->GetReg(3);
|
|
|
|
ctx.cpu_registers[4] = Core::g_app_core->GetReg(4);
|
|
|
|
ctx.cpu_registers[5] = Core::g_app_core->GetReg(5);
|
|
|
|
ctx.cpu_registers[6] = Core::g_app_core->GetReg(6);
|
|
|
|
ctx.cpu_registers[7] = Core::g_app_core->GetReg(7);
|
|
|
|
ctx.cpu_registers[8] = Core::g_app_core->GetReg(8);
|
|
|
|
ctx.cpu_registers[9] = Core::g_app_core->GetReg(9);
|
|
|
|
ctx.cpu_registers[10] = Core::g_app_core->GetReg(10);
|
|
|
|
ctx.cpu_registers[11] = Core::g_app_core->GetReg(11);
|
|
|
|
ctx.cpu_registers[12] = Core::g_app_core->GetReg(12);
|
|
|
|
ctx.sp = Core::g_app_core->GetReg(13);
|
|
|
|
ctx.lr = Core::g_app_core->GetReg(14);
|
|
|
|
ctx.pc = Core::g_app_core->GetPC();
|
|
|
|
ctx.cpsr = Core::g_app_core->GetCPSR();
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Loads a CPU context
|
|
|
|
void __KernelLoadContext(const ThreadContext &ctx) {
|
|
|
|
Core::g_app_core->SetReg(0, ctx.cpu_registers[0]);
|
|
|
|
Core::g_app_core->SetReg(1, ctx.cpu_registers[1]);
|
|
|
|
Core::g_app_core->SetReg(2, ctx.cpu_registers[2]);
|
|
|
|
Core::g_app_core->SetReg(3, ctx.cpu_registers[3]);
|
|
|
|
Core::g_app_core->SetReg(4, ctx.cpu_registers[4]);
|
|
|
|
Core::g_app_core->SetReg(5, ctx.cpu_registers[5]);
|
|
|
|
Core::g_app_core->SetReg(6, ctx.cpu_registers[6]);
|
|
|
|
Core::g_app_core->SetReg(7, ctx.cpu_registers[7]);
|
|
|
|
Core::g_app_core->SetReg(8, ctx.cpu_registers[8]);
|
|
|
|
Core::g_app_core->SetReg(9, ctx.cpu_registers[9]);
|
|
|
|
Core::g_app_core->SetReg(10, ctx.cpu_registers[10]);
|
|
|
|
Core::g_app_core->SetReg(11, ctx.cpu_registers[11]);
|
|
|
|
Core::g_app_core->SetReg(12, ctx.cpu_registers[12]);
|
|
|
|
Core::g_app_core->SetReg(13, ctx.sp);
|
|
|
|
Core::g_app_core->SetReg(14, ctx.lr);
|
|
|
|
//Core::g_app_core->SetReg(15, ctx.pc);
|
|
|
|
|
|
|
|
Core::g_app_core->SetPC(ctx.pc);
|
|
|
|
Core::g_app_core->SetCPSR(ctx.cpsr);
|
2014-05-15 00:50:30 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Resets a thread
|
|
|
|
void __KernelResetThread(Thread *t, s32 lowest_priority) {
|
|
|
|
memset(&t->context, 0, sizeof(ThreadContext));
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
t->context.pc = t->entry_point;
|
|
|
|
t->context.sp = t->stack_top;
|
|
|
|
|
|
|
|
if (t->current_priority < lowest_priority) {
|
|
|
|
t->current_priority = t->initial_priority;
|
2014-05-14 02:29:31 +00:00
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Creates a new thread
|
|
|
|
Thread *__KernelCreateThread(Handle &handle, const char *name, u32 entry_point, s32 priority, s32 processor_id, u32 stack_top, int stack_size=0x4000) {
|
|
|
|
static u32 _handle_count = 1;
|
|
|
|
|
|
|
|
Thread *t = new Thread;
|
|
|
|
|
|
|
|
handle = (_handle_count++);
|
|
|
|
|
|
|
|
g_thread_queue.push_back(handle);
|
|
|
|
g_thread_ready_queue.prepare(priority);
|
|
|
|
|
|
|
|
t->status = THREADSTATUS_DORMANT;
|
|
|
|
t->entry_point = entry_point;
|
|
|
|
t->stack_top = stack_top;
|
|
|
|
t->stack_size = stack_size;
|
|
|
|
t->initial_priority = t->current_priority = priority;
|
|
|
|
t->processor_id = processor_id;
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
|
|
|
|
strncpy(t->name, name, KERNELOBJECT_MAX_NAME_LENGTH);
|
|
|
|
t->name[KERNELOBJECT_MAX_NAME_LENGTH] = '\0';
|
|
|
|
|
|
|
|
return t;
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Change a thread to "ready" state
|
|
|
|
void __KernelChangeReadyState(Thread *t, bool ready) {
|
|
|
|
Handle handle = t->GetHandle();
|
|
|
|
if (t->IsReady()) {
|
|
|
|
if (!ready) {
|
|
|
|
g_thread_ready_queue.remove(t->current_priority, handle);
|
|
|
|
}
|
|
|
|
} else if (ready) {
|
|
|
|
if (t->IsRunning()) {
|
|
|
|
g_thread_ready_queue.push_front(t->current_priority, handle);
|
2014-05-14 02:00:11 +00:00
|
|
|
} else {
|
2014-05-15 22:27:08 +00:00
|
|
|
g_thread_ready_queue.push_back(t->current_priority, handle);
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
t->status = THREADSTATUS_READY;
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Changes a threads state
|
|
|
|
void __KernelChangeThreadState(Thread *t, ThreadStatus new_status) {
|
|
|
|
if (!t || t->status == new_status) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
__KernelChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
|
|
|
|
t->status = new_status;
|
|
|
|
|
|
|
|
if (new_status == THREADSTATUS_WAIT) {
|
|
|
|
if (t->wait_type == WAITTYPE_NONE) {
|
|
|
|
printf("ERROR: Waittype none not allowed here\n");
|
|
|
|
}
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Switches CPU context to that of the specified thread
|
|
|
|
void __KernelSwitchContext(Thread* t, const char *reason) {
|
2014-05-14 02:00:11 +00:00
|
|
|
Thread *cur = __GetCurrentThread();
|
2014-05-15 22:27:08 +00:00
|
|
|
|
|
|
|
// Save context for current thread
|
|
|
|
if (cur) {
|
|
|
|
__KernelSaveContext(cur->context);
|
|
|
|
|
|
|
|
if (cur->IsRunning()) {
|
|
|
|
__KernelChangeReadyState(cur, true);
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
// Load context of new thread
|
|
|
|
if (t) {
|
|
|
|
__SetCurrentThread(t);
|
|
|
|
__KernelChangeReadyState(t, false);
|
|
|
|
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
__KernelLoadContext(t->context);
|
2014-05-14 02:00:11 +00:00
|
|
|
} else {
|
2014-05-15 22:27:08 +00:00
|
|
|
__SetCurrentThread(NULL);
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Gets the next thread that is ready to be run by priority
|
|
|
|
Thread *__KernelNextThread() {
|
|
|
|
Handle next;
|
2014-05-14 02:00:11 +00:00
|
|
|
Thread *cur = __GetCurrentThread();
|
2014-05-15 22:27:08 +00:00
|
|
|
|
|
|
|
if (cur && cur->IsRunning()) {
|
|
|
|
next = g_thread_ready_queue.pop_first_better(cur->current_priority);
|
|
|
|
} else {
|
|
|
|
next = g_thread_ready_queue.pop_first();
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
if (next < 0) {
|
|
|
|
return NULL;
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
return g_kernel_objects.GetFast<Thread>(next);
|
2014-05-14 02:00:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
|
|
|
|
void __KernelCallThread(Thread *t) {
|
|
|
|
// Stop waiting
|
|
|
|
if (t->wait_type != WAITTYPE_NONE) {
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-05-15 00:50:30 +00:00
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
__KernelChangeThreadState(t, THREADSTATUS_READY);
|
2014-05-15 00:50:30 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Sets up the primary application thread
|
|
|
|
Handle __KernelSetupMainThread(s32 priority, int stack_size) {
|
|
|
|
Handle handle;
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
// Initialize new "main" thread
|
|
|
|
Thread *t = __KernelCreateThread(handle, "main", Core::g_app_core->GetPC(), priority,
|
|
|
|
0xFFFFFFFE, Memory::SCRATCHPAD_VADDR_END, stack_size);
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
__KernelResetThread(t, 0);
|
|
|
|
|
|
|
|
// If running another thread already, set it to "ready" state
|
2014-05-15 00:50:30 +00:00
|
|
|
Thread *cur = __GetCurrentThread();
|
2014-05-15 22:27:08 +00:00
|
|
|
if (cur && cur->IsRunning()) {
|
|
|
|
__KernelChangeReadyState(cur, true);
|
2014-05-15 00:50:30 +00:00
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
|
|
|
|
// Run new "main" thread
|
|
|
|
__SetCurrentThread(t);
|
|
|
|
t->status = THREADSTATUS_RUNNING;
|
|
|
|
__KernelLoadContext(t->context);
|
2014-05-15 00:50:30 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
return handle;
|
|
|
|
}
|
2014-05-15 00:50:30 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Resumes a thread from waiting by marking it as "ready"
|
|
|
|
void __KernelResumeThreadFromWait(Handle handle) {
|
|
|
|
u32 error;
|
|
|
|
Thread *t = g_kernel_objects.Get<Thread>(handle, error);
|
|
|
|
if (t) {
|
|
|
|
t->status &= ~THREADSTATUS_WAIT;
|
|
|
|
if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
|
|
|
|
__KernelChangeReadyState(t, true);
|
2014-05-15 00:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 22:27:08 +00:00
|
|
|
}
|
2014-05-15 00:50:30 +00:00
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Puts a thread in the wait state for the given type/reason
|
|
|
|
void __KernelWaitCurThread(WaitType wait_type, const char *reason) {
|
|
|
|
Thread *t = __GetCurrentThread();
|
|
|
|
t->wait_type = wait_type;
|
|
|
|
__KernelChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
|
2014-05-15 00:50:30 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 22:27:08 +00:00
|
|
|
/// Reschedules to the next available thread (call after current thread is suspended)
|
|
|
|
void __KernelReschedule(const char *reason) {
|
|
|
|
Thread *next = __KernelNextThread();
|
|
|
|
if (next > 0) {
|
|
|
|
__KernelSwitchContext(next, reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 02:00:11 +00:00
|
|
|
void __KernelThreadingInit() {
|
|
|
|
}
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2014-05-14 02:00:11 +00:00
|
|
|
void __KernelThreadingShutdown() {
|
|
|
|
}
|