-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathFinalizeTrigger.java
More file actions
215 lines (192 loc) · 7.59 KB
/
FinalizeTrigger.java
File metadata and controls
215 lines (192 loc) · 7.59 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package org.python.core.finalization;
import org.python.core.PyObject;
import org.python.core.JyAttribute;
import org.python.core.PySystemState;
import org.python.modules.gc;
/**
* To use finalizers on {@code PyObject}s, read the documentation of
* {@link org.python.core.finalization.FinalizablePyObject}.
*/
public class FinalizeTrigger {
/**
* This flag tells the finalize trigger to call
* {@link gc#notifyFinalize(PyObject)} after it called the finalizer.
*/
public static final byte NOTIFY_GC_FLAG = (1<<0);
/**
* Indicates that the underlying PyObject was never intended to be finalized.
* It is actually not finalizable and the trigger only exists to notify
* {@link org.python.modules.gc} that the underlying object was finalized.
* This is needed for some advanced gc-functionality.
*/
public static final byte NOT_FINALIZABLE_FLAG = (1<<3);
/**
* Indicates that only
* {@link org.python.core.finalization.FinalizableBuiltin}
* shall be called.
*/
public static final byte ONLY_BUILTIN_FLAG = (1<<4);
/**
* Indicates that this trigger was already finalized.
*/
public static final byte FINALIZED_FLAG = (1<<5);
/**
* This factory hook is reserved for use by JyNI.
* It allows to replace the default {@code FinalizeTrigger}.
* JyNI needs it to support garbage collection.
*/
public static FinalizeTriggerFactory factory;
public static FinalizeTrigger makeTrigger(PyObject toFinalize) {
if (factory != null) {
return factory.makeTrigger(toFinalize);
} else {
return new FinalizeTrigger(toFinalize);
}
}
public static boolean hasActiveTrigger(PyObject obj) {
Object fn = JyAttribute.getAttr(obj, JyAttribute.FINALIZE_TRIGGER_ATTR);
return fn != null && ((FinalizeTrigger) fn).isActive();
}
public static boolean isFinalizable(PyObject obj) {
return obj instanceof FinalizablePyObject || obj instanceof FinalizableBuiltin
|| obj instanceof FinalizablePyObjectDerived;
}
/**
* Recreates the {@code FinalizeTrigger} of the given object. This makes sure that
* once the resurrected object is gc'ed again, its {@code __del__}-method will be
* called again.
*/
public static void ensureFinalizer(PyObject resurrect) {
JyAttribute.setAttr(resurrect, JyAttribute.FINALIZE_TRIGGER_ATTR,
makeTrigger(resurrect));
}
public static void runFinalizer(PyObject toFinalize) {
runFinalizer(toFinalize, false);
}
public static void runFinalizer(PyObject toFinalize, boolean runBuiltinOnly) {
if (!runBuiltinOnly) {
if (toFinalize instanceof FinalizablePyObjectDerived) {
try {
((FinalizablePyObjectDerived) toFinalize).__del_derived__();
} catch (Exception e) {
}
} else if (toFinalize instanceof FinalizablePyObject) {
try {
((FinalizablePyObject) toFinalize).__del__();
} catch (Exception e) {
}
}
}
if (toFinalize instanceof FinalizableBuiltin) {
try {
((FinalizableBuiltin) toFinalize).__del_builtin__();
} catch (Exception e) {
}
}
}
public static void appendFinalizeTriggerForBuiltin(PyObject obj) {
if (obj instanceof FinalizableBuiltin) {
FinalizeTrigger ft = makeTrigger(obj);
ft.flags = ONLY_BUILTIN_FLAG;
JyAttribute.setAttr(obj, JyAttribute.FINALIZE_TRIGGER_ATTR, ft);
} else {
JyAttribute.delAttr(obj, JyAttribute.FINALIZE_TRIGGER_ATTR);
}
}
protected PyObject toFinalize;
public byte flags = 0;
public void clear() {
toFinalize = null;
}
public void trigger(PyObject toFinalize)
{
this.toFinalize = toFinalize;
}
public boolean isActive() {
return toFinalize != null;
}
protected FinalizeTrigger(PyObject toFinalize) {
this.toFinalize = toFinalize;
}
protected boolean isCyclic() {
gc.CycleMarkAttr cm = (gc.CycleMarkAttr)
JyAttribute.getAttr(toFinalize, JyAttribute.GC_CYCLE_MARK_ATTR);
if (cm != null && cm.isCyclic()) {
return true;
} else {
gc.markCyclicObjects(toFinalize, (flags & NOT_FINALIZABLE_FLAG) == 0);
cm = (gc.CycleMarkAttr)
JyAttribute.getAttr(toFinalize, JyAttribute.GC_CYCLE_MARK_ATTR);
return cm != null && cm.isCyclic();
}
}
protected boolean isUncollectable() {
gc.CycleMarkAttr cm = (gc.CycleMarkAttr)
JyAttribute.getAttr(toFinalize, JyAttribute.GC_CYCLE_MARK_ATTR);
if (cm != null && cm.isUncollectable()) {
return true;
} else {
gc.markCyclicObjects(toFinalize, (flags & NOT_FINALIZABLE_FLAG) == 0);
cm = (gc.CycleMarkAttr)
JyAttribute.getAttr(toFinalize, JyAttribute.GC_CYCLE_MARK_ATTR);
return cm != null && cm.isUncollectable();
}
}
public void performFinalization() {
if (toFinalize != null) {
byte saveGarbage = 0;
if ((gc.getJythonGCFlags() & gc.DONT_FINALIZE_CYCLIC_GARBAGE) != 0) {
if (isUncollectable()) {
saveGarbage = 1;
} else if (!isCyclic()) {
saveGarbage = -1;
runFinalizer(toFinalize, (flags & ONLY_BUILTIN_FLAG) != 0);
}
} else {
if ((flags & NOT_FINALIZABLE_FLAG) == 0) {
runFinalizer(toFinalize, (flags & ONLY_BUILTIN_FLAG) != 0);
}
}
if ((gc.getJythonGCFlags() & gc.VERBOSE_FINALIZE) != 0) {
gc.writeDebug("gc", "finalization of "+toFinalize);
}
if (saveGarbage == 1 || (saveGarbage == 0 &&
(gc.get_debug() & gc.DEBUG_SAVEALL) != 0 && isCyclic())) {
if ((flags & NOT_FINALIZABLE_FLAG) == 0) {
//Finalizable objects in gc.garbage get a special FinalizeTrigger
//that only runs the builtin finalizer. This is needed because
//from Python the user can't call the builtin-part of the
//finalizer by hand.
appendFinalizeTriggerForBuiltin(toFinalize);
}
gc.garbage.add(toFinalize);
if ((gc.getJythonGCFlags() & gc.VERBOSE_FINALIZE) != 0) {
gc.writeDebug("gc", toFinalize+" added to garbage.");
}
}
}
if ((flags & NOTIFY_GC_FLAG) != 0) {
if ((gc.getJythonGCFlags() & gc.VERBOSE_FINALIZE) != 0) {
gc.writeDebug("gc", "notify finalization of "+toFinalize);
}
gc.notifyFinalize(toFinalize);
flags &= ~NOTIFY_GC_FLAG;
}
}
protected void finalize() throws Throwable {
flags |= FINALIZED_FLAG;
gc.notifyPreFinalization();
if (gc.delayedFinalizationEnabled() && toFinalize != null) {
if ((gc.getJythonGCFlags() & gc.VERBOSE_FINALIZE) != 0) {
gc.writeDebug("gc", "delayed finalization for "+toFinalize);
}
gc.registerForDelayedFinalization(toFinalize);
} else {
performFinalization();
}
gc.notifyPostFinalization();
}
public boolean isFinalized() {
return (flags & FINALIZED_FLAG) != 0;
}
}