Path: blob/master/src/java.base/share/classes/jdk/internal/ref/PhantomCleanable.java
41159 views
/*1* Copyright (c) 2015, 2016, 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.Cleaner;28import java.lang.ref.Reference;29import java.lang.ref.PhantomReference;30import java.util.Objects;3132/**33* PhantomCleanable subclasses efficiently encapsulate cleanup state and34* the cleaning action.35* Subclasses implement the abstract {@link #performCleanup()} method36* to provide the cleaning action.37* When constructed, the object reference and the {@link Cleaner.Cleanable Cleanable}38* are registered with the {@link Cleaner}.39* The Cleaner invokes {@link Cleaner.Cleanable#clean() clean} after the40* referent becomes phantom reachable.41*/42public abstract class PhantomCleanable<T> extends PhantomReference<T>43implements Cleaner.Cleanable {4445/**46* Links to previous and next in a doubly-linked list.47*/48PhantomCleanable<?> prev = this, next = this;4950/**51* The list of PhantomCleanable; synchronizes insert and remove.52*/53private final PhantomCleanable<?> list;5455/**56* Constructs new {@code PhantomCleanable} with57* {@code non-null referent} and {@code non-null cleaner}.58* The {@code cleaner} is not retained; it is only used to59* register the newly constructed {@link Cleaner.Cleanable Cleanable}.60*61* @param referent the referent to track62* @param cleaner the {@code Cleaner} to register with63*/64public PhantomCleanable(T referent, Cleaner cleaner) {65super(Objects.requireNonNull(referent), CleanerImpl.getCleanerImpl(cleaner).queue);66this.list = CleanerImpl.getCleanerImpl(cleaner).phantomCleanableList;67insert();6869// Ensure referent and cleaner remain accessible70Reference.reachabilityFence(referent);71Reference.reachabilityFence(cleaner);72}7374/**75* Construct a new root of the list; not inserted.76*/77PhantomCleanable() {78super(null, null);79this.list = this;80}8182/**83* Insert this PhantomCleanable after the list head.84*/85private void insert() {86synchronized (list) {87prev = list;88next = list.next;89next.prev = this;90list.next = this;91}92}9394/**95* Remove this PhantomCleanable from the list.96*97* @return true if Cleanable was removed or false if not because98* it had already been removed before99*/100private boolean remove() {101synchronized (list) {102if (next != this) {103next.prev = prev;104prev.next = next;105prev = this;106next = this;107return true;108}109return false;110}111}112113/**114* Returns true if the list's next reference refers to itself.115*116* @return true if the list is empty117*/118boolean isListEmpty() {119synchronized (list) {120return list == list.next;121}122}123124/**125* Unregister this PhantomCleanable and invoke {@link #performCleanup()},126* ensuring at-most-once semantics.127*/128@Override129public final void clean() {130if (remove()) {131super.clear();132performCleanup();133}134}135136/**137* Unregister this PhantomCleanable and clear the reference.138* Due to inherent concurrency, {@link #performCleanup()} may still be invoked.139*/140@Override141public void clear() {142if (remove()) {143super.clear();144}145}146147/**148* The {@code performCleanup} abstract method is overridden149* to implement the cleaning logic.150* The {@code performCleanup} method should not be called except151* by the {@link #clean} method which ensures at most once semantics.152*/153protected abstract void performCleanup();154155/**156* This method always throws {@link UnsupportedOperationException}.157* Enqueuing details of {@link Cleaner.Cleanable}158* are a private implementation detail.159*160* @throws UnsupportedOperationException always161*/162@SuppressWarnings("deprecation")163@Override164public final boolean isEnqueued() {165throw new UnsupportedOperationException("isEnqueued");166}167168/**169* This method always throws {@link UnsupportedOperationException}.170* Enqueuing details of {@link Cleaner.Cleanable}171* are a private implementation detail.172*173* @throws UnsupportedOperationException always174*/175@Override176public final boolean enqueue() {177throw new UnsupportedOperationException("enqueue");178}179}180181182