From bd40d4f237994dbfc731f46b7a5982e87d8893f0 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 7 Dec 2019 14:02:07 -0800 Subject: [PATCH] Turn on fs.mitm multithreading --- .../ams_mitm/source/fs_mitm/fsmitm_module.cpp | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.cpp b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.cpp index 4f7be98d5..4cf9e14d2 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.cpp @@ -32,14 +32,55 @@ namespace ams::mitm::fs { constexpr size_t MaxSessions = 61; sf::hipc::ServerManager g_server_manager; + + constexpr size_t TotalThreads = 5; + static_assert(TotalThreads >= 1, "TotalThreads"); + constexpr size_t NumExtraThreads = TotalThreads - 1; + constexpr size_t ThreadStackSize = mitm::ModuleTraits::StackSize; + alignas(os::MemoryPageSize) u8 g_extra_thread_stacks[NumExtraThreads][ThreadStackSize]; + + os::Thread g_extra_threads[NumExtraThreads];; + + void LoopServerThread(void *arg) { + /* Loop forever, servicing our services. */ + g_server_manager.LoopProcess(); + } + + void ProcessForServerOnAllThreads() { + /* Initialize threads. */ + if constexpr (NumExtraThreads > 0) { + const u32 priority = os::GetCurrentThreadPriority(); + for (size_t i = 0; i < NumExtraThreads; i++) { + R_ASSERT(g_extra_threads[i].Initialize(LoopServerThread, nullptr, g_extra_thread_stacks[i], ThreadStackSize, priority)); + } + } + + /* Start extra threads. */ + if constexpr (NumExtraThreads > 0) { + for (size_t i = 0; i < NumExtraThreads; i++) { + R_ASSERT(g_extra_threads[i].Start()); + } + } + + /* Loop this thread. */ + LoopServerThread(nullptr); + + /* Wait for extra threads to finish. */ + if constexpr (NumExtraThreads > 0) { + for (size_t i = 0; i < NumExtraThreads; i++) { + R_ASSERT(g_extra_threads[i].Join()); + } + } + } + } void MitmModule::ThreadFunction(void *arg) { /* Create fs mitm. */ R_ASSERT(g_server_manager.RegisterMitmServer(MitmServiceName)); - /* Loop forever, servicing our services. */ - g_server_manager.LoopProcess(); + /* Process for the server. */ + ProcessForServerOnAllThreads(); } }