1 /* 2 * Copyright (c) 1997, 2015, 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 * Reference queues, to which registered reference objects are appended by the 30 * garbage collector after the appropriate reachability changes are detected. 31 * 32 * @author Mark Reinhold 33 * @since 1.2 34 */ 35 36 public class ReferenceQueue<T> { 37 38 /** 39 * Constructs a new reference-object queue. 40 */ 41 public ReferenceQueue() { } 42 43 private static class Null<S> extends ReferenceQueue<S> { 44 boolean enqueue(Reference<? extends S> r) { 45 return false; 46 } 47 } 48 49 static ReferenceQueue<Object> NULL = new Null<>(); 50 static ReferenceQueue<Object> ENQUEUED = new Null<>(); 51 52 static private class Lock { }; 53 private Lock lock = new Lock(); 54 private volatile Reference<? extends T> head = null; 55 private long queueLength = 0; 56 57 boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */ 58 synchronized (lock) { 59 // Check that since getting the lock this reference hasn't already been 60 // enqueued (and even then removed) 61 ReferenceQueue<?> queue = r.queue; 62 if ((queue == NULL) || (queue == ENQUEUED)) { 63 return false; 64 } 65 assert queue == this; 66 r.next = (head == null) ? r : head; 67 head = r; 68 queueLength++; 69 // Assignment of queue to special ENQUEUED queue must be 70 // after the reference is added to the queue's list. 71 // Otherwise, there is a race condition where the 72 // reference appears to be enqueued (isEnqueue returns 73 // true, another enqueue returns false), but polling the 74 // queue could find it still empty. The q.head and 75 // r.queue fields are volatile, ensuring that order. 76 r.queue = ENQUEUED; 77 if (r instanceof FinalReference) { 78 sun.misc.VM.addFinalRefCount(1); 79 } 80 lock.notifyAll(); 81 return true; 82 } 83 } 84 85 @SuppressWarnings("unchecked") 86 private Reference<? extends T> reallyPoll() { /* Must hold lock */ 87 Reference<? extends T> r = head; 88 if (r != null) { 89 head = (r.next == r) ? 90 null : 91 r.next; // Unchecked due to the next field having a raw type in Reference 92 r.queue = NULL; 93 r.next = r; 94 queueLength--; 95 if (r instanceof FinalReference) { 96 sun.misc.VM.addFinalRefCount(-1); 97 } 98 return r; 99 } 100 return null; 101 } 102 103 /** 104 * Polls this queue to see if a reference object is available. If one is 105 * available without further delay then it is removed from the queue and 106 * returned. Otherwise this method immediately returns <tt>null</tt>. 107 * 108 * @return A reference object, if one was immediately available, 109 * otherwise <code>null</code> 110 */ 111 public Reference<? extends T> poll() { 112 if (head == null) 113 return null; 114 synchronized (lock) { 115 return reallyPoll(); 116 } 117 } 118 119 /** 120 * Removes the next reference object in this queue, blocking until either 121 * one becomes available or the given timeout period expires. 122 * 123 * <p> This method does not offer real-time guarantees: It schedules the 124 * timeout as if by invoking the {@link Object#wait(long)} method. 125 * 126 * @param timeout If positive, block for up to <code>timeout</code> 127 * milliseconds while waiting for a reference to be 128 * added to this queue. If zero, block indefinitely. 129 * 130 * @return A reference object, if one was available within the specified 131 * timeout period, otherwise <code>null</code> 132 * 133 * @throws IllegalArgumentException 134 * If the value of the timeout argument is negative 135 * 136 * @throws InterruptedException 137 * If the timeout wait is interrupted 138 */ 139 public Reference<? extends T> remove(long timeout) 140 throws IllegalArgumentException, InterruptedException 141 { 142 if (timeout < 0) { 143 throw new IllegalArgumentException("Negative timeout value"); 144 } 145 synchronized (lock) { 146 Reference<? extends T> r = reallyPoll(); 147 if (r != null) return r; 148 long start = (timeout == 0) ? 0 : System.nanoTime(); 149 for (;;) { 150 lock.wait(timeout); 151 r = reallyPoll(); 152 if (r != null) return r; 153 if (timeout != 0) { 154 long end = System.nanoTime(); 155 timeout -= (end - start) / 1000_000; 156 if (timeout <= 0) return null; 157 start = end; 158 } 159 } 160 } 161 } 162 163 /** 164 * Removes the next reference object in this queue, blocking until one 165 * becomes available. 166 * 167 * @return A reference object, blocking until one becomes available 168 * @throws InterruptedException If the wait is interrupted 169 */ 170 public Reference<? extends T> remove() throws InterruptedException { 171 return remove(0); 172 } 173 174 }