3
3
#endif
4
4
5
5
#include "Python.h"
6
+ #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION_MUT()
6
7
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
7
8
#include "pycore_freelist.h" // _Py_FREELIST_POP()
8
9
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
@@ -77,8 +78,8 @@ typedef struct {
77
78
#define Task_Check (state , obj ) PyObject_TypeCheck(obj, state->TaskType)
78
79
79
80
#ifdef Py_GIL_DISABLED
80
- # define ASYNCIO_STATE_LOCK (state ) PyMutex_Lock (&state->mutex)
81
- # define ASYNCIO_STATE_UNLOCK (state ) PyMutex_Unlock(&state->mutex )
81
+ # define ASYNCIO_STATE_LOCK (state ) Py_BEGIN_CRITICAL_SECTION_MUT (&state->mutex)
82
+ # define ASYNCIO_STATE_UNLOCK (state ) Py_END_CRITICAL_SECTION( )
82
83
#else
83
84
# define ASYNCIO_STATE_LOCK (state ) ((void)state)
84
85
# define ASYNCIO_STATE_UNLOCK (state ) ((void)state)
@@ -1923,15 +1924,15 @@ register_task(asyncio_state *state, TaskObj *task)
1923
1924
assert (task != & state -> asyncio_tasks .tail );
1924
1925
if (task -> next != NULL ) {
1925
1926
// already registered
1926
- ASYNCIO_STATE_UNLOCK (state );
1927
- return ;
1927
+ goto exit ;
1928
1928
}
1929
1929
assert (task -> prev == NULL );
1930
1930
assert (state -> asyncio_tasks .head != NULL );
1931
1931
1932
1932
task -> next = state -> asyncio_tasks .head ;
1933
1933
state -> asyncio_tasks .head -> prev = task ;
1934
1934
state -> asyncio_tasks .head = task ;
1935
+ exit :
1935
1936
ASYNCIO_STATE_UNLOCK (state );
1936
1937
}
1937
1938
@@ -1951,8 +1952,7 @@ unregister_task(asyncio_state *state, TaskObj *task)
1951
1952
// not registered
1952
1953
assert (task -> prev == NULL );
1953
1954
assert (state -> asyncio_tasks .head != task );
1954
- ASYNCIO_STATE_UNLOCK (state );
1955
- return ;
1955
+ goto exit ;
1956
1956
}
1957
1957
task -> next -> prev = task -> prev ;
1958
1958
if (task -> prev == NULL ) {
@@ -1964,6 +1964,7 @@ unregister_task(asyncio_state *state, TaskObj *task)
1964
1964
task -> next = NULL ;
1965
1965
task -> prev = NULL ;
1966
1966
assert (state -> asyncio_tasks .head != task );
1967
+ exit :
1967
1968
ASYNCIO_STATE_UNLOCK (state );
1968
1969
}
1969
1970
@@ -3628,6 +3629,8 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
3628
3629
Py_DECREF (item );
3629
3630
}
3630
3631
Py_DECREF (eager_iter );
3632
+ int err = 0 ;
3633
+ ASYNCIO_STATE_LOCK (state );
3631
3634
TaskObj * head = state -> asyncio_tasks .head ;
3632
3635
Py_INCREF (head );
3633
3636
assert (head != NULL );
@@ -3639,11 +3642,16 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
3639
3642
Py_DECREF (tasks );
3640
3643
Py_DECREF (loop );
3641
3644
Py_DECREF (head );
3642
- return NULL ;
3645
+ err = 1 ;
3646
+ break ;
3643
3647
}
3644
3648
Py_INCREF (head -> next );
3645
3649
Py_SETREF (head , head -> next );
3646
3650
}
3651
+ ASYNCIO_STATE_UNLOCK (state );
3652
+ if (err ) {
3653
+ return NULL ;
3654
+ }
3647
3655
PyObject * scheduled_iter = PyObject_GetIter (state -> non_asyncio_tasks );
3648
3656
if (scheduled_iter == NULL ) {
3649
3657
Py_DECREF (tasks );
0 commit comments