mirror of https://github.com/python/cpython
GH-120024: Tidy up pycore_stackref.h, splitting into GIL and free-threading sections (GH-125095)
This commit is contained in:
parent
440632adb2
commit
eb18574cc3
|
@ -60,54 +60,22 @@ typedef union _PyStackRef {
|
||||||
#define Py_TAG_BITS ((uintptr_t)1)
|
#define Py_TAG_BITS ((uintptr_t)1)
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
#ifdef Py_GIL_DISABLED
|
||||||
static const _PyStackRef PyStackRef_NULL = { .bits = 0 | Py_TAG_DEFERRED};
|
|
||||||
#else
|
|
||||||
static const _PyStackRef PyStackRef_NULL = { .bits = 0 };
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
|
||||||
#define PyStackRef_IsNull(stackref) ((stackref).bits == PyStackRef_NULL.bits)
|
#define PyStackRef_IsNull(stackref) ((stackref).bits == PyStackRef_NULL.bits)
|
||||||
|
#define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_DEFERRED })
|
||||||
|
#define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) | Py_TAG_DEFERRED })
|
||||||
|
#define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) | Py_TAG_DEFERRED })
|
||||||
|
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
# define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) | Py_TAG_DEFERRED })
|
|
||||||
#else
|
|
||||||
# define PyStackRef_True ((_PyStackRef){.bits = ((uintptr_t)&_Py_TrueStruct) })
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
# define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) | Py_TAG_DEFERRED })
|
|
||||||
#else
|
|
||||||
# define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) })
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
# define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) | Py_TAG_DEFERRED })
|
|
||||||
#else
|
|
||||||
# define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) })
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Note: the following are all macros because MSVC (Windows) has trouble inlining them.
|
|
||||||
|
|
||||||
#define PyStackRef_Is(a, b) ((a).bits == (b).bits)
|
|
||||||
|
|
||||||
#define PyStackRef_IsDeferred(ref) (((ref).bits & Py_TAG_BITS) == Py_TAG_DEFERRED)
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
// Gets a PyObject * from a _PyStackRef
|
|
||||||
static inline PyObject *
|
static inline PyObject *
|
||||||
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
|
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
|
||||||
{
|
{
|
||||||
PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
|
PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
|
||||||
return cleared;
|
return cleared;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
# define PyStackRef_AsPyObjectBorrow(stackref) ((PyObject *)(stackref).bits)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Converts a PyStackRef back to a PyObject *, stealing the
|
#define PyStackRef_IsDeferred(ref) (((ref).bits & Py_TAG_BITS) == Py_TAG_DEFERRED)
|
||||||
// PyStackRef.
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
static inline PyObject *
|
static inline PyObject *
|
||||||
PyStackRef_AsPyObjectSteal(_PyStackRef stackref)
|
PyStackRef_AsPyObjectSteal(_PyStackRef stackref)
|
||||||
{
|
{
|
||||||
|
@ -117,18 +85,7 @@ PyStackRef_AsPyObjectSteal(_PyStackRef stackref)
|
||||||
}
|
}
|
||||||
return PyStackRef_AsPyObjectBorrow(stackref);
|
return PyStackRef_AsPyObjectBorrow(stackref);
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
# define PyStackRef_AsPyObjectSteal(stackref) PyStackRef_AsPyObjectBorrow(stackref)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Converts a PyStackRef back to a PyObject *, converting the
|
|
||||||
// stackref to a new reference.
|
|
||||||
#define PyStackRef_AsPyObjectNew(stackref) Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref))
|
|
||||||
|
|
||||||
#define PyStackRef_TYPE(stackref) Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref))
|
|
||||||
|
|
||||||
// Converts a PyObject * to a PyStackRef, stealing the reference
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
static inline _PyStackRef
|
static inline _PyStackRef
|
||||||
_PyStackRef_FromPyObjectSteal(PyObject *obj)
|
_PyStackRef_FromPyObjectSteal(PyObject *obj)
|
||||||
{
|
{
|
||||||
|
@ -139,13 +96,7 @@ _PyStackRef_FromPyObjectSteal(PyObject *obj)
|
||||||
return ((_PyStackRef){.bits = ((uintptr_t)(obj)) | tag});
|
return ((_PyStackRef){.bits = ((uintptr_t)(obj)) | tag});
|
||||||
}
|
}
|
||||||
# define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
|
# define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
|
||||||
#else
|
|
||||||
# define PyStackRef_FromPyObjectSteal(obj) ((_PyStackRef){.bits = ((uintptr_t)(obj))})
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
// Converts a PyObject * to a PyStackRef, with a new reference
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
static inline _PyStackRef
|
static inline _PyStackRef
|
||||||
PyStackRef_FromPyObjectNew(PyObject *obj)
|
PyStackRef_FromPyObjectNew(PyObject *obj)
|
||||||
{
|
{
|
||||||
|
@ -159,13 +110,8 @@ PyStackRef_FromPyObjectNew(PyObject *obj)
|
||||||
return (_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) | Py_TAG_PTR };
|
return (_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) | Py_TAG_PTR };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# define PyStackRef_FromPyObjectNew(obj) PyStackRef_FromPyObjectNew(_PyObject_CAST(obj))
|
#define PyStackRef_FromPyObjectNew(obj) PyStackRef_FromPyObjectNew(_PyObject_CAST(obj))
|
||||||
#else
|
|
||||||
# define PyStackRef_FromPyObjectNew(obj) ((_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) })
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
// Same as PyStackRef_FromPyObjectNew but only for immortal objects.
|
|
||||||
static inline _PyStackRef
|
static inline _PyStackRef
|
||||||
PyStackRef_FromPyObjectImmortal(PyObject *obj)
|
PyStackRef_FromPyObjectImmortal(PyObject *obj)
|
||||||
{
|
{
|
||||||
|
@ -175,24 +121,9 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
|
||||||
assert(_Py_IsImmortal(obj));
|
assert(_Py_IsImmortal(obj));
|
||||||
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_DEFERRED };
|
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_DEFERRED };
|
||||||
}
|
}
|
||||||
# define PyStackRef_FromPyObjectImmortal(obj) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))
|
#define PyStackRef_FromPyObjectImmortal(obj) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))
|
||||||
#else
|
|
||||||
# define PyStackRef_FromPyObjectImmortal(obj) ((_PyStackRef){ .bits = (uintptr_t)(obj) })
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#define PyStackRef_CLOSE(REF) \
|
||||||
#define PyStackRef_CLEAR(op) \
|
|
||||||
do { \
|
|
||||||
_PyStackRef *_tmp_op_ptr = &(op); \
|
|
||||||
_PyStackRef _tmp_old_op = (*_tmp_op_ptr); \
|
|
||||||
if (!PyStackRef_IsNull(_tmp_old_op)) { \
|
|
||||||
*_tmp_op_ptr = PyStackRef_NULL; \
|
|
||||||
PyStackRef_CLOSE(_tmp_old_op); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
# define PyStackRef_CLOSE(REF) \
|
|
||||||
do { \
|
do { \
|
||||||
_PyStackRef _close_tmp = (REF); \
|
_PyStackRef _close_tmp = (REF); \
|
||||||
assert(!PyStackRef_IsNull(_close_tmp)); \
|
assert(!PyStackRef_IsNull(_close_tmp)); \
|
||||||
|
@ -200,20 +131,7 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
|
||||||
Py_DECREF(PyStackRef_AsPyObjectBorrow(_close_tmp)); \
|
Py_DECREF(PyStackRef_AsPyObjectBorrow(_close_tmp)); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
#else
|
|
||||||
# define PyStackRef_CLOSE(stackref) Py_DECREF(PyStackRef_AsPyObjectBorrow(stackref))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define PyStackRef_XCLOSE(stackref) \
|
|
||||||
do { \
|
|
||||||
_PyStackRef _tmp = (stackref); \
|
|
||||||
if (!PyStackRef_IsNull(_tmp)) { \
|
|
||||||
PyStackRef_CLOSE(_tmp); \
|
|
||||||
} \
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
static inline _PyStackRef
|
static inline _PyStackRef
|
||||||
PyStackRef_DUP(_PyStackRef stackref)
|
PyStackRef_DUP(_PyStackRef stackref)
|
||||||
{
|
{
|
||||||
|
@ -227,9 +145,6 @@ PyStackRef_DUP(_PyStackRef stackref)
|
||||||
Py_INCREF(PyStackRef_AsPyObjectBorrow(stackref));
|
Py_INCREF(PyStackRef_AsPyObjectBorrow(stackref));
|
||||||
return stackref;
|
return stackref;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
# define PyStackRef_DUP(stackref) PyStackRef_FromPyObjectSteal(Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref)))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Convert a possibly deferred reference to a strong reference.
|
// Convert a possibly deferred reference to a strong reference.
|
||||||
static inline _PyStackRef
|
static inline _PyStackRef
|
||||||
|
@ -238,13 +153,62 @@ PyStackRef_AsStrongReference(_PyStackRef stackref)
|
||||||
return PyStackRef_FromPyObjectSteal(PyStackRef_AsPyObjectSteal(stackref));
|
return PyStackRef_FromPyObjectSteal(PyStackRef_AsPyObjectSteal(stackref));
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
|
||||||
_PyObjectStack_FromStackRefStack(PyObject **dst, const _PyStackRef *src, size_t length)
|
#else // Py_GIL_DISABLED
|
||||||
{
|
|
||||||
for (size_t i = 0; i < length; i++) {
|
// With GIL
|
||||||
dst[i] = PyStackRef_AsPyObjectBorrow(src[i]);
|
static const _PyStackRef PyStackRef_NULL = { .bits = 0 };
|
||||||
}
|
#define PyStackRef_IsNull(stackref) ((stackref).bits == 0)
|
||||||
}
|
#define PyStackRef_True ((_PyStackRef){.bits = (uintptr_t)&_Py_TrueStruct })
|
||||||
|
#define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) })
|
||||||
|
#define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) })
|
||||||
|
|
||||||
|
#define PyStackRef_AsPyObjectBorrow(stackref) ((PyObject *)(stackref).bits)
|
||||||
|
|
||||||
|
#define PyStackRef_AsPyObjectSteal(stackref) PyStackRef_AsPyObjectBorrow(stackref)
|
||||||
|
|
||||||
|
#define PyStackRef_FromPyObjectSteal(obj) ((_PyStackRef){.bits = ((uintptr_t)(obj))})
|
||||||
|
|
||||||
|
#define PyStackRef_FromPyObjectNew(obj) ((_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) })
|
||||||
|
|
||||||
|
#define PyStackRef_FromPyObjectImmortal(obj) ((_PyStackRef){ .bits = (uintptr_t)(obj) })
|
||||||
|
|
||||||
|
#define PyStackRef_CLOSE(stackref) Py_DECREF(PyStackRef_AsPyObjectBorrow(stackref))
|
||||||
|
|
||||||
|
#define PyStackRef_DUP(stackref) PyStackRef_FromPyObjectSteal(Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref)))
|
||||||
|
|
||||||
|
|
||||||
|
#endif // Py_GIL_DISABLED
|
||||||
|
|
||||||
|
// Note: this is a macro because MSVC (Windows) has trouble inlining it.
|
||||||
|
|
||||||
|
#define PyStackRef_Is(a, b) ((a).bits == (b).bits)
|
||||||
|
|
||||||
|
// Converts a PyStackRef back to a PyObject *, converting the
|
||||||
|
// stackref to a new reference.
|
||||||
|
#define PyStackRef_AsPyObjectNew(stackref) Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref))
|
||||||
|
|
||||||
|
#define PyStackRef_TYPE(stackref) Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref))
|
||||||
|
|
||||||
|
#define PyStackRef_CLEAR(op) \
|
||||||
|
do { \
|
||||||
|
_PyStackRef *_tmp_op_ptr = &(op); \
|
||||||
|
_PyStackRef _tmp_old_op = (*_tmp_op_ptr); \
|
||||||
|
if (!PyStackRef_IsNull(_tmp_old_op)) { \
|
||||||
|
*_tmp_op_ptr = PyStackRef_NULL; \
|
||||||
|
PyStackRef_CLOSE(_tmp_old_op); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define PyStackRef_XCLOSE(stackref) \
|
||||||
|
do { \
|
||||||
|
_PyStackRef _tmp = (stackref); \
|
||||||
|
if (!PyStackRef_IsNull(_tmp)) { \
|
||||||
|
PyStackRef_CLOSE(_tmp); \
|
||||||
|
} \
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// StackRef type checks
|
// StackRef type checks
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue