Initial changes to support systems with single arg pthread_setname_np()

This commit is contained in:
Brad Keifer
2025-11-05 11:36:37 +11:00
parent ffa88286e4
commit 28528abbcd
3 changed files with 33 additions and 13 deletions

View File

@@ -80,8 +80,19 @@ AC_SEARCH_LIBS([pthread_setname_np], [pthread],
AC_LINK_IFELSE([AC_LANG_PROGRAM([[@%:@include <pthread.h>]],
[[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.h>]],
[[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])])

View File

@@ -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);

View File

@@ -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