bpo-41247: asyncio.set_running_loop() cache running loop holder (GH-21401)

The running loop holder cache variable was always set to NULL when
calling set_running_loop.

Now set_running_loop saves the newly created running loop holder in the
cache variable for faster access in get_running_loop.

Automerge-Triggered-By: @1st1
This commit is contained in:
Tony Solomonik 2020-07-08 22:27:31 +03:00 committed by GitHub
parent b26a0db8ea
commit 529f42645d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -0,0 +1,2 @@
Always cache the running loop holder when running
``asyncio.set_running_loop``.

View File

@ -291,10 +291,13 @@ error:
static int static int
set_running_loop(PyObject *loop) set_running_loop(PyObject *loop)
{ {
cached_running_holder = NULL; PyObject *ts_dict = NULL;
cached_running_holder_tsid = 0;
PyThreadState *tstate = PyThreadState_Get();
if (tstate != NULL) {
ts_dict = _PyThreadState_GetDict(tstate); // borrowed
}
PyObject *ts_dict = PyThreadState_GetDict(); // borrowed
if (ts_dict == NULL) { if (ts_dict == NULL) {
PyErr_SetString( PyErr_SetString(
PyExc_RuntimeError, "thread-local storage is not available"); PyExc_RuntimeError, "thread-local storage is not available");
@ -314,6 +317,9 @@ set_running_loop(PyObject *loop)
} }
Py_DECREF(rl); Py_DECREF(rl);
cached_running_holder = (PyObject *)rl;
cached_running_holder_tsid = PyThreadState_GetID(tstate);
return 0; return 0;
} }