codekingpro/portable-devtools
114k
1#ifndef Py_INTERNAL_CELL_H2#define Py_INTERNAL_CELL_H3 4#include "pycore_critical_section.h"5#include "pycore_object.h"6#include "pycore_stackref.h"7 8#ifdef __cplusplus9extern "C" {10#endif11 12#ifndef Py_BUILD_CORE13# error "this header requires Py_BUILD_CORE define"14#endif15 16// Sets the cell contents to `value` and return previous contents. Steals a17// reference to `value`.18static inline PyObject *19PyCell_SwapTakeRef(PyCellObject *cell, PyObject *value)20{21 PyObject *old_value;22 Py_BEGIN_CRITICAL_SECTION(cell);23 old_value = cell->ob_ref;24 FT_ATOMIC_STORE_PTR_RELEASE(cell->ob_ref, value);25 Py_END_CRITICAL_SECTION();26 return old_value;27}28 29static inline void30PyCell_SetTakeRef(PyCellObject *cell, PyObject *value)31{32 PyObject *old_value = PyCell_SwapTakeRef(cell, value);33 Py_XDECREF(old_value);34}35 36// Gets the cell contents. Returns a new reference.37static inline PyObject *38PyCell_GetRef(PyCellObject *cell)39{40 PyObject *res;41 Py_BEGIN_CRITICAL_SECTION(cell);42#ifdef Py_GIL_DISABLED43 res = _Py_XNewRefWithLock(cell->ob_ref);44#else45 res = Py_XNewRef(cell->ob_ref);46#endif47 Py_END_CRITICAL_SECTION();48 return res;49}50 51static inline _PyStackRef52_PyCell_GetStackRef(PyCellObject *cell)53{54 PyObject *value;55#ifdef Py_GIL_DISABLED56 value = _PyObject_CAST(_Py_atomic_load_ptr(&cell->ob_ref));57 if (value == NULL) {58 return PyStackRef_NULL;59 }60 _PyStackRef ref;61 if (_Py_TryIncrefCompareStackRef(&cell->ob_ref, value, &ref)) {62 return ref;63 }64#endif65 value = PyCell_GetRef(cell);66 if (value == NULL) {67 return PyStackRef_NULL;68 }69 return PyStackRef_FromPyObjectSteal(value);70}71 72#ifdef __cplusplus73}74#endif75#endif /* !Py_INTERNAL_CELL_H */76 