8000 gh-121621: Move asyncio running loop to thread state by Fidget-Spinner · Pull Request #121695 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-121621: Move asyncio running loop to thread state #121695

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 21 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
Fidget-Spinner committed Jul 12, 2024
commit ba3d77174807733e3d6dd0e5f703db161e88bd0c
2 changes: 0 additions & 2 deletions Include/internal/pycore_pyatomic_ft_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ extern "C" {
#endif

#ifdef Py_GIL_DISABLED
#define FT_ATOMIC_ADD_UINT64(value, new_value) _Py_atomic_add_uint64(&value, new_value)
#define FT_ATOMIC_LOAD_PTR(value) _Py_atomic_load_ptr(&value)
#define FT_ATOMIC_STORE_PTR(value, new_value) _Py_atomic_store_ptr(&value, new_value)
#define FT_ATOMIC_LOAD_SSIZE(value) _Py_atomic_load_ssize(&value)
Expand Down Expand Up @@ -64,7 +63,6 @@ extern "C" {
_Py_atomic_store_uint32_relaxed(&value, new_value)

#else
#define FT_ATOMIC_ADD_UINT64(value, new_value) value += new_value
#define FT_ATOMIC_LOAD_PTR(value) value
#define FT_ATOMIC_STORE_PTR(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_SSIZE(value) value
Expand Down
24 changes: 18 additions & 6 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,7 @@ register_task(asyncio_state *state, TaskObj *task)
assert(task != &state->asyncio_tasks.tail);
if (task->next != NULL) {
// already registered
ASYNCIO_STATE_UNLOCK(state);
return;
}
assert(task->prev == NULL);
Expand All @@ -2057,7 +2058,7 @@ register_eager_task(asyncio_state *state, PyObject *task)
}

static void
unregister_task(asyncio_state *state, TaskObj *task)
unregister_task_no_lock(asyncio_state *state, TaskObj *task)
{
assert(Task_Check(state, task));
assert(task != &state->asyncio_tasks.tail);
Expand All @@ -2067,20 +2068,28 @@ unregister_task(asyncio_state *state, TaskObj *task)
assert(state->asyncio_tasks.head != task);
return;
}
ASYNCIO_STATE_LOCK(state);

task->next->prev = task->prev;
if (task->prev == NULL) {
assert(state->asyncio_tasks.head == task);
state->asyncio_tasks.head = task->next;
} else {
task->prev->next = task->next;
}
ASYNCIO_STATE_UNLOCK(state);

task->next = NULL;
task->prev = NULL;
assert(state->asyncio_tasks.head != task);
}

static void
unregister_task(asyncio_state *state, TaskObj *task)
{
ASYNCIO_STATE_LOCK(state);
unregister_task_no_lock(state, task);
ASYNCIO_STATE_UNLOCK(state);
}

static int
unregister_eager_task(asyncio_state *state, PyObject *task)
{
Expand Down Expand Up @@ -2233,8 +2242,12 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
// optimization: defer task name formatting
// store the task counter as PyLong in the name
// for deferred formatting in get_name
FT_ATOMIC_ADD_UINT64(state->task_name_counter, 1);
name = PyLong_FromUnsignedLongLong(state->task_name_counter);
#ifdef Py_GIL_DISABLED
unsigned long long counter = _Py_atomic_add_uint64(&state->task_name_counter, 1) + 1;
#else
unsigned long long counter = ++state->task_name_counter;
#endif
name = PyLong_FromUnsignedLongLong(counter);
} else if (!PyUnicode_CheckExact(name)) {
name = PyObject_Str(name);
} else {
Expand Down Expand Up @@ -3968,7 +3981,6 @@ static int
module_exec(PyObject *mod)
{
asyncio_state *state = get_asyncio_state(mod);

Py_SET_TYPE(&state->asyncio_tasks.tail, state->TaskType);
_Py_SetImmortalUntracked((PyObject *)&state->asyncio_tasks.tail);
state->asyncio_tasks.head = &state->asyncio_tasks.tail;
Expand Down
Loading
0