Skip to content

Commit bbc1859

Browse files
committed
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
1 parent 7aa777c commit bbc1859

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

lib/base/utility.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,23 @@ void Utility::SetThreadName(const String& name, bool os)
13111311
{
13121312
m_ThreadName.reset(new String(name));
13131313

1314+
#if defined(HAVE_PTHREAD_SET_NAME_NP) || defined(HAVE_PTHREAD_SETNAME_NP)
1315+
unsigned int len;
1316+
1317+
/* https://github.com/llvm/llvm-project/blob/6412184891526690cff804f87f986b1fa039f011/llvm/lib/Support/Unix/Threading.inc#L157 */
1318+
# ifdef PTHREAD_MAX_NAMELEN_NP
1319+
len = PTHREAD_MAX_NAMELEN_NP;
1320+
# elif defined(__APPLE__) /* PTHREAD_MAX_NAMELEN_NP */
1321+
len = 64;
1322+
# elif defined(__OpenBSD__) /* __APPLE__ */
1323+
len = 24;
1324+
# else /* __OpenBSD__ */
1325+
len = 16;
1326+
# endif /* PTHREAD_MAX_NAMELEN_NP */
1327+
1328+
String tname = name.SubStr(0, len - 1);
1329+
#endif
1330+
13141331
if (!os)
13151332
return;
13161333

@@ -1319,14 +1336,13 @@ void Utility::SetThreadName(const String& name, bool os)
13191336
#endif /* _WIN32 */
13201337

13211338
#ifdef HAVE_PTHREAD_SET_NAME_NP
1322-
pthread_set_name_np(pthread_self(), name.CStr());
1339+
pthread_set_name_np(pthread_self(), tname.CStr());
13231340
#elif defined(HAVE_PTHREAD_SETNAME_NP) /* HAVE_PTHREAD_SET_NAME_NP */
13241341
# ifdef __APPLE__
1325-
pthread_setname_np(name.CStr());
1342+
pthread_setname_np(tname.CStr());
13261343
# elif defined(__NetBSD__) /* __APPLE__ */
1327-
pthread_setname_np(pthread_self(), "%s", const_cast<char *>(name.CStr()));
1344+
pthread_setname_np(pthread_self(), "%s", const_cast<char *>(tname.CStr()));
13281345
# else /* __NetBSD__ */
1329-
String tname = name.SubStr(0, 15);
13301346
pthread_setname_np(pthread_self(), tname.CStr());
13311347
# endif /* __APPLE__ */
13321348
#endif /* HAVE_PTHREAD_SETNAME_NP */

0 commit comments

Comments
 (0)