changeset 841:d22bd44473d8

thread: handle FreeBSD and macOS arg differences for pthread name setting Both OSes have the same function (pthread_setname_np), but their arguments are slightly different. The FreeBSD version takes a pthread argument to identify which thread's name should be changed, while the macOS function always operates on the current thread. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Mon, 04 Oct 2021 22:31:19 -0400
parents 3d96091853b6
children 8ed31ab423c8
files cmake/config.cmake include/jeffpc/config.h.in thread.c
diffstat 3 files changed, 15 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/cmake/config.cmake	Sun Aug 29 09:51:37 2021 -0400
+++ b/cmake/config.cmake	Mon Oct 04 22:31:19 2021 -0400
@@ -67,13 +67,22 @@
 	""
 	"assert.h"
 	JEFFPC_HAVE___ASSERT_LINUX_STYLE)
+check_prototype_definition(pthread_setname_np
+	"int pthread_setname_np(pthread_t thread, const char *name)"
+	"0"
+	"pthread.h"
+	JEFFPC_HAVE_PTHREAD_SETNAME_NP_FREEBSD_STYLE)
+check_prototype_definition(pthread_setname_np
+	"int pthread_setname_np(const char *name)"
+	"0"
+	"pthread.h"
+	JEFFPC_HAVE_PTHREAD_SETNAME_NP_OSX_STYLE)
 check_function_exists(arc4random JEFFPC_HAVE_ARC4RANDOM)
 check_function_exists(arc4random_buf JEFFPC_HAVE_ARC4RANDOM_BUF)
 check_function_exists(assfail JEFFPC_HAVE_ASSFAIL)
 check_function_exists(addrtosymstr JEFFPC_HAVE_ADDRTOSYMSTR)
 check_function_exists(pthread_cond_reltimedwait_np
 	JEFFPC_HAVE_PTHREAD_COND_RELTIMEDWAIT_NP)
-check_function_exists(pthread_setname_np JEFFPC_HAVE_PTHREAD_SETNAME_NP)
 check_function_exists(reallocarray JEFFPC_HAVE_REALLOCARRAY)
 check_function_exists(recallocarray JEFFPC_HAVE_RECALLOCARRAY)
 check_include_files(alloca.h JEFFPC_HAVE_ALLOCA_H)
--- a/include/jeffpc/config.h.in	Sun Aug 29 09:51:37 2021 -0400
+++ b/include/jeffpc/config.h.in	Mon Oct 04 22:31:19 2021 -0400
@@ -35,7 +35,8 @@
 #cmakedefine JEFFPC_HAVE_ASSFAIL
 #cmakedefine JEFFPC_HAVE_ADDRTOSYMSTR
 #cmakedefine JEFFPC_HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
-#cmakedefine JEFFPC_HAVE_PTHREAD_SETNAME_NP
+#cmakedefine JEFFPC_HAVE_PTHREAD_SETNAME_NP_FREEBSD_STYLE
+#cmakedefine JEFFPC_HAVE_PTHREAD_SETNAME_NP_OSX_STYLE
 #cmakedefine JEFFPC_HAVE_REALLOCARRAY
 #cmakedefine JEFFPC_HAVE_RECALLOCARRAY
 #cmakedefine JEFFPC_HAVE_ALLOCA_H
--- a/thread.c	Sun Aug 29 09:51:37 2021 -0400
+++ b/thread.c	Mon Oct 04 22:31:19 2021 -0400
@@ -40,8 +40,10 @@
 
 	strcpy_safe(thread_name, name ? name : "", sizeof(thread_name));
 
-#ifdef JEFFPC_HAVE_PTHREAD_SETNAME_NP
+#if defined(JEFFPC_HAVE_PTHREAD_SETNAME_NP_FREEBSD_STYLE)
 	ret = -pthread_setname_np(xthr_self(), thread_name);
+#elif defined(JEFFPC_HAVE_PTHREAD_SETNAME_NP_OSX_STYLE)
+	ret = -pthread_setname_np(thread_name);
 #else
 	/* no known way to set OS thread name */
 	ret = 0;