mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-123619: Add an unstable C API function for enabling deferred reference counting (GH-123635)
Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
parent
29b5323c45
commit
d00878b06a
8 changed files with 128 additions and 1 deletions
|
@ -2519,6 +2519,35 @@ _PyObject_SetDeferredRefcount(PyObject *op)
|
|||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
PyUnstable_Object_EnableDeferredRefcount(PyObject *op)
|
||||
{
|
||||
#ifdef Py_GIL_DISABLED
|
||||
if (!PyType_IS_GC(Py_TYPE(op))) {
|
||||
// Deferred reference counting doesn't work
|
||||
// on untracked types.
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t bits = _Py_atomic_load_uint8(&op->ob_gc_bits);
|
||||
if ((bits & _PyGC_BITS_DEFERRED) != 0)
|
||||
{
|
||||
// Nothing to do.
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (_Py_atomic_compare_exchange_uint8(&op->ob_gc_bits, &bits, bits | _PyGC_BITS_DEFERRED) == 0)
|
||||
{
|
||||
// Someone beat us to it!
|
||||
return 0;
|
||||
}
|
||||
_Py_atomic_add_ssize(&op->ob_ref_shared, _Py_REF_SHARED(_Py_REF_DEFERRED, 0));
|
||||
return 1;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
_Py_ResurrectReference(PyObject *op)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue