https://github.com/python/cpython/commit/b70d45ab22e1d0f3b8a0b6ff5ff71157da5f2dfb
commit: b70d45ab22e1d0f3b8a0b6ff5ff71157da5f2dfb
branch: main
author: Xavier G. <xavie...@users.noreply.github.com>
committer: vstinner <vstin...@python.org>
date: 2025-03-21T11:12:35+01:00
summary:

gh-131268: Implement thread names on OpenBSD (#131528)

files:
M Modules/_threadmodule.c
M Modules/clinic/_threadmodule.c.h
M configure
M configure.ac
M pyconfig.h.in

diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 643f5dc47ab7a7..53b2e685a577db 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -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
 
@@ -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);
@@ -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
 
@@ -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) {
@@ -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[] = {
diff --git a/Modules/clinic/_threadmodule.c.h b/Modules/clinic/_threadmodule.c.h
index 09b7afebd6d8d9..367831c25a96c6 100644
--- a/Modules/clinic/_threadmodule.c.h
+++ b/Modules/clinic/_threadmodule.c.h
@@ -8,7 +8,7 @@ preserve
 #endif
 #include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
 
-#if (defined(HAVE_PTHREAD_GETNAME_NP) || defined(MS_WINDOWS))
+#if (defined(HAVE_PTHREAD_GETNAME_NP) || defined(HAVE_PTHREAD_GET_NAME_NP) || 
defined(MS_WINDOWS))
 
 PyDoc_STRVAR(_thread__get_name__doc__,
 "_get_name($module, /)\n"
@@ -28,9 +28,9 @@ _thread__get_name(PyObject *module, PyObject 
*Py_UNUSED(ignored))
     return _thread__get_name_impl(module);
 }
 
-#endif /* (defined(HAVE_PTHREAD_GETNAME_NP) || defined(MS_WINDOWS)) */
+#endif /* (defined(HAVE_PTHREAD_GETNAME_NP) || 
defined(HAVE_PTHREAD_GET_NAME_NP) || defined(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))
 
 PyDoc_STRVAR(_thread_set_name__doc__,
 "set_name($module, /, name)\n"
@@ -92,7 +92,7 @@ _thread_set_name(PyObject *module, PyObject *const *args, 
Py_ssize_t nargs, PyOb
     return return_value;
 }
 
-#endif /* (defined(HAVE_PTHREAD_SETNAME_NP) || defined(MS_WINDOWS)) */
+#endif /* (defined(HAVE_PTHREAD_SETNAME_NP) || 
defined(HAVE_PTHREAD_SET_NAME_NP) || defined(MS_WINDOWS)) */
 
 #ifndef _THREAD__GET_NAME_METHODDEF
     #define _THREAD__GET_NAME_METHODDEF
@@ -101,4 +101,4 @@ _thread_set_name(PyObject *module, PyObject *const *args, 
Py_ssize_t nargs, PyOb
 #ifndef _THREAD_SET_NAME_METHODDEF
     #define _THREAD_SET_NAME_METHODDEF
 #endif /* !defined(_THREAD_SET_NAME_METHODDEF) */
-/*[clinic end generated code: output=6e88ef6b126cece8 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=c7922811558d314f input=a9049054013a1b77]*/
diff --git a/configure b/configure
index c4b5e074254332..a058553480ca5a 100755
--- a/configure
+++ b/configure
@@ -19608,12 +19608,24 @@ if test "x$ac_cv_func_pthread_kill" = xyes
 then :
   printf "%s\n" "#define HAVE_PTHREAD_KILL 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "pthread_get_name_np" 
"ac_cv_func_pthread_get_name_np"
+if test "x$ac_cv_func_pthread_get_name_np" = xyes
+then :
+  printf "%s\n" "#define HAVE_PTHREAD_GET_NAME_NP 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "pthread_getname_np" 
"ac_cv_func_pthread_getname_np"
 if test "x$ac_cv_func_pthread_getname_np" = xyes
 then :
   printf "%s\n" "#define HAVE_PTHREAD_GETNAME_NP 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "pthread_set_name_np" 
"ac_cv_func_pthread_set_name_np"
+if test "x$ac_cv_func_pthread_set_name_np" = xyes
+then :
+  printf "%s\n" "#define HAVE_PTHREAD_SET_NAME_NP 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "pthread_setname_np" 
"ac_cv_func_pthread_setname_np"
 if test "x$ac_cv_func_pthread_setname_np" = xyes
@@ -30455,6 +30467,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
diff --git a/configure.ac b/configure.ac
index a9ebfc7883120f..23bd81ed4431b9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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 \
@@ -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
diff --git a/pyconfig.h.in b/pyconfig.h.in
index f12517301811dc..dbf7865447bc2e 100644
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -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
 
@@ -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
 

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to