Skip to content

Commit a2a4f5e

Browse files
colesburyFidget-Spinnerkumaraditya303
authored
[3.13] gh-121621: Use PyMutex for writes to asyncio state (GH-121622) (#121774)
(cherry picked from commit 5d6861a) Co-authored-by: Ken Jin <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
1 parent 55ff124 commit a2a4f5e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Modules/_asynciomodule.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ get_running_loop(asyncio_state *state, PyObject **loop)
293293
}
294294
}
295295

296+
// TODO GH-121621: This should be moved to PyThreadState
297+
// for easier and quicker access.
296298
state->cached_running_loop = rl;
297299
state->cached_running_loop_tsid = ts_id;
298300
}
@@ -336,6 +338,9 @@ set_running_loop(asyncio_state *state, PyObject *loop)
336338
return -1;
337339
}
338340

341+
342+
// TODO GH-121621: This should be moved to PyThreadState
343+
// for easier and quicker access.
339344
state->cached_running_loop = loop; // borrowed, kept alive by ts_dict
340345
state->cached_running_loop_tsid = PyThreadState_GetID(tstate);
341346

@@ -1616,6 +1621,7 @@ FutureIter_dealloc(futureiterobject *it)
16161621
state = get_asyncio_state(module);
16171622
}
16181623

1624+
// TODO GH-121621: This should be moved to thread state as well.
16191625
if (state && state->fi_freelist_len < FI_FREELIST_MAXLEN) {
16201626
state->fi_freelist_len++;
16211627
it->future = (FutureObj*) state->fi_freelist;
@@ -2146,7 +2152,12 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
21462152
// optimization: defer task name formatting
21472153
// store the task counter as PyLong in the name
21482154
// for deferred formatting in get_name
2149-
name = PyLong_FromUnsignedLongLong(++state->task_name_counter);
2155+
#ifdef Py_GIL_DISABLED
2156+
unsigned long long counter = _Py_atomic_add_uint64(&state->task_name_counter, 1) + 1;
2157+
#else
2158+
unsigned long long counter = ++state->task_name_counter;
2159+
#endif
2160+
name = PyLong_FromUnsignedLongLong(counter);
21502161
} else if (!PyUnicode_CheckExact(name)) {
21512162
name = PyObject_Str(name);
21522163
} else {

0 commit comments

Comments
 (0)