1 /*
   2  * Copyright 1997-2005 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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 extends ReferenceQueue {
  44         boolean enqueue(Reference r) {
  45             return false;
  46         }
  47     }
  48 
  49     static ReferenceQueue NULL = new Null();
  50     static ReferenceQueue ENQUEUED = new Null();
  51 
  52     static private class Lock { };
  53     private Lock lock = new Lock();
  54     private 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 (r) {
  59             if (r.queue == ENQUEUED) return false;
  60             synchronized (lock) {
  61                 r.queue = ENQUEUED;
  62                 r.next = (head == null) ? r : head;
  63                 head = r;
  64                 queueLength++;
  65                 if (r instanceof FinalReference) {
  66                     sun.misc.VM.addFinalRefCount(1);
  67                 }
  68                 lock.notifyAll();
  69                 return true;
  70             }
  71         }
  72     }
  73 
  74     private Reference<? extends T> reallyPoll() {       /* Must hold lock */
  75         if (head != null) {
  76             Reference<? extends T> r = head;
  77             head = (r.next == r) ? null : r.next;
  78             r.queue = NULL;
  79             r.next = r;
  80             queueLength--;
  81             if (r instanceof FinalReference) {
  82                 sun.misc.VM.addFinalRefCount(-1);
  83             }
  84             return r;
  85         }
  86         return null;
  87     }
  88 
  89     /**
  90      * Polls this queue to see if a reference object is available.  If one is
  91      * available without further delay then it is removed from the queue and
  92      * returned.  Otherwise this method immediately returns <tt>null</tt>.
  93      *
  94      * @return  A reference object, if one was immediately available,
  95      *          otherwise <code>null</code>
  96      */
  97     public Reference<? extends T> poll() {
  98         synchronized (lock) {
  99             return reallyPoll();
 100         }
 101     }
 102 
 103     /**
 104      * Removes the next reference object in this queue, blocking until either
 105      * one becomes available or the given timeout period expires.
 106      *
 107      * <p> This method does not offer real-time guarantees: It schedules the
 108      * timeout as if by invoking the {@link Object#wait(long)} method.
 109      *
 110      * @param  timeout  If positive, block for up to <code>timeout</code>
 111      *                  milliseconds while waiting for a reference to be
 112      *                  added to this queue.  If zero, block indefinitely.
 113      *
 114      * @return  A reference object, if one was available within the specified
 115      *          timeout period, otherwise <code>null</code>
 116      *
 117      * @throws  IllegalArgumentException
 118      *          If the value of the timeout argument is negative
 119      *
 120      * @throws  InterruptedException
 121      *          If the timeout wait is interrupted
 122      */
 123     public Reference<? extends T> remove(long timeout)
 124         throws IllegalArgumentException, InterruptedException
 125     {
 126         if (timeout < 0) {
 127             throw new IllegalArgumentException("Negative timeout value");
 128         }
 129         synchronized (lock) {
 130             Reference<? extends T> r = reallyPoll();
 131             if (r != null) return r;
 132             for (;;) {
 133                 lock.wait(timeout);
 134                 r = reallyPoll();
 135                 if (r != null) return r;
 136                 if (timeout != 0) return null;
 137             }
 138         }
 139     }
 140 
 141     /**
 142      * Removes the next reference object in this queue, blocking until one
 143      * becomes available.
 144      *
 145      * @return A reference object, blocking until one becomes available
 146      * @throws  InterruptedException  If the wait is interrupted
 147      */
 148     public Reference<? extends T> remove() throws InterruptedException {
 149         return remove(0);
 150     }
 151 
 152 }