0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang.ref;
0N/A
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.AccessController;
0N/A
0N/A
0N/Afinal class Finalizer extends FinalReference { /* Package-private; must be in
0N/A same package as the Reference
0N/A class */
0N/A
0N/A /* A native method that invokes an arbitrary object's finalize method is
0N/A required since the finalize method is protected
0N/A */
0N/A static native void invokeFinalizeMethod(Object o) throws Throwable;
0N/A
5874N/A private static ReferenceQueue queue = new ReferenceQueue();
5874N/A private static Finalizer unfinalized = null;
5874N/A private static final Object lock = new Object();
0N/A
0N/A private Finalizer
0N/A next = null,
0N/A prev = null;
0N/A
0N/A private boolean hasBeenFinalized() {
0N/A return (next == this);
0N/A }
0N/A
0N/A private void add() {
0N/A synchronized (lock) {
0N/A if (unfinalized != null) {
0N/A this.next = unfinalized;
0N/A unfinalized.prev = this;
0N/A }
0N/A unfinalized = this;
0N/A }
0N/A }
0N/A
0N/A private void remove() {
0N/A synchronized (lock) {
0N/A if (unfinalized == this) {
0N/A if (this.next != null) {
0N/A unfinalized = this.next;
0N/A } else {
0N/A unfinalized = this.prev;
0N/A }
0N/A }
0N/A if (this.next != null) {
0N/A this.next.prev = this.prev;
0N/A }
0N/A if (this.prev != null) {
0N/A this.prev.next = this.next;
0N/A }
0N/A this.next = this; /* Indicates that this has been finalized */
0N/A this.prev = this;
0N/A }
0N/A }
0N/A
0N/A private Finalizer(Object finalizee) {
0N/A super(finalizee, queue);
0N/A add();
0N/A }
0N/A
0N/A /* Invoked by VM */
0N/A static void register(Object finalizee) {
0N/A new Finalizer(finalizee);
0N/A }
0N/A
0N/A private void runFinalizer() {
0N/A synchronized (this) {
0N/A if (hasBeenFinalized()) return;
0N/A remove();
0N/A }
0N/A try {
0N/A Object finalizee = this.get();
0N/A if (finalizee != null && !(finalizee instanceof java.lang.Enum)) {
0N/A invokeFinalizeMethod(finalizee);
0N/A /* Clear stack slot containing this variable, to decrease
0N/A the chances of false retention with a conservative GC */
0N/A finalizee = null;
0N/A }
0N/A } catch (Throwable x) { }
0N/A super.clear();
0N/A }
0N/A
0N/A /* Create a privileged secondary finalizer thread in the system thread
0N/A group for the given Runnable, and wait for it to complete.
0N/A
0N/A This method is used by both runFinalization and runFinalizersOnExit.
0N/A The former method invokes all pending finalizers, while the latter
0N/A invokes all uninvoked finalizers if on-exit finalization has been
0N/A enabled.
0N/A
0N/A These two methods could have been implemented by offloading their work
0N/A to the regular finalizer thread and waiting for that thread to finish.
0N/A The advantage of creating a fresh thread, however, is that it insulates
0N/A invokers of these methods from a stalled or deadlocked finalizer thread.
0N/A */
0N/A private static void forkSecondaryFinalizer(final Runnable proc) {
28N/A AccessController.doPrivileged(
28N/A new PrivilegedAction<Void>() {
28N/A public Void run() {
0N/A ThreadGroup tg = Thread.currentThread().getThreadGroup();
0N/A for (ThreadGroup tgn = tg;
0N/A tgn != null;
0N/A tg = tgn, tgn = tg.getParent());
0N/A Thread sft = new Thread(tg, proc, "Secondary finalizer");
0N/A sft.start();
0N/A try {
0N/A sft.join();
0N/A } catch (InterruptedException x) {
0N/A /* Ignore */
0N/A }
0N/A return null;
28N/A }});
0N/A }
0N/A
0N/A /* Called by Runtime.runFinalization() */
0N/A static void runFinalization() {
0N/A forkSecondaryFinalizer(new Runnable() {
5874N/A private volatile boolean running;
0N/A public void run() {
5874N/A if (running)
5874N/A return;
5874N/A running = true;
0N/A for (;;) {
0N/A Finalizer f = (Finalizer)queue.poll();
0N/A if (f == null) break;
0N/A f.runFinalizer();
0N/A }
0N/A }
0N/A });
0N/A }
0N/A
0N/A /* Invoked by java.lang.Shutdown */
0N/A static void runAllFinalizers() {
0N/A forkSecondaryFinalizer(new Runnable() {
5874N/A private volatile boolean running;
0N/A public void run() {
5874N/A if (running)
5874N/A return;
5874N/A running = true;
0N/A for (;;) {
0N/A Finalizer f;
0N/A synchronized (lock) {
0N/A f = unfinalized;
0N/A if (f == null) break;
0N/A unfinalized = f.next;
0N/A }
0N/A f.runFinalizer();
0N/A }}});
0N/A }
0N/A
0N/A private static class FinalizerThread extends Thread {
5874N/A private volatile boolean running;
0N/A FinalizerThread(ThreadGroup g) {
0N/A super(g, "Finalizer");
0N/A }
0N/A public void run() {
5874N/A if (running)
5874N/A return;
5874N/A running = true;
0N/A for (;;) {
0N/A try {
0N/A Finalizer f = (Finalizer)queue.remove();
0N/A f.runFinalizer();
0N/A } catch (InterruptedException x) {
0N/A continue;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A static {
0N/A ThreadGroup tg = Thread.currentThread().getThreadGroup();
0N/A for (ThreadGroup tgn = tg;
0N/A tgn != null;
0N/A tg = tgn, tgn = tg.getParent());
0N/A Thread finalizer = new FinalizerThread(tg);
0N/A finalizer.setPriority(Thread.MAX_PRIORITY - 2);
0N/A finalizer.setDaemon(true);
0N/A finalizer.start();
0N/A }
0N/A
0N/A}