diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 0a8454fee..9ac4a5adc 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -13,7 +13,7 @@ namespace Kernel { -Handle g_main_thread = 0; +Thread* g_main_thread = nullptr; HandleTable g_handle_table; u64 g_program_id = 0; diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 7f86fd07d..244527154 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -16,6 +16,8 @@ const Handle INVALID_HANDLE = 0; namespace Kernel { +class Thread; + // TODO: Verify code const ResultCode ERR_OUT_OF_HANDLES(ErrorDescription::OutOfMemory, ErrorModule::Kernel, ErrorSummary::OutOfResource, ErrorLevel::Temporary); @@ -189,7 +191,7 @@ private: }; extern HandleTable g_handle_table; -extern Handle g_main_thread; +extern Thread* g_main_thread; /// The ID code of the currently running game /// TODO(Subv): This variable should not be here, diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 7a235a64b..f39c497d0 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -290,7 +290,7 @@ void DebugThreadQueue() { } } -ResultVal Thread::Create(const char* name, u32 entry_point, s32 priority, u32 arg, +ResultVal Thread::Create(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id, u32 stack_top, int stack_size) { _dbg_assert_(Kernel, name != nullptr); @@ -319,6 +319,7 @@ ResultVal Thread::Create(const char* name, u32 entry_point, s32 priority Thread* thread = new Thread; + // TODO(yuriks): Thread requires a handle to be inserted into the various scheduling queues for now. ResultVal handle = Kernel::g_handle_table.Create(thread); // TODO(yuriks): Plug memory leak if (handle.Failed()) @@ -342,7 +343,7 @@ ResultVal Thread::Create(const char* name, u32 entry_point, s32 priority ResetThread(thread, arg, 0); CallThread(thread); - return MakeResult(*handle); + return MakeResult(thread); } /// Set the priority of the thread specified by handle @@ -372,14 +373,13 @@ void Thread::SetPriority(s32 priority) { } /// Sets up the primary application thread -Handle SetupMainThread(s32 priority, int stack_size) { +Thread* SetupMainThread(s32 priority, int stack_size) { // Initialize new "main" thread - ResultVal handle = Thread::Create("main", Core::g_app_core->GetPC(), priority, 0, + ResultVal thread_res = Thread::Create("main", Core::g_app_core->GetPC(), priority, 0, THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size); // TODO(yuriks): Propagate error - _dbg_assert_(Kernel, handle.Succeeded()); - - Thread* thread = Kernel::g_handle_table.Get(*handle); + _dbg_assert_(Kernel, thread_res.Succeeded()); + Thread* thread = *thread_res; // If running another thread already, set it to "ready" state Thread* cur = GetCurrentThread(); @@ -392,7 +392,7 @@ Handle SetupMainThread(s32 priority, int stack_size) { thread->status = THREADSTATUS_RUNNING; LoadContext(thread->context); - return *handle; + return thread; } diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 3fdb27965..27f85f53a 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -53,7 +53,7 @@ namespace Kernel { class Thread : public Kernel::Object { public: - static ResultVal Create(const char* name, u32 entry_point, s32 priority, u32 arg, + static ResultVal Create(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id, u32 stack_top, int stack_size = Kernel::DEFAULT_STACK_SIZE); std::string GetName() const override { return name; } @@ -102,7 +102,7 @@ private: }; /// 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) void Reschedule(); diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index 760bcb878..a2a46ce6a 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -229,16 +229,17 @@ static Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top name = Common::StringFromFormat("unknown-%08x", entry_point); } - ResultVal thread = Kernel::Thread::Create(name.c_str(), entry_point, priority, arg, + ResultVal thread_res = Kernel::Thread::Create(name.c_str(), entry_point, priority, arg, processor_id, stack_top); - if (thread.Failed()) - return thread.Code().raw; + if (thread_res.Failed()) + 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, " "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; }