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 import sun.misc.Cleaner; 29 30 31 /** 32 * Abstract base class for reference objects. This class defines the 33 * operations common to all reference objects. Because reference objects are 34 * implemented in close cooperation with the garbage collector, this class may 35 * not be subclassed directly. 36 * 37 * @author Mark Reinhold 38 * @since 1.2 39 */ 40 41 public abstract class Reference<T> { 42 43 /* A Reference instance is in one of four possible internal states: 44 * 45 * Active: Subject to special treatment by the garbage collector. Some 46 * time after the collector detects that the reachability of the 47 * referent has changed to the appropriate state, it changes the 48 * instance's state to either Pending or Inactive, depending upon 49 * whether or not the instance was registered with a queue when it was 50 * created. In the former case it also adds the instance to the 51 * pending-Reference list. Newly-created instances are Active. 52 * 53 * Pending: An element of the pending-Reference list, waiting to be 54 * enqueued by the Reference-handler thread. Unregistered instances 55 * are never in this state. 56 * 57 * Enqueued: An element of the queue with which the instance was 58 * registered when it was created. When an instance is removed from 59 * its ReferenceQueue, it is made Inactive. Unregistered instances are 60 * never in this state. 61 * 62 * Inactive: Nothing more to do. Once an instance becomes Inactive its 63 * state will never change again. 64 * 65 * The state is encoded in the queue and next fields as follows: 66 * 67 * Active: queue = ReferenceQueue with which instance is registered, or 68 * ReferenceQueue.NULL if it was not registered with a queue; next = 69 * null. 70 * 71 * Pending: queue = ReferenceQueue with which instance is registered; 72 * next = Following instance in queue, or this if at end of list. 73 * 74 * Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance 75 * in queue, or this if at end of list. 76 * 77 * Inactive: queue = ReferenceQueue.NULL; next = this. 78 * 79 * With this scheme the collector need only examine the next field in order 80 * to determine whether a Reference instance requires special treatment: If 81 * the next field is null then the instance is active; if it is non-null, 82 * then the collector should treat the instance normally. 83 * 84 * To ensure that concurrent collector can discover active Reference 85 * objects without interfering with application threads that may apply 86 * the enqueue() method to those objects, collectors should link 87 * discovered objects through the discovered field. 88 */ 89 90 private T referent; /* Treated specially by GC */ 91 92 ReferenceQueue<? super T> queue; 93 94 Reference next; 95 transient private Reference<T> discovered; /* used by VM */ 96 97 98 /* Object used to synchronize with the garbage collector. The collector 99 * must acquire this lock at the beginning of each collection cycle. It is 100 * therefore critical that any code holding this lock complete as quickly 101 * as possible, allocate no new objects, and avoid calling user code. 102 */ 103 static private class Lock { }; 104 private static Lock lock = new Lock(); 105 106 107 /* List of References waiting to be enqueued. The collector adds 108 * References to this list, while the Reference-handler thread removes 109 * them. This list is protected by the above lock object. 110 */ 111 private static Reference pending = null; 112 113 /* High-priority thread to enqueue pending References 114 */ 115 private static class ReferenceHandler extends Thread { 116 117 ReferenceHandler(ThreadGroup g, String name) { 118 super(g, name); 119 } 120 121 public void run() { 122 for (;;) { 123 124 Reference r; 125 synchronized (lock) { 126 if (pending != null) { 127 r = pending; 128 Reference rn = r.next; 129 pending = (rn == r) ? null : rn; 130 r.next = r; 131 } else { 132 try { 133 lock.wait(); 134 } catch (InterruptedException x) { } 135 continue; 136 } 137 } 138 139 // Fast path for cleaners 140 if (r instanceof Cleaner) { 141 ((Cleaner)r).clean(); 142 continue; 143 } 144 145 ReferenceQueue q = r.queue; 146 if (q != ReferenceQueue.NULL) q.enqueue(r); 147 } 148 } 149 } 150 151 static { 152 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 153 for (ThreadGroup tgn = tg; 154 tgn != null; 155 tg = tgn, tgn = tg.getParent()); 156 Thread handler = new ReferenceHandler(tg, "Reference Handler"); 157 /* If there were a special system-only priority greater than 158 * MAX_PRIORITY, it would be used here 159 */ 160 handler.setPriority(Thread.MAX_PRIORITY); 161 handler.setDaemon(true); 162 handler.start(); 163 } 164 165 166 /* -- Referent accessor and setters -- */ 167 168 /** 169 * Returns this reference object's referent. If this reference object has 170 * been cleared, either by the program or by the garbage collector, then 171 * this method returns <code>null</code>. 172 * 173 * @return The object to which this reference refers, or 174 * <code>null</code> if this reference object has been cleared 175 */ 176 public T get() { 177 return this.referent; 178 } 179 180 /** 181 * Clears this reference object. Invoking this method will not cause this 182 * object to be enqueued. 183 * 184 * <p> This method is invoked only by Java code; when the garbage collector 185 * clears references it does so directly, without invoking this method. 186 */ 187 public void clear() { 188 this.referent = null; 189 } 190 191 192 /* -- Queue operations -- */ 193 194 /** 195 * Tells whether or not this reference object has been enqueued, either by 196 * the program or by the garbage collector. If this reference object was 197 * not registered with a queue when it was created, then this method will 198 * always return <code>false</code>. 199 * 200 * @return <code>true</code> if and only if this reference object has 201 * been enqueued 202 */ 203 public boolean isEnqueued() { 204 /* In terms of the internal states, this predicate actually tests 205 whether the instance is either Pending or Enqueued */ 206 synchronized (this) { 207 return (this.queue != ReferenceQueue.NULL) && (this.next != null); 208 } 209 } 210 211 /** 212 * Adds this reference object to the queue with which it is registered, 213 * if any. 214 * 215 * <p> This method is invoked only by Java code; when the garbage collector 216 * enqueues references it does so directly, without invoking this method. 217 * 218 * @return <code>true</code> if this reference object was successfully 219 * enqueued; <code>false</code> if it was already enqueued or if 220 * it was not registered with a queue when it was created 221 */ 222 public boolean enqueue() { 223 return this.queue.enqueue(this); 224 } 225 226 227 /* -- Constructors -- */ 228 229 Reference(T referent) { 230 this(referent, null); 231 } 232 233 Reference(T referent, ReferenceQueue<? super T> queue) { 234 this.referent = referent; 235 this.queue = (queue == null) ? ReferenceQueue.NULL : queue; 236 } 237 238 }