Skip to content

Commit

Permalink
pythongh-112529: Implement GC for free-threaded builds
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Jan 18, 2024
1 parent 24e41e3 commit c93a17c
Show file tree
Hide file tree
Showing 17 changed files with 1,934 additions and 20 deletions.
10 changes: 10 additions & 0 deletions Include/internal/pycore_freelist.h
Expand Up @@ -19,12 +19,14 @@ extern "C" {
# define PyList_MAXFREELIST 80
# define PyFloat_MAXFREELIST 100
# define PyContext_MAXFREELIST 255
# define _PyObjectStackChunk_MAXFREELIST 4
#else
# define PyTuple_NFREELISTS 0
# define PyTuple_MAXFREELIST 0
# define PyList_MAXFREELIST 0
# define PyFloat_MAXFREELIST 0
# define PyContext_MAXFREELIST 0
# define _PyObjectStackChunk_MAXFREELIST 0
#endif

struct _Py_list_state {
Expand Down Expand Up @@ -77,12 +79,20 @@ struct _Py_context_state {
#endif
};

struct _PyObjectStackChunk;

struct _Py_object_stack_state {
struct _PyObjectStackChunk *free_list;
Py_ssize_t numfree;
};

typedef struct _Py_freelist_state {
struct _Py_float_state float_state;
struct _Py_tuple_state tuple_state;
struct _Py_list_state list_state;
struct _Py_slice_state slice_state;
struct _Py_context_state context_state;
struct _Py_object_stack_state object_stack_state;
} _PyFreeListState;

#ifdef __cplusplus
Expand Down
31 changes: 22 additions & 9 deletions Include/internal/pycore_gc.h
Expand Up @@ -37,10 +37,22 @@ static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
}


/* Bit flags for ob_gc_bits (in Py_GIL_DISABLED builds) */
#ifdef Py_GIL_DISABLED
# define _PyGC_BITS_TRACKED (1)
# define _PyGC_BITS_FINALIZED (2)
# define _PyGC_BITS_UNREACHABLE (4)
# define _PyGC_BITS_FROZEN (8)
#endif

/* True if the object is currently tracked by the GC. */
static inline int _PyObject_GC_IS_TRACKED(PyObject *op) {
#ifdef Py_GIL_DISABLED
return (op->ob_gc_bits & _PyGC_BITS_TRACKED) != 0;
#else
PyGC_Head *gc = _Py_AS_GC(op);
return (gc->_gc_next != 0);
#endif
}
#define _PyObject_GC_IS_TRACKED(op) _PyObject_GC_IS_TRACKED(_Py_CAST(PyObject*, op))

Expand Down Expand Up @@ -107,20 +119,21 @@ static inline void _PyGCHead_SET_PREV(PyGC_Head *gc, PyGC_Head *prev) {
gc->_gc_prev = ((gc->_gc_prev & ~_PyGC_PREV_MASK) | uprev);
}

static inline int _PyGCHead_FINALIZED(PyGC_Head *gc) {
return ((gc->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0);
}
static inline void _PyGCHead_SET_FINALIZED(PyGC_Head *gc) {
gc->_gc_prev |= _PyGC_PREV_MASK_FINALIZED;
}

static inline int _PyGC_FINALIZED(PyObject *op) {
#ifdef Py_GIL_DISABLED
return (op->ob_gc_bits & _PyGC_BITS_FINALIZED) != 0;
#else
PyGC_Head *gc = _Py_AS_GC(op);
return _PyGCHead_FINALIZED(gc);
return ((gc->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0);
#endif
}
static inline void _PyGC_SET_FINALIZED(PyObject *op) {
#ifdef Py_GIL_DISABLED
op->ob_gc_bits |= _PyGC_BITS_FINALIZED;
#else
PyGC_Head *gc = _Py_AS_GC(op);
_PyGCHead_SET_FINALIZED(gc);
gc->_gc_prev |= _PyGC_PREV_MASK_FINALIZED;
#endif
}
static inline void _PyGC_CLEAR_FINALIZED(PyObject *op) {
PyGC_Head *gc = _Py_AS_GC(op);
Expand Down
8 changes: 8 additions & 0 deletions Include/internal/pycore_object.h
Expand Up @@ -322,13 +322,17 @@ static inline void _PyObject_GC_TRACK(
"object is in generation which is garbage collected",
filename, lineno, __func__);

#ifdef Py_GIL_DISABLED
op->ob_gc_bits |= _PyGC_BITS_TRACKED;
#else
PyInterpreterState *interp = _PyInterpreterState_GET();
PyGC_Head *generation0 = interp->gc.generation0;
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
_PyGCHead_SET_NEXT(last, gc);
_PyGCHead_SET_PREV(gc, last);
_PyGCHead_SET_NEXT(gc, generation0);
generation0->_gc_prev = (uintptr_t)gc;
#endif
}

/* Tell the GC to stop tracking this object.
Expand All @@ -352,13 +356,17 @@ static inline void _PyObject_GC_UNTRACK(
"object not tracked by the garbage collector",
filename, lineno, __func__);

#ifdef Py_GIL_DISABLED
op->ob_gc_bits &= ~_PyGC_BITS_TRACKED;
#else
PyGC_Head *gc = _Py_AS_GC(op);
PyGC_Head *prev = _PyGCHead_PREV(gc);
PyGC_Head *next = _PyGCHead_NEXT(gc);
_PyGCHead_SET_NEXT(prev, next);
_PyGCHead_SET_PREV(next, prev);
gc->_gc_next = 0;
gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
#endif
}

// Macros to accept any type for the parameter, and to automatically pass
Expand Down
84 changes: 84 additions & 0 deletions Include/internal/pycore_object_stack.h
@@ -0,0 +1,84 @@
#ifndef Py_INTERNAL_OBJECT_STACK_H
#define Py_INTERNAL_OBJECT_STACK_H

#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

// _PyObjectStack is a stack of Python objects implemented as a linked list of
// fixed size buffers.

// Chosen so that _PyObjectStackChunk is a power-of-two size.
#define _Py_OBJECT_STACK_CHUNK_SIZE 254

typedef struct _PyObjectStackChunk {
struct _PyObjectStackChunk *prev;
Py_ssize_t n;
PyObject *objs[_Py_OBJECT_STACK_CHUNK_SIZE];
} _PyObjectStackChunk;

typedef struct _PyObjectStack {
_PyObjectStackChunk *head;
} _PyObjectStack;


extern _PyObjectStackChunk *
_PyObjectStackChunk_New(void);

extern void
_PyObjectStackChunk_Free(_PyObjectStackChunk *);

extern void
_PyObjectStackChunk_ClearFreeList(_PyFreeListState *state, int is_finalization);

// Push an item onto the stack. Return -1 on allocation failure, 0 on success.
static inline int
_PyObjectStack_Push(_PyObjectStack *stack, PyObject *obj)
{
_PyObjectStackChunk *buf = stack->head;
if (buf == NULL || buf->n == _Py_OBJECT_STACK_CHUNK_SIZE) {
buf = _PyObjectStackChunk_New();
if (buf == NULL) {
return -1;
}
buf->prev = stack->head;
buf->n = 0;
stack->head = buf;
}

assert(buf->n >= 0 && buf->n < _Py_OBJECT_STACK_CHUNK_SIZE);
buf->objs[buf->n] = obj;
buf->n++;
return 0;
}

// Pop the top item from the stack. Return NULL if the stack is empty.
static inline PyObject *
_PyObjectStack_Pop(_PyObjectStack *stack)
{
_PyObjectStackChunk *buf = stack->head;
if (buf == NULL) {
return NULL;
}
assert(buf->n > 0 && buf->n <= _Py_OBJECT_STACK_CHUNK_SIZE);
buf->n--;
PyObject *obj = buf->objs[buf->n];
if (buf->n == 0) {
stack->head = buf->prev;
_PyObjectStackChunk_Free(buf);
}
return obj;
}

// Remove all items from the stack
extern void
_PyObjectStack_Clear(_PyObjectStack *stack);

#ifdef __cplusplus
}
#endif
#endif // !Py_INTERNAL_OBJECT_STACK_H
2 changes: 1 addition & 1 deletion Lib/gzip.py
Expand Up @@ -349,7 +349,7 @@ def closed(self):

def close(self):
fileobj = self.fileobj
if fileobj is None:
if fileobj is None or self._buffer.closed:
return
try:
if self.mode == WRITE:
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_asyncgen.py
Expand Up @@ -377,6 +377,7 @@ async def async_gen_wrapper():

self.compare_generators(sync_gen_wrapper(), async_gen_wrapper())

@unittest.skip("sgross: warning ahhh...")
def test_async_gen_exception_12(self):
async def gen():
with self.assertWarnsRegex(RuntimeWarning,
Expand Down
13 changes: 11 additions & 2 deletions Lib/test/test_gc.py
@@ -1,7 +1,7 @@
import unittest
import unittest.mock
from test.support import (verbose, refcount_test,
cpython_only, requires_subprocess)
cpython_only, requires_subprocess, Py_GIL_DISABLED)
from test.support.import_helper import import_module
from test.support.os_helper import temp_dir, TESTFN, unlink
from test.support.script_helper import assert_python_ok, make_script
Expand Down Expand Up @@ -815,6 +815,15 @@ def test_freeze(self):
self.assertEqual(gc.get_freeze_count(), 0)

def test_get_objects(self):
gc.collect()
l = []
l.append(l)
self.assertTrue(
any(l is element for element in gc.get_objects())
)

@unittest.skipIf(Py_GIL_DISABLED, 'need generational GC')
def test_get_objects_generations(self):
gc.collect()
l = []
l.append(l)
Expand Down Expand Up @@ -1225,7 +1234,7 @@ def test_refcount_errors(self):
p.stderr.close()
# Verify that stderr has a useful error message:
self.assertRegex(stderr,
br'gc\.c:[0-9]+: gc_decref: Assertion "gc_get_refs\(g\) > 0" failed.')
br'gc.*\.c:[0-9]+: .*: Assertion "gc_get_refs\(.+\) .*" failed.')
self.assertRegex(stderr,
br'refcount is too small')
# "address : 0x7fb5062efc18"
Expand Down
4 changes: 1 addition & 3 deletions Lib/test/test_io.py
Expand Up @@ -3652,10 +3652,8 @@ def _check_create_at_shutdown(self, **kwargs):
codecs.lookup('utf-8')
class C:
def __init__(self):
self.buf = io.BytesIO()
def __del__(self):
io.TextIOWrapper(self.buf, **{kwargs})
io.TextIOWrapper(io.BytesIO(), **{kwargs})
print("ok")
c = C()
""".format(iomod=iomod, kwargs=kwargs)
Expand Down
3 changes: 3 additions & 0 deletions Makefile.pre.in
Expand Up @@ -439,6 +439,7 @@ PYTHON_OBJS= \
Python/modsupport.o \
Python/mysnprintf.o \
Python/mystrtoul.o \
Python/object_stack.o \
Python/optimizer.o \
Python/optimizer_analysis.o \
Python/parking_lot.o \
Expand Down Expand Up @@ -1832,6 +1833,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_frame.h \
$(srcdir)/Include/internal/pycore_freelist.h \
$(srcdir)/Include/internal/pycore_function.h \
$(srcdir)/Include/internal/pycore_gc.h \
$(srcdir)/Include/internal/pycore_genobject.h \
$(srcdir)/Include/internal/pycore_getopt.h \
$(srcdir)/Include/internal/pycore_gil.h \
Expand All @@ -1853,6 +1855,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_namespace.h \
$(srcdir)/Include/internal/pycore_object.h \
$(srcdir)/Include/internal/pycore_object_alloc.h \
$(srcdir)/Include/internal/pycore_object_stack.h \
$(srcdir)/Include/internal/pycore_object_state.h \
$(srcdir)/Include/internal/pycore_obmalloc.h \
$(srcdir)/Include/internal/pycore_obmalloc_init.h \
Expand Down
1 change: 1 addition & 0 deletions PCbuild/_freeze_module.vcxproj
Expand Up @@ -230,6 +230,7 @@
<ClCompile Include="..\Python\modsupport.c" />
<ClCompile Include="..\Python\mysnprintf.c" />
<ClCompile Include="..\Python\mystrtoul.c" />
<ClCompile Include="..\Python\object_stack.c" />
<ClCompile Include="..\Python\optimizer.c" />
<ClCompile Include="..\Python\optimizer_analysis.c" />
<ClCompile Include="..\Python\parking_lot.c" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/_freeze_module.vcxproj.filters
Expand Up @@ -289,6 +289,9 @@
<ClCompile Include="..\Objects\object.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\object_stack.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Objects\obmalloc.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Expand Up @@ -591,6 +591,7 @@
<ClCompile Include="..\Python\modsupport.c" />
<ClCompile Include="..\Python\mysnprintf.c" />
<ClCompile Include="..\Python\mystrtoul.c" />
<ClCompile Include="..\Python\object_stack.c" />
<ClCompile Include="..\Python\optimizer.c" />
<ClCompile Include="..\Python\optimizer_analysis.c" />
<ClCompile Include="..\Python\parking_lot.c" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Expand Up @@ -1355,6 +1355,9 @@
<ClCompile Include="..\Python\mystrtoul.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\object_stack.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\optimizer.c">
<Filter>Python</Filter>
</ClCompile>
Expand Down
8 changes: 6 additions & 2 deletions Python/gc.c
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -1942,3 +1944,5 @@ PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg)
done:
gcstate->enabled = origenstate;
}

#endif // Py_GIL_DISABLED

0 comments on commit c93a17c

Please sign in to comment.