Index: head/include/pthread.h =================================================================== --- head/include/pthread.h +++ head/include/pthread.h @@ -301,6 +301,9 @@ int pthread_getprio(pthread_t); int pthread_setprio(pthread_t, int); void pthread_yield(void); + +int pthread_getname_np(pthread_t, char *, size_t); +int pthread_setname_np(pthread_t, const char *); #endif int pthread_mutexattr_getprioceiling( Index: head/lib/libc/include/namespace.h =================================================================== --- head/lib/libc/include/namespace.h +++ head/lib/libc/include/namespace.h @@ -138,6 +138,7 @@ #define pthread_getaffinity_np _pthread_getaffinity_np #define pthread_getconcurrency _pthread_getconcurrency #define pthread_getcpuclockid _pthread_getcpuclockid +#define pthread_getname_np _pthread_getname_np #define pthread_getprio _pthread_getprio #define pthread_getschedparam _pthread_getschedparam #define pthread_getspecific _pthread_getspecific @@ -191,6 +192,7 @@ #define pthread_setcancelstate _pthread_setcancelstate #define pthread_setcanceltype _pthread_setcanceltype #define pthread_setconcurrency _pthread_setconcurrency +#define pthread_setname_np _pthread_setname_np #define pthread_setprio _pthread_setprio #define pthread_setschedparam _pthread_setschedparam #define pthread_setspecific _pthread_setspecific Index: head/lib/libc/include/un-namespace.h =================================================================== --- head/lib/libc/include/un-namespace.h +++ head/lib/libc/include/un-namespace.h @@ -119,6 +119,7 @@ #undef pthread_getaffinity_np #undef pthread_getconcurrency #undef pthread_getcpuclockid +#undef pthread_getname_np #undef pthread_getprio #undef pthread_getschedparam #undef pthread_getspecific @@ -172,6 +173,7 @@ #undef pthread_setcancelstate #undef pthread_setcanceltype #undef pthread_setconcurrency +#undef pthread_setname_np #undef pthread_setprio #undef pthread_setschedparam #undef pthread_setspecific Index: head/lib/libthr/pthread.map =================================================================== --- head/lib/libthr/pthread.map +++ head/lib/libthr/pthread.map @@ -328,5 +328,7 @@ }; FBSD_1.6 { + pthread_getname_np; pthread_peekjoin_np; + pthread_setname_np; }; Index: head/lib/libthr/thread/thr_info.c =================================================================== --- head/lib/libthr/thread/thr_info.c +++ head/lib/libthr/thread/thr_info.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" +#include #include #include #include @@ -45,39 +46,66 @@ #include "thr_private.h" -__weak_reference(_pthread_set_name_np, pthread_set_name_np); - static void -thr_set_name_np(struct pthread *thread, const char *name) +thr_set_name_np(struct pthread *thread, char **tmp_name) { free(thread->name); - thread->name = name != NULL ? strdup(name) : NULL; + thread->name = *tmp_name; + *tmp_name = NULL; } -/* Set the thread name for debug. */ -void -_pthread_set_name_np(pthread_t thread, const char *name) +/* Set the thread name. */ +__weak_reference(_pthread_setname_np, pthread_setname_np); +int +_pthread_setname_np(pthread_t thread, const char *name) { struct pthread *curthread; + char *tmp_name; + int res; + if (name != NULL) { + tmp_name = strdup(name); + if (tmp_name == NULL) + return (ENOMEM); + } else { + tmp_name = NULL; + } curthread = _get_curthread(); if (curthread == thread) { + res = 0; THR_THREAD_LOCK(curthread, thread); - thr_set_name(thread->tid, name); - thr_set_name_np(thread, name); + if (thr_set_name(thread->tid, name) == -1) + res = errno; + else + thr_set_name_np(thread, &tmp_name); THR_THREAD_UNLOCK(curthread, thread); } else { + res = ESRCH; if (_thr_find_thread(curthread, thread, 0) == 0) { if (thread->state != PS_DEAD) { - thr_set_name(thread->tid, name); - thr_set_name_np(thread, name); + if (thr_set_name(thread->tid, name) == -1) { + res = errno; + } else { + thr_set_name_np(thread, &tmp_name); + res = 0; + } } THR_THREAD_UNLOCK(curthread, thread); } } + free(tmp_name); + return (res); } +/* Set the thread name for debug. */ +__weak_reference(_pthread_set_name_np, pthread_set_name_np); +void +_pthread_set_name_np(pthread_t thread, const char *name) +{ + (void)_pthread_setname_np(thread, name); +} + static void thr_get_name_np(struct pthread *thread, char *buf, size_t len) { @@ -88,13 +116,14 @@ buf[0] = '\0'; } -__weak_reference(_pthread_get_name_np, pthread_get_name_np); - -void -_pthread_get_name_np(pthread_t thread, char *buf, size_t len) +__weak_reference(_pthread_getname_np, pthread_getname_np); +int +_pthread_getname_np(pthread_t thread, char *buf, size_t len) { struct pthread *curthread; + int res; + res = 0; curthread = _get_curthread(); if (curthread == thread) { THR_THREAD_LOCK(curthread, thread); @@ -104,8 +133,21 @@ if (_thr_find_thread(curthread, thread, 0) == 0) { if (thread->state != PS_DEAD) thr_get_name_np(thread, buf, len); + else + res = ESRCH; THR_THREAD_UNLOCK(curthread, thread); - } else if (len > 0) - buf[0] = '\0'; + } else { + res = ESRCH; + if (len > 0) + buf[0] = '\0'; + } } + return (res); +} + +__weak_reference(_pthread_get_name_np, pthread_get_name_np); +void +_pthread_get_name_np(pthread_t thread, char *buf, size_t len) +{ + (void)_pthread_getname_np(thread, buf, len); } Index: head/share/man/man3/Makefile =================================================================== --- head/share/man/man3/Makefile +++ head/share/man/man3/Makefile @@ -493,7 +493,9 @@ PTHREAD_MLINKS+=pthread_rwlock_wrlock.3 pthread_rwlock_trywrlock.3 PTHREAD_MLINKS+=pthread_schedparam.3 pthread_getschedparam.3 \ pthread_schedparam.3 pthread_setschedparam.3 -PTHREAD_MLINKS+=pthread_set_name_np.3 pthread_get_name_np.3 +PTHREAD_MLINKS+=pthread_set_name_np.3 pthread_get_name_np.3 \ + pthread_set_name_np.3 pthread_getname_np.3 \ + pthread_set_name_np.3 pthread_setname_np.3 PTHREAD_MLINKS+=pthread_spin_init.3 pthread_spin_destroy.3 \ pthread_spin_lock.3 pthread_spin_trylock.3 \ pthread_spin_lock.3 pthread_spin_unlock.3 Index: head/share/man/man3/pthread_set_name_np.3 =================================================================== --- head/share/man/man3/pthread_set_name_np.3 +++ head/share/man/man3/pthread_set_name_np.3 @@ -24,12 +24,14 @@ .\" .\" $FreeBSD$ .\" -.Dd August 12, 2018 +.Dd June 3, 2020 .Dt PTHREAD_SET_NAME_NP 3 .Os .Sh NAME .Nm pthread_get_name_np , +.Nm pthread_getname_np , .Nm pthread_set_name_np +.Nm pthread_setname_np .Nd set and retrieve the thread name .Sh LIBRARY .Lb libpthread @@ -37,19 +39,27 @@ .In pthread_np.h .Ft void .Fn pthread_get_name_np "pthread_t thread" "char *name" "size_t len" +.Ft int +.Fn pthread_getname_np "pthread_t thread" "char *name" "size_t len" .Ft void .Fn pthread_set_name_np "pthread_t thread" "const char *name" +.Ft int +.Fn pthread_setname_np "pthread_t thread" "const char *name" .Sh DESCRIPTION The .Fn pthread_set_name_np -function applies a copy of the given +and +.Fn pthread_setname_np +functions apply a copy of the given .Fa name to the given .Fa thread . .Pp The .Fn pthread_get_name_np -function retrieves the +and +.Fn pthread_getname_np +functions retrieve the .Fa name associated with .Fa thread . @@ -61,7 +71,23 @@ .Fa name will be empty. .Sh ERRORS -Because of the debugging nature of these functions, all errors that may +The +.Nm pthread_getname_np +and +.Nm pthread_setname_np +will fail if +.Bl -tag -width Er +.It Bq Er ESRCH +No thread could be found in the current process corresponding to that +specified by the given thread ID +.Fa thread . +.El +.Pp +Because of the debugging nature of +.Nm pthread_get_name_np +and +.Nm pthread_set_name_np +functions, all errors that may appear inside are silently ignored. .Sh SEE ALSO .Xr thr_set_name 2 @@ -70,6 +96,11 @@ and .Fn pthread_get_name_np are non-standard extensions. +.Fn pthread_setname_np +and +.Fn pthread_getname_np +are also non-standard, but are implemented by larger number of operating +systems so they are in fact more portable. .Sh AUTHORS This manual page was written by .An Alexey Zelkin Aq Mt phantom@FreeBSD.org