Fix Thread::setThreadName. (#358)

This commit is contained in:
Branimir Karadžić
2025-12-16 18:36:31 -08:00
committed by GitHub
parent a69b0a72f7
commit ce31b14454
2 changed files with 31 additions and 41 deletions

View File

@@ -6,8 +6,8 @@
#ifndef BX_THREAD_H_HEADER_GUARD #ifndef BX_THREAD_H_HEADER_GUARD
#define BX_THREAD_H_HEADER_GUARD #define BX_THREAD_H_HEADER_GUARD
#include "allocator.h"
#include "mpscqueue.h" #include "mpscqueue.h"
#include "string.h"
#if BX_CONFIG_SUPPORTS_THREADING #if BX_CONFIG_SUPPORTS_THREADING
@@ -39,7 +39,7 @@ namespace bx
/// @param[in] _name Thread name used by debugger. /// @param[in] _name Thread name used by debugger.
/// @returns True if thread is created, otherwise returns false. /// @returns True if thread is created, otherwise returns false.
/// ///
bool init(ThreadFn _fn, void* _userData = NULL, uint32_t _stackSize = 0, const char* _name = NULL); bool init(ThreadFn _fn, void* _userData = NULL, uint32_t _stackSize = 0, const StringView& _name = "");
/// ///
void shutdown(); void shutdown();
@@ -51,7 +51,7 @@ namespace bx
int32_t getExitCode() const; int32_t getExitCode() const;
/// ///
void setThreadName(const char* _name); void setThreadName(const StringView& _name);
/// ///
void push(void* _ptr); void push(void* _ptr);
@@ -72,7 +72,7 @@ namespace bx
uint32_t m_stackSize; uint32_t m_stackSize;
int32_t m_exitCode; int32_t m_exitCode;
bool m_running; bool m_running;
char m_name[64]; FixedString64 m_name;
}; };
/// ///

View File

@@ -109,23 +109,14 @@ namespace bx
} }
} }
bool Thread::init(ThreadFn _fn, void* _userData, uint32_t _stackSize, const char* _name) bool Thread::init(ThreadFn _fn, void* _userData, uint32_t _stackSize, const StringView& _name)
{ {
BX_ASSERT(!m_running, "Already running!"); BX_ASSERT(!m_running, "Already running!");
m_fn = _fn; m_fn = _fn;
m_userData = _userData; m_userData = _userData;
m_stackSize = _stackSize; m_stackSize = _stackSize;
m_name = _name;
if (NULL != _name)
{
BX_WARN(strLen(_name) < int32_t(BX_COUNTOF(m_name) )-1, "Truncating thread name.");
strCopy(m_name, BX_COUNTOF(m_name), _name);
}
else
{
m_name[0] = '\0';
}
ThreadInternal* ti = (ThreadInternal*)m_internal; ThreadInternal* ti = (ThreadInternal*)m_internal;
#if BX_CRT_NONE #if BX_CRT_NONE
@@ -223,40 +214,40 @@ namespace bx
return m_exitCode; return m_exitCode;
} }
void Thread::setThreadName(const char* _name) void Thread::setThreadName(const StringView& _name)
{ {
if (NULL == _name if (_name.isEmpty() )
|| 0 == strLen(_name) )
{ {
return; return;
} }
m_name = _name;
ThreadInternal* ti = (ThreadInternal*)m_internal; ThreadInternal* ti = (ThreadInternal*)m_internal;
BX_UNUSED(ti); BX_UNUSED(ti);
#if BX_CRT_NONE #if BX_PLATFORM_OSX \
BX_UNUSED(_name);
#elif BX_PLATFORM_OSX \
|| BX_PLATFORM_IOS \ || BX_PLATFORM_IOS \
|| BX_PLATFORM_VISIONOS || BX_PLATFORM_VISIONOS
pthread_setname_np(_name); pthread_setname_np(m_name.getCPtr() );
#elif BX_CRT_GLIBC #elif BX_CRT_GLIBC
pthread_setname_np(ti->m_handle, _name); pthread_setname_np(ti->m_handle, m_name.getCPtr() );
#elif BX_PLATFORM_LINUX #elif BX_PLATFORM_LINUX
prctl(PR_SET_NAME,_name, 0, 0, 0); prctl(PR_SET_NAME, m_name.getCPtr(), 0, 0, 0);
#elif BX_PLATFORM_WINDOWS #elif BX_PLATFORM_WINDOWS
// Try to use the new thread naming API from Win10 Creators update onwards if we have it
typedef HRESULT (WINAPI *SetThreadDescriptionProc)(HANDLE, PCWSTR); typedef HRESULT (WINAPI *SetThreadDescriptionProc)(HANDLE, PCWSTR);
SetThreadDescriptionProc SetThreadDescription = dlsym<SetThreadDescriptionProc>((void*)GetModuleHandleA("Kernel32.dll"), "SetThreadDescription"); SetThreadDescriptionProc SetThreadDescription = dlsym<SetThreadDescriptionProc>( (void*)GetModuleHandleA("Kernel32.dll"), "SetThreadDescription");
if (NULL != SetThreadDescription) if (NULL != SetThreadDescription)
{ {
uint32_t length = (uint32_t)strLen(_name)+1; const uint32_t length = m_name.getLength();
uint32_t size = length*sizeof(wchar_t); const uint32_t max = (length+1)*sizeof(wchar_t);
wchar_t* name = (wchar_t*)BX_STACK_ALLOC(size); wchar_t* name = (wchar_t*)BX_STACK_ALLOC(max);
mbstowcs(name, _name, size-2); mbstowcs(name, m_name.getCPtr(), length);
name[size-2] = 0; name[length] = 0;
SetThreadDescription(ti->m_handle, name); SetThreadDescription(ti->m_handle, name);
} }
else
{
# if BX_COMPILER_MSVC # if BX_COMPILER_MSVC
# pragma pack(push, 8) # pragma pack(push, 8)
struct ThreadName struct ThreadName
@@ -269,7 +260,7 @@ namespace bx
# pragma pack(pop) # pragma pack(pop)
ThreadName tn; ThreadName tn;
tn.type = 0x1000; tn.type = 0x1000;
tn.name = _name; tn.name = m_name.getCPtr();
tn.id = ti->m_threadId; tn.id = ti->m_threadId;
tn.flags = 0; tn.flags = 0;
@@ -285,8 +276,7 @@ namespace bx
{ {
} }
# endif // BX_COMPILER_MSVC # endif // BX_COMPILER_MSVC
#else }
BX_UNUSED(_name);
#endif // BX_PLATFORM_ #endif // BX_PLATFORM_
} }