Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Include/internal/pycore_pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ static inline void _PyObject_XDecRefDelayed(PyObject *obj)
}
#endif

#ifdef Py_GIL_DISABLED
// This is the delayed-free equivalent of Py_XSETREF(), providing the same
// atomic reference assignment semantics but with deferred cleanup suitable
// for concurrent access patterns in free-threaded Python.
PyAPI_FUNC(void) _PyObject_XSetRefDelayed(PyObject **p_obj, PyObject *obj);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not adding this function to pycore_object.h header instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because _PyObject_XDecRefDelayed is also declared here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to place _PyObject_XDecRefDelayed and _PyObject_XSetRefDelayed in pycore_object.h instead if you prefer. (I agree that they should probably be in the same header as each other).

#else
static inline void _PyObject_XSetRefDelayed(PyObject **p_obj, PyObject *obj)
{
Py_XSETREF(*p_obj, obj);
}
#endif

// Periodically process delayed free requests.
extern void _PyMem_ProcessDelayed(PyThreadState *tstate);

Expand Down
9 changes: 7 additions & 2 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pycore_gc.h" // _PyGC_CLEAR_FINALIZED()
#include "pycore_genobject.h" // _PyGen_SetStopIterationValue()
#include "pycore_interpframe.h" // _PyFrame_GetCode()
#include "pycore_pymem.h" // _PyObject_XSetRefDelayed()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_utils.h" // RESUME_AFTER_YIELD_FROM
Expand Down Expand Up @@ -718,7 +719,9 @@ gen_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
"__name__ must be set to a string object");
return -1;
}
Py_XSETREF(op->gi_name, Py_NewRef(value));
Py_BEGIN_CRITICAL_SECTION(self);
_PyObject_XSetRefDelayed(&op->gi_name, Py_NewRef(value));
Py_END_CRITICAL_SECTION();
return 0;
}

Expand All @@ -740,7 +743,9 @@ gen_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
"__qualname__ must be set to a string object");
return -1;
}
Py_XSETREF(op->gi_qualname, Py_NewRef(value));
Py_BEGIN_CRITICAL_SECTION(self);
_PyObject_XSetRefDelayed(&op->gi_qualname, Py_NewRef(value));
Py_END_CRITICAL_SECTION();
return 0;
}

Expand Down
18 changes: 18 additions & 0 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,24 @@ _PyObject_XDecRefDelayed(PyObject *ptr)
}
#endif

#ifdef Py_GIL_DISABLED
void
_PyObject_XSetRefDelayed(PyObject **ptr, PyObject *value)
{
PyObject *old = *ptr;
FT_ATOMIC_STORE_PTR_RELEASE(*ptr, value);
if (old == _Py_NULL) {
return;
}
if (!_Py_IsImmortal(old)) {
_PyObject_XDecRefDelayed(old);
}
else {
Py_DECREF(old);
}
}
#endif

static struct _mem_work_chunk *
work_queue_first(struct llist_node *head)
{
Expand Down
8 changes: 1 addition & 7 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3967,13 +3967,7 @@ _PyObject_SetDict(PyObject *obj, PyObject *value)
return -1;
}
Py_BEGIN_CRITICAL_SECTION(obj);
PyObject *olddict = *dictptr;
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
#ifdef Py_GIL_DISABLED
_PyObject_XDecRefDelayed(olddict);
#else
Py_XDECREF(olddict);
#endif
_PyObject_XSetRefDelayed(dictptr, Py_NewRef(value));
Py_END_CRITICAL_SECTION();
return 0;
}
Expand Down
Loading