|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package java.lang.ref; |
|
|
|
import java.security.PrivilegedAction; |
|
import java.security.AccessController; |
|
import jdk.internal.misc.JavaLangAccess; |
|
import jdk.internal.misc.SharedSecrets; |
|
import jdk.internal.misc.VM; |
|
|
|
final class Finalizer extends FinalReference<Object> { /* Package-private; must be in |
|
same package as the Reference |
|
class */ |
|
|
|
private static ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
|
|
|
|
|
private static Finalizer unfinalized = null; |
|
|
|
|
|
private static final Object lock = new Object(); |
|
|
|
private Finalizer next, prev; |
|
|
|
private Finalizer(Object finalizee) { |
|
super(finalizee, queue); |
|
|
|
synchronized (lock) { |
|
if (unfinalized != null) { |
|
this.next = unfinalized; |
|
unfinalized.prev = this; |
|
} |
|
unfinalized = this; |
|
} |
|
} |
|
|
|
static ReferenceQueue<Object> getQueue() { |
|
return queue; |
|
} |
|
|
|
|
|
static void register(Object finalizee) { |
|
new Finalizer(finalizee); |
|
} |
|
|
|
private void runFinalizer(JavaLangAccess jla) { |
|
synchronized (lock) { |
|
if (this.next == this) |
|
return; |
|
|
|
if (unfinalized == this) |
|
unfinalized = this.next; |
|
else |
|
this.prev.next = this.next; |
|
if (this.next != null) |
|
this.next.prev = this.prev; |
|
this.prev = null; |
|
this.next = this; |
|
} |
|
|
|
try { |
|
Object finalizee = this.get(); |
|
if (finalizee != null && !(finalizee instanceof java.lang.Enum)) { |
|
jla.invokeFinalize(finalizee); |
|
|
|
// Clear stack slot containing this variable, to decrease |
|
|
|
finalizee = null; |
|
} |
|
} catch (Throwable x) { } |
|
super.clear(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static void forkSecondaryFinalizer(final Runnable proc) { |
|
AccessController.doPrivileged( |
|
new PrivilegedAction<>() { |
|
public Void run() { |
|
ThreadGroup tg = Thread.currentThread().getThreadGroup(); |
|
for (ThreadGroup tgn = tg; |
|
tgn != null; |
|
tg = tgn, tgn = tg.getParent()); |
|
Thread sft = new Thread(tg, proc, "Secondary finalizer", 0, false); |
|
sft.start(); |
|
try { |
|
sft.join(); |
|
} catch (InterruptedException x) { |
|
Thread.currentThread().interrupt(); |
|
} |
|
return null; |
|
}}); |
|
} |
|
|
|
|
|
static void runFinalization() { |
|
if (VM.initLevel() == 0) { |
|
return; |
|
} |
|
|
|
forkSecondaryFinalizer(new Runnable() { |
|
private volatile boolean running; |
|
public void run() { |
|
|
|
if (running) |
|
return; |
|
final JavaLangAccess jla = SharedSecrets.getJavaLangAccess(); |
|
running = true; |
|
for (Finalizer f; (f = (Finalizer)queue.poll()) != null; ) |
|
f.runFinalizer(jla); |
|
} |
|
}); |
|
} |
|
|
|
private static class FinalizerThread extends Thread { |
|
private volatile boolean running; |
|
FinalizerThread(ThreadGroup g) { |
|
super(g, null, "Finalizer", 0, false); |
|
} |
|
public void run() { |
|
|
|
if (running) |
|
return; |
|
|
|
// Finalizer thread starts before System.initializeSystemClass |
|
|
|
while (VM.initLevel() == 0) { |
|
|
|
try { |
|
VM.awaitInitLevel(1); |
|
} catch (InterruptedException x) { |
|
// ignore and continue |
|
} |
|
} |
|
final JavaLangAccess jla = SharedSecrets.getJavaLangAccess(); |
|
running = true; |
|
for (;;) { |
|
try { |
|
Finalizer f = (Finalizer)queue.remove(); |
|
f.runFinalizer(jla); |
|
} catch (InterruptedException x) { |
|
// ignore and continue |
|
} |
|
} |
|
} |
|
} |
|
|
|
static { |
|
ThreadGroup tg = Thread.currentThread().getThreadGroup(); |
|
for (ThreadGroup tgn = tg; |
|
tgn != null; |
|
tg = tgn, tgn = tg.getParent()); |
|
Thread finalizer = new FinalizerThread(tg); |
|
finalizer.setPriority(Thread.MAX_PRIORITY - 2); |
|
finalizer.setDaemon(true); |
|
finalizer.start(); |
|
} |
|
|
|
} |