gh-112529: Implement GC for free-threaded builds (#114262)

* gh-112529: Implement GC for free-threaded builds

This implements a mark and sweep GC for the free-threaded builds of
CPython. The implementation relies on mimalloc to find GC tracked
objects (i.e., "containers").
This commit is contained in:
Sam Gross 2024-01-25 13:27:36 -05:00 committed by GitHub
parent 4850410b60
commit b52fc70d1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 1952 additions and 22 deletions

View file

@ -15,6 +15,8 @@
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
#include "pydtrace.h"
#ifndef Py_GIL_DISABLED
typedef struct _gc_runtime_state GCState;
#ifdef Py_DEBUG
@ -964,10 +966,10 @@ finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable)
PyGC_Head *gc = GC_NEXT(collectable);
PyObject *op = FROM_GC(gc);
gc_list_move(gc, &seen);
if (!_PyGCHead_FINALIZED(gc) &&
if (!_PyGC_FINALIZED(op) &&
(finalize = Py_TYPE(op)->tp_finalize) != NULL)
{
_PyGCHead_SET_FINALIZED(gc);
_PyGC_SET_FINALIZED(op);
Py_INCREF(op);
finalize(op);
assert(!_PyErr_Occurred(tstate));
@ -1942,3 +1944,5 @@ PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg)
done:
gcstate->enabled = origenstate;
}
#endif // Py_GIL_DISABLED