From bbb7e7837fcad3bac10aef69a1b331243c2b1c4c Mon Sep 17 00:00:00 2001 From: Robert Pluim Date: Thu, 19 Dec 2019 17:33:16 +0100 Subject: [PATCH] Use pthread_setname_np to set thread name * configure.ac: Remove check for sys/prctl.h and prctl, check for pthread_setname_np instead. * systhread.c: Remove sys/prctl.h include. (sys_thread_create) [HAVE_PTHREAD_SETNAME_NP]: Use pthread_setname_np to set the name of the newly created thread (Bug#38632). --- configure.ac | 4 ++-- src/systhread.c | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index 3b6a2a6d16..34a2654494 100644 --- a/configure.ac +++ b/configure.ac @@ -1767,7 +1767,7 @@ AC_DEFUN sys/sysinfo.h coff.h pty.h sys/resource.h - sys/utsname.h pwd.h utmp.h util.h sys/prctl.h) + sys/utsname.h pwd.h utmp.h util.h) AC_CACHE_CHECK([for ADDR_NO_RANDOMIZE], [emacs_cv_personality_addr_no_randomize], @@ -4180,7 +4180,7 @@ AC_DEFUN sendto recvfrom getsockname getifaddrs freeifaddrs \ gai_strerror sync \ getpwent endpwent getgrent endgrent \ -cfmakeraw cfsetspeed __executable_start log2 prctl) +cfmakeraw cfsetspeed __executable_start log2 pthread_setname_np) LIBS=$OLD_LIBS dnl No need to check for posix_memalign if aligned_alloc works. diff --git a/src/systhread.c b/src/systhread.c index 6f4de536fb..55bde837e3 100644 --- a/src/systhread.c +++ b/src/systhread.c @@ -98,10 +98,6 @@ sys_thread_yield (void) #include -#ifdef HAVE_SYS_PRCTL_H -#include -#endif - void sys_mutex_init (sys_mutex_t *mutex) { @@ -227,9 +223,18 @@ sys_thread_create (sys_thread_t *thread_ptr, const char *name, if (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED)) { result = pthread_create (thread_ptr, &attr, func, arg) == 0; -#if defined (HAVE_SYS_PRCTL_H) && defined (HAVE_PRCTL) && defined (PR_SET_NAME) +#ifdef HAVE_PTHREAD_SETNAME_NP if (result && name != NULL) - prctl (PR_SET_NAME, name); + { + /* We need to truncate here otherwise pthread_setname_np + fails to set the name. TASK_COMM_LEN is what the length + is called in the Linux kernel headers (Bug#38632). */ +#define TASK_COMM_LEN 16 + char p_name[TASK_COMM_LEN]; + strncpy (p_name, name, TASK_COMM_LEN - 1); + p_name[TASK_COMM_LEN - 1] = '\0'; + pthread_setname_np (*thread_ptr, p_name); + } #endif } -- 2.23.0