Skip to content

Commit 5c3cda0

Browse files
authored
bpo-39947: Add PyThreadState_GetID() function (GH-19163)
Add PyThreadState_GetID() function: get the unique identifier of a Python thread state.
1 parent 0e427c6 commit 5c3cda0

File tree

6 files changed

+25
-2
lines changed

6 files changed

+25
-2
lines changed

Doc/c-api/init.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,15 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
10841084
.. versionadded:: 3.9
10851085
10861086
1087+
.. c:function:: uint64_t PyThreadState_GetID(PyThreadState *tstate)
1088+
1089+
Get the unique thread state identifier of the Python thread state *tstate*.
1090+
1091+
*tstate* must not be ``NULL``.
1092+
1093+
.. versionadded:: 3.9
1094+
1095+
10871096
.. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate)
10881097
10891098
Get the interpreter of the Python thread state *tstate*.

Doc/whatsnew/3.9.rst

+2
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ Build and C API Changes
429429
:c:func:`PyInterpreterState_Get` functions to get the interpreter.
430430
New :c:func:`PyThreadState_GetFrame` function to get the current frame of a
431431
Python thread state.
432+
New :c:func:`PyThreadState_GetID` function: get the unique identifier of a
433+
Python thread state.
432434
(Contributed by Victor Stinner in :issue:`39947`.)
433435

434436
* Add ``--with-platlibdir`` option to the ``configure`` script: name of the

Include/pystate.h

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
9191
/* New in 3.9 */
9292
PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);
9393
PyAPI_FUNC(struct _frame*) PyThreadState_GetFrame(PyThreadState *tstate);
94+
PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate);
9495
#endif
9596

9697
typedef
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:func:`PyThreadState_GetID` function: get the unique identifier of a
2+
Python thread state.

Modules/_asynciomodule.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ get_running_loop(PyObject **loop)
231231
PyObject *rl;
232232

233233
PyThreadState *ts = PyThreadState_Get();
234-
if (ts->id == cached_running_holder_tsid && cached_running_holder != NULL) {
234+
uint64_t ts_id = PyThreadState_GetID(ts);
235+
if (ts_id == cached_running_holder_tsid && cached_running_holder != NULL) {
235236
// Fast path, check the cache.
236237
rl = cached_running_holder; // borrowed
237238
}
@@ -253,7 +254,7 @@ get_running_loop(PyObject **loop)
253254
}
254255

255256
cached_running_holder = rl; // borrowed
256-
cached_running_holder_tsid = ts->id;
257+
cached_running_holder_tsid = ts_id;
257258
}
258259

259260
assert(Py_IS_TYPE(rl, &PyRunningLoopHolder_Type));

Python/pystate.c

+8
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,14 @@ PyThreadState_GetFrame(PyThreadState *tstate)
10211021
}
10221022

10231023

1024+
uint64_t
1025+
PyThreadState_GetID(PyThreadState *tstate)
1026+
{
1027+
assert(tstate != NULL);
1028+
return tstate->id;
1029+
}
1030+
1031+
10241032
/* Asynchronously raise an exception in a thread.
10251033
Requested by Just van Rossum and Alex Martelli.
10261034
To prevent naive misuse, you must write your own extension

0 commit comments

Comments
 (0)