2014-05-09 22:11:18 -04: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 18:27:08 -04:00
|
|
|
#include "common/thread_queue_list.h"
|
2014-05-09 22:11:18 -04:00
|
|
|
|
2014-05-13 22:00:11 -04:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/mem_map.h"
|
2014-05-14 20:50:30 -04:00
|
|
|
#include "core/hle/hle.h"
|
2014-05-20 18:28:38 -04:00
|
|
|
#include "core/hle/svc.h"
|
2014-05-09 22:11:18 -04:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
namespace Kernel {
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
enum ThreadStatus {
|
2014-05-17 00:56:00 -04:00
|
|
|
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-13 22:00:11 -04:00
|
|
|
};
|
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
enum WaitType {
|
|
|
|
WAITTYPE_NONE,
|
|
|
|
WAITTYPE_SLEEP,
|
|
|
|
WAITTYPE_SEMA,
|
|
|
|
WAITTYPE_EVENTFLAG,
|
|
|
|
WAITTYPE_THREADEND,
|
|
|
|
WAITTYPE_VBLANK,
|
|
|
|
WAITTYPE_MUTEX,
|
|
|
|
WAITTYPE_SYNCH,
|
2014-05-09 22:11:18 -04:00
|
|
|
};
|
|
|
|
|
2014-05-20 18:13:25 -04:00
|
|
|
class Thread : public Kernel::Object {
|
2014-05-09 22:11:18 -04:00
|
|
|
public:
|
2014-05-15 18:27:08 -04:00
|
|
|
|
2014-05-20 18:20:35 -04:00
|
|
|
const char* GetName() { return name; }
|
|
|
|
const char* GetTypeName() { return "Thread"; }
|
2014-05-15 18:27:08 -04:00
|
|
|
|
2014-05-20 18:13:25 -04:00
|
|
|
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Thread; }
|
|
|
|
Kernel::HandleType GetHandleType() const { return Kernel::HandleType::Thread; }
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-15 18:27:08 -04: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-09 22:11:18 -04:00
|
|
|
|
|
|
|
ThreadContext context;
|
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
u32 status;
|
|
|
|
u32 entry_point;
|
|
|
|
u32 stack_top;
|
|
|
|
u32 stack_size;
|
2014-05-09 22:11:18 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
s32 initial_priority;
|
|
|
|
s32 current_priority;
|
2014-05-09 22:11:18 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
s32 processor_id;
|
2014-05-09 22:11:18 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
WaitType wait_type;
|
2014-05-09 22:11:18 -04:00
|
|
|
|
2014-05-20 18:13:25 -04:00
|
|
|
char name[Kernel::MAX_NAME_LENGTH + 1];
|
2014-05-09 22:11:18 -04:00
|
|
|
};
|
|
|
|
|
2014-05-13 22:00:11 -04:00
|
|
|
// Lists all thread ids that aren't deleted/etc.
|
2014-05-15 18:27:08 -04:00
|
|
|
std::vector<Handle> g_thread_queue;
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
// Lists only ready thread ids.
|
|
|
|
Common::ThreadQueueList<Handle> g_thread_ready_queue;
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
Handle g_current_thread_handle;
|
|
|
|
Thread* g_current_thread;
|
2014-05-13 22:00:11 -04:00
|
|
|
|
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
/// Gets the current thread
|
2014-05-20 18:20:35 -04:00
|
|
|
inline Thread* __GetCurrentThread() {
|
2014-05-15 18:27:08 -04:00
|
|
|
return g_current_thread;
|
|
|
|
}
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
/// Sets the current thread
|
2014-05-20 18:20:35 -04:00
|
|
|
inline void __SetCurrentThread(Thread* t) {
|
2014-05-15 18:27:08 -04:00
|
|
|
g_current_thread = t;
|
|
|
|
g_current_thread_handle = t->GetHandle();
|
|
|
|
}
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
/// Saves the current CPU context
|
2014-05-20 19:37:46 -04:00
|
|
|
void __SaveContext(ThreadContext& ctx) {
|
2014-05-20 18:50:16 -04:00
|
|
|
Core::g_app_core->SaveContext(ctx);
|
2014-05-13 22:00:11 -04:00
|
|
|
}
|
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
/// Loads a CPU context
|
2014-05-20 19:37:46 -04:00
|
|
|
void __LoadContext(const ThreadContext& ctx) {
|
2014-05-20 18:50:16 -04:00
|
|
|
Core::g_app_core->LoadContext(ctx);
|
2014-05-14 20:50:30 -04:00
|
|
|
}
|
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
/// Resets a thread
|
2014-05-20 19:37:46 -04:00
|
|
|
void __ResetThread(Thread* t, s32 lowest_priority) {
|
2014-05-15 18:27:08 -04:00
|
|
|
memset(&t->context, 0, sizeof(ThreadContext));
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-15 18:27:08 -04: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-13 22:29:31 -04:00
|
|
|
}
|
2014-05-15 18:27:08 -04:00
|
|
|
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-05-13 22:00:11 -04:00
|
|
|
}
|
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
/// Change a thread to "ready" state
|
2014-05-20 19:37:46 -04:00
|
|
|
void __ChangeReadyState(Thread* t, bool ready) {
|
2014-05-15 18:27:08 -04:00
|
|
|
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-13 22:00:11 -04:00
|
|
|
} else {
|
2014-05-15 18:27:08 -04:00
|
|
|
g_thread_ready_queue.push_back(t->current_priority, handle);
|
2014-05-13 22:00:11 -04:00
|
|
|
}
|
2014-05-15 18:27:08 -04:00
|
|
|
t->status = THREADSTATUS_READY;
|
2014-05-13 22:00:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
/// Changes a threads state
|
2014-05-20 19:37:46 -04:00
|
|
|
void __ChangeThreadState(Thread* t, ThreadStatus new_status) {
|
2014-05-15 18:27:08 -04:00
|
|
|
if (!t || t->status == new_status) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-20 19:37:46 -04:00
|
|
|
__ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
|
2014-05-15 18:27:08 -04:00
|
|
|
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-13 22:00:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 00:56:00 -04:00
|
|
|
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
|
2014-05-20 19:37:46 -04:00
|
|
|
void __CallThread(Thread* t) {
|
2014-05-17 00:56:00 -04:00
|
|
|
// Stop waiting
|
|
|
|
if (t->wait_type != WAITTYPE_NONE) {
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
}
|
2014-05-20 19:37:46 -04:00
|
|
|
__ChangeThreadState(t, THREADSTATUS_READY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Switches CPU context to that of the specified thread
|
|
|
|
void __SwitchContext(Thread* t, const char* reason) {
|
|
|
|
Thread* cur = __GetCurrentThread();
|
|
|
|
|
|
|
|
// Save context for current thread
|
|
|
|
if (cur) {
|
|
|
|
__SaveContext(cur->context);
|
|
|
|
|
|
|
|
if (cur->IsRunning()) {
|
|
|
|
__ChangeReadyState(cur, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load context of new thread
|
|
|
|
if (t) {
|
|
|
|
__SetCurrentThread(t);
|
|
|
|
__ChangeReadyState(t, false);
|
|
|
|
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
__LoadContext(t->context);
|
|
|
|
} else {
|
|
|
|
__SetCurrentThread(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the next thread that is ready to be run by priority
|
|
|
|
Thread* __NextThread() {
|
|
|
|
Handle next;
|
|
|
|
Thread* cur = __GetCurrentThread();
|
|
|
|
|
|
|
|
if (cur && cur->IsRunning()) {
|
|
|
|
next = g_thread_ready_queue.pop_first_better(cur->current_priority);
|
|
|
|
} else {
|
|
|
|
next = g_thread_ready_queue.pop_first();
|
|
|
|
}
|
|
|
|
if (next < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return Kernel::g_object_pool.GetFast<Thread>(next);
|
|
|
|
}
|
|
|
|
|
2014-05-20 21:00:10 -04:00
|
|
|
/// Puts a thread in the wait state for the given type/reason
|
|
|
|
void __WaitCurThread(WaitType wait_type, const char* reason) {
|
|
|
|
Thread* t = __GetCurrentThread();
|
|
|
|
t->wait_type = wait_type;
|
|
|
|
__ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
|
|
|
|
}
|
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
/// Resumes a thread from waiting by marking it as "ready"
|
2014-05-20 21:00:10 -04:00
|
|
|
void ResumeThreadFromWait(Handle handle) {
|
2014-05-20 19:37:46 -04:00
|
|
|
u32 error;
|
|
|
|
Thread* t = Kernel::g_object_pool.Get<Thread>(handle, error);
|
|
|
|
if (t) {
|
|
|
|
t->status &= ~THREADSTATUS_WAIT;
|
|
|
|
if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
|
|
|
|
__ChangeReadyState(t, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 00:56:00 -04:00
|
|
|
/// Creates a new thread
|
2014-05-20 19:37:46 -04:00
|
|
|
Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority,
|
2014-05-17 00:56:00 -04:00
|
|
|
s32 processor_id, u32 stack_top, int stack_size) {
|
|
|
|
|
2014-05-20 18:20:35 -04:00
|
|
|
Thread* t = new Thread;
|
2014-05-17 00:56:00 -04:00
|
|
|
|
2014-05-20 18:13:25 -04:00
|
|
|
handle = Kernel::g_object_pool.Create(t);
|
2014-05-17 00:56:00 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-05-20 18:13:25 -04:00
|
|
|
strncpy(t->name, name, Kernel::MAX_NAME_LENGTH);
|
|
|
|
t->name[Kernel::MAX_NAME_LENGTH] = '\0';
|
2014-05-17 00:56:00 -04:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new thread - wrapper for external user
|
2014-05-20 19:37:46 -04:00
|
|
|
Handle CreateThread(const char* name, u32 entry_point, s32 priority, s32 processor_id,
|
2014-05-17 00:56:00 -04:00
|
|
|
u32 stack_top, int stack_size) {
|
|
|
|
if (name == NULL) {
|
2014-05-20 19:37:46 -04:00
|
|
|
ERROR_LOG(KERNEL, "CreateThread(): NULL name");
|
2014-05-17 00:56:00 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((u32)stack_size < 0x200) {
|
2014-05-20 19:37:46 -04:00
|
|
|
ERROR_LOG(KERNEL, "CreateThread(name=%s): invalid stack_size=0x%08X", name,
|
2014-05-17 00:56:00 -04:00
|
|
|
stack_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
|
|
|
|
s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
|
2014-05-20 19:37:46 -04:00
|
|
|
WARN_LOG(KERNEL, "CreateThread(name=%s): invalid priority=0x%08X, clamping to %08X",
|
2014-05-17 00:56:00 -04:00
|
|
|
name, priority, new_priority);
|
|
|
|
// TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
|
|
|
|
// validity of this
|
|
|
|
priority = new_priority;
|
|
|
|
}
|
|
|
|
if (!Memory::GetPointer(entry_point)) {
|
2014-05-20 19:37:46 -04:00
|
|
|
ERROR_LOG(KERNEL, "CreateThread(name=%s): invalid entry %08x", name, entry_point);
|
2014-05-17 00:56:00 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Handle handle;
|
2014-05-20 19:37:46 -04:00
|
|
|
Thread* t = CreateThread(handle, name, entry_point, priority, processor_id, stack_top,
|
2014-05-17 00:56:00 -04:00
|
|
|
stack_size);
|
|
|
|
|
|
|
|
HLE::EatCycles(32000);
|
|
|
|
|
|
|
|
// This won't schedule to the new thread, but it may to one woken from eating cycles.
|
|
|
|
// Technically, this should not eat all at once, and reschedule in the middle, but that's hard.
|
|
|
|
HLE::ReSchedule("thread created");
|
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
__CallThread(t);
|
2014-05-17 00:56:00 -04:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
/// Gets the current thread
|
|
|
|
Handle GetCurrentThread() {
|
|
|
|
return __GetCurrentThread()->GetHandle();
|
2014-05-13 22:00:11 -04:00
|
|
|
}
|
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
/// Sets up the primary application thread
|
2014-05-20 19:37:46 -04:00
|
|
|
Handle SetupMainThread(s32 priority, int stack_size) {
|
2014-05-15 18:27:08 -04:00
|
|
|
Handle handle;
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
// Initialize new "main" thread
|
2014-05-20 19:37:46 -04:00
|
|
|
Thread* t = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority,
|
2014-05-17 00:56:00 -04:00
|
|
|
THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
|
2014-05-13 22:00:11 -04:00
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
__ResetThread(t, 0);
|
2014-05-15 18:27:08 -04:00
|
|
|
|
|
|
|
// If running another thread already, set it to "ready" state
|
2014-05-20 18:20:35 -04:00
|
|
|
Thread* cur = __GetCurrentThread();
|
2014-05-15 18:27:08 -04:00
|
|
|
if (cur && cur->IsRunning()) {
|
2014-05-20 19:37:46 -04:00
|
|
|
__ChangeReadyState(cur, true);
|
2014-05-14 20:50:30 -04:00
|
|
|
}
|
2014-05-15 18:27:08 -04:00
|
|
|
|
|
|
|
// Run new "main" thread
|
|
|
|
__SetCurrentThread(t);
|
|
|
|
t->status = THREADSTATUS_RUNNING;
|
2014-05-20 19:37:46 -04:00
|
|
|
__LoadContext(t->context);
|
2014-05-14 20:50:30 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
return handle;
|
|
|
|
}
|
2014-05-14 20:50:30 -04:00
|
|
|
|
2014-05-15 18:27:08 -04:00
|
|
|
/// Reschedules to the next available thread (call after current thread is suspended)
|
2014-05-20 19:37:46 -04:00
|
|
|
void Reschedule(const char* reason) {
|
|
|
|
Thread* next = __NextThread();
|
2014-05-15 18:27:08 -04:00
|
|
|
if (next > 0) {
|
2014-05-20 19:37:46 -04:00
|
|
|
__SwitchContext(next, reason);
|
2014-05-15 18:27:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 00:56:00 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-17 13:47:55 -04:00
|
|
|
/// Put current thread in a wait state - on WaitSynchronization
|
2014-05-20 19:37:46 -04:00
|
|
|
void WaitThread_Synchronization() {
|
2014-05-17 00:56:00 -04:00
|
|
|
// TODO(bunnei): Just a placeholder function for now... FixMe
|
2014-05-20 19:37:46 -04:00
|
|
|
__WaitCurThread(WAITTYPE_SYNCH, "waitSynchronization called");
|
2014-05-17 00:56:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-05-15 18:27:08 -04:00
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
void ThreadingInit() {
|
2014-05-13 22:00:11 -04:00
|
|
|
}
|
2014-05-09 22:11:18 -04:00
|
|
|
|
2014-05-20 19:37:46 -04:00
|
|
|
void ThreadingShutdown() {
|
2014-05-13 22:00:11 -04:00
|
|
|
}
|
2014-05-20 19:37:46 -04:00
|
|
|
|
|
|
|
} // namespace
|