From f93aea4c06f0f79ffb8d701defd4630785b512df Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 11 Oct 2023 09:57:58 -0700 Subject: [PATCH] kern: split Process/Thread exit to separate WorkerTaskManagers --- .../mesosphere/kern_k_worker_task_manager.hpp | 3 ++- libraries/libmesosphere/source/kern_k_process.cpp | 4 ++-- libraries/libmesosphere/source/kern_k_thread.cpp | 13 ++++++++----- libraries/libmesosphere/source/kern_main.cpp | 5 +++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_worker_task_manager.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_worker_task_manager.hpp index df0d81ff3..cb87f22b6 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_worker_task_manager.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_worker_task_manager.hpp @@ -25,7 +25,8 @@ namespace ams::kern { static constexpr s32 ExitWorkerPriority = 11; enum WorkerType { - WorkerType_Exit, + WorkerType_ExitThread, + WorkerType_ExitProcess, WorkerType_Count, }; diff --git a/libraries/libmesosphere/source/kern_k_process.cpp b/libraries/libmesosphere/source/kern_k_process.cpp index 9dbd3a4f4..07d122280 100644 --- a/libraries/libmesosphere/source/kern_k_process.cpp +++ b/libraries/libmesosphere/source/kern_k_process.cpp @@ -491,7 +491,7 @@ namespace ams::kern { MESOSPHERE_LOG("KProcess::Exit() pid=%ld name=%-12s\n", m_process_id, m_name); /* Register the process as a work task. */ - KWorkerTaskManager::AddTask(KWorkerTaskManager::WorkerType_Exit, this); + KWorkerTaskManager::AddTask(KWorkerTaskManager::WorkerType_ExitProcess, this); } /* Exit the current thread. */ @@ -536,7 +536,7 @@ namespace ams::kern { MESOSPHERE_LOG("KProcess::Terminate() FAIL pid=%ld name=%-12s\n", m_process_id, m_name); /* Register the process as a work task. */ - KWorkerTaskManager::AddTask(KWorkerTaskManager::WorkerType_Exit, this); + KWorkerTaskManager::AddTask(KWorkerTaskManager::WorkerType_ExitProcess, this); } } diff --git a/libraries/libmesosphere/source/kern_k_thread.cpp b/libraries/libmesosphere/source/kern_k_thread.cpp index 942e85b10..4ec7aa1dd 100644 --- a/libraries/libmesosphere/source/kern_k_thread.cpp +++ b/libraries/libmesosphere/source/kern_k_thread.cpp @@ -476,10 +476,6 @@ namespace ams::kern { m_parent->ClearRunningThread(this); } - /* Signal. */ - m_signaled = true; - KSynchronizationObject::NotifyAvailable(); - /* Call the on thread termination handler. */ KThreadContext::OnThreadTerminating(this); @@ -507,6 +503,13 @@ namespace ams::kern { cpu::SynchronizeCores(m_parent->GetPhysicalCoreMask()); } + /* Acquire the scheduler lock. */ + KScopedSchedulerLock sl; + + /* Signal. */ + m_signaled = true; + KSynchronizationObject::NotifyAvailable(); + /* Close the thread. */ this->Close(); } @@ -1328,7 +1331,7 @@ namespace ams::kern { this->StartTermination(); /* Register the thread as a work task. */ - KWorkerTaskManager::AddTask(KWorkerTaskManager::WorkerType_Exit, this); + KWorkerTaskManager::AddTask(KWorkerTaskManager::WorkerType_ExitThread, this); } MESOSPHERE_PANIC("KThread::Exit() would return"); diff --git a/libraries/libmesosphere/source/kern_main.cpp b/libraries/libmesosphere/source/kern_main.cpp index 14209eb1f..0f1782c2a 100644 --- a/libraries/libmesosphere/source/kern_main.cpp +++ b/libraries/libmesosphere/source/kern_main.cpp @@ -115,8 +115,9 @@ namespace ams::kern { /* Perform more core-0 specific initialization. */ if (core_id == 0) { - /* Initialize the exit worker manager, so that threads and processes may exit cleanly. */ - Kernel::GetWorkerTaskManager(KWorkerTaskManager::WorkerType_Exit).Initialize(KWorkerTaskManager::ExitWorkerPriority); + /* Initialize the exit worker managers, so that threads and processes may exit cleanly. */ + Kernel::GetWorkerTaskManager(KWorkerTaskManager::WorkerType_ExitThread).Initialize(KWorkerTaskManager::ExitWorkerPriority); + Kernel::GetWorkerTaskManager(KWorkerTaskManager::WorkerType_ExitProcess).Initialize(KWorkerTaskManager::ExitWorkerPriority); /* Setup so that we may sleep later, and reserve memory for secure applets. */ KSystemControl::InitializePhase2();