Skip to content

Commit 46fa0a3

Browse files
committed
gh-133931: Introduce _PyObject_XSetRefDelayed to replace Py_XSETREF
1 parent ec39fd2 commit 46fa0a3

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

Include/internal/pycore_pymem.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ static inline void _PyObject_XDecRefDelayed(PyObject *obj)
100100
}
101101
#endif
102102

103+
#ifdef Py_GIL_DISABLED
104+
PyAPI_FUNC(void) _PyObject_XSetRefDelayed(PyObject **p_obj, PyObject *obj);
105+
#else
106+
static inline void _PyObject_XSetRefDelayed(PyObject **p_obj, PyObject *obj)
107+
{
108+
Py_XSETREF(*p_obj, obj);
109+
}
110+
#endif
111+
103112
// Periodically process delayed free requests.
104113
extern void _PyMem_ProcessDelayed(PyThreadState *tstate);
105114

Objects/genobject.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pycore_gc.h" // _PyGC_CLEAR_FINALIZED()
1111
#include "pycore_genobject.h" // _PyGen_SetStopIterationValue()
1212
#include "pycore_interpframe.h" // _PyFrame_GetCode()
13+
#include "pycore_pymem.h" // _PyObject_XSetRefDelayed()
1314
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
1415
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
1516
#include "pycore_opcode_utils.h" // RESUME_AFTER_YIELD_FROM
@@ -718,7 +719,9 @@ gen_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
718719
"__name__ must be set to a string object");
719720
return -1;
720721
}
721-
Py_XSETREF(op->gi_name, Py_NewRef(value));
722+
Py_BEGIN_CRITICAL_SECTION(self);
723+
_PyObject_XSetRefDelayed(&op->gi_name, value);
724+
Py_END_CRITICAL_SECTION();
722725
return 0;
723726
}
724727

@@ -740,7 +743,9 @@ gen_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
740743
"__qualname__ must be set to a string object");
741744
return -1;
742745
}
743-
Py_XSETREF(op->gi_qualname, Py_NewRef(value));
746+
Py_BEGIN_CRITICAL_SECTION(self);
747+
_PyObject_XSetRefDelayed(&op->gi_qualname, value);
748+
Py_END_CRITICAL_SECTION();
744749
return 0;
745750
}
746751

Objects/object.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,13 +1931,7 @@ PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
19311931
return -1;
19321932
}
19331933
Py_BEGIN_CRITICAL_SECTION(obj);
1934-
PyObject *olddict = *dictptr;
1935-
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
1936-
#ifdef Py_GIL_DISABLED
1937-
_PyObject_XDecRefDelayed(olddict);
1938-
#else
1939-
Py_XDECREF(olddict);
1940-
#endif
1934+
_PyObject_XSetRefDelayed(dictptr, value);
19411935
Py_END_CRITICAL_SECTION();
19421936
return 0;
19431937
}

Objects/obmalloc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,17 @@ _PyObject_XDecRefDelayed(PyObject *ptr)
12311231
}
12321232
#endif
12331233

1234+
#ifdef Py_GIL_DISABLED
1235+
void
1236+
_PyObject_XSetRefDelayed(PyObject **ptr, PyObject *value)
1237+
{
1238+
PyObject *old = *ptr;
1239+
FT_ATOMIC_STORE_PTR_RELEASE(*ptr, Py_NewRef(value));
1240+
_PyObject_XDecRefDelayed(old);
1241+
Py_XDECREF(old);
1242+
}
1243+
#endif
1244+
12341245
static struct _mem_work_chunk *
12351246
work_queue_first(struct llist_node *head)
12361247
{

0 commit comments

Comments
 (0)