aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/pystate.c
diff options
context:
space:
mode:
authorKumar Aditya <kumaraditya@python.org>2025-02-07 00:21:07 +0530
committerGitHub <noreply@github.com>2025-02-06 19:51:07 +0100
commit0d68b14a0d8f493b2f403f64608bcfc055457053 (patch)
treed074c86070595096fc1530dcfcf69fd1fecaefee /Python/pystate.c
parentb4ff8b22b3066b814c3758f87eaddfa923e657ed (diff)
downloadcpython-0d68b14a0d8f493b2f403f64608bcfc055457053.tar.gz
cpython-0d68b14a0d8f493b2f403f64608bcfc055457053.zip
gh-128002: use per threads tasks linked list in asyncio (#128869)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index e6770ef40df..89a652850e9 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -643,6 +643,8 @@ init_interpreter(PyInterpreterState *interp,
_Py_brc_init_state(interp);
#endif
llist_init(&interp->mem_free_queue.head);
+ llist_init(&interp->asyncio_tasks_head);
+ interp->asyncio_tasks_lock = (PyMutex){0};
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
interp->monitors.tools[i] = 0;
}
@@ -1512,7 +1514,7 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->delete_later = NULL;
llist_init(&_tstate->mem_free_queue);
-
+ llist_init(&_tstate->asyncio_tasks_head);
if (interp->stoptheworld.requested || _PyRuntime.stoptheworld.requested) {
// Start in the suspended state if there is an ongoing stop-the-world.
tstate->state = _Py_THREAD_SUSPENDED;
@@ -1692,6 +1694,14 @@ PyThreadState_Clear(PyThreadState *tstate)
Py_CLEAR(((_PyThreadStateImpl *)tstate)->asyncio_running_loop);
Py_CLEAR(((_PyThreadStateImpl *)tstate)->asyncio_running_task);
+
+ PyMutex_Lock(&tstate->interp->asyncio_tasks_lock);
+ // merge any lingering tasks from thread state to interpreter's
+ // tasks list
+ llist_concat(&tstate->interp->asyncio_tasks_head,
+ &((_PyThreadStateImpl *)tstate)->asyncio_tasks_head);
+ PyMutex_Unlock(&tstate->interp->asyncio_tasks_lock);
+
Py_CLEAR(tstate->dict);
Py_CLEAR(tstate->async_exc);