From a45fa01387fd15b4bf8291cf02931cecac86c3a4 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Fri, 2 Apr 2021 10:51:53 -0400 Subject: rts: Fix usage of pthread_setname_np Previously we used this non-portable function unconditionally, breaking FreeBSD. Fixes #19637. --- configure.ac | 68 ++++++++++++++++++++++++++++++++++++++++++++------- rts/posix/OSThreads.c | 8 ++++-- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/configure.ac b/configure.ac index bcda0b1ffb..383aeaa99f 100644 --- a/configure.ac +++ b/configure.ac @@ -1214,20 +1214,70 @@ AC_CHECK_FUNC(pthread_create, AC_DEFINE_UNQUOTED([NEED_PTHREAD_LIB], [$need_lpthread], [Define 1 if we need to link code using pthreads with -lpthread]) +dnl Setting thread names +dnl ~~~~~~~~~~~~~~~~~~~~ +dnl The portability situation here is complicated: +dnl +dnl * FreeBSD supports pthread_set_name_np in +dnl * glibc supports pthread_setname_np +dnl * Darwin supports pthread_setname_np but does not take a +dnl pthread_t argument. +dnl +AC_CHECK_HEADERS([pthread_np.h]) + dnl ** pthread_setname_np is a recent addition to glibc, and OS X has dnl a different single-argument version. AC_CHECK_LIB(pthread, pthread_setname_np) -AC_MSG_CHECKING(for pthread_setname_np) -AC_LINK_IFELSE([AC_LANG_PROGRAM( -[[ -#define _GNU_SOURCE -#include -]], -[[pthread_setname_np(pthread_self(), "name");]])], + +AC_MSG_CHECKING([for pthread_setname_np (Darwin)]) +AC_LINK_IFELSE([ + AC_LANG_PROGRAM( + [[ + #include + ]], + [[pthread_setname_np("name");]] + )], + [ + AC_MSG_RESULT(yes) + AC_DEFINE([HAVE_PTHREAD_SETNAME_NP_DARWIN], [1], + [Define to 1 if you have the Darwin version of pthread_setname_np]) + ], + AC_MSG_RESULT(no) +) + +dnl glibc +AC_MSG_CHECKING([for pthread_setname_np (glibc)]) +AC_LINK_IFELSE([ + AC_LANG_PROGRAM( + [[ + #define _GNU_SOURCE + #include + ]], + [[pthread_setname_np(pthread_self(), "name");]] + )], + [ AC_MSG_RESULT(yes) AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], - [Define to 1 if you have the glibc version of pthread_setname_np]), - AC_MSG_RESULT(no) + [Define to 1 if you have the glibc version of pthread_setname_np]) + ], + AC_MSG_RESULT(no) +) + +dnl FreeBSD +AC_MSG_CHECKING([for pthread_set_name_np]) +AC_LINK_IFELSE([ + AC_LANG_PROGRAM( + [[ + #include + ]], + [[pthread_setname_np(pthread_self(), "name");]] + )], + [ + AC_MSG_RESULT(yes) + AC_DEFINE([HAVE_PTHREAD_SET_NAME_NP], [1], + [Define to 1 if you have pthread_set_name_np]) + ], + AC_MSG_RESULT(no) ) dnl ** check for eventfd which is needed by the I/O manager diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index 200a4ccdad..b815894a9f 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -30,7 +30,7 @@ #if defined(HAVE_PTHREAD_H) #include -#if defined(freebsd_HOST_OS) +#if defined(HAVE_PTHREAD_NP_H) #include #endif #endif @@ -153,8 +153,12 @@ createOSThread (OSThreadId* pId, char *name STG_UNUSED, int result = pthread_create(pId, NULL, startProc, param); if (!result) { pthread_detach(*pId); -#if defined(HAVE_PTHREAD_SETNAME_NP) +#if defined(HAVE_PTHREAD_SET_NAME_NP) + pthread_set_name_np(*pId, name); +#elif defined(HAVE_PTHREAD_SETNAME_NP) pthread_setname_np(*pId, name); +#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN) + pthread_setname_np(name); #endif } return result; -- cgit v1.2.1