2014-05-10 03:11:18 +01:00
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <list>
|
2014-06-06 03:35:36 +01:00
|
|
|
#include <algorithm>
|
2014-05-10 03:11:18 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#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-05-14 03:00:11 +01:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/mem_map.h"
|
2014-05-15 01:50:30 +01:00
|
|
|
#include "core/hle/hle.h"
|
2014-05-20 23:28:38 +01:00
|
|
|
#include "core/hle/svc.h"
|
2014-05-10 03:11:18 +01:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
namespace Kernel {
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-20 23:13:25 +01:00
|
|
|
class Thread : public Kernel::Object {
|
2014-05-10 03:11:18 +01:00
|
|
|
public:
|
2014-05-15 23:27:08 +01:00
|
|
|
|
2014-06-06 05:10:50 +01:00
|
|
|
const char* GetName() const { return name; }
|
|
|
|
const char* GetTypeName() const { return "Thread"; }
|
2014-05-15 23:27:08 +01:00
|
|
|
|
2014-06-06 05:10:50 +01:00
|
|
|
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Thread; }
|
2014-05-20 23:13:25 +01:00
|
|
|
Kernel::HandleType GetHandleType() const { return Kernel::HandleType::Thread; }
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-15 23:27:08 +01: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 03:11:18 +01:00
|
|
|
|
2014-05-27 03:17:49 +01:00
|
|
|
/**
|
|
|
|
* Wait for kernel object to synchronize
|
|
|
|
* @param wait Boolean wait set if current thread should wait as a result of sync operation
|
|
|
|
* @return Result of operation, 0 on success, otherwise error code
|
|
|
|
*/
|
|
|
|
Result WaitSynchronization(bool* wait) {
|
2014-06-06 03:35:36 +01:00
|
|
|
if (status != THREADSTATUS_DORMANT) {
|
|
|
|
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());
|
|
|
|
*wait = true;
|
|
|
|
}
|
2014-05-27 02:01:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-10 03:11:18 +01:00
|
|
|
ThreadContext context;
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
u32 status;
|
|
|
|
u32 entry_point;
|
|
|
|
u32 stack_top;
|
|
|
|
u32 stack_size;
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
s32 initial_priority;
|
|
|
|
s32 current_priority;
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
s32 processor_id;
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
WaitType wait_type;
|
2014-06-06 03:35:36 +01:00
|
|
|
Handle wait_handle;
|
|
|
|
|
|
|
|
std::vector<Handle> waiting_threads;
|
2014-05-10 03:11:18 +01:00
|
|
|
|
2014-05-20 23:13:25 +01:00
|
|
|
char name[Kernel::MAX_NAME_LENGTH + 1];
|
2014-05-10 03:11:18 +01:00
|
|
|
};
|
|
|
|
|
2014-05-14 03:00:11 +01:00
|
|
|
// Lists all thread ids that aren't deleted/etc.
|
2014-05-15 23:27:08 +01:00
|
|
|
std::vector<Handle> g_thread_queue;
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
// Lists only ready thread ids.
|
|
|
|
Common::ThreadQueueList<Handle> g_thread_ready_queue;
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
Handle g_current_thread_handle;
|
|
|
|
Thread* g_current_thread;
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
/// Gets the current thread
|
2014-05-23 00:06:12 +01:00
|
|
|
inline Thread* GetCurrentThread() {
|
2014-05-15 23:27:08 +01:00
|
|
|
return g_current_thread;
|
|
|
|
}
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-23 00:06:12 +01:00
|
|
|
/// Gets the current thread handle
|
|
|
|
Handle GetCurrentThreadHandle() {
|
|
|
|
return GetCurrentThread()->GetHandle();
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
/// Sets the current thread
|
2014-05-23 00:06:12 +01:00
|
|
|
inline void SetCurrentThread(Thread* t) {
|
2014-05-15 23:27:08 +01:00
|
|
|
g_current_thread = t;
|
|
|
|
g_current_thread_handle = t->GetHandle();
|
|
|
|
}
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Saves the current CPU context
|
2014-05-23 00:06:12 +01:00
|
|
|
void SaveContext(ThreadContext& ctx) {
|
2014-05-20 23:50:16 +01:00
|
|
|
Core::g_app_core->SaveContext(ctx);
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Loads a CPU context
|
2014-05-23 00:06:12 +01:00
|
|
|
void LoadContext(ThreadContext& ctx) {
|
2014-05-20 23:50:16 +01:00
|
|
|
Core::g_app_core->LoadContext(ctx);
|
2014-05-15 01:50:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Resets a thread
|
2014-05-23 00:06:12 +01:00
|
|
|
void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
|
2014-05-15 23:27:08 +01:00
|
|
|
memset(&t->context, 0, sizeof(ThreadContext));
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-22 23:50:36 +01:00
|
|
|
t->context.cpu_registers[0] = arg;
|
2014-06-06 04:19:55 +01:00
|
|
|
t->context.pc = t->context.reg_15 = 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-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
|
|
|
}
|
2014-05-15 23:27:08 +01:00
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-06-06 03:35:36 +01:00
|
|
|
t->wait_handle = 0;
|
2014-05-14 03:00:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Change a thread to "ready" state
|
2014-05-23 00:06:12 +01:00
|
|
|
void ChangeReadyState(Thread* t, bool ready) {
|
2014-05-15 23:27:08 +01: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-14 03:00:11 +01:00
|
|
|
} else {
|
2014-05-15 23:27:08 +01:00
|
|
|
g_thread_ready_queue.push_back(t->current_priority, handle);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:35:36 +01:00
|
|
|
/// Verify that a thread has not been released from waiting
|
|
|
|
inline bool VerifyWait(const Handle& thread, WaitType type, Handle handle) {
|
|
|
|
Handle wait_id = 0;
|
2014-06-07 01:05:16 +01:00
|
|
|
Thread* t = g_object_pool.GetFast<Thread>(thread);
|
2014-06-06 22:57:52 +01:00
|
|
|
if (t != nullptr && type == t->wait_type && handle == t->wait_handle) {
|
|
|
|
return true;
|
2014-06-06 03:35:36 +01:00
|
|
|
} else {
|
|
|
|
ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread);
|
2014-06-06 22:57:52 +01:00
|
|
|
return false;
|
2014-06-06 03:35:36 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stops the current thread
|
|
|
|
void StopThread(Handle thread, const char* reason) {
|
|
|
|
u32 error;
|
2014-06-07 01:05:16 +01:00
|
|
|
Thread* t = g_object_pool.Get<Thread>(thread, error);
|
2014-06-06 03:35:36 +01:00
|
|
|
if (t) {
|
|
|
|
ChangeReadyState(t, false);
|
|
|
|
t->status = THREADSTATUS_DORMANT;
|
|
|
|
for (size_t i = 0; i < t->waiting_threads.size(); ++i) {
|
|
|
|
const Handle waiting_thread = t->waiting_threads[i];
|
|
|
|
if (VerifyWait(waiting_thread, WAITTYPE_THREADEND, thread)) {
|
|
|
|
ResumeThreadFromWait(waiting_thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t->waiting_threads.clear();
|
|
|
|
|
|
|
|
// Stopped threads are never waiting.
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
t->wait_handle = 0;
|
|
|
|
} else {
|
|
|
|
ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Changes a threads state
|
2014-05-23 00:06:12 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (new_status == THREADSTATUS_WAIT) {
|
|
|
|
if (t->wait_type == WAITTYPE_NONE) {
|
2014-06-06 03:35:36 +01:00
|
|
|
ERROR_LOG(KERNEL, "Waittype none not allowed");
|
2014-05-15 23:27:08 +01:00
|
|
|
}
|
2014-05-14 03:00:11 +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-05-23 00:06:12 +01:00
|
|
|
void CallThread(Thread* t) {
|
2014-05-17 05:56:00 +01:00
|
|
|
// Stop waiting
|
|
|
|
if (t->wait_type != WAITTYPE_NONE) {
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
|
|
|
}
|
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-05-23 00:32:45 +01:00
|
|
|
void SwitchContext(Thread* t) {
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-05-21 00:37:46 +01:00
|
|
|
|
|
|
|
// Save context for current thread
|
|
|
|
if (cur) {
|
2014-05-23 00:06:12 +01:00
|
|
|
SaveContext(cur->context);
|
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-05-23 00:06:12 +01:00
|
|
|
SetCurrentThread(t);
|
|
|
|
ChangeReadyState(t, false);
|
2014-05-21 00:37:46 +01:00
|
|
|
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
|
|
|
t->wait_type = WAITTYPE_NONE;
|
2014-05-23 00:06:12 +01:00
|
|
|
LoadContext(t->context);
|
2014-05-21 00:37:46 +01:00
|
|
|
} else {
|
2014-06-06 05:35:49 +01:00
|
|
|
SetCurrentThread(nullptr);
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the next thread that is ready to be run by priority
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* NextThread() {
|
2014-05-21 00:37:46 +01:00
|
|
|
Handle next;
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* cur = GetCurrentThread();
|
2014-05-21 00:37:46 +01: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-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
|
|
|
}
|
|
|
|
return Kernel::g_object_pool.GetFast<Thread>(next);
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:36:56 +01:00
|
|
|
/// Puts the current thread in the wait state for the given type
|
2014-06-06 03:35:36 +01:00
|
|
|
void WaitCurrentThread(WaitType wait_type, Handle wait_handle) {
|
2014-05-23 00:06:12 +01:00
|
|
|
Thread* t = GetCurrentThread();
|
2014-05-21 02:00:10 +01:00
|
|
|
t->wait_type = wait_type;
|
2014-06-06 03:35:36 +01:00
|
|
|
t->wait_handle = wait_handle;
|
2014-05-23 00:06:12 +01:00
|
|
|
ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
|
2014-05-21 02:00:10 +01:00
|
|
|
}
|
|
|
|
|
2014-05-21 00:37:46 +01:00
|
|
|
/// Resumes a thread from waiting by marking it as "ready"
|
2014-05-21 02:00:10 +01:00
|
|
|
void ResumeThreadFromWait(Handle handle) {
|
2014-05-21 00:37:46 +01: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))) {
|
2014-05-23 00:06:12 +01:00
|
|
|
ChangeReadyState(t, true);
|
2014-05-21 00:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:35:36 +01:00
|
|
|
/// Prints the thread queue for debugging purposes
|
|
|
|
void DebugThreadQueue() {
|
|
|
|
Thread* thread = GetCurrentThread();
|
|
|
|
if (!thread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
INFO_LOG(KERNEL, "0x%02X 0x%08X (current)", thread->current_priority, GetCurrentThreadHandle());
|
|
|
|
for (u32 i = 0; i < g_thread_queue.size(); i++) {
|
|
|
|
Handle handle = g_thread_queue[i];
|
|
|
|
s32 priority = g_thread_ready_queue.contains(handle);
|
|
|
|
if (priority != -1) {
|
|
|
|
INFO_LOG(KERNEL, "0x%02X 0x%08X", priority, handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 05:56:00 +01:00
|
|
|
/// Creates a new thread
|
2014-05-21 00:37:46 +01:00
|
|
|
Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority,
|
2014-05-17 05:56:00 +01:00
|
|
|
s32 processor_id, u32 stack_top, int stack_size) {
|
|
|
|
|
2014-05-21 02:02:35 +01:00
|
|
|
_assert_msg_(KERNEL, (priority >= THREADPRIO_HIGHEST && priority <= THREADPRIO_LOWEST),
|
|
|
|
"CreateThread priority=%d, outside of allowable range!", priority)
|
|
|
|
|
2014-05-20 23:20:35 +01:00
|
|
|
Thread* t = new Thread;
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-05-20 23:13:25 +01:00
|
|
|
handle = Kernel::g_object_pool.Create(t);
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-05-17 05:56:00 +01:00
|
|
|
g_thread_queue.push_back(handle);
|
|
|
|
g_thread_ready_queue.prepare(priority);
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-05-17 05:56:00 +01:00
|
|
|
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-06-06 03:35:36 +01:00
|
|
|
t->wait_handle = 0;
|
|
|
|
|
2014-05-20 23:13:25 +01:00
|
|
|
strncpy(t->name, name, Kernel::MAX_NAME_LENGTH);
|
|
|
|
t->name[Kernel::MAX_NAME_LENGTH] = '\0';
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-05-17 05:56:00 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new thread - wrapper for external user
|
2014-05-22 23:50:36 +01:00
|
|
|
Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id,
|
2014-05-17 05:56:00 +01:00
|
|
|
u32 stack_top, int stack_size) {
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-06-06 05:35:49 +01:00
|
|
|
if (name == nullptr) {
|
|
|
|
ERROR_LOG(KERNEL, "CreateThread(): nullptr name");
|
2014-05-17 05:56:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((u32)stack_size < 0x200) {
|
2014-05-21 00:37:46 +01:00
|
|
|
ERROR_LOG(KERNEL, "CreateThread(name=%s): invalid stack_size=0x%08X", name,
|
2014-05-17 05:56:00 +01:00
|
|
|
stack_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
|
|
|
|
s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
|
2014-05-21 00:37:46 +01:00
|
|
|
WARN_LOG(KERNEL, "CreateThread(name=%s): invalid priority=0x%08X, clamping to %08X",
|
2014-05-17 05:56:00 +01: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-21 00:37:46 +01:00
|
|
|
ERROR_LOG(KERNEL, "CreateThread(name=%s): invalid entry %08x", name, entry_point);
|
2014-05-17 05:56:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Handle handle;
|
2014-05-21 00:37:46 +01:00
|
|
|
Thread* t = CreateThread(handle, name, entry_point, priority, processor_id, stack_top,
|
2014-05-17 05:56:00 +01:00
|
|
|
stack_size);
|
|
|
|
|
2014-05-23 00:06:12 +01:00
|
|
|
ResetThread(t, arg, 0);
|
2014-06-02 02:42:50 +01:00
|
|
|
CallThread(t);
|
|
|
|
|
2014-05-17 05:56:00 +01:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-06-02 03:12:54 +01:00
|
|
|
/// Get the priority of the thread specified by handle
|
|
|
|
u32 GetThreadPriority(const Handle handle) {
|
|
|
|
Thread* thread = g_object_pool.GetFast<Thread>(handle);
|
2014-06-06 05:35:49 +01:00
|
|
|
_assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!");
|
2014-06-02 03:12:54 +01:00
|
|
|
return thread->current_priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the priority of the thread specified by handle
|
|
|
|
Result SetThreadPriority(Handle handle, s32 priority) {
|
2014-06-06 05:35:49 +01:00
|
|
|
Thread* thread = nullptr;
|
2014-06-02 03:12:54 +01:00
|
|
|
if (!handle) {
|
|
|
|
thread = GetCurrentThread(); // TODO(bunnei): Is this correct behavior?
|
|
|
|
} else {
|
|
|
|
thread = g_object_pool.GetFast<Thread>(handle);
|
|
|
|
}
|
2014-06-06 05:35:49 +01:00
|
|
|
_assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!");
|
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);
|
|
|
|
WARN_LOG(KERNEL, "invalid priority=0x%08X, clamping to %08X", priority, new_priority);
|
|
|
|
// 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;
|
|
|
|
g_thread_ready_queue.remove(old, handle);
|
|
|
|
thread->current_priority = priority;
|
|
|
|
g_thread_ready_queue.prepare(thread->current_priority);
|
|
|
|
|
|
|
|
// Change thread status to "ready" and push to ready queue
|
|
|
|
if (thread->IsRunning()) {
|
|
|
|
thread->status = (thread->status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY;
|
|
|
|
}
|
|
|
|
if (thread->IsReady()) {
|
|
|
|
g_thread_ready_queue.push_back(thread->current_priority, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
/// Sets up the primary application thread
|
2014-05-21 00:37:46 +01:00
|
|
|
Handle SetupMainThread(s32 priority, int stack_size) {
|
2014-05-15 23:27:08 +01:00
|
|
|
Handle handle;
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
// Initialize new "main" thread
|
2014-05-21 00:37:46 +01:00
|
|
|
Thread* t = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority,
|
2014-05-17 05:56:00 +01:00
|
|
|
THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
|
2014-05-14 03:00:11 +01:00
|
|
|
|
2014-05-23 00:06:12 +01:00
|
|
|
ResetThread(t, 0, 0);
|
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-05-15 23:27:08 +01:00
|
|
|
|
|
|
|
// Run new "main" thread
|
2014-05-23 00:06:12 +01:00
|
|
|
SetCurrentThread(t);
|
2014-05-15 23:27:08 +01:00
|
|
|
t->status = THREADSTATUS_RUNNING;
|
2014-05-23 00:06:12 +01:00
|
|
|
LoadContext(t->context);
|
2014-05-15 01:50:30 +01:00
|
|
|
|
2014-05-15 23:27:08 +01:00
|
|
|
return handle;
|
|
|
|
}
|
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-05-15 23:27:08 +01:00
|
|
|
if (next > 0) {
|
2014-06-01 15:35:42 +01:00
|
|
|
INFO_LOG(KERNEL, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle());
|
2014-06-06 03:35:36 +01:00
|
|
|
|
2014-05-23 00:32:45 +01:00
|
|
|
SwitchContext(next);
|
2014-05-15 23:27:08 +01:00
|
|
|
|
2014-06-06 03:35:36 +01: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.
|
|
|
|
// This results in the current thread yielding on a VBLANK once, and then it will be
|
|
|
|
// immediately placed back in the queue for execution.
|
|
|
|
if (prev->wait_type == WAITTYPE_VBLANK) {
|
|
|
|
ResumeThreadFromWait(prev->GetHandle());
|
|
|
|
}
|
2014-05-22 23:50:36 +01: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-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
|