jdk/src/share/classes/java/lang/ref/Reference.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.misc.Cleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Abstract base class for reference objects.  This class defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * operations common to all reference objects.  Because reference objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * implemented in close cooperation with the garbage collector, this class may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * not be subclassed directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public abstract class Reference<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /* A Reference instance is in one of four possible internal states:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *     Active: Subject to special treatment by the garbage collector.  Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *     time after the collector detects that the reachability of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *     referent has changed to the appropriate state, it changes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *     instance's state to either Pending or Inactive, depending upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *     whether or not the instance was registered with a queue when it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *     created.  In the former case it also adds the instance to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *     pending-Reference list.  Newly-created instances are Active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *     Pending: An element of the pending-Reference list, waiting to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *     enqueued by the Reference-handler thread.  Unregistered instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *     are never in this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *     Enqueued: An element of the queue with which the instance was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *     registered when it was created.  When an instance is removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *     its ReferenceQueue, it is made Inactive.  Unregistered instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *     never in this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *     Inactive: Nothing more to do.  Once an instance becomes Inactive its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *     state will never change again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The state is encoded in the queue and next fields as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *     Active: queue = ReferenceQueue with which instance is registered, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *     ReferenceQueue.NULL if it was not registered with a queue; next =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *     null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *     Pending: queue = ReferenceQueue with which instance is registered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *     next = Following instance in queue, or this if at end of list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *     Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *     in queue, or this if at end of list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *     Inactive: queue = ReferenceQueue.NULL; next = this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * With this scheme the collector need only examine the next field in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * to determine whether a Reference instance requires special treatment: If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * the next field is null then the instance is active; if it is non-null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * then the collector should treat the instance normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * To ensure that concurrent collector can discover active Reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * objects without interfering with application threads that may apply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * the enqueue() method to those objects, collectors should link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * discovered objects through the discovered field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private T referent;         /* Treated specially by GC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    ReferenceQueue<? super T> queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    Reference next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    transient private Reference<T> discovered;  /* used by VM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* Object used to synchronize with the garbage collector.  The collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * must acquire this lock at the beginning of each collection cycle.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * therefore critical that any code holding this lock complete as quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * as possible, allocate no new objects, and avoid calling user code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static private class Lock { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static Lock lock = new Lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /* List of References waiting to be enqueued.  The collector adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * References to this list, while the Reference-handler thread removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * them.  This list is protected by the above lock object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static Reference pending = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /* High-priority thread to enqueue pending References
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static class ReferenceHandler extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        ReferenceHandler(ThreadGroup g, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            super(g, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                Reference r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    if (pending != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        r = pending;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        Reference rn = r.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        pending = (rn == r) ? null : rn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        r.next = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            lock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                // Fast path for cleaners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if (r instanceof Cleaner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    ((Cleaner)r).clean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                ReferenceQueue q = r.queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (q != ReferenceQueue.NULL) q.enqueue(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        for (ThreadGroup tgn = tg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
             tgn != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
             tg = tgn, tgn = tg.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Thread handler = new ReferenceHandler(tg, "Reference Handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        /* If there were a special system-only priority greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * MAX_PRIORITY, it would be used here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        handler.setPriority(Thread.MAX_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        handler.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        handler.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /* -- Referent accessor and setters -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns this reference object's referent.  If this reference object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * been cleared, either by the program or by the garbage collector, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * this method returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return   The object to which this reference refers, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *           <code>null</code> if this reference object has been cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public T get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return this.referent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Clears this reference object.  Invoking this method will not cause this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * object to be enqueued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p> This method is invoked only by Java code; when the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * clears references it does so directly, without invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.referent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /* -- Queue operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Tells whether or not this reference object has been enqueued, either by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the program or by the garbage collector.  If this reference object was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * not registered with a queue when it was created, then this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * always return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return   <code>true</code> if and only if this reference object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *           been enqueued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public boolean isEnqueued() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        /* In terms of the internal states, this predicate actually tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
           whether the instance is either Pending or Enqueued */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return (this.queue != ReferenceQueue.NULL) && (this.next != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Adds this reference object to the queue with which it is registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p> This method is invoked only by Java code; when the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * enqueues references it does so directly, without invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @return   <code>true</code> if this reference object was successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *           enqueued; <code>false</code> if it was already enqueued or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *           it was not registered with a queue when it was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public boolean enqueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return this.queue.enqueue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /* -- Constructors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    Reference(T referent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        this(referent, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    Reference(T referent, ReferenceQueue<? super T> queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this.referent = referent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}