Skip to content

gh-131268: implement thread names on OpenBSD #131528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2392,8 +2392,16 @@ PyDoc_STRVAR(thread__get_main_thread_ident_doc,
Internal only. Return a non-zero integer that uniquely identifies the main thread\n\
of the main interpreter.");

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

#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(MS_WINDOWS)
#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(HAVE_PTHREAD_GET_NAME_NP) || defined(MS_WINDOWS)
/*[clinic input]
_thread._get_name

Expand All @@ -2408,7 +2416,12 @@ _thread__get_name_impl(PyObject *module)
// Linux and macOS are limited to respectively 16 and 64 bytes
char name[100];
pthread_t thread = pthread_self();
#ifdef HAVE_PTHREAD_GETNAME_NP
int rc = pthread_getname_np(thread, name, Py_ARRAY_LENGTH(name));
#else /* defined(HAVE_PTHREAD_GET_NAME_NP) */
int rc = 0; /* pthread_get_name_np() returns void */
pthread_get_name_np(thread, name, Py_ARRAY_LENGTH(name));
#endif
if (rc) {
errno = rc;
return PyErr_SetFromErrno(PyExc_OSError);
Expand All @@ -2435,10 +2448,10 @@ _thread__get_name_impl(PyObject *module)
return name_obj;
#endif
}
#endif // HAVE_PTHREAD_GETNAME_NP
#endif // HAVE_PTHREAD_GETNAME_NP || HAVE_PTHREAD_GET_NAME_NP || MS_WINDOWS


#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(MS_WINDOWS)
#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP) || defined(MS_WINDOWS)
/*[clinic input]
_thread.set_name

Expand Down Expand Up @@ -2487,9 +2500,13 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
#elif defined(__NetBSD__)
pthread_t thread = pthread_self();
int rc = pthread_setname_np(thread, "%s", (void *)name);
#else
#elif defined(HAVE_PTHREAD_SETNAME_NP)
pthread_t thread = pthread_self();
int rc = pthread_setname_np(thread, name);
#else /* defined(HAVE_PTHREAD_SET_NAME_NP) */
pthread_t thread = pthread_self();
int rc = 0; /* pthread_set_name_np() returns void */
pthread_set_name_np(thread, name);
#endif
Py_DECREF(name_encoded);
if (rc) {
Expand Down Expand Up @@ -2527,7 +2544,7 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
Py_RETURN_NONE;
#endif
}
#endif // HAVE_PTHREAD_SETNAME_NP
#endif // HAVE_PTHREAD_SETNAME_NP || HAVE_PTHREAD_SET_NAME_NP || MS_WINDOWS


static PyMethodDef thread_methods[] = {
Expand Down
10 changes: 5 additions & 5 deletions Modules/clinic/_threadmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5147,7 +5147,8 @@ AC_CHECK_FUNCS([ \
posix_spawn_file_actions_addclosefrom_np \
pread preadv preadv2 process_vm_readv \
pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \
pthread_kill pthread_getname_np pthread_setname_np pthread_getattr_np \
pthread_kill pthread_get_name_np pthread_getname_np pthread_set_name_np
pthread_setname_np pthread_getattr_np \
ptsname ptsname_r pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \
Expand Down Expand Up @@ -7563,6 +7564,7 @@ case "$ac_sys_system" in
Darwin) _PYTHREAD_NAME_MAXLEN=63;;
iOS) _PYTHREAD_NAME_MAXLEN=63;;
FreeBSD*) _PYTHREAD_NAME_MAXLEN=19;; # gh-131268
OpenBSD*) _PYTHREAD_NAME_MAXLEN=23;; # gh-131268
*) _PYTHREAD_NAME_MAXLEN=;;
esac
if test -n "$_PYTHREAD_NAME_MAXLEN"; then
Expand Down
6 changes: 6 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,9 @@
/* Define to 1 if you have the 'pthread_getname_np' function. */
#undef HAVE_PTHREAD_GETNAME_NP

/* Define to 1 if you have the 'pthread_get_name_np' function. */
#undef HAVE_PTHREAD_GET_NAME_NP

/* Define to 1 if you have the <pthread.h> header file. */
#undef HAVE_PTHREAD_H

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

/* Define to 1 if you have the 'pthread_set_name_np' function. */
#undef HAVE_PTHREAD_SET_NAME_NP

/* Define to 1 if you have the 'pthread_sigmask' function. */
#undef HAVE_PTHREAD_SIGMASK

Expand Down
Loading