Thread: Convert some uses of handles to pointers

This commit is contained in:
Yuri Kunde Schlesner 2014-12-22 11:36:36 -02:00
parent 333557152c
commit 4637df2721
5 changed files with 20 additions and 17 deletions

View file

@ -13,7 +13,7 @@
namespace Kernel { namespace Kernel {
Handle g_main_thread = 0; Thread* g_main_thread = nullptr;
HandleTable g_handle_table; HandleTable g_handle_table;
u64 g_program_id = 0; u64 g_program_id = 0;

View file

@ -16,6 +16,8 @@ const Handle INVALID_HANDLE = 0;
namespace Kernel { namespace Kernel {
class Thread;
// TODO: Verify code // TODO: Verify code
const ResultCode ERR_OUT_OF_HANDLES(ErrorDescription::OutOfMemory, ErrorModule::Kernel, const ResultCode ERR_OUT_OF_HANDLES(ErrorDescription::OutOfMemory, ErrorModule::Kernel,
ErrorSummary::OutOfResource, ErrorLevel::Temporary); ErrorSummary::OutOfResource, ErrorLevel::Temporary);
@ -189,7 +191,7 @@ private:
}; };
extern HandleTable g_handle_table; extern HandleTable g_handle_table;
extern Handle g_main_thread; extern Thread* g_main_thread;
/// The ID code of the currently running game /// The ID code of the currently running game
/// TODO(Subv): This variable should not be here, /// TODO(Subv): This variable should not be here,

View file

@ -290,7 +290,7 @@ void DebugThreadQueue() {
} }
} }
ResultVal<Handle> Thread::Create(const char* name, u32 entry_point, s32 priority, u32 arg, ResultVal<Thread*> Thread::Create(const char* name, u32 entry_point, s32 priority, u32 arg,
s32 processor_id, u32 stack_top, int stack_size) { s32 processor_id, u32 stack_top, int stack_size) {
_dbg_assert_(Kernel, name != nullptr); _dbg_assert_(Kernel, name != nullptr);
@ -319,6 +319,7 @@ ResultVal<Handle> Thread::Create(const char* name, u32 entry_point, s32 priority
Thread* thread = new Thread; Thread* thread = new Thread;
// TODO(yuriks): Thread requires a handle to be inserted into the various scheduling queues for now.
ResultVal<Handle> handle = Kernel::g_handle_table.Create(thread); ResultVal<Handle> handle = Kernel::g_handle_table.Create(thread);
// TODO(yuriks): Plug memory leak // TODO(yuriks): Plug memory leak
if (handle.Failed()) if (handle.Failed())
@ -342,7 +343,7 @@ ResultVal<Handle> Thread::Create(const char* name, u32 entry_point, s32 priority
ResetThread(thread, arg, 0); ResetThread(thread, arg, 0);
CallThread(thread); CallThread(thread);
return MakeResult<Handle>(*handle); return MakeResult<Thread*>(thread);
} }
/// Set the priority of the thread specified by handle /// Set the priority of the thread specified by handle
@ -372,14 +373,13 @@ void Thread::SetPriority(s32 priority) {
} }
/// Sets up the primary application thread /// Sets up the primary application thread
Handle SetupMainThread(s32 priority, int stack_size) { Thread* SetupMainThread(s32 priority, int stack_size) {
// Initialize new "main" thread // Initialize new "main" thread
ResultVal<Handle> handle = Thread::Create("main", Core::g_app_core->GetPC(), priority, 0, ResultVal<Thread*> thread_res = Thread::Create("main", Core::g_app_core->GetPC(), priority, 0,
THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size); THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
// TODO(yuriks): Propagate error // TODO(yuriks): Propagate error
_dbg_assert_(Kernel, handle.Succeeded()); _dbg_assert_(Kernel, thread_res.Succeeded());
Thread* thread = *thread_res;
Thread* thread = Kernel::g_handle_table.Get<Thread>(*handle);
// If running another thread already, set it to "ready" state // If running another thread already, set it to "ready" state
Thread* cur = GetCurrentThread(); Thread* cur = GetCurrentThread();
@ -392,7 +392,7 @@ Handle SetupMainThread(s32 priority, int stack_size) {
thread->status = THREADSTATUS_RUNNING; thread->status = THREADSTATUS_RUNNING;
LoadContext(thread->context); LoadContext(thread->context);
return *handle; return thread;
} }

View file

@ -53,7 +53,7 @@ namespace Kernel {
class Thread : public Kernel::Object { class Thread : public Kernel::Object {
public: public:
static ResultVal<Handle> Create(const char* name, u32 entry_point, s32 priority, u32 arg, static ResultVal<Thread*> Create(const char* name, u32 entry_point, s32 priority, u32 arg,
s32 processor_id, u32 stack_top, int stack_size = Kernel::DEFAULT_STACK_SIZE); s32 processor_id, u32 stack_top, int stack_size = Kernel::DEFAULT_STACK_SIZE);
std::string GetName() const override { return name; } std::string GetName() const override { return name; }
@ -102,7 +102,7 @@ private:
}; };
/// Sets up the primary application thread /// Sets up the primary application thread
Handle SetupMainThread(s32 priority, int stack_size=Kernel::DEFAULT_STACK_SIZE); Thread* SetupMainThread(s32 priority, int stack_size = Kernel::DEFAULT_STACK_SIZE);
/// Reschedules to the next available thread (call after current thread is suspended) /// Reschedules to the next available thread (call after current thread is suspended)
void Reschedule(); void Reschedule();

View file

@ -229,16 +229,17 @@ static Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top
name = Common::StringFromFormat("unknown-%08x", entry_point); name = Common::StringFromFormat("unknown-%08x", entry_point);
} }
ResultVal<Handle> thread = Kernel::Thread::Create(name.c_str(), entry_point, priority, arg, ResultVal<Kernel::Thread*> thread_res = Kernel::Thread::Create(name.c_str(), entry_point, priority, arg,
processor_id, stack_top); processor_id, stack_top);
if (thread.Failed()) if (thread_res.Failed())
return thread.Code().raw; return thread_res.Code().raw;
Kernel::Thread* thread = *thread_res;
Core::g_app_core->SetReg(1, *thread); Core::g_app_core->SetReg(1, thread->GetHandle());
LOG_TRACE(Kernel_SVC, "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, " LOG_TRACE(Kernel_SVC, "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, "
"threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point, "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point,
name.c_str(), arg, stack_top, priority, processor_id, *thread); name.c_str(), arg, stack_top, priority, processor_id, thread->GetHandle());
return 0; return 0;
} }