summaryrefslogtreecommitdiffstats
path: root/Python/executor_cases.c.h
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-03-29 17:35:43 (GMT)
committerGitHub <noreply@github.com>2024-03-29 17:35:43 (GMT)
commit19c1dd60c5b53fb0533610ad139ef591294f26e8 (patch)
treef76436f1512efe26885b65c49585a761a1658e82 /Python/executor_cases.c.h
parent397d88db5e9ab2a43de3fdf5f8b973a949edc405 (diff)
downloadcpython-19c1dd60c5b53fb0533610ad139ef591294f26e8.zip
cpython-19c1dd60c5b53fb0533610ad139ef591294f26e8.tar.gz
cpython-19c1dd60c5b53fb0533610ad139ef591294f26e8.tar.bz2
gh-117323: Make `cell` thread-safe in free-threaded builds (#117330)
Use critical sections to lock around accesses to cell contents. The critical sections are no-ops in the default (with GIL) build.
Diffstat (limited to 'Python/executor_cases.c.h')
-rw-r--r--Python/executor_cases.c.h19
1 files changed, 7 insertions, 12 deletions
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index 224b600..ce0dc23 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -1362,14 +1362,13 @@
case _DELETE_DEREF: {
oparg = CURRENT_OPARG();
PyObject *cell = GETLOCAL(oparg);
- PyObject *oldobj = PyCell_GET(cell);
// Can't use ERROR_IF here.
// Fortunately we don't need its superpower.
+ PyObject *oldobj = PyCell_SwapTakeRef((PyCellObject *)cell, NULL);
if (oldobj == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
JUMP_TO_ERROR();
}
- PyCell_SET(cell, NULL);
Py_DECREF(oldobj);
break;
}
@@ -1387,13 +1386,12 @@
JUMP_TO_ERROR();
}
if (!value) {
- PyObject *cell = GETLOCAL(oparg);
- value = PyCell_GET(cell);
+ PyCellObject *cell = (PyCellObject *)GETLOCAL(oparg);
+ value = PyCell_GetRef(cell);
if (value == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
JUMP_TO_ERROR();
}
- Py_INCREF(value);
}
Py_DECREF(class_dict);
stack_pointer[-1] = value;
@@ -1403,13 +1401,12 @@
case _LOAD_DEREF: {
PyObject *value;
oparg = CURRENT_OPARG();
- PyObject *cell = GETLOCAL(oparg);
- value = PyCell_GET(cell);
+ PyCellObject *cell = (PyCellObject *)GETLOCAL(oparg);
+ value = PyCell_GetRef(cell);
if (value == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
if (true) JUMP_TO_ERROR();
}
- Py_INCREF(value);
stack_pointer[0] = value;
stack_pointer += 1;
break;
@@ -1419,10 +1416,8 @@
PyObject *v;
oparg = CURRENT_OPARG();
v = stack_pointer[-1];
- PyObject *cell = GETLOCAL(oparg);
- PyObject *oldobj = PyCell_GET(cell);
- PyCell_SET(cell, v);
- Py_XDECREF(oldobj);
+ PyCellObject *cell = (PyCellObject *)GETLOCAL(oparg);
+ PyCell_SetTakeRef(cell, v);
stack_pointer += -1;
break;
}