emul/compact/src/main/java/java/lang/ref/Reference.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Jan 2013 18:15:21 +0100
branchemul
changeset 604 3fcc279c921b
parent 601 5198affdb915
permissions -rw-r--r--
Making new clases (mostly java.io.Object*) compilable
     1 /*
     2  * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang.ref;
    27 
    28 
    29 /**
    30  * Abstract base class for reference objects.  This class defines the
    31  * operations common to all reference objects.  Because reference objects are
    32  * implemented in close cooperation with the garbage collector, this class may
    33  * not be subclassed directly.
    34  *
    35  * @author   Mark Reinhold
    36  * @since    1.2
    37  */
    38 
    39 public abstract class Reference<T> {
    40 
    41     /* A Reference instance is in one of four possible internal states:
    42      *
    43      *     Active: Subject to special treatment by the garbage collector.  Some
    44      *     time after the collector detects that the reachability of the
    45      *     referent has changed to the appropriate state, it changes the
    46      *     instance's state to either Pending or Inactive, depending upon
    47      *     whether or not the instance was registered with a queue when it was
    48      *     created.  In the former case it also adds the instance to the
    49      *     pending-Reference list.  Newly-created instances are Active.
    50      *
    51      *     Pending: An element of the pending-Reference list, waiting to be
    52      *     enqueued by the Reference-handler thread.  Unregistered instances
    53      *     are never in this state.
    54      *
    55      *     Enqueued: An element of the queue with which the instance was
    56      *     registered when it was created.  When an instance is removed from
    57      *     its ReferenceQueue, it is made Inactive.  Unregistered instances are
    58      *     never in this state.
    59      *
    60      *     Inactive: Nothing more to do.  Once an instance becomes Inactive its
    61      *     state will never change again.
    62      *
    63      * The state is encoded in the queue and next fields as follows:
    64      *
    65      *     Active: queue = ReferenceQueue with which instance is registered, or
    66      *     ReferenceQueue.NULL if it was not registered with a queue; next =
    67      *     null.
    68      *
    69      *     Pending: queue = ReferenceQueue with which instance is registered;
    70      *     next = Following instance in queue, or this if at end of list.
    71      *
    72      *     Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
    73      *     in queue, or this if at end of list.
    74      *
    75      *     Inactive: queue = ReferenceQueue.NULL; next = this.
    76      *
    77      * With this scheme the collector need only examine the next field in order
    78      * to determine whether a Reference instance requires special treatment: If
    79      * the next field is null then the instance is active; if it is non-null,
    80      * then the collector should treat the instance normally.
    81      *
    82      * To ensure that concurrent collector can discover active Reference
    83      * objects without interfering with application threads that may apply
    84      * the enqueue() method to those objects, collectors should link
    85      * discovered objects through the discovered field.
    86      */
    87 
    88     private T referent;         /* Treated specially by GC */
    89 
    90     ReferenceQueue<? super T> queue;
    91 
    92     Reference next;
    93     transient private Reference<T> discovered;  /* used by VM */
    94 
    95 
    96     /* Object used to synchronize with the garbage collector.  The collector
    97      * must acquire this lock at the beginning of each collection cycle.  It is
    98      * therefore critical that any code holding this lock complete as quickly
    99      * as possible, allocate no new objects, and avoid calling user code.
   100      */
   101     static private class Lock { };
   102     private static Lock lock = new Lock();
   103 
   104 
   105     /* List of References waiting to be enqueued.  The collector adds
   106      * References to this list, while the Reference-handler thread removes
   107      * them.  This list is protected by the above lock object.
   108      */
   109     private static Reference pending = null;
   110 
   111 
   112 
   113     /* -- Referent accessor and setters -- */
   114 
   115     /**
   116      * Returns this reference object's referent.  If this reference object has
   117      * been cleared, either by the program or by the garbage collector, then
   118      * this method returns <code>null</code>.
   119      *
   120      * @return   The object to which this reference refers, or
   121      *           <code>null</code> if this reference object has been cleared
   122      */
   123     public T get() {
   124         return this.referent;
   125     }
   126 
   127     /**
   128      * Clears this reference object.  Invoking this method will not cause this
   129      * object to be enqueued.
   130      *
   131      * <p> This method is invoked only by Java code; when the garbage collector
   132      * clears references it does so directly, without invoking this method.
   133      */
   134     public void clear() {
   135         this.referent = null;
   136     }
   137 
   138 
   139     /* -- Queue operations -- */
   140 
   141     /**
   142      * Tells whether or not this reference object has been enqueued, either by
   143      * the program or by the garbage collector.  If this reference object was
   144      * not registered with a queue when it was created, then this method will
   145      * always return <code>false</code>.
   146      *
   147      * @return   <code>true</code> if and only if this reference object has
   148      *           been enqueued
   149      */
   150     public boolean isEnqueued() {
   151         /* In terms of the internal states, this predicate actually tests
   152            whether the instance is either Pending or Enqueued */
   153         synchronized (this) {
   154             return (this.queue != ReferenceQueue.NULL) && (this.next != null);
   155         }
   156     }
   157 
   158     /**
   159      * Adds this reference object to the queue with which it is registered,
   160      * if any.
   161      *
   162      * <p> This method is invoked only by Java code; when the garbage collector
   163      * enqueues references it does so directly, without invoking this method.
   164      *
   165      * @return   <code>true</code> if this reference object was successfully
   166      *           enqueued; <code>false</code> if it was already enqueued or if
   167      *           it was not registered with a queue when it was created
   168      */
   169     public boolean enqueue() {
   170         return this.queue.enqueue(this);
   171     }
   172 
   173 
   174     /* -- Constructors -- */
   175 
   176     Reference(T referent) {
   177         this(referent, null);
   178     }
   179 
   180     Reference(T referent, ReferenceQueue<? super T> queue) {
   181         this.referent = referent;
   182         this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
   183     }
   184 
   185 }