jaroslav@601: /* jaroslav@601: * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@601: * jaroslav@601: * This code is free software; you can redistribute it and/or modify it jaroslav@601: * under the terms of the GNU General Public License version 2 only, as jaroslav@601: * published by the Free Software Foundation. Oracle designates this jaroslav@601: * particular file as subject to the "Classpath" exception as provided jaroslav@601: * by Oracle in the LICENSE file that accompanied this code. jaroslav@601: * jaroslav@601: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@601: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@601: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@601: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@601: * accompanied this code). jaroslav@601: * jaroslav@601: * You should have received a copy of the GNU General Public License version jaroslav@601: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@601: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@601: * jaroslav@601: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@601: * or visit www.oracle.com if you need additional information or have any jaroslav@601: * questions. jaroslav@601: */ jaroslav@601: jaroslav@601: package java.lang.ref; jaroslav@601: jaroslav@601: jaroslav@601: /** jaroslav@601: * Abstract base class for reference objects. This class defines the jaroslav@601: * operations common to all reference objects. Because reference objects are jaroslav@601: * implemented in close cooperation with the garbage collector, this class may jaroslav@601: * not be subclassed directly. jaroslav@601: * jaroslav@601: * @author Mark Reinhold jaroslav@601: * @since 1.2 jaroslav@601: */ jaroslav@601: jaroslav@601: public abstract class Reference { jaroslav@601: jaroslav@601: /* A Reference instance is in one of four possible internal states: jaroslav@601: * jaroslav@601: * Active: Subject to special treatment by the garbage collector. Some jaroslav@601: * time after the collector detects that the reachability of the jaroslav@601: * referent has changed to the appropriate state, it changes the jaroslav@601: * instance's state to either Pending or Inactive, depending upon jaroslav@601: * whether or not the instance was registered with a queue when it was jaroslav@601: * created. In the former case it also adds the instance to the jaroslav@601: * pending-Reference list. Newly-created instances are Active. jaroslav@601: * jaroslav@601: * Pending: An element of the pending-Reference list, waiting to be jaroslav@601: * enqueued by the Reference-handler thread. Unregistered instances jaroslav@601: * are never in this state. jaroslav@601: * jaroslav@601: * Enqueued: An element of the queue with which the instance was jaroslav@601: * registered when it was created. When an instance is removed from jaroslav@601: * its ReferenceQueue, it is made Inactive. Unregistered instances are jaroslav@601: * never in this state. jaroslav@601: * jaroslav@601: * Inactive: Nothing more to do. Once an instance becomes Inactive its jaroslav@601: * state will never change again. jaroslav@601: * jaroslav@601: * The state is encoded in the queue and next fields as follows: jaroslav@601: * jaroslav@601: * Active: queue = ReferenceQueue with which instance is registered, or jaroslav@601: * ReferenceQueue.NULL if it was not registered with a queue; next = jaroslav@601: * null. jaroslav@601: * jaroslav@601: * Pending: queue = ReferenceQueue with which instance is registered; jaroslav@601: * next = Following instance in queue, or this if at end of list. jaroslav@601: * jaroslav@601: * Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance jaroslav@601: * in queue, or this if at end of list. jaroslav@601: * jaroslav@601: * Inactive: queue = ReferenceQueue.NULL; next = this. jaroslav@601: * jaroslav@601: * With this scheme the collector need only examine the next field in order jaroslav@601: * to determine whether a Reference instance requires special treatment: If jaroslav@601: * the next field is null then the instance is active; if it is non-null, jaroslav@601: * then the collector should treat the instance normally. jaroslav@601: * jaroslav@601: * To ensure that concurrent collector can discover active Reference jaroslav@601: * objects without interfering with application threads that may apply jaroslav@601: * the enqueue() method to those objects, collectors should link jaroslav@601: * discovered objects through the discovered field. jaroslav@601: */ jaroslav@601: jaroslav@601: private T referent; /* Treated specially by GC */ jaroslav@601: jaroslav@601: ReferenceQueue queue; jaroslav@601: jaroslav@601: Reference next; jaroslav@601: transient private Reference discovered; /* used by VM */ jaroslav@601: jaroslav@601: jaroslav@601: /* Object used to synchronize with the garbage collector. The collector jaroslav@601: * must acquire this lock at the beginning of each collection cycle. It is jaroslav@601: * therefore critical that any code holding this lock complete as quickly jaroslav@601: * as possible, allocate no new objects, and avoid calling user code. jaroslav@601: */ jaroslav@601: static private class Lock { }; jaroslav@601: private static Lock lock = new Lock(); jaroslav@601: jaroslav@601: jaroslav@601: /* List of References waiting to be enqueued. The collector adds jaroslav@601: * References to this list, while the Reference-handler thread removes jaroslav@601: * them. This list is protected by the above lock object. jaroslav@601: */ jaroslav@601: private static Reference pending = null; jaroslav@601: jaroslav@601: jaroslav@601: jaroslav@601: /* -- Referent accessor and setters -- */ jaroslav@601: jaroslav@601: /** jaroslav@601: * Returns this reference object's referent. If this reference object has jaroslav@601: * been cleared, either by the program or by the garbage collector, then jaroslav@601: * this method returns null. jaroslav@601: * jaroslav@601: * @return The object to which this reference refers, or jaroslav@601: * null if this reference object has been cleared jaroslav@601: */ jaroslav@601: public T get() { jaroslav@601: return this.referent; jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Clears this reference object. Invoking this method will not cause this jaroslav@601: * object to be enqueued. jaroslav@601: * jaroslav@601: *

This method is invoked only by Java code; when the garbage collector jaroslav@601: * clears references it does so directly, without invoking this method. jaroslav@601: */ jaroslav@601: public void clear() { jaroslav@601: this.referent = null; jaroslav@601: } jaroslav@601: jaroslav@601: jaroslav@601: /* -- Queue operations -- */ jaroslav@601: jaroslav@601: /** jaroslav@601: * Tells whether or not this reference object has been enqueued, either by jaroslav@601: * the program or by the garbage collector. If this reference object was jaroslav@601: * not registered with a queue when it was created, then this method will jaroslav@601: * always return false. jaroslav@601: * jaroslav@601: * @return true if and only if this reference object has jaroslav@601: * been enqueued jaroslav@601: */ jaroslav@601: public boolean isEnqueued() { jaroslav@601: /* In terms of the internal states, this predicate actually tests jaroslav@601: whether the instance is either Pending or Enqueued */ jaroslav@601: synchronized (this) { jaroslav@601: return (this.queue != ReferenceQueue.NULL) && (this.next != null); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Adds this reference object to the queue with which it is registered, jaroslav@601: * if any. jaroslav@601: * jaroslav@601: *

This method is invoked only by Java code; when the garbage collector jaroslav@601: * enqueues references it does so directly, without invoking this method. jaroslav@601: * jaroslav@601: * @return true if this reference object was successfully jaroslav@601: * enqueued; false if it was already enqueued or if jaroslav@601: * it was not registered with a queue when it was created jaroslav@601: */ jaroslav@601: public boolean enqueue() { jaroslav@601: return this.queue.enqueue(this); jaroslav@601: } jaroslav@601: jaroslav@601: jaroslav@601: /* -- Constructors -- */ jaroslav@601: jaroslav@601: Reference(T referent) { jaroslav@601: this(referent, null); jaroslav@601: } jaroslav@601: jaroslav@601: Reference(T referent, ReferenceQueue queue) { jaroslav@601: this.referent = referent; jaroslav@601: this.queue = (queue == null) ? ReferenceQueue.NULL : queue; jaroslav@601: } jaroslav@601: jaroslav@601: }