Skip to content

Commit 50b289a

Browse files
authored
Merge pull request #10641 from brad0/netbsd_pthread_setname_np
Fix usage of pthread_setname_np() on NetBSD
2 parents de35c0d + bbc1859 commit 50b289a

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

lib/base/utility.cpp

Lines changed: 23 additions & 5 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,13 +1336,14 @@ 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());
1326-
# else /* __APPLE__ */
1327-
String tname = name.SubStr(0, 15);
1328-
pthread_setname_np(pthread_self(), tname.CStr());
1342+
pthread_setname_np(tname.CStr());
1343+
# elif defined(__NetBSD__) /* __APPLE__ */
1344+
pthread_setname_np(pthread_self(), "%s", const_cast<char *>(tname.CStr()));
1345+
# else /* __NetBSD__ */
1346+
pthread_setname_np(pthread_self(), tname.CStr());
13291347
# endif /* __APPLE__ */
13301348
#endif /* HAVE_PTHREAD_SETNAME_NP */
13311349
}

0 commit comments

Comments
 (0)