[Python-checkins] bpo-39947: Add PyThreadState_GetID() function (GH-19163)

Victor Stinner webhook-mailer at python.org
Wed Mar 25 16:23:57 EDT 2020


https://github.com/python/cpython/commit/5c3cda0d1a850a1a9b43892f48376b8876bd5863
commit: 5c3cda0d1a850a1a9b43892f48376b8876bd5863
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-03-25T21:23:53+01:00
summary:

bpo-39947: Add PyThreadState_GetID() function (GH-19163)

Add PyThreadState_GetID() function: get the unique identifier of a
Python thread state.

files:
A Misc/NEWS.d/next/C API/2020-03-25-19-44-55.bpo-39947.2OxvPt.rst
M Doc/c-api/init.rst
M Doc/whatsnew/3.9.rst
M Include/pystate.h
M Modules/_asynciomodule.c
M Python/pystate.c

diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst
index 294c1b9f72339..f78ab99d5b360 100644
--- a/Doc/c-api/init.rst
+++ b/Doc/c-api/init.rst
@@ -1084,6 +1084,15 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
    .. versionadded:: 3.9
 
 
+.. c:function:: uint64_t PyThreadState_GetID(PyThreadState *tstate)
+
+   Get the unique thread state identifier of the Python thread state *tstate*.
+
+   *tstate* must not be ``NULL``.
+
+   .. versionadded:: 3.9
+
+
 .. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate)
 
    Get the interpreter of the Python thread state *tstate*.
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index b11c29bdf090f..778e443f8d077 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -429,6 +429,8 @@ Build and C API Changes
   :c:func:`PyInterpreterState_Get` functions to get the interpreter.
   New :c:func:`PyThreadState_GetFrame` function to get the current frame of a
   Python thread state.
+  New :c:func:`PyThreadState_GetID` function: get the unique identifier of a
+  Python thread state.
   (Contributed by Victor Stinner in :issue:`39947`.)
 
 * Add ``--with-platlibdir`` option to the ``configure`` script: name of the
diff --git a/Include/pystate.h b/Include/pystate.h
index 5866fef3d0952..c46d3fef15d77 100644
--- a/Include/pystate.h
+++ b/Include/pystate.h
@@ -91,6 +91,7 @@ PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
 /* New in 3.9 */
 PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);
 PyAPI_FUNC(struct _frame*) PyThreadState_GetFrame(PyThreadState *tstate);
+PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate);
 #endif
 
 typedef
diff --git a/Misc/NEWS.d/next/C API/2020-03-25-19-44-55.bpo-39947.2OxvPt.rst b/Misc/NEWS.d/next/C API/2020-03-25-19-44-55.bpo-39947.2OxvPt.rst
new file mode 100644
index 0000000000000..e9910a544436e
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2020-03-25-19-44-55.bpo-39947.2OxvPt.rst	
@@ -0,0 +1,2 @@
+Add :c:func:`PyThreadState_GetID` function: get the unique identifier of a
+Python thread state.
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index 1092b08411474..c10866aba7ce3 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -231,7 +231,8 @@ get_running_loop(PyObject **loop)
     PyObject *rl;
 
     PyThreadState *ts = PyThreadState_Get();
-    if (ts->id == cached_running_holder_tsid && cached_running_holder != NULL) {
+    uint64_t ts_id = PyThreadState_GetID(ts);
+    if (ts_id == cached_running_holder_tsid && cached_running_holder != NULL) {
         // Fast path, check the cache.
         rl = cached_running_holder;  // borrowed
     }
@@ -253,7 +254,7 @@ get_running_loop(PyObject **loop)
         }
 
         cached_running_holder = rl;  // borrowed
-        cached_running_holder_tsid = ts->id;
+        cached_running_holder_tsid = ts_id;
     }
 
     assert(Py_IS_TYPE(rl, &PyRunningLoopHolder_Type));
diff --git a/Python/pystate.c b/Python/pystate.c
index 66a1d3b492d4b..246665b2810b5 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1021,6 +1021,14 @@ PyThreadState_GetFrame(PyThreadState *tstate)
 }
 
 
+uint64_t
+PyThreadState_GetID(PyThreadState *tstate)
+{
+    assert(tstate != NULL);
+    return tstate->id;
+}
+
+
 /* Asynchronously raise an exception in a thread.
    Requested by Just van Rossum and Alex Martelli.
    To prevent naive misuse, you must write your own extension



More information about the Python-checkins mailing list