Path: blob/master/src/java.base/share/classes/jdk/internal/ref/Cleaner.java
41159 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.ref;2627import java.lang.ref.*;28import java.security.AccessController;29import java.security.PrivilegedAction;303132/**33* General-purpose phantom-reference-based cleaners.34*35* <p> Cleaners are a lightweight and more robust alternative to finalization.36* They are lightweight because they are not created by the VM and thus do not37* require a JNI upcall to be created, and because their cleanup code is38* invoked directly by the reference-handler thread rather than by the39* finalizer thread. They are more robust because they use phantom references,40* the weakest type of reference object, thereby avoiding the nasty ordering41* problems inherent to finalization.42*43* <p> A cleaner tracks a referent object and encapsulates a thunk of arbitrary44* cleanup code. Some time after the GC detects that a cleaner's referent has45* become phantom-reachable, the reference-handler thread will run the cleaner.46* Cleaners may also be invoked directly; they are thread safe and ensure that47* they run their thunks at most once.48*49* <p> Cleaners are not a replacement for finalization. They should be used50* only when the cleanup code is extremely simple and straightforward.51* Nontrivial cleaners are inadvisable since they risk blocking the52* reference-handler thread and delaying further cleanup and finalization.53*54*55* @author Mark Reinhold56*/5758public class Cleaner59extends PhantomReference<Object>60{6162// Dummy reference queue, needed because the PhantomReference constructor63// insists that we pass a queue. Nothing will ever be placed on this queue64// since the reference handler invokes cleaners explicitly.65//66private static final ReferenceQueue<Object> dummyQueue = new ReferenceQueue<>();6768// Doubly-linked list of live cleaners, which prevents the cleaners69// themselves from being GC'd before their referents70//71private static Cleaner first = null;7273private Cleaner74next = null,75prev = null;7677private static synchronized Cleaner add(Cleaner cl) {78if (first != null) {79cl.next = first;80first.prev = cl;81}82first = cl;83return cl;84}8586private static synchronized boolean remove(Cleaner cl) {8788// If already removed, do nothing89if (cl.next == cl)90return false;9192// Update list93if (first == cl) {94if (cl.next != null)95first = cl.next;96else97first = cl.prev;98}99if (cl.next != null)100cl.next.prev = cl.prev;101if (cl.prev != null)102cl.prev.next = cl.next;103104// Indicate removal by pointing the cleaner to itself105cl.next = cl;106cl.prev = cl;107return true;108109}110111private final Runnable thunk;112113private Cleaner(Object referent, Runnable thunk) {114super(referent, dummyQueue);115this.thunk = thunk;116}117118/**119* Creates a new cleaner.120*121* @param ob the referent object to be cleaned122* @param thunk123* The cleanup code to be run when the cleaner is invoked. The124* cleanup code is run directly from the reference-handler thread,125* so it should be as simple and straightforward as possible.126*127* @return The new cleaner128*/129public static Cleaner create(Object ob, Runnable thunk) {130if (thunk == null)131return null;132return add(new Cleaner(ob, thunk));133}134135/**136* Runs this cleaner, if it has not been run before.137*/138@SuppressWarnings("removal")139public void clean() {140if (!remove(this))141return;142try {143thunk.run();144} catch (final Throwable x) {145AccessController.doPrivileged(new PrivilegedAction<>() {146public Void run() {147if (System.err != null)148new Error("Cleaner terminated abnormally", x)149.printStackTrace();150System.exit(1);151return null;152}});153}154}155}156157158