From 23039a35b49f1fc98ea65d1d05e9127e8fbd0aba Mon Sep 17 00:00:00 2001 From: RichardG867 Date: Wed, 10 Jan 2024 10:27:11 -0300 Subject: [PATCH] More thread name clean-ups --- src/qt/qt_platform.cpp | 9 +++++---- src/unix/unix.c | 5 +++-- src/win/win.c | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index 09feb5f5e..6890bd407 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -761,27 +761,28 @@ plat_set_thread_name(void *thread, const char *name) if (!kernel32_handle) { kernel32_handle = dynld_module("kernel32.dll", kernel32_imports); 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; } } if (pSetThreadDescription) { size_t len = strlen(name) + 1; - wchar_t wname[len]; + wchar_t wname[len + 1]; mbstowcs(wname, name, len); pSetThreadDescription(thread ? (HANDLE) thread : GetCurrentThread(), wname); } #else # ifdef Q_OS_DARWIN + if (thread) /* Apple pthread can only set self's name */ + return; char truncated[64]; # else char truncated[16]; # endif strncpy(truncated, name, sizeof(truncated) - 1); # ifdef Q_OS_DARWIN - if (!thread) - pthread_setname_np(truncated); + pthread_setname_np(truncated); # else pthread_setname_np(thread ? *((pthread_t *) thread) : pthread_self(), truncated); # endif diff --git a/src/unix/unix.c b/src/unix/unix.c index 62d3bd36d..4f21ddd53 100644 --- a/src/unix/unix.c +++ b/src/unix/unix.c @@ -1386,14 +1386,15 @@ void plat_set_thread_name(void *thread, const char *name) { #ifdef __APPLE__ + if (thread) /* Apple pthread can only set self's name */ + return; char truncated[64]; #else char truncated[16]; #endif strncpy(truncated, name, sizeof(truncated) - 1); #ifdef __APPLE__ - if (!thread) - pthread_setname_np(truncated); + pthread_setname_np(truncated); #else pthread_setname_np(thread ? *((pthread_t *) thread) : pthread_self(), truncated); #endif diff --git a/src/win/win.c b/src/win/win.c index 7e752f90f..d77ab32fd 100644 --- a/src/win/win.c +++ b/src/win/win.c @@ -1293,14 +1293,14 @@ plat_set_thread_name(void *thread, const char *name) if (!kernel32_handle) { kernel32_handle = dynld_module("kernel32.dll", kernel32_imports); 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; } } if (pSetThreadDescription) { size_t len = strlen(name) + 1; - wchar_t wname[len]; + wchar_t wname[len + 1]; mbstowcs(wname, name, len); pSetThreadDescription(thread ? (HANDLE) thread : GetCurrentThread(), wname); }