From 28528abbcd13174854551effe9e6f33f60dea93b Mon Sep 17 00:00:00 2001 From: Brad Keifer <15224368+bradkeifer@users.noreply.github.com> Date: Wed, 5 Nov 2025 11:36:37 +1100 Subject: [PATCH] Initial changes to support systems with single arg pthread_setname_np() --- configure.ac | 16 ++++++++++++++-- src/misc.c | 26 +++++++++++++++++--------- src/misc.h | 4 ++-- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/configure.ac b/configure.ac index 66d92954..fe14ab37 100644 --- a/configure.ac +++ b/configure.ac @@ -80,8 +80,19 @@ AC_SEARCH_LIBS([pthread_setname_np], [pthread], AC_LINK_IFELSE([AC_LANG_PROGRAM([[@%:@include ]], [[pthread_setname_np(pthread_self(), "name");]])], [AC_MSG_RESULT([yes]) - AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], 1, - [Define to 1 if you have pthread_setname_np])], + AC_DEFINE([HAVE_PTHREAD_SETNAME_NP_2], 1, + [Define to 1 if you have pthread_setname_np with 2 args])], + [AC_MSG_RESULT([[no]])])], + [AC_SEARCH_LIBS([pthread_set_name_np], [pthread], + [AC_CHECK_FUNCS([pthread_set_name_np])])]) +AC_SEARCH_LIBS([pthread_setname_np], [pthread], + [dnl Validate pthread_setname_np with 1 arg (some have 2) + AC_MSG_CHECKING([[for single-parameter pthread_setname_np]]) + AC_LINK_IFELSE([AC_LANG_PROGRAM([[@%:@include ]], + [[pthread_setname_np("name");]])], + [AC_MSG_RESULT([yes]) + AC_DEFINE([HAVE_PTHREAD_SETNAME_NP_1], 1, + [Define to 1 if you have pthread_setname_np with 1 arg])], [AC_MSG_RESULT([[no]])])], [AC_SEARCH_LIBS([pthread_set_name_np], [pthread], [AC_CHECK_FUNCS([pthread_set_name_np])])]) @@ -94,6 +105,7 @@ AC_SEARCH_LIBS([pthread_getname_np], [pthread], AC_SEARCH_LIBS([pthread_getthreadid_np], [pthread], [AC_DEFINE([HAVE_PTHREAD_GETTHREADID_NP], 1, [Define to 1 if you have pthread_getthreadid_np])]) + AC_SEARCH_LIBS([uuid_generate_random], [uuid], [AC_DEFINE([HAVE_UUID], 1, [Define to 1 if you have uuid_generate_random])]) diff --git a/src/misc.c b/src/misc.c index 885e0a23..8f0b9263 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1852,14 +1852,16 @@ mutex_init(pthread_mutex_t *mutex) return err; } -int -thread_gettid() +int64_t +thread_gettid(pthread_t p) { - int tid = -1; + int64_t tid = -1; #if defined(HAVE_GETTID) - tid = (int)gettid(); + tid = (int64_t)gettid(); #elif defined(HAVE_PTHREAD_GETTHREADID_NP) - tid = pthread_getthreadid_np(); + tid = (int64_t)pthread_getthreadid_np(); +#else //defacto thread id + tid = (int64_t)p; #endif return tid; } @@ -1879,19 +1881,25 @@ thread_getname(pthread_t thread, char *name, size_t len) void thread_getnametid(char *buf, size_t len) { - int tid; + int64_t tid; char thread_name[32]; pthread_t p = pthread_self(); thread_getname(p, thread_name, sizeof(thread_name)); - tid = thread_gettid() % 10000; - snprintf(buf, len, "%s (%d)", thread_name, tid); + tid = thread_gettid(p) % 10000; + snprintf(buf, len, "%s (%" PRId64 ")", thread_name, tid); } void thread_setname(pthread_t thread, const char *name) { -#if defined(HAVE_PTHREAD_SETNAME_NP) +#if defined(HAVE_PTHREAD_SETNAME_NP_1) + pthread_setname_np(name); + DPRINTF(E_DBG, L_MISC, + "%s: Single argument pthread_setname_np(%s). Be aware! It must be called from thread whose name is to be changed\n", + __func__, name + ); +#elif defined(HAVE_PTHREAD_SETNAME_NP_2) pthread_setname_np(thread, name); #elif defined(HAVE_PTHREAD_SET_NAME_NP) pthread_set_name_np(thread, name); diff --git a/src/misc.h b/src/misc.h index 40612eb8..dce0eda7 100644 --- a/src/misc.h +++ b/src/misc.h @@ -319,8 +319,8 @@ int mutex_init(pthread_mutex_t *mutex); // wrapper for gettid/pthread_getthreadid_np -int -thread_gettid(); +int64_t +thread_gettid(pthread_t p); // wrapper for pthread_getname_np/pthread_get_name_np void