Debian Codegraph @

Include/internal/pycore_cell.h

raw
1 #ifndef Py_INTERNAL_CELL_H
2 #define Py_INTERNAL_CELL_H
3
4 #include "pycore_critical_section.h"
5 #include "pycore_object.h"
6 #include "pycore_stackref.h"
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 #ifndef Py_BUILD_CORE
13 # error "this header requires Py_BUILD_CORE define"
14 #endif
15
16 // Sets the cell contents to `value` and return previous contents. Steals a
17 // reference to `value`.
18 static inline PyObject *
19 PyCell_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
29 static inline void
30 PyCell_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.
37 static inline PyObject *
38 PyCell_GetRef(PyCellObject *cell)
39 {
40 PyObject *res;
41 Py_BEGIN_CRITICAL_SECTION(cell);
42 #ifdef Py_GIL_DISABLED
43 res = _Py_XNewRefWithLock(cell->ob_ref);
44 #else
45 res = Py_XNewRef(cell->ob_ref);
46 #endif
47 Py_END_CRITICAL_SECTION();
48 return res;
49 }
50
51 static inline _PyStackRef
52 _PyCell_GetStackRef(PyCellObject *cell)
53 {
54 PyObject *value;
55 #ifdef Py_GIL_DISABLED
56 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 #endif
65 value = PyCell_GetRef(cell);
66 if (value == NULL) {
67 return PyStackRef_NULL;
68 }
69 return PyStackRef_FromPyObjectSteal(value);
70 }
71
72 #ifdef __cplusplus
73 }
74 #endif
75 #endif /* !Py_INTERNAL_CELL_H */