Skip to content

Commit c6d324d

Browse files
committed
pythongh-59705: Add _thread.set_name() function
On Linux, threading.Thread now sets the thread name to the operating system. configure now checks if pthread_setname_np() function is available.
1 parent 3cf83d9 commit c6d324d

File tree

7 files changed

+139
-2
lines changed

7 files changed

+139
-2
lines changed

Lib/threading.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
__all__.append('get_native_id')
4949
except AttributeError:
5050
_HAVE_THREAD_NATIVE_ID = False
51+
try:
52+
_set_name = _thread.set_name
53+
_HAVE_SET_NAME = True
54+
except AttributeError:
55+
_HAVE_SET_NAME = False
5156
ThreadError = _thread.error
5257
try:
5358
_CRLock = _thread.RLock
@@ -1027,6 +1032,8 @@ def _bootstrap_inner(self):
10271032
self._set_ident()
10281033
if _HAVE_THREAD_NATIVE_ID:
10291034
self._set_native_id()
1035+
if _HAVE_SET_NAME and self._name:
1036+
_set_name(self._name)
10301037
self._started.set()
10311038
with _active_limbo_lock:
10321039
_active[self._ident] = self
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
On Linux, :class:`threading.Thread` now sets the thread name to the
2+
operating system. Patch by Victor Stinner.

Modules/_threadmodule.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# include <signal.h> // SIGINT
1818
#endif
1919

20+
#include "clinic/_threadmodule.c.h"
21+
2022
// ThreadError is just an alias to PyExc_RuntimeError
2123
#define ThreadError PyExc_RuntimeError
2224

@@ -44,6 +46,13 @@ get_thread_state(PyObject *module)
4446
return (thread_module_state *)state;
4547
}
4648

49+
50+
/*[clinic input]
51+
module _thread
52+
[clinic start generated code]*/
53+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=be8dbe5cc4b16df7]*/
54+
55+
4756
// _ThreadHandle type
4857

4958
// Handles state transitions according to the following diagram:
@@ -2354,6 +2363,27 @@ PyDoc_STRVAR(thread__get_main_thread_ident_doc,
23542363
Internal only. Return a non-zero integer that uniquely identifies the main thread\n\
23552364
of the main interpreter.");
23562365

2366+
2367+
#ifdef HAVE_PTHREAD_SETNAME_NP
2368+
/*[clinic input]
2369+
_thread.set_name
2370+
2371+
name: str
2372+
2373+
Set the name of the current thread.
2374+
[clinic start generated code]*/
2375+
2376+
static PyObject *
2377+
_thread_set_name_impl(PyObject *module, const char *name)
2378+
/*[clinic end generated code: output=f051ce549bcd6b8e input=7e29bbdbfb046a04]*/
2379+
{
2380+
pthread_t thread = pthread_self();
2381+
pthread_setname_np(thread, name);
2382+
Py_RETURN_NONE;
2383+
}
2384+
#endif // HAVE_PTHREAD_SETNAME_NP
2385+
2386+
23572387
static PyMethodDef thread_methods[] = {
23582388
{"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
23592389
METH_VARARGS, start_new_thread_doc},
@@ -2393,6 +2423,7 @@ static PyMethodDef thread_methods[] = {
23932423
METH_O, thread__make_thread_handle_doc},
23942424
{"_get_main_thread_ident", thread__get_main_thread_ident,
23952425
METH_NOARGS, thread__get_main_thread_ident_doc},
2426+
_THREAD_SET_NAME_METHODDEF
23962427
{NULL, NULL} /* sentinel */
23972428
};
23982429

Modules/clinic/_threadmodule.c.h

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5105,8 +5105,10 @@ AC_CHECK_FUNCS([ \
51055105
mknod mknodat mktime mmap mremap nice openat opendir pathconf pause pipe \
51065106
pipe2 plock poll posix_fadvise posix_fallocate posix_openpt posix_spawn posix_spawnp \
51075107
posix_spawn_file_actions_addclosefrom_np \
5108-
pread preadv preadv2 process_vm_readv pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \
5109-
pthread_kill ptsname ptsname_r pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
5108+
pread preadv preadv2 process_vm_readv \
5109+
pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \
5110+
pthread_kill pthread_setname_np \
5111+
ptsname ptsname_r pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
51105112
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
51115113
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \
51125114
sem_timedwait sem_unlink sendfile setegid seteuid setgid sethostname \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,9 @@
990990
/* Define to 1 if you have the `pthread_kill' function. */
991991
#undef HAVE_PTHREAD_KILL
992992

993+
/* Define to 1 if you have the `pthread_setname_np' function. */
994+
#undef HAVE_PTHREAD_SETNAME_NP
995+
993996
/* Define to 1 if you have the `pthread_sigmask' function. */
994997
#undef HAVE_PTHREAD_SIGMASK
995998

0 commit comments

Comments
 (0)