rt/emul/compact/src/main/java/java/lang/ref/Reference.java
changeset 772 d382dacfd73f
parent 604 3fcc279c921b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/ref/Reference.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,185 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.ref;
    1.30 +
    1.31 +
    1.32 +/**
    1.33 + * Abstract base class for reference objects.  This class defines the
    1.34 + * operations common to all reference objects.  Because reference objects are
    1.35 + * implemented in close cooperation with the garbage collector, this class may
    1.36 + * not be subclassed directly.
    1.37 + *
    1.38 + * @author   Mark Reinhold
    1.39 + * @since    1.2
    1.40 + */
    1.41 +
    1.42 +public abstract class Reference<T> {
    1.43 +
    1.44 +    /* A Reference instance is in one of four possible internal states:
    1.45 +     *
    1.46 +     *     Active: Subject to special treatment by the garbage collector.  Some
    1.47 +     *     time after the collector detects that the reachability of the
    1.48 +     *     referent has changed to the appropriate state, it changes the
    1.49 +     *     instance's state to either Pending or Inactive, depending upon
    1.50 +     *     whether or not the instance was registered with a queue when it was
    1.51 +     *     created.  In the former case it also adds the instance to the
    1.52 +     *     pending-Reference list.  Newly-created instances are Active.
    1.53 +     *
    1.54 +     *     Pending: An element of the pending-Reference list, waiting to be
    1.55 +     *     enqueued by the Reference-handler thread.  Unregistered instances
    1.56 +     *     are never in this state.
    1.57 +     *
    1.58 +     *     Enqueued: An element of the queue with which the instance was
    1.59 +     *     registered when it was created.  When an instance is removed from
    1.60 +     *     its ReferenceQueue, it is made Inactive.  Unregistered instances are
    1.61 +     *     never in this state.
    1.62 +     *
    1.63 +     *     Inactive: Nothing more to do.  Once an instance becomes Inactive its
    1.64 +     *     state will never change again.
    1.65 +     *
    1.66 +     * The state is encoded in the queue and next fields as follows:
    1.67 +     *
    1.68 +     *     Active: queue = ReferenceQueue with which instance is registered, or
    1.69 +     *     ReferenceQueue.NULL if it was not registered with a queue; next =
    1.70 +     *     null.
    1.71 +     *
    1.72 +     *     Pending: queue = ReferenceQueue with which instance is registered;
    1.73 +     *     next = Following instance in queue, or this if at end of list.
    1.74 +     *
    1.75 +     *     Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
    1.76 +     *     in queue, or this if at end of list.
    1.77 +     *
    1.78 +     *     Inactive: queue = ReferenceQueue.NULL; next = this.
    1.79 +     *
    1.80 +     * With this scheme the collector need only examine the next field in order
    1.81 +     * to determine whether a Reference instance requires special treatment: If
    1.82 +     * the next field is null then the instance is active; if it is non-null,
    1.83 +     * then the collector should treat the instance normally.
    1.84 +     *
    1.85 +     * To ensure that concurrent collector can discover active Reference
    1.86 +     * objects without interfering with application threads that may apply
    1.87 +     * the enqueue() method to those objects, collectors should link
    1.88 +     * discovered objects through the discovered field.
    1.89 +     */
    1.90 +
    1.91 +    private T referent;         /* Treated specially by GC */
    1.92 +
    1.93 +    ReferenceQueue<? super T> queue;
    1.94 +
    1.95 +    Reference next;
    1.96 +    transient private Reference<T> discovered;  /* used by VM */
    1.97 +
    1.98 +
    1.99 +    /* Object used to synchronize with the garbage collector.  The collector
   1.100 +     * must acquire this lock at the beginning of each collection cycle.  It is
   1.101 +     * therefore critical that any code holding this lock complete as quickly
   1.102 +     * as possible, allocate no new objects, and avoid calling user code.
   1.103 +     */
   1.104 +    static private class Lock { };
   1.105 +    private static Lock lock = new Lock();
   1.106 +
   1.107 +
   1.108 +    /* List of References waiting to be enqueued.  The collector adds
   1.109 +     * References to this list, while the Reference-handler thread removes
   1.110 +     * them.  This list is protected by the above lock object.
   1.111 +     */
   1.112 +    private static Reference pending = null;
   1.113 +
   1.114 +
   1.115 +
   1.116 +    /* -- Referent accessor and setters -- */
   1.117 +
   1.118 +    /**
   1.119 +     * Returns this reference object's referent.  If this reference object has
   1.120 +     * been cleared, either by the program or by the garbage collector, then
   1.121 +     * this method returns <code>null</code>.
   1.122 +     *
   1.123 +     * @return   The object to which this reference refers, or
   1.124 +     *           <code>null</code> if this reference object has been cleared
   1.125 +     */
   1.126 +    public T get() {
   1.127 +        return this.referent;
   1.128 +    }
   1.129 +
   1.130 +    /**
   1.131 +     * Clears this reference object.  Invoking this method will not cause this
   1.132 +     * object to be enqueued.
   1.133 +     *
   1.134 +     * <p> This method is invoked only by Java code; when the garbage collector
   1.135 +     * clears references it does so directly, without invoking this method.
   1.136 +     */
   1.137 +    public void clear() {
   1.138 +        this.referent = null;
   1.139 +    }
   1.140 +
   1.141 +
   1.142 +    /* -- Queue operations -- */
   1.143 +
   1.144 +    /**
   1.145 +     * Tells whether or not this reference object has been enqueued, either by
   1.146 +     * the program or by the garbage collector.  If this reference object was
   1.147 +     * not registered with a queue when it was created, then this method will
   1.148 +     * always return <code>false</code>.
   1.149 +     *
   1.150 +     * @return   <code>true</code> if and only if this reference object has
   1.151 +     *           been enqueued
   1.152 +     */
   1.153 +    public boolean isEnqueued() {
   1.154 +        /* In terms of the internal states, this predicate actually tests
   1.155 +           whether the instance is either Pending or Enqueued */
   1.156 +        synchronized (this) {
   1.157 +            return (this.queue != ReferenceQueue.NULL) && (this.next != null);
   1.158 +        }
   1.159 +    }
   1.160 +
   1.161 +    /**
   1.162 +     * Adds this reference object to the queue with which it is registered,
   1.163 +     * if any.
   1.164 +     *
   1.165 +     * <p> This method is invoked only by Java code; when the garbage collector
   1.166 +     * enqueues references it does so directly, without invoking this method.
   1.167 +     *
   1.168 +     * @return   <code>true</code> if this reference object was successfully
   1.169 +     *           enqueued; <code>false</code> if it was already enqueued or if
   1.170 +     *           it was not registered with a queue when it was created
   1.171 +     */
   1.172 +    public boolean enqueue() {
   1.173 +        return this.queue.enqueue(this);
   1.174 +    }
   1.175 +
   1.176 +
   1.177 +    /* -- Constructors -- */
   1.178 +
   1.179 +    Reference(T referent) {
   1.180 +        this(referent, null);
   1.181 +    }
   1.182 +
   1.183 +    Reference(T referent, ReferenceQueue<? super T> queue) {
   1.184 +        this.referent = referent;
   1.185 +        this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
   1.186 +    }
   1.187 +
   1.188 +}