@@ -68,9 +68,6 @@ typedef struct {
68
68
/* Imports from traceback. */
69
69
PyObject * traceback_extract_stack ;
70
70
71
- PyObject * cached_running_loop ; // Borrowed reference
72
- volatile uint64_t cached_running_loop_tsid ;
73
-
74
71
/* Counter for autogenerated Task names */
75
72
uint64_t task_name_counter ;
76
73
@@ -262,101 +259,15 @@ get_future_loop(asyncio_state *state, PyObject *fut)
262
259
return PyObject_GetAttr (fut , & _Py_ID (_loop ));
263
260
}
264
261
265
-
266
- static int
267
- get_running_loop (asyncio_state * state , PyObject * * loop )
268
- {
269
- PyObject * rl ;
270
-
271
- PyThreadState * ts = _PyThreadState_GET ();
272
- uint64_t ts_id = PyThreadState_GetID (ts );
273
- if (state -> cached_running_loop_tsid == ts_id &&
274
- state -> cached_running_loop != NULL )
275
- {
276
- // Fast path, check the cache.
277
- rl = state -> cached_running_loop ;
278
- }
279
- else {
280
- PyObject * ts_dict = _PyThreadState_GetDict (ts ); // borrowed
281
- if (ts_dict == NULL ) {
282
- goto not_found ;
283
- }
284
-
285
- rl = PyDict_GetItemWithError (
286
- ts_dict , & _Py_ID (__asyncio_running_event_loop__ )); // borrowed
287
- if (rl == NULL ) {
288
- if (PyErr_Occurred ()) {
289
- goto error ;
290
- }
291
- else {
292
- goto not_found ;
293
- }
294
- }
295
-
296
- // TODO GH-121621: This should be moved to PyThreadState
297
- // for easier and quicker access.
298
- state -> cached_running_loop = rl ;
299
- state -> cached_running_loop_tsid = ts_id ;
300
- }
301
-
302
-
303
- if (rl == Py_None ) {
304
- goto not_found ;
305
- }
306
-
307
- * loop = Py_NewRef (rl );
308
- return 0 ;
309
-
310
- not_found :
311
- * loop = NULL ;
312
- return 0 ;
313
-
314
- error :
315
- * loop = NULL ;
316
- return -1 ;
317
- }
318
-
319
-
320
- static int
321
- set_running_loop (asyncio_state * state , PyObject * loop )
322
- {
323
- PyObject * ts_dict = NULL ;
324
-
325
- PyThreadState * tstate = _PyThreadState_GET ();
326
- if (tstate != NULL ) {
327
- ts_dict = _PyThreadState_GetDict (tstate ); // borrowed
328
- }
329
-
330
- if (ts_dict == NULL ) {
331
- PyErr_SetString (
332
- PyExc_RuntimeError , "thread-local storage is not available" );
333
- return -1 ;
334
- }
335
- if (PyDict_SetItem (
336
- ts_dict , & _Py_ID (__asyncio_running_event_loop__ ), loop ) < 0 )
337
- {
338
- return -1 ;
339
- }
340
-
341
-
342
- // TODO GH-121621: This should be moved to PyThreadState
343
- // for easier and quicker access.
344
- state -> cached_running_loop = loop ; // borrowed, kept alive by ts_dict
345
- state -> cached_running_loop_tsid = PyThreadState_GetID (tstate );
346
-
347
- return 0 ;
348
- }
349
-
350
-
351
262
static PyObject *
352
263
get_event_loop (asyncio_state * state )
353
264
{
354
265
PyObject * loop ;
355
266
PyObject * policy ;
356
267
357
- if ( get_running_loop ( state , & loop )) {
358
- return NULL ;
359
- }
268
+ PyThreadState * ts = _PyThreadState_GET ();
269
+ loop = Py_XNewRef ( ts -> asyncio_running_loop ) ;
270
+
360
271
if (loop != NULL ) {
361
272
return loop ;
362
273
}
@@ -3278,11 +3189,8 @@ static PyObject *
3278
3189
_asyncio__get_running_loop_impl (PyObject * module )
3279
3190
/*[clinic end generated code: output=b4390af721411a0a input=0a21627e25a4bd43]*/
3280
3191
{
3281
- PyObject * loop ;
3282
- asyncio_state * state = get_asyncio_state (module );
3283
- if (get_running_loop (state , & loop )) {
3284
- return NULL ;
3285
- }
3192
+ PyThreadState * ts = _PyThreadState_GET ();
3193
+ PyObject * loop = Py_XNewRef (ts -> asyncio_running_loop );
3286
3194
if (loop == NULL ) {
3287
3195
/* There's no currently running event loop */
3288
3196
Py_RETURN_NONE ;
@@ -3305,10 +3213,11 @@ static PyObject *
3305
3213
_asyncio__set_running_loop (PyObject * module , PyObject * loop )
3306
3214
/*[clinic end generated code: output=ae56bf7a28ca189a input=4c9720233d606604]*/
3307
3215
{
3308
- asyncio_state * state = get_asyncio_state ( module );
3309
- if (set_running_loop ( state , loop ) ) {
3310
- return NULL ;
3216
+ PyThreadState * ts = _PyThreadState_GET ( );
3217
+ if (loop == Py_None ) {
3218
+ loop = NULL ;
3311
3219
}
3220
+ Py_XSETREF (ts -> asyncio_running_loop , Py_XNewRef (loop ));
3312
3221
Py_RETURN_NONE ;
3313
3222
}
3314
3223
@@ -3346,14 +3255,13 @@ _asyncio_get_running_loop_impl(PyObject *module)
3346
3255
/*[clinic end generated code: output=c247b5f9e529530e input=2a3bf02ba39f173d]*/
3347
3256
{
3348
3257
PyObject * loop ;
3349
- asyncio_state * state = get_asyncio_state (module );
3350
- if (get_running_loop (state , & loop )) {
3351
- return NULL ;
3352
- }
3258
+ PyThreadState * ts = _PyThreadState_GET ();
3259
+ loop = Py_XNewRef (ts -> asyncio_running_loop );
3353
3260
if (loop == NULL ) {
3354
3261
/* There's no currently running event loop */
3355
3262
PyErr_SetString (
3356
3263
PyExc_RuntimeError , "no running event loop" );
3264
+ return NULL ;
3357
3265
}
3358
3266
return loop ;
3359
3267
}
0 commit comments