More thread name clean-ups

This commit is contained in:
RichardG867
2024-01-10 10:27:11 -03:00
parent 267c3c464c
commit 23039a35b4
3 changed files with 10 additions and 8 deletions

View File

@@ -761,27 +761,28 @@ plat_set_thread_name(void *thread, const char *name)
if (!kernel32_handle) { if (!kernel32_handle) {
kernel32_handle = dynld_module("kernel32.dll", kernel32_imports); kernel32_handle = dynld_module("kernel32.dll", kernel32_imports);
if (!kernel32_handle) { if (!kernel32_handle) {
kernel32_handle = kernel32_imports; /* dummy pointer to store that we tried */ kernel32_handle = kernel32_imports; /* store dummy pointer to avoid trying again */
pSetThreadDescription = NULL; pSetThreadDescription = NULL;
} }
} }
if (pSetThreadDescription) { if (pSetThreadDescription) {
size_t len = strlen(name) + 1; size_t len = strlen(name) + 1;
wchar_t wname[len]; wchar_t wname[len + 1];
mbstowcs(wname, name, len); mbstowcs(wname, name, len);
pSetThreadDescription(thread ? (HANDLE) thread : GetCurrentThread(), wname); pSetThreadDescription(thread ? (HANDLE) thread : GetCurrentThread(), wname);
} }
#else #else
# ifdef Q_OS_DARWIN # ifdef Q_OS_DARWIN
if (thread) /* Apple pthread can only set self's name */
return;
char truncated[64]; char truncated[64];
# else # else
char truncated[16]; char truncated[16];
# endif # endif
strncpy(truncated, name, sizeof(truncated) - 1); strncpy(truncated, name, sizeof(truncated) - 1);
# ifdef Q_OS_DARWIN # ifdef Q_OS_DARWIN
if (!thread) pthread_setname_np(truncated);
pthread_setname_np(truncated);
# else # else
pthread_setname_np(thread ? *((pthread_t *) thread) : pthread_self(), truncated); pthread_setname_np(thread ? *((pthread_t *) thread) : pthread_self(), truncated);
# endif # endif

View File

@@ -1386,14 +1386,15 @@ void
plat_set_thread_name(void *thread, const char *name) plat_set_thread_name(void *thread, const char *name)
{ {
#ifdef __APPLE__ #ifdef __APPLE__
if (thread) /* Apple pthread can only set self's name */
return;
char truncated[64]; char truncated[64];
#else #else
char truncated[16]; char truncated[16];
#endif #endif
strncpy(truncated, name, sizeof(truncated) - 1); strncpy(truncated, name, sizeof(truncated) - 1);
#ifdef __APPLE__ #ifdef __APPLE__
if (!thread) pthread_setname_np(truncated);
pthread_setname_np(truncated);
#else #else
pthread_setname_np(thread ? *((pthread_t *) thread) : pthread_self(), truncated); pthread_setname_np(thread ? *((pthread_t *) thread) : pthread_self(), truncated);
#endif #endif

View File

@@ -1293,14 +1293,14 @@ plat_set_thread_name(void *thread, const char *name)
if (!kernel32_handle) { if (!kernel32_handle) {
kernel32_handle = dynld_module("kernel32.dll", kernel32_imports); kernel32_handle = dynld_module("kernel32.dll", kernel32_imports);
if (!kernel32_handle) { if (!kernel32_handle) {
kernel32_handle = kernel32_imports; /* dummy pointer to store that we tried */ kernel32_handle = kernel32_imports; /* store dummy pointer to avoid trying again */
pSetThreadDescription = NULL; pSetThreadDescription = NULL;
} }
} }
if (pSetThreadDescription) { if (pSetThreadDescription) {
size_t len = strlen(name) + 1; size_t len = strlen(name) + 1;
wchar_t wname[len]; wchar_t wname[len + 1];
mbstowcs(wname, name, len); mbstowcs(wname, name, len);
pSetThreadDescription(thread ? (HANDLE) thread : GetCurrentThread(), wname); pSetThreadDescription(thread ? (HANDLE) thread : GetCurrentThread(), wname);
} }