Skip to content
Merged
Changes from 7 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
37 changes: 35 additions & 2 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +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 @@ -341,6 +352,8 @@ get_running_loop(asyncio_state *state, PyObject **loop)
}
}

// 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;
}
Expand Down Expand Up @@ -384,6 +397,9 @@ set_running_loop(asyncio_state *state, PyObject *loop)
return -1;
}


// 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);

Expand Down Expand Up @@ -1667,6 +1683,7 @@ 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) {
state->fi_freelist_len++;
it->future = (FutureObj*) state->fi_freelist;
Expand Down Expand Up @@ -2018,10 +2035,12 @@ 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) {
// already registered
ASYNCIO_STATE_UNLOCK(state);
return;
}
assert(task->prev == NULL);
Expand All @@ -2030,6 +2049,7 @@ register_task(asyncio_state *state, TaskObj *task)
task->next = state->asyncio_tasks.head;
state->asyncio_tasks.head->prev = task;
state->asyncio_tasks.head = task;
ASYNCIO_STATE_UNLOCK(state);
}

static int
Expand All @@ -2039,7 +2059,7 @@ register_eager_task(asyncio_state *state, PyObject *task)
}

static void
unregister_task(asyncio_state *state, TaskObj *task)
unregister_task_lock_held(asyncio_state *state, TaskObj *task)
{
assert(Task_Check(state, task));
assert(task != &state->asyncio_tasks.tail);
Expand All @@ -2061,6 +2081,14 @@ unregister_task(asyncio_state *state, TaskObj *task)
assert(state->asyncio_tasks.head != task);
}

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

static int
unregister_eager_task(asyncio_state *state, PyObject *task)
{
Expand Down Expand Up @@ -2213,7 +2241,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
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