Skip to content

Commit 2ef84b7

Browse files
committed
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
1 parent 3b23ffe commit 2ef84b7

File tree

7 files changed

+106
-22
lines changed

7 files changed

+106
-22
lines changed

Diff for: include/pthread.h

+3
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ void pthread_testcancel(void);
301301
int pthread_getprio(pthread_t);
302302
int pthread_setprio(pthread_t, int);
303303
void pthread_yield(void);
304+
305+
int pthread_getname_np(pthread_t, char *, size_t);
306+
int pthread_setname_np(pthread_t, const char *);
304307
#endif
305308

306309
int pthread_mutexattr_getprioceiling(

Diff for: lib/libc/include/namespace.h

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
#define pthread_getaffinity_np _pthread_getaffinity_np
139139
#define pthread_getconcurrency _pthread_getconcurrency
140140
#define pthread_getcpuclockid _pthread_getcpuclockid
141+
#define pthread_getname_np _pthread_getname_np
141142
#define pthread_getprio _pthread_getprio
142143
#define pthread_getschedparam _pthread_getschedparam
143144
#define pthread_getspecific _pthread_getspecific
@@ -191,6 +192,7 @@
191192
#define pthread_setcancelstate _pthread_setcancelstate
192193
#define pthread_setcanceltype _pthread_setcanceltype
193194
#define pthread_setconcurrency _pthread_setconcurrency
195+
#define pthread_setname_np _pthread_setname_np
194196
#define pthread_setprio _pthread_setprio
195197
#define pthread_setschedparam _pthread_setschedparam
196198
#define pthread_setspecific _pthread_setspecific

Diff for: lib/libc/include/un-namespace.h

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#undef pthread_getaffinity_np
120120
#undef pthread_getconcurrency
121121
#undef pthread_getcpuclockid
122+
#undef pthread_getname_np
122123
#undef pthread_getprio
123124
#undef pthread_getschedparam
124125
#undef pthread_getspecific
@@ -172,6 +173,7 @@
172173
#undef pthread_setcancelstate
173174
#undef pthread_setcanceltype
174175
#undef pthread_setconcurrency
176+
#undef pthread_setname_np
175177
#undef pthread_setprio
176178
#undef pthread_setschedparam
177179
#undef pthread_setspecific

Diff for: lib/libthr/pthread.map

+2
Original file line numberDiff line numberDiff line change
@@ -328,5 +328,7 @@ FBSD_1.5 {
328328
};
329329

330330
FBSD_1.6 {
331+
pthread_getname_np;
331332
pthread_peekjoin_np;
333+
pthread_setname_np;
332334
};

Diff for: lib/libthr/thread/thr_info.c

+59-17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
__FBSDID("$FreeBSD$");
3838

3939
#include "namespace.h"
40+
#include <sys/errno.h>
4041
#include <stdlib.h>
4142
#include <string.h>
4243
#include <pthread.h>
@@ -45,37 +46,64 @@ __FBSDID("$FreeBSD$");
4546

4647
#include "thr_private.h"
4748

48-
__weak_reference(_pthread_set_name_np, pthread_set_name_np);
49-
5049
static void
51-
thr_set_name_np(struct pthread *thread, const char *name)
50+
thr_set_name_np(struct pthread *thread, char **tmp_name)
5251
{
5352

5453
free(thread->name);
55-
thread->name = name != NULL ? strdup(name) : NULL;
54+
thread->name = *tmp_name;
55+
*tmp_name = NULL;
5656
}
5757

58-
/* Set the thread name for debug. */
59-
void
60-
_pthread_set_name_np(pthread_t thread, const char *name)
58+
/* Set the thread name. */
59+
__weak_reference(_pthread_setname_np, pthread_setname_np);
60+
int
61+
_pthread_setname_np(pthread_t thread, const char *name)
6162
{
6263
struct pthread *curthread;
64+
char *tmp_name;
65+
int res;
6366

67+
if (name != NULL) {
68+
tmp_name = strdup(name);
69+
if (tmp_name == NULL)
70+
return (ENOMEM);
71+
} else {
72+
tmp_name = NULL;
73+
}
6474
curthread = _get_curthread();
6575
if (curthread == thread) {
76+
res = 0;
6677
THR_THREAD_LOCK(curthread, thread);
67-
thr_set_name(thread->tid, name);
68-
thr_set_name_np(thread, name);
78+
if (thr_set_name(thread->tid, name) == -1)
79+
res = errno;
80+
else
81+
thr_set_name_np(thread, &tmp_name);
6982
THR_THREAD_UNLOCK(curthread, thread);
7083
} else {
84+
res = ESRCH;
7185
if (_thr_find_thread(curthread, thread, 0) == 0) {
7286
if (thread->state != PS_DEAD) {
73-
thr_set_name(thread->tid, name);
74-
thr_set_name_np(thread, name);
87+
if (thr_set_name(thread->tid, name) == -1) {
88+
res = errno;
89+
} else {
90+
thr_set_name_np(thread, &tmp_name);
91+
res = 0;
92+
}
7593
}
7694
THR_THREAD_UNLOCK(curthread, thread);
7795
}
7896
}
97+
free(tmp_name);
98+
return (res);
99+
}
100+
101+
/* Set the thread name for debug. */
102+
__weak_reference(_pthread_set_name_np, pthread_set_name_np);
103+
void
104+
_pthread_set_name_np(pthread_t thread, const char *name)
105+
{
106+
(void)_pthread_setname_np(thread, name);
79107
}
80108

81109
static void
@@ -88,13 +116,14 @@ thr_get_name_np(struct pthread *thread, char *buf, size_t len)
88116
buf[0] = '\0';
89117
}
90118

