Skip to content

bpo-39947: Add PyThreadState_GetID() function #19163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:func:`PyThreadState_GetID` function: get the unique identifier of a
Python thread state.
5 changes: 3 additions & 2 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -252,7 +253,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));
Expand Down
8 changes: 8 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,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
Expand Down