Skip to content

Commit

Permalink
use critical section
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 committed Aug 10, 2024
1 parent 5bd9aa7 commit beb012a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ typedef struct {
#define Task_Check(state, obj) PyObject_TypeCheck(obj, state->TaskType)

#ifdef Py_GIL_DISABLED
# define ASYNCIO_STATE_LOCK(state) PyMutex_Lock(&state->mutex)
# define ASYNCIO_STATE_UNLOCK(state) PyMutex_Unlock(&state->mutex)
# define ASYNCIO_STATE_LOCK(state) Py_BEGIN_CRITICAL_SECTION_MUT(&state->mutex)
# define ASYNCIO_STATE_UNLOCK(state) Py_END_CRITICAL_SECTION(&state->mutex)
#else
# define ASYNCIO_STATE_LOCK(state) ((void)state)
# define ASYNCIO_STATE_UNLOCK(state) ((void)state)
Expand Down Expand Up @@ -1923,15 +1923,15 @@ register_task(asyncio_state *state, TaskObj *task)
assert(task != &state->asyncio_tasks.tail);
if (task->next != NULL) {
// already registered
ASYNCIO_STATE_UNLOCK(state);
return;
goto exit;
}
assert(task->prev == NULL);
assert(state->asyncio_tasks.head != NULL);

task->next = state->asyncio_tasks.head;
state->asyncio_tasks.head->prev = task;
state->asyncio_tasks.head = task;
exit:
ASYNCIO_STATE_UNLOCK(state);
}

Expand All @@ -1951,8 +1951,7 @@ unregister_task(asyncio_state *state, TaskObj *task)
// not registered
assert(task->prev == NULL);
assert(state->asyncio_tasks.head != task);
ASYNCIO_STATE_UNLOCK(state);
return;
goto exit;
}
task->next->prev = task->prev;
if (task->prev == NULL) {
Expand All @@ -1964,6 +1963,7 @@ unregister_task(asyncio_state *state, TaskObj *task)
task->next = NULL;
task->prev = NULL;
assert(state->asyncio_tasks.head != task);
exit:
ASYNCIO_STATE_UNLOCK(state);
}

Expand Down Expand Up @@ -3637,11 +3637,10 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
while (head != tail)
{
if (add_one_task(state, tasks, (PyObject *)head, loop) < 0) {
ASYNCIO_STATE_UNLOCK(state);
Py_DECREF(tasks);
Py_DECREF(loop);
Py_DECREF(head);
return NULL;
goto error;
}
Py_INCREF(head->next);
Py_SETREF(head, head->next);
Expand All @@ -3666,6 +3665,9 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
Py_DECREF(scheduled_iter);
Py_DECREF(loop);
return tasks;
error:
ASYNCIO_STATE_UNLOCK(state);
return NULL;
}

static int
Expand Down

0 comments on commit beb012a

Please sign in to comment.