91-
__weak_reference(_pthread_get_name_np, pthread_get_name_np);
92-
93-
void
94-
_pthread_get_name_np(pthread_t thread, char *buf, size_t len)
119+
__weak_reference(_pthread_getname_np, pthread_getname_np);
120+
int
121+
_pthread_getname_np(pthread_t thread, char *buf, size_t len)
95122
{
96123
struct pthread *curthread;
124+
int res;
97125

126+
res = 0;
98127
curthread = _get_curthread();
99128
if (curthread == thread) {
100129
THR_THREAD_LOCK(curthread, thread);
@@ -104,8 +133,21 @@ _pthread_get_name_np(pthread_t thread, char *buf, size_t len)
104133
if (_thr_find_thread(curthread, thread, 0) == 0) {
105134
if (thread->state != PS_DEAD)
106135
thr_get_name_np(thread, buf, len);
136+
else
137+
res = ESRCH;
107138
THR_THREAD_UNLOCK(curthread, thread);
108-
} else if (len > 0)
109-
buf[0] = '\0';
139+
} else {
140+
res = ESRCH;
141+
if (len > 0)
142+
buf[0] = '\0';
143+
}
110144
}
145+
return (res);
146+
}
147+
148+
__weak_reference(_pthread_get_name_np, pthread_get_name_np);
149+
void
150+
_pthread_get_name_np(pthread_t thread, char *buf, size_t len)
151+
{
152+
(void)_pthread_getname_np(thread, buf, len);
111153
}

Diff for: share/man/man3/Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,9 @@ PTHREAD_MLINKS+=pthread_rwlock_rdlock.3 pthread_rwlock_tryrdlock.3
493493
PTHREAD_MLINKS+=pthread_rwlock_wrlock.3 pthread_rwlock_trywrlock.3
494494
PTHREAD_MLINKS+=pthread_schedparam.3 pthread_getschedparam.3 \
495495
pthread_schedparam.3 pthread_setschedparam.3
496-
PTHREAD_MLINKS+=pthread_set_name_np.3 pthread_get_name_np.3
496+
PTHREAD_MLINKS+=pthread_set_name_np.3 pthread_get_name_np.3 \
497+
pthread_set_name_np.3 pthread_getname_np.3 \
498+
pthread_set_name_np.3 pthread_setname_np.3
497499
PTHREAD_MLINKS+=pthread_spin_init.3 pthread_spin_destroy.3 \
498500
pthread_spin_lock.3 pthread_spin_trylock.3 \
499501
pthread_spin_lock.3 pthread_spin_unlock.3

Diff for: share/man/man3/pthread_set_name_np.3

+35-4
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,42 @@
2424
.\"
2525
.\" $FreeBSD$
2626
.\"
27-
.Dd August 12, 2018
27+
.Dd June 3, 2020
2828
.Dt PTHREAD_SET_NAME_NP 3
2929
.Os
3030
.Sh NAME
3131
.Nm pthread_get_name_np ,
32+
.Nm pthread_getname_np ,
3233
.Nm pthread_set_name_np
34+
.Nm pthread_setname_np
3335
.Nd set and retrieve the thread name
3436
.Sh LIBRARY
3537
.Lb libpthread
3638
.Sh SYNOPSIS
3739
.In pthread_np.h
3840
.Ft void
3941
.Fn pthread_get_name_np "pthread_t thread" "char *name" "size_t len"
42+
.Ft int
43+
.Fn pthread_getname_np "pthread_t thread" "char *name" "size_t len"
4044
.Ft void
4145
.Fn pthread_set_name_np "pthread_t thread" "const char *name"
46+
.Ft int
47+
.Fn pthread_setname_np "pthread_t thread" "const char *name"
4248
.Sh DESCRIPTION
4349
The
4450
.Fn pthread_set_name_np
45-
function applies a copy of the given
51+
and
52+
.Fn pthread_setname_np
53+
functions apply a copy of the given
4654
.Fa name
4755
to the given
4856
.Fa thread .
4957
.Pp
5058
The
5159
.Fn pthread_get_name_np
52-
function retrieves the
60+
and
61+
.Fn pthread_getname_np
62+
functions retrieve the
5363
.Fa name
5464
associated with
5565
.Fa thread .
@@ -61,7 +71,23 @@ the buffer pointed to by
6171
.Fa name
6272
will be empty.
6373
.Sh ERRORS
64-
Because of the debugging nature of these functions, all errors that may
74+
The
75+
.Nm pthread_getname_np
76+
and
77+
.Nm pthread_setname_np
78+
will fail if
79+
.Bl -tag -width Er
80+
.It Bq Er ESRCH
81+
No thread could be found in the current process corresponding to that
82+
specified by the given thread ID
83+
.Fa thread .
84+
.El
85+
.Pp
86+
Because of the debugging nature of
87+
.Nm pthread_get_name_np
88+
and
89+
.Nm pthread_set_name_np
90+
functions, all errors that may
6591
appear inside are silently ignored.
6692
.Sh SEE ALSO
6793
.Xr thr_set_name 2
@@ -70,6 +96,11 @@ appear inside are silently ignored.
7096
and
7197
.Fn pthread_get_name_np
7298
are non-standard extensions.
99+
.Fn pthread_setname_np
100+
and
101+
.Fn pthread_getname_np
102+
are also non-standard, but are implemented by larger number of operating
103+
systems so they are in fact more portable.
73104
.Sh AUTHORS
74105
This manual page was written by
75106
.An Alexey Zelkin Aq Mt [email protected]

0 commit comments

Comments
 (0)