2014-05-09 19:11:18 -07:00
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
|
|
|
// Licensed under GPLv2
|
2014-11-19 00:49:13 -08:00
|
|
|
// Refer to the license.txt file included.
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-06-05 19:35:36 -07:00
|
|
|
#include <algorithm>
|
2014-08-17 20:03:22 -07:00
|
|
|
#include <list>
|
2014-05-09 19:11:18 -07:00
|
|
|
#include <map>
|
2014-08-17 20:03:22 -07:00
|
|
|
#include <vector>
|
2014-05-09 19:11:18 -07:00
|
|
|
|
|
|
|
#include "common/common.h"
|
2014-05-15 15:27:08 -07:00
|
|
|
#include "common/thread_queue_list.h"
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-13 19:00:11 -07:00
|
|
|
#include "core/core.h"
|
2014-05-14 17:50:30 -07:00
|
|
|
#include "core/hle/hle.h"
|
2014-05-09 19:11:18 -07:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
2014-12-07 12:44:21 -08:00
|
|
|
#include "core/hle/kernel/mutex.h"
|
2014-10-22 20:20:01 -07:00
|
|
|
#include "core/hle/result.h"
|
|
|
|
#include "core/mem_map.h"
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
namespace Kernel {
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-05-20 15:13:25 -07:00
|
|
|
class Thread : public Kernel::Object {
|
2014-05-09 19:11:18 -07:00
|
|
|
public:
|
2014-05-15 15:27:08 -07:00
|
|
|
|
2014-10-25 21:56:13 -07:00
|
|
|
std::string GetName() const override { return name; }
|
|
|
|
std::string GetTypeName() const override { return "Thread"; }
|
2014-05-15 15:27:08 -07:00
|
|
|
|
2014-06-05 21:10:50 -07:00
|
|
|
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Thread; }
|
2014-10-25 21:56:13 -07:00
|
|
|
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Thread; }
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-05-15 15:27:08 -07: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 19:11:18 -07:00
|
|
|
|
2014-10-22 20:20:01 -07:00
|
|
|
ResultVal<bool> WaitSynchronization() override {
|
2014-11-24 09:51:50 -08:00
|
|
|
const bool wait = status != THREADSTATUS_DORMANT;
|
|
|
|
if (wait) {
|
2014-06-05 19:35:36 -07:00
|
|
|
Handle thread = GetCurrentThreadHandle();
|
|
|
|
if (std::find(waiting_threads.begin(), waiting_threads.end(), thread) == waiting_threads.end()) {
|
|
|
|
waiting_threads.push_back(thread);
|
|
|
|
}
|
|
|
|
WaitCurrentThread(WAITTYPE_THREADEND, this->GetHandle());
|
|
|
|
}
|
2014-11-24 09:51:50 -08:00
|
|
|
|
|
|
|
return MakeResult<bool>(wait);
|
2014-05-26 18:01:27 -07:00
|
|
|
}
|
|
|
|
|
2014-05-09 19:11:18 -07:00
|
|
|
ThreadContext context;
|
|
|
|
|
2014-12-03 20:22:06 -08:00
|
|
|
u32 thread_id;
|
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
u32 status;
|
|
|
|
u32 entry_point;
|
|
|
|
u32 stack_top;
|
|
|
|
u32 stack_size;
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
s32 initial_priority;
|
|
|
|
s32 current_priority;
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
s32 processor_id;
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
WaitType wait_type;
|
2014-06-05 19:35:36 -07:00
|
|
|
Handle wait_handle;
|
2014-12-02 21:46:34 -08:00
|
|
|
VAddr wait_address;
|
2014-06-05 19:35:36 -07:00
|
|
|
|
|
|
|
std::vector<Handle> waiting_threads;
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-08-17 20:03:22 -07:00
|
|
|
std::string name;
|
2014-05-09 19:11:18 -07:00
|
|
|
};
|
|
|
|
|
2014-05-13 19:00:11 -07:00
|
|
|
// Lists all thread ids that aren't deleted/etc.
|
2014-11-18 05:48:11 -08:00
|
|
|
static std::vector<Handle> thread_queue;
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
// Lists only ready thread ids.
|
2014-11-18 05:48:11 -08:00
|
|
|
static Common::ThreadQueueList<Handle> thread_ready_queue;
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-11-18 05:48:11 -08:00
|
|
|
static Handle current_thread_handle;
|
|
|
|
static Thread* current_thread;
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-12-04 05:13:53 -08: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
|
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
/// Gets the current thread
|
2014-05-22 16:06:12 -07:00
|
|
|
inline Thread* GetCurrentThread() {
|
2014-11-18 05:48:11 -08:00
|
|
|
return current_thread;
|
2014-05-15 15:27:08 -07:00
|
|
|
}
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-05-22 16:06:12 -07:00
|
|
|
/// Gets the current thread handle
|
|
|
|
Handle GetCurrentThreadHandle() {
|
|
|
|
return GetCurrentThread()->GetHandle();
|
|
|
|
}
|
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
/// Sets the current thread
|
2014-05-22 16:06:12 -07:00
|
|
|
inline void SetCurrentThread(Thread* t) {
|
2014-11-18 05:48:11 -08:00
|
|
|
current_thread = t;
|
|
|
|
current_thread_handle = t->GetHandle();
|
2014-05-15 15:27:08 -07:00
|
|
|
}
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
/// Saves the current CPU context
|
2014-05-22 16:06:12 -07:00
|
|
|
void SaveContext(ThreadContext& ctx) {
|
2014-05-20 15:50:16 -07:00
|
|
|
Core::g_app_core->SaveContext(ctx);
|
2014-05-13 19:00:11 -07:00
|
|
|
}
|
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
/// Loads a CPU context
|
2014-05-22 16:06:12 -07:00
|
|
|
void LoadContext(ThreadContext& ctx) {
|
2014-05-20 15:50:16 -07:00
|
|
|
Core::g_app_core->LoadContext(ctx);
|
2014-05-14 17:50:30 -07:00
|
|
|
}
|
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
/// Resets a thread
|
2014-05-22 16:06:12 -07:00
|
|
|
void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
|
2014-05-15 15:27:08 -07:00
|
|
|
memset(&t->context, 0, sizeof(ThreadContext));
|
2014-05-13 19:00:11 -07:00
|
|
|
|
2014-05-22 15:50:36 -07:00
|
|
|
t->context.cpu_registers[0] = arg;
|
2014-06-05 20:19:55 -07:00
|
|
|
t->context.pc = t->context.reg_15 = t->entry_point;
|
2014-05-15 15:27:08 -07:00
|
|
|
t->context.sp = t->stack_top;
|
2014-05-22 15:50:36 -07:00
|
|
|
t->context.cpsr = 0x1F; // Usermode
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-08-26 20:58:03 -07: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 15:27:08 -07:00
|
|
|
if (t->current_priority < lowest_priority) {
|
|
|
|
t->current_priority = t->initial_priority;
|
2014-05-13 19:29:31 -07:00
|
|
|
}
|
2014-05-15 15:27:08 -07:00
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-06-05 19:35:36 -07:00
|
|
|
t->wait_handle = 0;
|
2014-12-02 21:46:34 -08:00
|
|
|
t->wait_address = 0;
|
2014-05-13 19:00:11 -07:00
|
|
|
}
|
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
/// Change a thread to "ready" state
|
2014-05-22 16:06:12 -07:00
|
|
|
void ChangeReadyState(Thread* t, bool ready) {
|
2014-05-15 15:27:08 -07:00
|
|
|
Handle handle = t->GetHandle();
|
|
|
|
if (t->IsReady()) {
|
|
|
|
if (!ready) {
|
2014-11-18 05:48:11 -08:00
|
|
|
thread_ready_queue.remove(t->current_priority, handle);
|
2014-05-15 15:27:08 -07:00
|
|
|
}
|
|
|
|
} else if (ready) {
|
|
|
|
if (t->IsRunning()) {
|
2014-11-18 05:48:11 -08:00
|
|
|
thread_ready_queue.push_front(t->current_priority, handle);
|
2014-05-13 19:00:11 -07:00
|
|
|
} else {
|
2014-11-18 05:48:11 -08:00
|
|
|
thread_ready_queue.push_back(t->current_priority, handle);
|
2014-05-13 19:00:11 -07:00
|
|
|
}
|
2014-05-15 15:27:08 -07:00
|
|
|
t->status = THREADSTATUS_READY;
|
2014-05-13 19:00:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:35:36 -07:00
|
|
|
/// Verify that a thread has not been released from waiting
|
2014-12-02 21:46:34 -08:00
|
|
|
static bool VerifyWait(const Thread* thread, WaitType type, Handle wait_handle) {
|
2014-12-05 17:53:49 -08:00
|
|
|
_dbg_assert_(Kernel, thread != nullptr);
|
2014-11-25 21:38:50 -08:00
|
|
|
return (type == thread->wait_type) && (wait_handle == thread->wait_handle) && (thread->IsWaiting());
|
2014-06-05 19:35:36 -07:00
|
|
|
}
|
|
|
|
|
2014-12-02 21:46:34 -08:00
|
|
|
/// Verify that a thread has not been released from waiting (with wait address)
|
|
|
|
static bool VerifyWait(const Thread* thread, WaitType type, Handle wait_handle, VAddr wait_address) {
|
2014-12-05 17:53:49 -08:00
|
|
|
_dbg_assert_(Kernel, thread != nullptr);
|
2014-12-02 21:46:34 -08:00
|
|
|
return VerifyWait(thread, type, wait_handle) && (wait_address == thread->wait_address);
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:35:36 -07:00
|
|
|
/// Stops the current thread
|
2014-10-22 20:20:01 -07:00
|
|
|
ResultCode StopThread(Handle handle, const char* reason) {
|
|
|
|
Thread* thread = g_object_pool.Get<Thread>(handle);
|
|
|
|
if (thread == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-12-07 12:44:21 -08:00
|
|
|
// Release all the mutexes that this thread holds
|
|
|
|
ReleaseThreadMutexes(handle);
|
|
|
|
|
2014-06-09 19:14:03 -07:00
|
|
|
ChangeReadyState(thread, false);
|
|
|
|
thread->status = THREADSTATUS_DORMANT;
|
2014-11-24 09:51:50 -08:00
|
|
|
for (Handle waiting_handle : thread->waiting_threads) {
|
|
|
|
Thread* waiting_thread = g_object_pool.Get<Thread>(waiting_handle);
|
2014-06-09 19:14:03 -07:00
|
|
|
if (VerifyWait(waiting_thread, WAITTYPE_THREADEND, handle)) {
|
2014-11-24 09:51:50 -08:00
|
|
|
ResumeThreadFromWait(waiting_handle);
|
2014-06-05 19:35:36 -07:00
|
|
|
}
|
|
|
|
}
|
2014-06-09 19:14:03 -07:00
|
|
|
thread->waiting_threads.clear();
|
|
|
|
|
|
|
|
// Stopped threads are never waiting.
|
|
|
|
thread->wait_type = WAITTYPE_NONE;
|
|
|
|
thread->wait_handle = 0;
|
2014-12-02 21:46:34 -08:00
|
|
|
thread->wait_address = 0;
|
2014-10-22 20:20:01 -07:00
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
2014-06-05 19:35:36 -07:00
|
|
|
}
|
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
/// Changes a threads state
|
2014-05-22 16:06:12 -07:00
|
|
|
void ChangeThreadState(Thread* t, ThreadStatus new_status) {
|
2014-05-15 15:27:08 -07:00
|
|
|
if (!t || t->status == new_status) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 16:06:12 -07:00
|
|
|
ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
|
2014-05-15 15:27:08 -07:00
|
|
|
t->status = new_status;
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
if (new_status == THREADSTATUS_WAIT) {
|
|
|
|
if (t->wait_type == WAITTYPE_NONE) {
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_ERROR(Kernel, "Waittype none not allowed");
|
2014-05-15 15:27:08 -07:00
|
|
|
}
|
2014-05-13 19:00:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 19:48:19 -07:00
|
|
|
/// Arbitrate the highest priority thread that is waiting
|
|
|
|
Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) {
|
|
|
|
Handle highest_priority_thread = 0;
|
|
|
|
s32 priority = THREADPRIO_LOWEST;
|
|
|
|
|
|
|
|
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
2014-11-24 09:51:50 -08:00
|
|
|
for (Handle handle : thread_queue) {
|
|
|
|
Thread* thread = g_object_pool.Get<Thread>(handle);
|
2014-07-06 19:48:19 -07:00
|
|
|
|
2014-12-02 21:46:34 -08:00
|
|
|
if (!VerifyWait(thread, WAITTYPE_ARB, arbiter, address))
|
2014-07-06 19:48:19 -07:00
|
|
|
continue;
|
|
|
|
|
2014-10-22 20:20:01 -07:00
|
|
|
if (thread == nullptr)
|
|
|
|
continue; // TODO(yuriks): Thread handle will hang around forever. Should clean up.
|
2014-12-02 21:46:34 -08:00
|
|
|
|
2014-07-06 19:48:19 -07:00
|
|
|
if(thread->current_priority <= priority) {
|
|
|
|
highest_priority_thread = handle;
|
|
|
|
priority = thread->current_priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If a thread was arbitrated, resume it
|
|
|
|
if (0 != highest_priority_thread)
|
|
|
|
ResumeThreadFromWait(highest_priority_thread);
|
|
|
|
|
|
|
|
return highest_priority_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Arbitrate all threads currently waiting
|
|
|
|
void ArbitrateAllThreads(u32 arbiter, u32 address) {
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-07-06 19:48:19 -07:00
|
|
|
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
|
2014-11-24 09:51:50 -08:00
|
|
|
for (Handle handle : thread_queue) {
|
|
|
|
Thread* thread = g_object_pool.Get<Thread>(handle);
|
2014-07-06 19:48:19 -07:00
|
|
|
|
2014-12-02 21:46:34 -08:00
|
|
|
if (VerifyWait(thread, WAITTYPE_ARB, arbiter, address))
|
2014-07-06 19:48:19 -07:00
|
|
|
ResumeThreadFromWait(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 21:56:00 -07:00
|
|
|
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
|
2014-05-22 16:06:12 -07:00
|
|
|
void CallThread(Thread* t) {
|
2014-05-16 21:56:00 -07:00
|
|
|
// Stop waiting
|
|
|
|
if (t->wait_type != WAITTYPE_NONE) {
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
}
|
2014-05-22 16:06:12 -07:00
|
|
|
ChangeThreadState(t, THREADSTATUS_READY);
|
2014-05-20 16:37:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Switches CPU context to that of the specified thread
|
2014-05-22 16:32:45 -07:00
|
|
|
void SwitchContext(Thread* t) {
|
2014-05-22 16:06:12 -07:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
// Save context for current thread
|
|
|
|
if (cur) {
|
2014-05-22 16:06:12 -07:00
|
|
|
SaveContext(cur->context);
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
if (cur->IsRunning()) {
|
2014-05-22 16:06:12 -07:00
|
|
|
ChangeReadyState(cur, true);
|
2014-05-20 16:37:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load context of new thread
|
|
|
|
if (t) {
|
2014-05-22 16:06:12 -07:00
|
|
|
SetCurrentThread(t);
|
|
|
|
ChangeReadyState(t, false);
|
2014-05-20 16:37:46 -07:00
|
|
|
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-05-22 16:06:12 -07:00
|
|
|
LoadContext(t->context);
|
2014-05-20 16:37:46 -07:00
|
|
|
} else {
|
2014-06-05 21:35:49 -07:00
|
|
|
SetCurrentThread(nullptr);
|
2014-05-20 16:37:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the next thread that is ready to be run by priority
|
2014-05-22 16:06:12 -07:00
|
|
|
Thread* NextThread() {
|
2014-05-20 16:37:46 -07:00
|
|
|
Handle next;
|
2014-05-22 16:06:12 -07:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
if (cur && cur->IsRunning()) {
|
2014-11-18 05:48:11 -08:00
|
|
|
next = thread_ready_queue.pop_first_better(cur->current_priority);
|
2014-05-20 16:37:46 -07:00
|
|
|
} else {
|
2014-11-18 05:48:11 -08:00
|
|
|
next = thread_ready_queue.pop_first();
|
2014-05-20 16:37:46 -07:00
|
|
|
}
|
2014-05-21 18:42:18 -07:00
|
|
|
if (next == 0) {
|
2014-06-05 21:35:49 -07:00
|
|
|
return nullptr;
|
2014-05-20 16:37:46 -07:00
|
|
|
}
|
2014-10-22 20:20:01 -07:00
|
|
|
return Kernel::g_object_pool.Get<Thread>(next);
|
2014-05-20 16:37:46 -07:00
|
|
|
}
|
|
|
|
|
2014-06-05 19:35:36 -07:00
|
|
|
void WaitCurrentThread(WaitType wait_type, Handle wait_handle) {
|
2014-06-09 19:14:03 -07:00
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
thread->wait_type = wait_type;
|
|
|
|
thread->wait_handle = wait_handle;
|
|
|
|
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
|
2014-05-20 18:00:10 -07:00
|
|
|
}
|
|
|
|
|
2014-12-02 21:46:34 -08:00
|
|
|
void WaitCurrentThread(WaitType wait_type, Handle wait_handle, VAddr wait_address) {
|
|
|
|
WaitCurrentThread(wait_type, wait_handle);
|
|
|
|
GetCurrentThread()->wait_address = wait_address;
|
|
|
|
}
|
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
/// Resumes a thread from waiting by marking it as "ready"
|
2014-05-20 18:00:10 -07:00
|
|
|
void ResumeThreadFromWait(Handle handle) {
|
2014-10-22 20:20:01 -07:00
|
|
|
Thread* thread = Kernel::g_object_pool.Get<Thread>(handle);
|
2014-06-09 19:14:03 -07:00
|
|
|
if (thread) {
|
|
|
|
thread->status &= ~THREADSTATUS_WAIT;
|
|
|
|
if (!(thread->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
|
|
|
|
ChangeReadyState(thread, true);
|
2014-05-20 16:37:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:35:36 -07:00
|
|
|
/// Prints the thread queue for debugging purposes
|
|
|
|
void DebugThreadQueue() {
|
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
if (!thread) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_DEBUG(Kernel, "0x%02X 0x%08X (current)", thread->current_priority, GetCurrentThreadHandle());
|
2014-11-18 05:48:11 -08:00
|
|
|
for (u32 i = 0; i < thread_queue.size(); i++) {
|
|
|
|
Handle handle = thread_queue[i];
|
|
|
|
s32 priority = thread_ready_queue.contains(handle);
|
2014-06-05 19:35:36 -07:00
|
|
|
if (priority != -1) {
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_DEBUG(Kernel, "0x%02X 0x%08X", priority, handle);
|
2014-06-05 19:35:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 21:56:00 -07:00
|
|
|
/// Creates a new thread
|
2014-05-20 16:37:46 -07:00
|
|
|
Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority,
|
2014-05-16 21:56:00 -07:00
|
|
|
s32 processor_id, u32 stack_top, int stack_size) {
|
|
|
|
|
2014-11-19 00:49:13 -08:00
|
|
|
_assert_msg_(KERNEL, (priority >= THREADPRIO_HIGHEST && priority <= THREADPRIO_LOWEST),
|
2014-12-05 17:53:49 -08:00
|
|
|
"priority=%d, outside of allowable range!", priority)
|
2014-05-20 18:02:35 -07:00
|
|
|
|
2014-06-09 19:14:03 -07:00
|
|
|
Thread* thread = new Thread;
|
2014-06-05 19:35:36 -07:00
|
|
|
|
2014-06-09 19:14:03 -07:00
|
|
|
handle = Kernel::g_object_pool.Create(thread);
|
2014-06-05 19:35:36 -07:00
|
|
|
|
2014-11-18 05:48:11 -08:00
|
|
|
thread_queue.push_back(handle);
|
|
|
|
thread_ready_queue.prepare(priority);
|
2014-06-05 19:35:36 -07:00
|
|
|
|
2014-12-04 11:59:56 -08:00
|
|
|
thread->thread_id = next_thread_id++;
|
2014-06-09 19:14:03 -07: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;
|
|
|
|
thread->wait_type = WAITTYPE_NONE;
|
|
|
|
thread->wait_handle = 0;
|
2014-12-02 21:46:34 -08:00
|
|
|
thread->wait_address = 0;
|
2014-08-17 20:03:22 -07:00
|
|
|
thread->name = name;
|
2014-06-05 19:35:36 -07:00
|
|
|
|
2014-06-09 19:14:03 -07:00
|
|
|
return thread;
|
2014-05-16 21:56:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new thread - wrapper for external user
|
2014-05-22 15:50:36 -07:00
|
|
|
Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id,
|
2014-05-16 21:56:00 -07:00
|
|
|
u32 stack_top, int stack_size) {
|
2014-06-05 19:35:36 -07:00
|
|
|
|
2014-06-05 21:35:49 -07:00
|
|
|
if (name == nullptr) {
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_ERROR(Kernel_SVC, "nullptr name");
|
2014-05-16 21:56:00 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((u32)stack_size < 0x200) {
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_ERROR(Kernel_SVC, "(name=%s): invalid stack_size=0x%08X", name,
|
2014-05-16 21:56:00 -07:00
|
|
|
stack_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
|
|
|
|
s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_WARNING(Kernel_SVC, "(name=%s): invalid priority=%d, clamping to %d",
|
2014-05-16 21:56:00 -07: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-12-05 17:53:49 -08:00
|
|
|
LOG_ERROR(Kernel_SVC, "(name=%s): invalid entry %08x", name, entry_point);
|
2014-05-16 21:56:00 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Handle handle;
|
2014-11-19 00:49:13 -08:00
|
|
|
Thread* thread = CreateThread(handle, name, entry_point, priority, processor_id, stack_top,
|
2014-05-16 21:56:00 -07:00
|
|
|
stack_size);
|
|
|
|
|
2014-06-09 19:14:03 -07:00
|
|
|
ResetThread(thread, arg, 0);
|
|
|
|
CallThread(thread);
|
2014-06-01 18:42:50 -07:00
|
|
|
|
2014-05-16 21:56:00 -07:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-06-01 19:12:54 -07:00
|
|
|
/// Get the priority of the thread specified by handle
|
2014-10-22 20:20:01 -07:00
|
|
|
ResultVal<u32> GetThreadPriority(const Handle handle) {
|
|
|
|
Thread* thread = g_object_pool.Get<Thread>(handle);
|
|
|
|
if (thread == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
|
|
|
|
|
|
|
return MakeResult<u32>(thread->current_priority);
|
2014-06-01 19:12:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the priority of the thread specified by handle
|
2014-10-22 20:20:01 -07:00
|
|
|
ResultCode SetThreadPriority(Handle handle, s32 priority) {
|
2014-06-05 21:35:49 -07:00
|
|
|
Thread* thread = nullptr;
|
2014-06-01 19:12:54 -07:00
|
|
|
if (!handle) {
|
|
|
|
thread = GetCurrentThread(); // TODO(bunnei): Is this correct behavior?
|
|
|
|
} else {
|
2014-10-22 20:20:01 -07:00
|
|
|
thread = g_object_pool.Get<Thread>(handle);
|
|
|
|
if (thread == nullptr) {
|
|
|
|
return InvalidHandle(ErrorModule::Kernel);
|
|
|
|
}
|
2014-06-01 19:12:54 -07:00
|
|
|
}
|
2014-06-05 21:35:49 -07:00
|
|
|
_assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!");
|
2014-06-01 19:12:54 -07: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-05 17:53:49 -08:00
|
|
|
LOG_WARNING(Kernel_SVC, "invalid priority=%d, clamping to %d", priority, new_priority);
|
2014-06-01 19:12:54 -07:00
|
|
|
// TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
|
|
|
|
// validity of this
|
|
|
|
priority = new_priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change thread priority
|
|
|
|
s32 old = thread->current_priority;
|
2014-11-18 05:48:11 -08:00
|
|
|
thread_ready_queue.remove(old, handle);
|
2014-06-01 19:12:54 -07:00
|
|
|
thread->current_priority = priority;
|
2014-11-18 05:48:11 -08:00
|
|
|
thread_ready_queue.prepare(thread->current_priority);
|
2014-06-01 19:12:54 -07:00
|
|
|
|
|
|
|
// Change thread status to "ready" and push to ready queue
|
|
|
|
if (thread->IsRunning()) {
|
|
|
|
thread->status = (thread->status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY;
|
|
|
|
}
|
|
|
|
if (thread->IsReady()) {
|
2014-11-18 05:48:11 -08:00
|
|
|
thread_ready_queue.push_back(thread->current_priority, handle);
|
2014-06-01 19:12:54 -07:00
|
|
|
}
|
|
|
|
|
2014-10-22 20:20:01 -07:00
|
|
|
return RESULT_SUCCESS;
|
2014-06-01 19:12:54 -07:00
|
|
|
}
|
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
/// Sets up the primary application thread
|
2014-05-20 16:37:46 -07:00
|
|
|
Handle SetupMainThread(s32 priority, int stack_size) {
|
2014-05-15 15:27:08 -07:00
|
|
|
Handle handle;
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
// Initialize new "main" thread
|
2014-11-19 00:49:13 -08:00
|
|
|
Thread* thread = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority,
|
2014-05-16 21:56:00 -07:00
|
|
|
THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-06-09 19:14:03 -07:00
|
|
|
ResetThread(thread, 0, 0);
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
// If running another thread already, set it to "ready" state
|
2014-05-22 16:06:12 -07:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-05-15 15:27:08 -07:00
|
|
|
if (cur && cur->IsRunning()) {
|
2014-05-22 16:06:12 -07:00
|
|
|
ChangeReadyState(cur, true);
|
2014-05-14 17:50:30 -07:00
|
|
|
}
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
// Run new "main" thread
|
2014-06-09 19:14:03 -07:00
|
|
|
SetCurrentThread(thread);
|
|
|
|
thread->status = THREADSTATUS_RUNNING;
|
|
|
|
LoadContext(thread->context);
|
2014-05-14 17:50:30 -07:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
return handle;
|
|
|
|
}
|
2014-05-14 17:50:30 -07:00
|
|
|
|
2014-06-05 19:35:36 -07:00
|
|
|
|
2014-05-15 15:27:08 -07:00
|
|
|
/// Reschedules to the next available thread (call after current thread is suspended)
|
2014-05-22 16:32:45 -07:00
|
|
|
void Reschedule() {
|
2014-05-22 16:06:12 -07:00
|
|
|
Thread* prev = GetCurrentThread();
|
|
|
|
Thread* next = NextThread();
|
2014-06-05 19:35:36 -07:00
|
|
|
HLE::g_reschedule = false;
|
2014-05-15 15:27:08 -07:00
|
|
|
if (next > 0) {
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_TRACE(Kernel, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle());
|
2014-11-19 00:49:13 -08:00
|
|
|
|
2014-05-22 16:32:45 -07:00
|
|
|
SwitchContext(next);
|
2014-05-15 15:27:08 -07:00
|
|
|
|
2014-06-05 19:35:36 -07:00
|
|
|
// Hack - There is no mechanism yet to waken the primary thread if it has been put to sleep
|
|
|
|
// by a simulated VBLANK thread switch. So, we'll just immediately set it to "ready" again.
|
2014-11-19 00:49:13 -08:00
|
|
|
// This results in the current thread yielding on a VBLANK once, and then it will be
|
2014-06-05 19:35:36 -07:00
|
|
|
// immediately placed back in the queue for execution.
|
|
|
|
if (prev->wait_type == WAITTYPE_VBLANK) {
|
|
|
|
ResumeThreadFromWait(prev->GetHandle());
|
|
|
|
}
|
2014-05-22 15:50:36 -07:00
|
|
|
}
|
2014-05-16 21:56:00 -07:00
|
|
|
}
|
|
|
|
|
2014-12-03 20:22:06 -08:00
|
|
|
ResultCode GetThreadId(u32* thread_id, Handle handle) {
|
|
|
|
Thread* thread = g_object_pool.Get<Thread>(handle);
|
|
|
|
if (thread == nullptr)
|
|
|
|
return ResultCode(ErrorDescription::InvalidHandle, ErrorModule::OS,
|
|
|
|
ErrorSummary::WrongArgument, ErrorLevel::Permanent);
|
|
|
|
|
|
|
|
*thread_id = thread->thread_id;
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-05-16 21:56:00 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-05-15 15:27:08 -07:00
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
void ThreadingInit() {
|
2014-12-04 05:13:53 -08:00
|
|
|
next_thread_id = INITIAL_THREAD_ID;
|
2014-05-13 19:00:11 -07:00
|
|
|
}
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2014-05-20 16:37:46 -07:00
|
|
|
void ThreadingShutdown() {
|
2014-05-13 19:00:11 -07:00
|
|
|
}
|
2014-05-20 16:37:46 -07:00
|
|
|
|
|
|
|
} // namespace
|