From 7aa777c40f6f60473a934abb9e4fe826ae272a0a Mon Sep 17 00:00:00 2001 From: Brad Smith Date: Tue, 25 Nov 2025 15:43:23 -0500 Subject: [PATCH 1/2] Fix usage of pthread_setname_np() on NetBSD NetBSD's pthread_setname_np() takes a slightly different set of parameters. --- lib/base/utility.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 658c781e4..0fe3191ce 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -1322,10 +1322,12 @@ void Utility::SetThreadName(const String& name, bool os) pthread_set_name_np(pthread_self(), name.CStr()); #elif defined(HAVE_PTHREAD_SETNAME_NP) /* HAVE_PTHREAD_SET_NAME_NP */ # ifdef __APPLE__ - pthread_setname_np(name.CStr()); -# else /* __APPLE__ */ - String tname = name.SubStr(0, 15); - pthread_setname_np(pthread_self(), tname.CStr()); + pthread_setname_np(name.CStr()); +# elif defined(__NetBSD__) /* __APPLE__ */ + pthread_setname_np(pthread_self(), "%s", const_cast(name.CStr())); +# else /* __NetBSD__ */ + String tname = name.SubStr(0, 15); + pthread_setname_np(pthread_self(), tname.CStr()); # endif /* __APPLE__ */ #endif /* HAVE_PTHREAD_SETNAME_NP */ } From bbc1859aa18fb49b54b802e1b86b7568931ecda3 Mon Sep 17 00:00:00 2001 From: Brad Smith Date: Thu, 27 Nov 2025 07:14:10 -0500 Subject: [PATCH 2/2] Have thread name length clamping also apply for the other OSes This covers macOS, *BSDs, Linux, Cygwin. NetBSD PTHREAD_MAX_NAMELEN_NP 32 macOS 64 OpenBSD 24 Linux/FreeBSD/DragonFly/Cygwin 16 --- lib/base/utility.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 0fe3191ce..adc7c31fb 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -1311,6 +1311,23 @@ void Utility::SetThreadName(const String& name, bool os) { m_ThreadName.reset(new String(name)); +#if defined(HAVE_PTHREAD_SET_NAME_NP) || defined(HAVE_PTHREAD_SETNAME_NP) + unsigned int len; + + /* https://github.com/llvm/llvm-project/blob/6412184891526690cff804f87f986b1fa039f011/llvm/lib/Support/Unix/Threading.inc#L157 */ +# ifdef PTHREAD_MAX_NAMELEN_NP + len = PTHREAD_MAX_NAMELEN_NP; +# elif defined(__APPLE__) /* PTHREAD_MAX_NAMELEN_NP */ + len = 64; +# elif defined(__OpenBSD__) /* __APPLE__ */ + len = 24; +# else /* __OpenBSD__ */ + len = 16; +# endif /* PTHREAD_MAX_NAMELEN_NP */ + + String tname = name.SubStr(0, len - 1); +#endif + if (!os) return; @@ -1319,14 +1336,13 @@ void Utility::SetThreadName(const String& name, bool os) #endif /* _WIN32 */ #ifdef HAVE_PTHREAD_SET_NAME_NP - pthread_set_name_np(pthread_self(), name.CStr()); + pthread_set_name_np(pthread_self(), tname.CStr()); #elif defined(HAVE_PTHREAD_SETNAME_NP) /* HAVE_PTHREAD_SET_NAME_NP */ # ifdef __APPLE__ - pthread_setname_np(name.CStr()); + pthread_setname_np(tname.CStr()); # elif defined(__NetBSD__) /* __APPLE__ */ - pthread_setname_np(pthread_self(), "%s", const_cast(name.CStr())); + pthread_setname_np(pthread_self(), "%s", const_cast(tname.CStr())); # else /* __NetBSD__ */ - String tname = name.SubStr(0, 15); pthread_setname_np(pthread_self(), tname.CStr()); # endif /* __APPLE__ */ #endif /* HAVE_PTHREAD_SETNAME_NP */