gh-59705: Add _thread.set_name() function (#127338)

On Linux, threading.Thread now sets the thread name to the operating
system.

* configure now checks if pthread_getname_np()
  and pthread_setname_np() functions are available.
* Add PYTHREAD_NAME_MAXLEN macro.
* Add _thread._NAME_MAXLEN constant for test_threading.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Victor Stinner 2024-12-06 17:27:12 +01:00 committed by GitHub
parent 12680ec5bd
commit 67b18a18b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 342 additions and 2 deletions

View file

@ -48,6 +48,10 @@
__all__.append('get_native_id')
except AttributeError:
_HAVE_THREAD_NATIVE_ID = False
try:
_set_name = _thread.set_name
except AttributeError:
_set_name = None
ThreadError = _thread.error
try:
_CRLock = _thread.RLock
@ -1027,6 +1031,11 @@ def _bootstrap_inner(self):
self._set_ident()
if _HAVE_THREAD_NATIVE_ID:
self._set_native_id()
if _set_name is not None and self._name:
try:
_set_name(self._name)
except OSError:
pass
self._started.set()
with _active_limbo_lock:
_active[self._ident] = self