Skip to content
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 143747b8a011c3403bb78d0a1afcdeb8d01711da
2 changes: 2 additions & 0 deletions Include/internal/pycore_pyatomic_ft_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 @@ -63,6 +64,7 @@ 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
46 changes: 22 additions & 24 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,21 @@ typedef struct {

#define FI_FREELIST_MAXLEN 255

#ifdef Py_GIL_DISABLED
# define ASYNCIO_STATE_LOCK(state) PyMutex_Lock(&state->mutex)
# define ASYNCIO_STATE_UNLOCK(state) PyMutex_Unlock(&state->mutex)
#else
# define ASYNCIO_STATE_LOCK(state) (void(state))
# define ASYNCIO_STATE_UNLOCK(state) (void(state))
#endif

typedef struct futureiterobject futureiterobject;

/* State of the _asyncio module */
typedef struct {
#ifdef Py_GIL_DISABLED
PyMutex mutex;
#endif
PyTypeObject *FutureIterType;
PyTypeObject *TaskStepMethWrapper_Type;
PyTypeObject *FutureType;
Expand Down Expand Up @@ -342,10 +352,10 @@ get_running_loop(asyncio_state *state, PyObject **loop)
}
}

PyMutex_Lock(&state->mutex);
// TODO GH-121621: The should be moved to PyThreadState
// for easier and quicker access.
state->cached_running_loop = rl;
state->cached_running_loop_tsid = ts_id;
PyMutex_Unlock(&state->mutex);
}


Expand Down Expand Up @@ -387,11 +397,11 @@ set_running_loop(asyncio_state *state, PyObject *loop)
return -1;
}

PyMutex_Lock(&state->mutex);

// TODO GH-121621: The should be moved to PyThreadState
// for easier and quicker access.
state->cached_running_loop = loop; // borrowed, kept alive by ts_dict
state->cached_running_loop_tsid = PyThreadState_GetID(tstate);
PyMutex_Unlock(&state->mutex);

return 0;
}

Expand Down Expand Up @@ -1672,12 +1682,11 @@ FutureIter_dealloc(futureiterobject *it)
state = get_asyncio_state(module);
}

// TODO GH-121621: This should be moved to thread state as well.
if (state && state->fi_freelist_len < FI_FREELIST_MAXLEN) {
PyMutex_Lock(&state->mutex);
state->fi_freelist_len++;
it->future = (FutureObj*) state->fi_freelist;
state->fi_freelist = it;
PyMutex_Unlock(&state->mutex);
}
else {
PyObject_GC_Del(it);
Expand Down Expand Up @@ -1884,11 +1893,9 @@ future_new_iter(PyObject *fut)
ENSURE_FUTURE_ALIVE(state, fut)

if (state->fi_freelist_len) {
PyMutex_Lock(&state->mutex);
state->fi_freelist_len--;
it = state->fi_freelist;
state->fi_freelist = (futureiterobject*) it->future;
PyMutex_Unlock(&state->mutex);
it->future = NULL;
_Py_NewReference((PyObject*) it);
}
Expand Down Expand Up @@ -2027,6 +2034,7 @@ static PyMethodDef TaskWakeupDef = {
static void
register_task(asyncio_state *state, TaskObj *task)
{
ASYNCIO_STATE_LOCK(state);
assert(Task_Check(state, task));
assert(task != &state->asyncio_tasks.tail);
if (task->next != NULL) {
Expand All @@ -2037,10 +2045,9 @@ register_task(asyncio_state *state, TaskObj *task)
assert(state->asyncio_tasks.head != NULL);

task->next = state->asyncio_tasks.head;
PyMutex_Lock(&state->mutex);
state->asyncio_tasks.head->prev = task;
state->asyncio_tasks.head = task;
PyMutex_Unlock(&state->mutex);
ASYNCIO_STATE_UNLOCK(state);
}

static int
Expand All @@ -2060,15 +2067,15 @@ 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);
PyMutex_Lock(&state->mutex);
state->asyncio_tasks.head = task->next;
PyMutex_Unlock(&state->mutex);
} else {
task->prev->next = task->next;
}
ASYNCIO_STATE_UNLOCK(state);
task->next = NULL;
task->prev = NULL;
assert(state->asyncio_tasks.head != task);
Expand Down Expand Up @@ -2226,9 +2233,8 @@ _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
PyMutex_Lock(&state->mutex);
name = PyLong_FromUnsignedLongLong(++state->task_name_counter);
PyMutex_Unlock(&state->mutex);
FT_ATOMIC_ADD_UINT64(state->task_name_counter, 1);
name = PyLong_FromUnsignedLongLong(state->task_name_counter);
} else if (!PyUnicode_CheckExact(name)) {
name = PyObject_Str(name);
} else {
Expand Down Expand Up @@ -3765,7 +3771,6 @@ module_free_freelists(asyncio_state *state)
PyObject *current;

next = (PyObject*) state->fi_freelist;
PyMutex_Lock(&state->mutex);
while (next != NULL) {
assert(state->fi_freelist_len > 0);
state->fi_freelist_len--;
Expand All @@ -3776,7 +3781,6 @@ module_free_freelists(asyncio_state *state)
}
assert(state->fi_freelist_len == 0);
state->fi_freelist = NULL;
PyMutex_Unlock(&state->mutex);
}

static int
Expand Down Expand Up @@ -3861,7 +3865,6 @@ module_init(asyncio_state *state)
{
PyObject *module = NULL;

PyMutex_Lock(&state->mutex);
state->asyncio_mod = PyImport_ImportModule("asyncio");
if (state->asyncio_mod == NULL) {
goto fail;
Expand Down Expand Up @@ -3931,13 +3934,10 @@ module_init(asyncio_state *state)
goto fail;
}

PyMutex_Unlock(&state->mutex);

Py_DECREF(module);
return 0;

fail:
PyMutex_Unlock(&state->mutex);
Py_CLEAR(module);
return -1;

Expand Down Expand Up @@ -3969,11 +3969,9 @@ module_exec(PyObject *mod)
{
asyncio_state *state = get_asyncio_state(mod);

PyMutex_Lock(&state->mutex);
Py_SET_TYPE(&state->asyncio_tasks.tail, state->TaskType);
_Py_SetImmortalUntracked((PyObject *)&state->asyncio_tasks.tail);
state->asyncio_tasks.head = &state->asyncio_tasks.tail;
PyMutex_Unlock(&state->mutex);

#define CREATE_TYPE(m, tp, spec, base) \
do { \
Expand Down