diff --git a/include/pthread.h b/include/pthread.h index c22ad216d01b..143401ddf68b 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -301,6 +301,9 @@ void pthread_testcancel(void); 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( diff --git a/lib/libc/include/namespace.h b/lib/libc/include/namespace.h index 58b5254b8fe2..d0c26ee581e3 100644 --- a/lib/libc/include/namespace.h +++ b/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 diff --git a/lib/libc/include/un-namespace.h b/lib/libc/include/un-namespace.h index 154fe697a6f9..8d05ab90c7dc 100644 --- a/lib/libc/include/un-namespace.h +++ b/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 diff --git a/lib/libthr/pthread.map b/lib/libthr/pthread.map index ab6f39ac00ad..232d65084ba9 100644 --- a/lib/libthr/pthread.map +++ b/lib/libthr/pthread.map @@ -328,5 +328,7 @@ FBSD_1.5 { }; FBSD_1.6 { + pthread_getname_np; pthread_peekjoin_np; + pthread_setname_np; }; diff --git a/lib/libthr/thread/thr_info.c b/lib/libthr/thread/thr_info.c index d11b488bcc1f..117d55c7bc4f 100644 --- a/lib/libthr/thread/thr_info.c +++ b/lib/libthr/thread/thr_info.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" +#include #include #include #include @@ -45,37 +46,64 @@ __FBSDID("$FreeBSD$"); #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 @@ -88,13 +116,14 @@ thr_get_name_np(struct pthread *thread, char *buf, size_t len) 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 @@ _pthread_get_name_np(pthread_t thread, char *buf, size_t len) 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); } diff --git a/share/man/man3/Makefile b/share/man/man3/Makefile index 0caa8ea28553..7fb09f970a26 100644 --- a/share/man/man3/Makefile +++ b/share/man/man3/Makefile @@ -493,7 +493,9 @@ PTHREAD_MLINKS+=pthread_rwlock_rdlock.3 pthread_rwlock_tryrdlock.3 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 diff --git a/share/man/man3/pthread_set_name_np.3 b/share/man/man3/pthread_set_name_np.3 index 3c7ddff56dd5..af668165d91a 100644 --- a/share/man/man3/pthread_set_name_np.3 +++ b/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 @@ the buffer pointed to by .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 @@ appear inside are silently ignored. 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