forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpycore_cell.h
More file actions
103 lines (93 loc) · 2.48 KB
/
pycore_cell.h
File metadata and controls
103 lines (93 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef Py_INTERNAL_CELL_H
#define Py_INTERNAL_CELL_H
#include "pycore_critical_section.h"
#include "pycore_object.h"
#include "pycore_stackref.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
// Sets the cell contents to `value` and return previous contents. Steals a
// reference to `value`.
static inline PyObject *
PyCell_SwapTakeRef(PyCellObject *cell, PyObject *value, int* result)
{
PyObject *old_value = NULL;
*result = 0;
Py_BEGIN_CRITICAL_SECTION(cell);
do {
if(!Py_CHECKWRITE(cell)){
PyRegion_CLEARLOCAL(value);
*result = -1;
break;
}
// Simple and fast clear
if (PyRegion_IsLocal(cell)) {
old_value = cell->ob_ref;
FT_ATOMIC_STORE_PTR_RELEASE(cell->ob_ref, value);
break;
}
old_value = cell->ob_ref;
if (PyRegion_AddLocalRef(old_value)) {
assert(false && "This should never fail, since we have a ref to cell");
}
if (PyRegion_TakeRef(cell, value)) {
PyRegion_CLEARLOCAL(value);
*result = -1;
break;
}
FT_ATOMIC_STORE_PTR_RELEASE(cell->ob_ref, value);
PyRegion_RemoveRef(cell, old_value);
} while (false);
Py_END_CRITICAL_SECTION();
return old_value;
}
static inline int
PyCell_SetTakeRef(PyCellObject *cell, PyObject *value)
{
int result = 0;
PyObject *old_value = PyCell_SwapTakeRef(cell, value, &result);
PyRegion_RemoveRef(cell, old_value);
Py_XDECREF(old_value);
return result;
}
// Gets the cell contents. Returns a new reference.
static inline PyObject *
PyCell_GetRef(PyCellObject *cell)
{
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(cell);
#ifdef Py_GIL_DISABLED
res = _Py_XNewRefWithLock(cell->ob_ref);
#else
res = PyRegion_XNewRef(cell->ob_ref);
#endif
Py_END_CRITICAL_SECTION();
return res;
}
static inline _PyStackRef
_PyCell_GetStackRef(PyCellObject *cell)
{
PyObject *value;
#ifdef Py_GIL_DISABLED
value = _Py_atomic_load_ptr(&cell->ob_ref);
if (value == NULL) {
return PyStackRef_NULL;
}
_PyStackRef ref;
if (_Py_TryIncrefCompareStackRef(&cell->ob_ref, value, &ref)) {
return ref;
}
#endif
value = PyCell_GetRef(cell);
if (value == NULL) {
return PyStackRef_NULL;
}
return PyStackRef_FromPyObjectSteal(value);
}
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_CELL_H */