From ff5f376c331a8bac5a2df95035797c0631c1281e Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Fri, 15 Oct 2021 19:59:29 -0700 Subject: [PATCH] tipc: enable named-thread dispatch --- .../impl/ams_system_thread_definitions.hpp | 5 ++-- .../stratosphere/tipc/tipc_server_manager.hpp | 28 +++++++++++++++++-- stratosphere/sm/source/sm_tipc_server.cpp | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/libraries/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp b/libraries/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp index c183b21ef..f7684adb6 100644 --- a/libraries/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp +++ b/libraries/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp @@ -28,6 +28,7 @@ namespace ams::impl { /* sm. */ AMS_DEFINE_SYSTEM_THREAD(-1, sm, Main); + AMS_DEFINE_SYSTEM_THREAD(-1, sm, DispatcherThread); /* spl. */ AMS_DEFINE_SYSTEM_THREAD(-1, spl, Main); @@ -177,5 +178,5 @@ namespace ams::impl { } -#define AMS_GET_SYSTEM_THREAD_PRIORITY(__AMS_MODULE__, __AMS_THREAD_NAME__) ::ams::impl::SystemThreadDefinition_##__AMS_MODULE__##_##__AMS_THREAD_NAME__.priority -#define AMS_GET_SYSTEM_THREAD_NAME(__AMS_MODULE__, __AMS_THREAD_NAME__) ::ams::impl::SystemThreadDefinition_##__AMS_MODULE__##_##__AMS_THREAD_NAME__.name +#define AMS_GET_SYSTEM_THREAD_PRIORITY(__AMS_MODULE__, __AMS_THREAD_NAME__) ( ::ams::impl::SystemThreadDefinition_##__AMS_MODULE__##_##__AMS_THREAD_NAME__ ).priority +#define AMS_GET_SYSTEM_THREAD_NAME(__AMS_MODULE__, __AMS_THREAD_NAME__) ( ::ams::impl::SystemThreadDefinition_##__AMS_MODULE__##_##__AMS_THREAD_NAME__ ).name diff --git a/libraries/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp b/libraries/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp index 4ae593f94..dcdb6f599 100644 --- a/libraries/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp +++ b/libraries/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp @@ -379,10 +379,15 @@ namespace ams::tipc { } template - void InitializePortThread(s32 priority) { + void InitializePortThread(s32 priority, const char *name) { /* Create the thread. */ R_ABORT_UNLESS(os::CreateThread(m_port_threads + Ix, &LoopAutoForPortThreadFunction, this, s_port_stacks + Ix, ThreadStackSize, priority)); + /* Set the thread name pointer. */ + if (name != nullptr) { + os::SetThreadNamePointer(m_port_threads + Ix, name); + } + /* Start the thread. */ os::StartThread(m_port_threads + Ix); } @@ -420,7 +425,7 @@ namespace ams::tipc { [thread_priority, this](std::index_sequence) ALWAYS_INLINE_LAMBDA { /* Create all threads. */ - (this->InitializePortThread(thread_priority), ...); + (this->InitializePortThread(thread_priority, nullptr), ...); }(std::make_index_sequence()); } @@ -428,6 +433,25 @@ namespace ams::tipc { this->LoopAutoForPort(); } + void LoopAuto(int priority, const char *name) { + /* If we have additional threads, create and start them. */ + if constexpr (NumPorts > 1) { + [priority, name, this](std::index_sequence) ALWAYS_INLINE_LAMBDA { + /* Create all threads. */ + (this->InitializePortThread(priority, name), ...); + }(std::make_index_sequence()); + } + + /* Check current thread. */ + { + AMS_ASSERT(priority == os::GetThreadPriority(os::GetCurrentThread())); + /* N does not do: os::SetThreadNamePointer(os::GetCurrentThread(), name); */ + } + + /* Process for the last port. */ + this->LoopAutoForPort(); + } + tipc::ServiceObjectBase *AllocateObject(size_t port_index, os::NativeHandle handle, DeferralManagerBaseType &deferral_manager) { /* Check that the port index is valid. */ AMS_ABORT_UNLESS(port_index < NumPorts); diff --git a/stratosphere/sm/source/sm_tipc_server.cpp b/stratosphere/sm/source/sm_tipc_server.cpp index 12457cfe3..e207308c3 100644 --- a/stratosphere/sm/source/sm_tipc_server.cpp +++ b/stratosphere/sm/source/sm_tipc_server.cpp @@ -72,7 +72,7 @@ namespace ams::sm { void LoopProcessTipcServer() { /* Loop processing the server on all threads. */ - g_server_manager.LoopAuto(); + g_server_manager.LoopAuto(AMS_GET_SYSTEM_THREAD_PRIORITY(sm, DispatcherThread), AMS_GET_SYSTEM_THREAD_NAME(sm, DispatcherThread)); } void TriggerResume(sm::ServiceName service_name) {