Skip to content

Commit 560df73

Browse files
committed
Fix some coroutine thread safety.
1 parent 99105e7 commit 560df73

File tree

2 files changed

+141
-20
lines changed

2 files changed

+141
-20
lines changed

Objects/clinic/genobject.c.h

Lines changed: 101 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/genobject.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
/*[clinic input]
2121
class generator "PyGenObject *" "&PyGen_Type"
2222
class async_generator "PyAsyncGenObject *" "&PyAsyncGen_Type"
23+
class coroutine "PyCoroObject *" "&PyCoro_Type"
2324
[clinic start generated code]*/
24-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=403b2ee985491847]*/
25+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=593e3f7a2581251e]*/
2526

2627
#include "clinic/genobject.c.h"
2728

@@ -1195,8 +1196,15 @@ coro_get_cr_await(PyObject *coro, void *Py_UNUSED(ignored))
11951196
return yf;
11961197
}
11971198

1199+
/*[clinic input]
1200+
@getter
1201+
@critical_section
1202+
coroutine.cr_suspended as cr_getsuspended
1203+
[clinic start generated code]*/
1204+
11981205
static PyObject *
1199-
cr_getsuspended(PyObject *self, void *Py_UNUSED(ignored))
1206+
cr_getsuspended_get_impl(PyCoroObject *self)
1207+
/*[clinic end generated code: output=fa37923084e2a87d input=2581134666285807]*/
12001208
{
12011209
PyCoroObject *coro = _PyCoroObject_CAST(self);
12021210
if (FRAME_STATE_SUSPENDED(coro->cr_frame_state)) {
@@ -1205,8 +1213,15 @@ cr_getsuspended(PyObject *self, void *Py_UNUSED(ignored))
12051213
Py_RETURN_FALSE;
12061214
}
12071215

1216+
/*[clinic input]
1217+
@getter
1218+
@critical_section
1219+
coroutine.cr_running as cr_getrunning
1220+
[clinic start generated code]*/
1221+
12081222
static PyObject *
1209-
cr_getrunning(PyObject *self, void *Py_UNUSED(ignored))
1223+
cr_getrunning_get_impl(PyCoroObject *self)
1224+
/*[clinic end generated code: output=153bd71b7b6e4842 input=9b43ec12b69a9f01]*/
12101225
{
12111226
PyCoroObject *coro = _PyCoroObject_CAST(self);
12121227
if (coro->cr_frame_state == FRAME_EXECUTING) {
@@ -1215,14 +1230,17 @@ cr_getrunning(PyObject *self, void *Py_UNUSED(ignored))
12151230
Py_RETURN_FALSE;
12161231
}
12171232

1233+
/*[clinic input]
1234+
@critical_section
1235+
@getter
1236+
coroutine.cr_frame as cr_getframe
1237+
[clinic start generated code]*/
1238+
12181239
static PyObject *
1219-
cr_getframe(PyObject *coro, void *Py_UNUSED(ignored))
1240+
cr_getframe_get_impl(PyCoroObject *self)
1241+
/*[clinic end generated code: output=300f3facb67ebc50 input=32fca6ffc44085b7]*/
12201242
{
1221-
PyObject *res;
1222-
Py_BEGIN_CRITICAL_SECTION(coro);
1223-
res = gen_getframe_lock_held(_PyGen_CAST(coro), "cr_frame");
1224-
Py_END_CRITICAL_SECTION();
1225-
return res;
1243+
return gen_getframe_lock_held(_PyGen_CAST(self), "cr_frame");
12261244
}
12271245

12281246
static PyObject *
@@ -1239,10 +1257,10 @@ static PyGetSetDef coro_getsetlist[] = {
12391257
PyDoc_STR("qualified name of the coroutine")},
12401258
{"cr_await", coro_get_cr_await, NULL,
12411259
PyDoc_STR("object being awaited on, or None")},
1242-
{"cr_running", cr_getrunning, NULL, NULL},
1243-
{"cr_frame", cr_getframe, NULL, NULL},
1260+
CR_GETRUNNING_GETSETDEF
1261+
CR_GETFRAME_GETSETDEF
12441262
{"cr_code", cr_getcode, NULL, NULL},
1245-
{"cr_suspended", cr_getsuspended, NULL, NULL},
1263+
CR_GETSUSPENDED_GETSETDEF
12461264
{NULL} /* Sentinel */
12471265
};
12481266

@@ -1641,14 +1659,17 @@ async_gen_athrow(PyAsyncGenObject *o, PyObject *args)
16411659
return async_gen_athrow_new(o, args);
16421660
}
16431661

1662+
/*[clinic input]
1663+
@critical_section
1664+
@getter
1665+
async_generator.ag_frame as ag_getframe
1666+
[clinic start generated code]*/
1667+
16441668
static PyObject *
1645-
ag_getframe(PyObject *ag, void *Py_UNUSED(ignored))
1669+
ag_getframe_get_impl(PyAsyncGenObject *self)
1670+
/*[clinic end generated code: output=7a31c7181090a4fb input=b059ce210436a682]*/
16461671
{
1647-
PyObject *res;
1648-
Py_BEGIN_CRITICAL_SECTION(ag);
1649-
res = gen_getframe_lock_held((PyGenObject *)ag, "ag_frame");
1650-
Py_END_CRITICAL_SECTION();
1651-
return res;
1672+
return gen_getframe_lock_held(_PyGen_CAST(self), "ag_frame");
16521673
}
16531674

16541675
static PyObject *
@@ -1677,7 +1698,7 @@ static PyGetSetDef async_gen_getsetlist[] = {
16771698
PyDoc_STR("qualified name of the async generator")},
16781699
{"ag_await", coro_get_cr_await, NULL,
16791700
PyDoc_STR("object being awaited on, or None")},
1680-
{"ag_frame", ag_getframe, NULL, NULL},
1701+
AG_GETFRAME_GETSETDEF
16811702
{"ag_code", ag_getcode, NULL, NULL},
16821703
AG_GETSUSPENDED_GETSETDEF
16831704
{NULL} /* Sentinel */

0 commit comments

Comments
 (0)