summaryrefslogtreecommitdiffstats
path: root/Modules/_threadmodule.c
diff options
context:
space:
mode:
authorJake Tesler <jake.tesler@gmail.com>2019-05-22 15:43:17 (GMT)
committerVictor Stinner <vstinner@redhat.com>2019-05-22 15:43:16 (GMT)
commitb121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5 (patch)
treef8e48c2d296e82b1378bb2bf25a34e1ca8321c85 /Modules/_threadmodule.c
parentb3be4072888a4ce054993c2801802721466ea02d (diff)
downloadcpython-b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5.zip
cpython-b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5.tar.gz
cpython-b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5.tar.bz2
bpo-36084: Add native thread ID (TID) to threading.Thread (GH-13463)
Add native thread ID (TID) to threading.Thread objects (supported platforms: Windows, FreeBSD, Linux, macOS).
Diffstat (limited to 'Modules/_threadmodule.c')
-rw-r--r--Modules/_threadmodule.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 3c02d8d..fee25ab 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1159,6 +1159,22 @@ allocated consecutive numbers starting at 1, this behavior should not\n\
be relied upon, and the number should be seen purely as a magic cookie.\n\
A thread's identity may be reused for another thread after it exits.");
+#ifdef PY_HAVE_THREAD_NATIVE_ID
+static PyObject *
+thread_get_native_id(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ unsigned long native_id = PyThread_get_thread_native_id();
+ return PyLong_FromUnsignedLong(native_id);
+}
+
+PyDoc_STRVAR(get_native_id_doc,
+"get_native_id() -> integer\n\
+\n\
+Return a non-negative integer identifying the thread as reported\n\
+by the OS (kernel). This may be used to uniquely identify a\n\
+particular thread within a system.");
+#endif
+
static PyObject *
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
{
@@ -1310,6 +1326,10 @@ static PyMethodDef thread_methods[] = {
METH_NOARGS, interrupt_doc},
{"get_ident", thread_get_ident,
METH_NOARGS, get_ident_doc},
+#ifdef PY_HAVE_THREAD_NATIVE_ID
+ {"get_native_id", thread_get_native_id,
+ METH_NOARGS, get_native_id_doc},
+#endif
{"_count", thread__count,
METH_NOARGS, _count_doc},
{"stack_size", (PyCFunction)thread_stack_size,