From 771431f62539a991cc4d8cf5dc4908bb5b366da2 Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Thu, 27 Dec 2018 20:28:15 -0500
Subject: [PATCH] kernel/thread: Move process thread initialization into
 process.cpp

This function isn't a general purpose function that should be exposed to
everything, given it's specific to initializing the main thread for a
Process instance.

Given that, it's a tad bit more sensible to place this within
process.cpp, which keeps it visible only to the code that actually needs
it.
---
 src/core/hle/kernel/process.cpp | 31 ++++++++++++++++++++++++++++++-
 src/core/hle/kernel/thread.cpp  | 24 ------------------------
 src/core/hle/kernel/thread.h    | 11 -----------
 3 files changed, 30 insertions(+), 36 deletions(-)

diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp
index 4f209a979..81a23dfbf 100644
--- a/src/core/hle/kernel/process.cpp
+++ b/src/core/hle/kernel/process.cpp
@@ -20,6 +20,35 @@
 #include "core/settings.h"
 
 namespace Kernel {
+namespace {
+/**
+ * Sets up the primary application thread
+ *
+ * @param owner_process The parent process for the main thread
+ * @param kernel The kernel instance to create the main thread under.
+ * @param entry_point The address at which the thread should start execution
+ * @param priority The priority to give the main thread
+ */
+void SetupMainThread(Process& owner_process, KernelCore& kernel, VAddr entry_point, u32 priority) {
+    // Setup page table so we can write to memory
+    SetCurrentPageTable(&owner_process.VMManager().page_table);
+
+    // Initialize new "main" thread
+    const VAddr stack_top = owner_process.VMManager().GetTLSIORegionEndAddress();
+    auto thread_res = Thread::Create(kernel, "main", entry_point, priority, 0, THREADPROCESSORID_0,
+                                     stack_top, owner_process);
+
+    SharedPtr<Thread> thread = std::move(thread_res).Unwrap();
+
+    // Register 1 must be a handle to the main thread
+    const Handle guest_handle = owner_process.GetHandleTable().Create(thread).Unwrap();
+    thread->SetGuestHandle(guest_handle);
+    thread->GetContext().cpu_registers[1] = guest_handle;
+
+    // Threads by default are dormant, wake up the main thread so it runs when the scheduler fires
+    thread->ResumeFromWait();
+}
+} // Anonymous namespace
 
 CodeSet::CodeSet() = default;
 CodeSet::~CodeSet() = default;
@@ -86,7 +115,7 @@ void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) {
     vm_manager.LogLayout();
     ChangeStatus(ProcessStatus::Running);
 
-    Kernel::SetupMainThread(kernel, entry_point, main_thread_priority, *this);
+    SetupMainThread(*this, kernel, entry_point, main_thread_priority);
 }
 
 void Process::PrepareForTermination() {
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 434655638..d3984dfc4 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -12,7 +12,6 @@
 #include "common/assert.h"
 #include "common/common_types.h"
 #include "common/logging/log.h"
-#include "common/math_util.h"
 #include "common/thread_queue_list.h"
 #include "core/arm/arm_interface.h"
 #include "core/core.h"
@@ -232,29 +231,6 @@ void Thread::BoostPriority(u32 priority) {
     current_priority = priority;
 }
 
-SharedPtr<Thread> SetupMainThread(KernelCore& kernel, VAddr entry_point, u32 priority,
-                                  Process& owner_process) {
-    // Setup page table so we can write to memory
-    SetCurrentPageTable(&owner_process.VMManager().page_table);
-
-    // Initialize new "main" thread
-    const VAddr stack_top = owner_process.VMManager().GetTLSIORegionEndAddress();
-    auto thread_res = Thread::Create(kernel, "main", entry_point, priority, 0, THREADPROCESSORID_0,
-                                     stack_top, owner_process);
-
-    SharedPtr<Thread> thread = std::move(thread_res).Unwrap();
-
-    // Register 1 must be a handle to the main thread
-    const Handle guest_handle = owner_process.GetHandleTable().Create(thread).Unwrap();
-    thread->SetGuestHandle(guest_handle);
-    thread->GetContext().cpu_registers[1] = guest_handle;
-
-    // Threads by default are dormant, wake up the main thread so it runs when the scheduler fires
-    thread->ResumeFromWait();
-
-    return thread;
-}
-
 void Thread::SetWaitSynchronizationResult(ResultCode result) {
     context.cpu_registers[0] = result.raw;
 }
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index fe5398d56..cc68eed2f 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -455,17 +455,6 @@ private:
     ThreadActivity activity = ThreadActivity::Normal;
 };
 
-/**
- * Sets up the primary application thread
- * @param kernel The kernel instance to create the main thread under.
- * @param entry_point The address at which the thread should start execution
- * @param priority The priority to give the main thread
- * @param owner_process The parent process for the main thread
- * @return A shared pointer to the main thread
- */
-SharedPtr<Thread> SetupMainThread(KernelCore& kernel, VAddr entry_point, u32 priority,
-                                  Process& owner_process);
-
 /**
  * Gets the current thread
  */