From d5ab36175ed54cd3335146751b0928878dba5f8f Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 15 May 2023 20:31:54 +0200 Subject: SetCurrentThreadName now sets thread name in trace --- src/zencore/thread.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/zencore/thread.cpp') diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp index 86609c210..fe81cc786 100644 --- a/src/zencore/thread.cpp +++ b/src/zencore/thread.cpp @@ -35,6 +35,8 @@ # include #endif +#include + #include ZEN_THIRD_PARTY_INCLUDES_START @@ -94,6 +96,11 @@ const bool bNoZombieChildren = []() { void SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName) { + std::string ThreadNameZ{ThreadName}; + const int ThreadId = GetCurrentThreadId(); + + trace::ThreadRegister(ThreadNameZ.c_str(), /* system id */ ThreadId, /* sort id */ 0); + #if ZEN_PLATFORM_WINDOWS // The SetThreadDescription API works even if no debugger is attached. static auto SetThreadDescriptionFunc = @@ -103,20 +110,17 @@ SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName) { SetThreadDescriptionFunc(::GetCurrentThread(), Utf8ToWide(ThreadName).c_str()); } + // The debugger needs to be around to catch the name in the exception. If // there isn't a debugger, we are just needlessly throwing an exception. - if (!::IsDebuggerPresent()) - return; - - std::string ThreadNameZ{ThreadName}; - SetNameInternal(GetCurrentThreadId(), ThreadNameZ.c_str()); -#else - std::string ThreadNameZ{ThreadName}; -# if ZEN_PLATFORM_MAC + if (::IsDebuggerPresent()) + { + SetNameInternal(ThreadId, ThreadNameZ.c_str()); + } +#elif ZEN_PLATFORM_MAC pthread_setname_np(ThreadNameZ.c_str()); -# else +#else pthread_setname_np(pthread_self(), ThreadNameZ.c_str()); -# endif #endif } // namespace zen -- cgit v1.2.3