ReferenceQueue.java 5.12 KB
Newer Older
Tom Tromey committed
1
/* java.lang.ref.ReferenceQueue
2
   Copyright (C) 1999, 2004, 2006 Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.lang.ref;

/**
 * This is the queue, where references can enqueue themselve on.  Each
 * reference may be registered to a queue at initialization time and
 * will be appended to the queue, when the enqueue method is called.
 *
 * The enqueue method may be automatically called by the garbage
 * collector if it detects, that the object is only reachable through
 * the Reference objects.
 *
 * @author Jochen Hoenicke
 * @see Reference#enqueue()
 */
53
public class ReferenceQueue<T>
Tom Tromey committed
54 55 56 57 58 59 60 61 62
{
  /**
   * This is a linked list of references.  If this is null, the list is
   * empty.  Otherwise this points to the first reference on the queue.
   * The first reference will point to the next reference via the 
   * <code>nextOnQueue</code> field.  The last reference will point to
   * itself (not to null, since <code>nextOnQueue</code> is used to 
   * determine if a reference is enqueued).
   */
63 64 65 66 67 68 69
  private Reference<? extends T> first;

  /**
   * This is the lock that protects our linked list and is used to signal
   * a thread waiting in remove().
   */
  private final Object lock = new Object();
Tom Tromey committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

  /**
   * Creates a new empty reference queue.
   */
  public ReferenceQueue()
  {
  }

  /**
   * Checks if there is a reference on the queue, returning it
   * immediately.  The reference will be dequeued.
   *
   * @return a reference on the queue, if there is one,
   * <code>null</code> otherwise.  
   */
85
  public Reference<? extends T> poll()
Tom Tromey committed
86 87 88 89 90 91 92
  { 
    return dequeue();
  }

  /**
   * This is called by reference to enqueue itself on this queue.  
   * @param ref the reference that should be enqueued.
93
   * @return true if successful, false if not.
Tom Tromey committed
94
   */
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  final boolean enqueue(Reference<? extends T> ref)
    {
    synchronized (lock)
      {
        if (ref.queue != this)
          return false;

        /* last reference will point to itself */
        ref.nextOnQueue = first == null ? ref : first;
        ref.queue = null;
        first = ref;
        /* this wakes only one remove thread. */
        lock.notify();
        return true;
      }
Tom Tromey committed
110 111 112 113 114 115
  }

  /**
   * Remove a reference from the queue, if there is one.
   * @return the first element of the queue, or null if there isn't any.
   */
116
  private Reference<? extends T> dequeue()
Tom Tromey committed
117
  {
118 119 120 121 122 123 124 125 126 127
    synchronized (lock)
      {
        if (first == null)
          return null;
  
        Reference<? extends T> result = first;
        first = (first == first.nextOnQueue) ? null : first.nextOnQueue;
        result.nextOnQueue = null;
        return result;
      }
Tom Tromey committed
128 129 130 131 132 133 134 135 136 137 138
  }

  /**
   * Removes a reference from the queue, blocking for <code>timeout</code>
   * until a reference is enqueued.
   * @param timeout the timeout period in milliseconds, <code>0</code> means
   * wait forever.
   * @return the reference removed from the queue, or 
   * <code>null</code> if timeout period expired.  
   * @exception InterruptedException if the wait was interrupted.
   */
139
  public Reference<? extends T> remove(long timeout)
Tom Tromey committed
140 141
    throws InterruptedException
  {
142
    synchronized (lock)
Tom Tromey committed
143
      {
144 145
        if (first == null)
          lock.wait(timeout);
Tom Tromey committed
146 147 148 149 150 151 152 153 154 155 156 157 158
      }

    return dequeue();
  }
    

  /**
   * Removes a reference from the queue, blocking until a reference is
   * enqueued.
   *
   * @return the reference removed from the queue.  
   * @exception InterruptedException if the wait was interrupted.
   */
159
  public Reference<? extends T> remove()
Tom Tromey committed
160 161 162 163 164
    throws InterruptedException
  {
    return remove(0L);
  }
}