1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.lang.ref; 28 29 import dalvik.annotation.optimization.FastNative; 30 31 32 /** 33 * Abstract base class for reference objects. This class defines the 34 * operations common to all reference objects. Because reference objects are 35 * implemented in close cooperation with the garbage collector, this class may 36 * not be subclassed directly. 37 * 38 * @author Mark Reinhold 39 * @since 1.2 40 */ 41 42 public abstract class Reference<T> { 43 /** 44 * Forces JNI path. 45 * If GC is not in progress (ie: not going through slow path), the referent 46 * can be quickly returned through intrinsic without passing through JNI. 47 * This flag forces the JNI path so that it can be tested and benchmarked. 48 */ 49 private static boolean disableIntrinsic = false; 50 51 /** 52 * Slow path flag for the reference processor. 53 * Used by the reference processor to determine whether or not the referent 54 * can be immediately returned. Because the referent might get swept during 55 * GC, the slow path, which passes through JNI, must be taken. 56 */ 57 private static boolean slowPathEnabled = false; 58 59 volatile T referent; /* Treated specially by GC */ 60 final ReferenceQueue<? super T> queue; 61 62 /* 63 * This field forms a singly-linked list of reference objects that have 64 * been enqueued. The queueNext field is non-null if and only if this 65 * reference has been enqueued. After this reference has been enqueued and 66 * before it has been removed from its queue, the queueNext field points 67 * to the next reference on the queue. The last reference on a queue 68 * points to itself. Once this reference has been removed from the 69 * reference queue, the queueNext field points to the 70 * ReferenceQueue.sQueueNextUnenqueued sentinel reference object for the 71 * rest of this reference's lifetime. 72 * <p> 73 * Access to the queueNext field is guarded by synchronization on a lock 74 * internal to 'queue'. 75 */ 76 Reference queueNext; 77 78 /** 79 * The pendingNext field is initially set by the GC. After the GC forms a 80 * complete circularly linked list, the list is handed off to the 81 * ReferenceQueueDaemon using the ReferenceQueue.class lock. The 82 * ReferenceQueueDaemon can then read the pendingNext fields without 83 * additional synchronization. 84 */ 85 Reference<?> pendingNext; 86 87 /* -- Referent accessor and setters -- */ 88 89 /** 90 * Returns this reference object's referent. If this reference object has 91 * been cleared, either by the program or by the garbage collector, then 92 * this method returns <code>null</code>. 93 * 94 * @return The object to which this reference refers, or 95 * <code>null</code> if this reference object has been cleared 96 */ get()97 public T get() { 98 return getReferent(); 99 } 100 101 @FastNative getReferent()102 private final native T getReferent(); 103 104 /** 105 * Clears this reference object. Invoking this method will not cause this 106 * object to be enqueued. 107 * 108 * <p> This method is invoked only by Java code; when the garbage collector 109 * clears references it does so directly, without invoking this method. 110 */ clear()111 public void clear() { 112 clearReferent(); 113 } 114 115 // Direct access to the referent is prohibited, clearReferent blocks and set 116 // the referent to null when it is safe to do so. 117 @FastNative clearReferent()118 native void clearReferent(); 119 120 /* -- Queue operations -- */ 121 122 /** 123 * Tells whether or not this reference object has been enqueued, either by 124 * the program or by the garbage collector. If this reference object was 125 * not registered with a queue when it was created, then this method will 126 * always return <code>false</code>. 127 * 128 * @return <code>true</code> if and only if this reference object has 129 * been enqueued 130 */ isEnqueued()131 public boolean isEnqueued() { 132 // Contrary to what the documentation says, this method returns false 133 // after this reference object has been removed from its queue 134 // (b/26647823). ReferenceQueue.isEnqueued preserves this historically 135 // incorrect behavior. 136 return queue != null && queue.isEnqueued(this); 137 } 138 139 /** 140 * Adds this reference object to the queue with which it is registered, 141 * if any. 142 * 143 * <p> This method is invoked only by Java code; when the garbage collector 144 * enqueues references it does so directly, without invoking this method. 145 * 146 * @return <code>true</code> if this reference object was successfully 147 * enqueued; <code>false</code> if it was already enqueued or if 148 * it was not registered with a queue when it was created 149 */ enqueue()150 public boolean enqueue() { 151 return queue != null && queue.enqueue(this); 152 } 153 154 155 /* -- Constructors -- */ 156 Reference(T referent)157 Reference(T referent) { 158 this(referent, null); 159 } 160 Reference(T referent, ReferenceQueue<? super T> queue)161 Reference(T referent, ReferenceQueue<? super T> queue) { 162 this.referent = referent; 163 this.queue = queue; 164 } 165 } 166