@@ -135,9 +135,6 @@ typedef struct {
135
135
/* Imports from traceback. */
136
136
PyObject * traceback_extract_stack ;
137
137
138
- PyObject * cached_running_loop ; // Borrowed reference
139
- volatile uint64_t cached_running_loop_tsid ;
140
-
141
138
/* Counter for autogenerated Task names */
142
139
uint64_t task_name_counter ;
143
140
@@ -321,101 +318,15 @@ get_future_loop(asyncio_state *state, PyObject *fut)
321
318
return PyObject_GetAttr (fut , & _Py_ID (_loop ));
322
319
}
323
320
324
-
325
- static int
326
- get_running_loop (asyncio_state * state , PyObject * * loop )
327
- {
328
- PyObject * rl ;
329
-
330
- PyThreadState * ts = _PyThreadState_GET ();
331
- uint64_t ts_id = PyThreadState_GetID (ts );
332
- if (state -> cached_running_loop_tsid == ts_id &&
333
- state -> cached_running_loop != NULL )
334
- {
335
- // Fast path, check the cache.
336
- rl = state -> cached_running_loop ;
337
- }
338
- else {
339
- PyObject * ts_dict = _PyThreadState_GetDict (ts ); // borrowed
340
- if (ts_dict == NULL ) {
341
- goto not_found ;
342
- }
343
-
344
- rl = PyDict_GetItemWithError (
345
- ts_dict , & _Py_ID (__asyncio_running_event_loop__ )); // borrowed
346
- if (rl == NULL ) {
347
- if (PyErr_Occurred ()) {
348
- goto error ;
349
- }
350
- else {
351
- goto not_found ;
352
- }
353
- }
354
-
355
- // TODO GH-121621: This should be moved to PyThreadState
356
- // for easier and quicker access.
357
- state -> cached_running_loop = rl ;
358
- state -> cached_running_loop_tsid = ts_id ;
359
- }
360
-
361
-
362
- if (rl == Py_None ) {
363
- goto not_found ;
364
- }
365
-
366
- * loop = Py_NewRef (rl );
367
- return 0 ;
368
-
369
- not_found :
370
- * loop = NULL ;
371
- return 0 ;
372
-
373
- error :
374
- * loop = NULL ;
375
- return -1 ;
376
- }
377
-
378
-
379
- static int
380
- set_running_loop (asyncio_state * state , PyObject * loop )
381
- {
382
- PyObject * ts_dict = NULL ;
383
-
384
- PyThreadState * tstate = _PyThreadState_GET ();
385
- if (tstate != NULL ) {
386
- ts_dict = _PyThreadState_GetDict (tstate ); // borrowed
387
- }
388
-
389
- if (ts_dict == NULL ) {
390
- PyErr_SetString (
391
- PyExc_RuntimeError , "thread-local storage is not available" );
392
- return -1 ;
393
- }
394
- if (PyDict_SetItem (
395
- ts_dict , & _Py_ID (__asyncio_running_event_loop__ ), loop ) < 0 )
396
- {
397
- return -1 ;
398
- }
399
-
400
-
401
- // TODO GH-121621: This should be moved to PyThreadState
402
- // for easier and quicker access.
403
- state -> cached_running_loop = loop ; // borrowed, kept alive by ts_dict
404
- state -> cached_running_loop_tsid = PyThreadState_GetID (tstate );
405
-
406
- return 0 ;
407
- }
408
-
409
-
410
321
static PyObject *
411
322
get_event_loop (asyncio_state * state )
412
323
{
413
324
PyObject * loop ;
414
325
PyObject * policy ;
415
326
416
- if ( get_running_loop ( state , & loop )) {
417
- return NULL ;
418
- }
327
+ PyThreadState * ts = _PyThreadState_GET ();
328
+ loop = Py_XNewRef ( ts -> asyncio_running_loop ) ;
329
+
419
330
if (loop != NULL ) {
420
331
return loop ;
421
332
}
@@ -3367,11 +3278,8 @@ static PyObject *
3367
3278
_asyncio__get_running_loop_impl (PyObject * module )
3368
3279
/*[clinic end generated code: output=b4390af721411a0a input=0a21627e25a4bd43]*/
3369
3280
{
3370
- PyObject * loop ;
3371
- asyncio_state * state = get_asyncio_state (module );
3372
- if (get_running_loop (state , & loop )) {
3373
- return NULL ;
3374
- }
3281
+ PyThreadState * ts = _PyThreadState_GET ();
3282
+ PyObject * loop = Py_XNewRef (ts -> asyncio_running_loop );
3375
3283
if (loop == NULL ) {
3376
3284
/* There's no currently running event loop */
3377
3285
Py_RETURN_NONE ;
@@ -3394,10 +3302,11 @@ static PyObject *
3394
3302
_asyncio__set_running_loop (PyObject * module , PyObject * loop )
3395
3303
/*[clinic end generated code: output=ae56bf7a28ca189a input=4c9720233d606604]*/
3396
3304
{
3397
- asyncio_state * state = get_asyncio_state ( module );
3398
- if (set_running_loop ( state , loop ) ) {
3399
- return NULL ;
3305
+ PyThreadState * ts = _PyThreadState_GET ( );
3306
+ if (loop == Py_None ) {
3307
+ loop = NULL ;
3400
3308
}
3309
+ Py_XSETREF (ts -> asyncio_running_loop , Py_XNewRef (loop ));
3401
3310
Py_RETURN_NONE ;
3402
3311
}
3403
3312
@@ -3435,14 +3344,13 @@ _asyncio_get_running_loop_impl(PyObject *module)
3435
3344
/*[clinic end generated code: output=c247b5f9e529530e input=2a3bf02ba39f173d]*/
3436
3345
{
3437
3346
PyObject * loop ;
3438
- asyncio_state * state = get_asyncio_state (module );
3439
- if (get_running_loop (state , & loop )) {
3440
- return NULL ;
3441
- }
3347
+ PyThreadState * ts = _PyThreadState_GET ();
3348
+ loop = Py_XNewRef (ts -> asyncio_running_loop );
3442
3349
if (loop == NULL ) {
3443
3350
/* There's no currently running event loop */
3444
3351
PyErr_SetString (
3445
3352
PyExc_RuntimeError , "no running event loop" );
3353
+ return NULL ;
3446
3354
}
3447
3355
return loop ;
3448
3356
}
0 commit comments