Add pthread_getname_np() and pthread_setname_np() aliases for

pthread_get_name_np() and pthread_set_name_np().

This re-applies r361770 after compatibility fixes.

Reviewed by:	antoine, jkim, markj
Tested by:	antoine (exp-run)
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D25117
This commit is contained in:
Konstantin Belousov 2020-06-10 22:13:24 +00:00
parent 3b23ffe271
commit 2ef84b7da9
7 changed files with 106 additions and 22 deletions

View File

@ -301,6 +301,9 @@ void pthread_testcancel(void);
int pthread_getprio(pthread_t); int pthread_getprio(pthread_t);
int pthread_setprio(pthread_t, int); int pthread_setprio(pthread_t, int);
void pthread_yield(void); void pthread_yield(void);
int pthread_getname_np(pthread_t, char *, size_t);
int pthread_setname_np(pthread_t, const char *);
#endif #endif
int pthread_mutexattr_getprioceiling( int pthread_mutexattr_getprioceiling(

View File

@ -138,6 +138,7 @@
#define pthread_getaffinity_np _pthread_getaffinity_np #define pthread_getaffinity_np _pthread_getaffinity_np
#define pthread_getconcurrency _pthread_getconcurrency #define pthread_getconcurrency _pthread_getconcurrency
#define pthread_getcpuclockid _pthread_getcpuclockid #define pthread_getcpuclockid _pthread_getcpuclockid
#define pthread_getname_np _pthread_getname_np
#define pthread_getprio _pthread_getprio #define pthread_getprio _pthread_getprio
#define pthread_getschedparam _pthread_getschedparam #define pthread_getschedparam _pthread_getschedparam
#define pthread_getspecific _pthread_getspecific #define pthread_getspecific _pthread_getspecific
@ -191,6 +192,7 @@
#define pthread_setcancelstate _pthread_setcancelstate #define pthread_setcancelstate _pthread_setcancelstate
#define pthread_setcanceltype _pthread_setcanceltype #define pthread_setcanceltype _pthread_setcanceltype
#define pthread_setconcurrency _pthread_setconcurrency #define pthread_setconcurrency _pthread_setconcurrency
#define pthread_setname_np _pthread_setname_np
#define pthread_setprio _pthread_setprio #define pthread_setprio _pthread_setprio
#define pthread_setschedparam _pthread_setschedparam #define pthread_setschedparam _pthread_setschedparam
#define pthread_setspecific _pthread_setspecific #define pthread_setspecific _pthread_setspecific

View File

@ -119,6 +119,7 @@
#undef pthread_getaffinity_np #undef pthread_getaffinity_np
#undef pthread_getconcurrency #undef pthread_getconcurrency
#undef pthread_getcpuclockid #undef pthread_getcpuclockid
#undef pthread_getname_np
#undef pthread_getprio #undef pthread_getprio
#undef pthread_getschedparam #undef pthread_getschedparam
#undef pthread_getspecific #undef pthread_getspecific
@ -172,6 +173,7 @@
#undef pthread_setcancelstate #undef pthread_setcancelstate
#undef pthread_setcanceltype #undef pthread_setcanceltype
#undef pthread_setconcurrency #undef pthread_setconcurrency
#undef pthread_setname_np
#undef pthread_setprio #undef pthread_setprio
#undef pthread_setschedparam #undef pthread_setschedparam
#undef pthread_setspecific #undef pthread_setspecific

View File

@ -328,5 +328,7 @@ FBSD_1.5 {
}; };
FBSD_1.6 { FBSD_1.6 {
pthread_getname_np;
pthread_peekjoin_np; pthread_peekjoin_np;
pthread_setname_np;
}; };

View File

@ -37,6 +37,7 @@
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include "namespace.h" #include "namespace.h"
#include <sys/errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
@ -45,37 +46,64 @@ __FBSDID("$FreeBSD$");
#include "thr_private.h" #include "thr_private.h"
__weak_reference(_pthread_set_name_np, pthread_set_name_np);
static void 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); free(thread->name);
thread->name = name != NULL ? strdup(name) : NULL; thread->name = *tmp_name;
*tmp_name = NULL;
} }
/* Set the thread name for debug. */ /* Set the thread name. */
void __weak_reference(_pthread_setname_np, pthread_setname_np);
_pthread_set_name_np(pthread_t thread, const char *name) int
_pthread_setname_np(pthread_t thread, const char *name)
{ {
struct pthread *curthread; 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(); curthread = _get_curthread();
if (curthread == thread) { if (curthread == thread) {
res = 0;
THR_THREAD_LOCK(curthread, thread); THR_THREAD_LOCK(curthread, thread);
thr_set_name(thread->tid, name); if (thr_set_name(thread->tid, name) == -1)
thr_set_name_np(thread, name); res = errno;
else
thr_set_name_np(thread, &tmp_name);
THR_THREAD_UNLOCK(curthread, thread); THR_THREAD_UNLOCK(curthread, thread);
} else { } else {
res = ESRCH;
if (_thr_find_thread(curthread, thread, 0) == 0) { if (_thr_find_thread(curthread, thread, 0) == 0) {
if (thread->state != PS_DEAD) { if (thread->state != PS_DEAD) {
thr_set_name(thread->tid, name); if (thr_set_name(thread->tid, name) == -1) {
thr_set_name_np(thread, name); res = errno;
} else {
thr_set_name_np(thread, &tmp_name);
res = 0;
}
} }
THR_THREAD_UNLOCK(curthread, thread); 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 static void
@ -88,13 +116,14 @@ thr_get_name_np(struct pthread *thread, char *buf, size_t len)
buf[0] = '\0'; buf[0] = '\0';
} }
__weak_reference(_pthread_get_name_np, pthread_get_name_np); __weak_reference(_pthread_getname_np, pthread_getname_np);
int
void _pthread_getname_np(pthread_t thread, char *buf, size_t len)
_pthread_get_name_np(pthread_t thread, char *buf, size_t len)
{ {
struct pthread *curthread; struct pthread *curthread;
int res;
res = 0;
curthread = _get_curthread(); curthread = _get_curthread();
if (curthread == thread) { if (curthread == thread) {
THR_THREAD_LOCK(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 (_thr_find_thread(curthread, thread, 0) == 0) {
if (thread->state != PS_DEAD) if (thread->state != PS_DEAD)
thr_get_name_np(thread, buf, len); thr_get_name_np(thread, buf, len);
else
res = ESRCH;
THR_THREAD_UNLOCK(curthread, thread); THR_THREAD_UNLOCK(curthread, thread);
} else if (len > 0) } else {
buf[0] = '\0'; 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);
} }

View File

@ -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_rwlock_wrlock.3 pthread_rwlock_trywrlock.3
PTHREAD_MLINKS+=pthread_schedparam.3 pthread_getschedparam.3 \ PTHREAD_MLINKS+=pthread_schedparam.3 pthread_getschedparam.3 \
pthread_schedparam.3 pthread_setschedparam.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_MLINKS+=pthread_spin_init.3 pthread_spin_destroy.3 \
pthread_spin_lock.3 pthread_spin_trylock.3 \ pthread_spin_lock.3 pthread_spin_trylock.3 \
pthread_spin_lock.3 pthread_spin_unlock.3 pthread_spin_lock.3 pthread_spin_unlock.3

View File

@ -24,12 +24,14 @@
.\" .\"
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd August 12, 2018 .Dd June 3, 2020
.Dt PTHREAD_SET_NAME_NP 3 .Dt PTHREAD_SET_NAME_NP 3
.Os .Os
.Sh NAME .Sh NAME
.Nm pthread_get_name_np , .Nm pthread_get_name_np ,
.Nm pthread_getname_np ,
.Nm pthread_set_name_np .Nm pthread_set_name_np
.Nm pthread_setname_np
.Nd set and retrieve the thread name .Nd set and retrieve the thread name
.Sh LIBRARY .Sh LIBRARY
.Lb libpthread .Lb libpthread
@ -37,19 +39,27 @@
.In pthread_np.h .In pthread_np.h
.Ft void .Ft void
.Fn pthread_get_name_np "pthread_t thread" "char *name" "size_t len" .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 .Ft void
.Fn pthread_set_name_np "pthread_t thread" "const char *name" .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 .Sh DESCRIPTION
The The
.Fn pthread_set_name_np .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 .Fa name
to the given to the given
.Fa thread . .Fa thread .
.Pp .Pp
The The
.Fn pthread_get_name_np .Fn pthread_get_name_np
function retrieves the and
.Fn pthread_getname_np
functions retrieve the
.Fa name .Fa name
associated with associated with
.Fa thread . .Fa thread .
@ -61,7 +71,23 @@ the buffer pointed to by
.Fa name .Fa name
will be empty. will be empty.
.Sh ERRORS .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. appear inside are silently ignored.
.Sh SEE ALSO .Sh SEE ALSO
.Xr thr_set_name 2 .Xr thr_set_name 2
@ -70,6 +96,11 @@ appear inside are silently ignored.
and and
.Fn pthread_get_name_np .Fn pthread_get_name_np
are non-standard extensions. 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 .Sh AUTHORS
This manual page was written by This manual page was written by
.An Alexey Zelkin Aq Mt phantom@FreeBSD.org .An Alexey Zelkin Aq Mt phantom@FreeBSD.org