Skip to content

Commit cc98cad

Browse files
committed
pythongh-131268: implement thread names on OpenBSD
1 parent ce79274 commit cc98cad

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

Modules/_threadmodule.c

+22-5
Original file line numberDiff line numberDiff line change
@@ -2392,8 +2392,16 @@ PyDoc_STRVAR(thread__get_main_thread_ident_doc,
23922392
Internal only. Return a non-zero integer that uniquely identifies the main thread\n\
23932393
of the main interpreter.");
23942394

2395+
#if defined(__OpenBSD__)
2396+
/* pthread_*_np functions, especially pthread_{get,set}_name_np().
2397+
pthread_np.h exists on both OpenBSD and FreeBSD but the latter declares
2398+
pthread_getname_np() and pthread_setname_np() in pthread.h as long as
2399+
__BSD_VISIBLE remains set.
2400+
*/
2401+
# include <pthread_np.h>
2402+
#endif
23952403

2396-
#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(MS_WINDOWS)
2404+
#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(HAVE_PTHREAD_GET_NAME_NP) || defined(MS_WINDOWS)
23972405
/*[clinic input]
23982406
_thread._get_name
23992407
@@ -2408,7 +2416,12 @@ _thread__get_name_impl(PyObject *module)
24082416
// Linux and macOS are limited to respectively 16 and 64 bytes
24092417
char name[100];
24102418
pthread_t thread = pthread_self();
2419+
#ifdef HAVE_PTHREAD_GETNAME_NP
24112420
int rc = pthread_getname_np(thread, name, Py_ARRAY_LENGTH(name));
2421+
#else /* defined(HAVE_PTHREAD_GET_NAME_NP) */
2422+
int rc = 0; /* pthread_get_name_np() returns void */
2423+
pthread_get_name_np(thread, name, Py_ARRAY_LENGTH(name));
2424+
#endif
24122425
if (rc) {
24132426
errno = rc;
24142427
return PyErr_SetFromErrno(PyExc_OSError);
@@ -2435,10 +2448,10 @@ _thread__get_name_impl(PyObject *module)
24352448
return name_obj;
24362449
#endif
24372450
}
2438-
#endif // HAVE_PTHREAD_GETNAME_NP
2451+
#endif // HAVE_PTHREAD_GETNAME_NP || HAVE_PTHREAD_GET_NAME_NP || MS_WINDOWS
24392452

24402453

2441-
#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(MS_WINDOWS)
2454+
#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP) || defined(MS_WINDOWS)
24422455
/*[clinic input]
24432456
_thread.set_name
24442457
@@ -2487,9 +2500,13 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
24872500
#elif defined(__NetBSD__)
24882501
pthread_t thread = pthread_self();
24892502
int rc = pthread_setname_np(thread, "%s", (void *)name);
2490-
#else
2503+
#elif defined(HAVE_PTHREAD_SETNAME_NP)
24912504
pthread_t thread = pthread_self();
24922505
int rc = pthread_setname_np(thread, name);
2506+
#else /* defined(HAVE_PTHREAD_SET_NAME_NP) */
2507+
pthread_t thread = pthread_self();
2508+
int rc = 0; /* pthread_set_name_np() returns void */
2509+
pthread_set_name_np(thread, name);
24932510
#endif
24942511
Py_DECREF(name_encoded);
24952512
if (rc) {
@@ -2527,7 +2544,7 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
25272544
Py_RETURN_NONE;
25282545
#endif
25292546
}
2530-
#endif // HAVE_PTHREAD_SETNAME_NP
2547+
#endif // HAVE_PTHREAD_SETNAME_NP || HAVE_PTHREAD_SET_NAME_NP || MS_WINDOWS
25312548

25322549

25332550
static PyMethodDef thread_methods[] = {

Modules/clinic/_threadmodule.c.h

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+3-1
Original file line numberDiff line numberDiff line change
@@ -5147,7 +5147,8 @@ AC_CHECK_FUNCS([ \
51475147
posix_spawn_file_actions_addclosefrom_np \
51485148
pread preadv preadv2 process_vm_readv \
51495149
pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \
5150-
pthread_kill pthread_getname_np pthread_setname_np pthread_getattr_np \
5150+
pthread_kill pthread_get_name_np pthread_getname_np pthread_set_name_np
5151+
pthread_setname_np pthread_getattr_np \
51515152
ptsname ptsname_r pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
51525153
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
51535154
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \
@@ -7563,6 +7564,7 @@ case "$ac_sys_system" in
75637564
Darwin) _PYTHREAD_NAME_MAXLEN=63;;
75647565
iOS) _PYTHREAD_NAME_MAXLEN=63;;
75657566
FreeBSD*) _PYTHREAD_NAME_MAXLEN=19;; # gh-131268
7567+
OpenBSD*) _PYTHREAD_NAME_MAXLEN=23;; # gh-131268
75667568
*) _PYTHREAD_NAME_MAXLEN=;;
75677569
esac
75687570
if test -n "$_PYTHREAD_NAME_MAXLEN"; then

pyconfig.h.in

+6
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,9 @@
993993
/* Define to 1 if you have the 'pthread_getname_np' function. */
994994
#undef HAVE_PTHREAD_GETNAME_NP
995995

996+
/* Define to 1 if you have the 'pthread_get_name_np' function. */
997+
#undef HAVE_PTHREAD_GET_NAME_NP
998+
996999
/* Define to 1 if you have the <pthread.h> header file. */
9971000
#undef HAVE_PTHREAD_H
9981001

@@ -1005,6 +1008,9 @@
10051008
/* Define to 1 if you have the 'pthread_setname_np' function. */
10061009
#undef HAVE_PTHREAD_SETNAME_NP
10071010

1011+
/* Define to 1 if you have the 'pthread_set_name_np' function. */
1012+
#undef HAVE_PTHREAD_SET_NAME_NP
1013+
10081014
/* Define to 1 if you have the 'pthread_sigmask' function. */
10091015
#undef HAVE_PTHREAD_SIGMASK
10101016

0 commit comments

Comments
 (0)