/* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.lang.ref; import sun.misc.ManagedLocalsThread; import sun.misc.SharedSecrets; import sun.misc.VM; import java.security.AccessController; import java.security.PrivilegedAction; /* Package-private; must be in same package as the Reference class */ class Finalizer extends FinalReference implements Cleaner { Finalizer(T finalizee) { super(finalizee, null); } /** Invoked by VM for objects overriding finalize() method */ static void register(Object finalizee) { Finalizer finalizer = new Finalizer<>(finalizee); finalizer.link(); } @Override public void clean() { T finalizee = delete(); if (finalizee == null) { return; } unlink(); try { if (!(finalizee instanceof java.lang.Enum)) { invokeFinalizee(finalizee); } } catch (Throwable x) { } finalizee = null; } /* Invoke the finalize() method on the finalizee (overridden by Finalizator) */ void invokeFinalizee(T finalizee) throws Throwable { SharedSecrets.getJavaLangAccess().invokeFinalize(finalizee); finalizee = null; } @Override public void clear() { T finalizee = delete(); if (finalizee == null) { return; } unlink(); /* Clear stack slot containing this variable, to decrease the chances of false retention with a conservative GC */ finalizee = null; } /* Create a privileged secondary finalizer thread in the system thread group for the given Runnable, and wait for it to complete. This method is used by both runFinalization and runFinalizersOnExit. The former method invokes all pending finalizers, while the latter invokes all uninvoked finalizers if on-exit finalization has been enabled. These two methods could have been implemented by offloading their work to the regular finalizer thread and waiting for that thread to finish. The advantage of creating a fresh thread, however, is that it insulates invokers of these methods from a stalled or deadlocked finalizer thread. */ 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 ManagedLocalsThread(tg, proc, "Secondary finalizer"); sft.start(); try { sft.join(); } catch (InterruptedException x) { Thread.currentThread().interrupt(); } return null; }}); } /* Called by Runtime.runFinalization() */ static void runFinalization() { if (!VM.isBooted()) { return; } forkSecondaryFinalizer(new Runnable() { private volatile boolean running; public void run() { // in case of recursive call to run() if (running) return; running = true; ReferenceHandling.runFinalization(); } }); } /* Invoked by java.lang.Shutdown */ static void runAllFinalizers() { if (!VM.isBooted()) { return; } forkSecondaryFinalizer(new Runnable() { private volatile boolean running; public void run() { // in case of recursive call to run() if (running) return; running = true; for (DLList ucList : uncleanedLists) for (Reference f = ucList.first(); f != null; f = ucList.succ(f)) { ((Cleaner)f).clean(); }}}); } /** * Atomically clear the Finalizer and return the finalizee. * * @return the finalizee or null */ private T delete() { T referent = getReferentVolatile(); return (referent != null) && casReferent(referent, null) ? referent : null; } // Methods and state that enable Finalizer to be an element of DLList /** A constructor used for special Reference instances used in DLList */ Finalizer(boolean setPrevToSelf, boolean setNextToSelf) { super(null, null); if (setPrevToSelf) prevDll = this; if (setNextToSelf) nextDll = this; } @SuppressWarnings("unused") // assigned through Unsafe private volatile Reference prevDll, nextDll; boolean isDeletedDll() { return getReferentVolatile() == null; } Reference getPrevDll() { return prevDll; } void lazySetPrevDll(Reference val) { UNSAFE.putOrderedObject(this, PREV_DLL, val); } boolean casPrevDll(Reference cmp, Reference val) { return UNSAFE.compareAndSwapObject(this, PREV_DLL, cmp, val); } Reference getNextDll() { return nextDll; } void lazySetNextDll(Reference val) { UNSAFE.putOrderedObject(this, NEXT_DLL, val); } boolean casNextDll(Reference cmp, Reference val) { return UNSAFE.compareAndSwapObject(this, NEXT_DLL, cmp, val); } // Unsafe machinery private static final sun.misc.Unsafe UNSAFE; private static final long PREV_DLL; private static final long NEXT_DLL; static { try { UNSAFE = sun.misc.Unsafe.getUnsafe(); Class fc = Finalizer.class; PREV_DLL = UNSAFE.objectFieldOffset(fc.getDeclaredField("prevDll")); NEXT_DLL = UNSAFE.objectFieldOffset(fc.getDeclaredField("nextDll")); } catch (Exception e) { throw new Error(e); } } }