| 1 | /* java.lang.ref.ReferenceQueue
|
|---|
| 2 | Copyright (C) 1999 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.lang.ref;
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * This is the queue, where references can enqueue themselve on. Each
|
|---|
| 43 | * reference may be registered to a queue at initialization time and
|
|---|
| 44 | * will be appended to the queue, when the enqueue method is called.
|
|---|
| 45 | *
|
|---|
| 46 | * The enqueue method may be automatically called by the garbage
|
|---|
| 47 | * collector if it detects, that the object is only reachable through
|
|---|
| 48 | * the Reference objects.
|
|---|
| 49 | *
|
|---|
| 50 | * @author Jochen Hoenicke
|
|---|
| 51 | * @see Reference#enqueue()
|
|---|
| 52 | */
|
|---|
| 53 | public class ReferenceQueue
|
|---|
| 54 | {
|
|---|
| 55 | /**
|
|---|
| 56 | * This is a linked list of references. If this is null, the list is
|
|---|
| 57 | * empty. Otherwise this points to the first reference on the queue.
|
|---|
| 58 | * The first reference will point to the next reference via the
|
|---|
| 59 | * <code>nextOnQueue</code> field. The last reference will point to
|
|---|
| 60 | * itself (not to null, since <code>nextOnQueue</code> is used to
|
|---|
| 61 | * determine if a reference is enqueued).
|
|---|
| 62 | */
|
|---|
| 63 | private Reference first;
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Creates a new empty reference queue.
|
|---|
| 67 | */
|
|---|
| 68 | public ReferenceQueue()
|
|---|
| 69 | {
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Checks if there is a reference on the queue, returning it
|
|---|
| 74 | * immediately. The reference will be dequeued.
|
|---|
| 75 | *
|
|---|
| 76 | * @return a reference on the queue, if there is one,
|
|---|
| 77 | * <code>null</code> otherwise.
|
|---|
| 78 | */
|
|---|
| 79 | public synchronized Reference poll()
|
|---|
| 80 | {
|
|---|
| 81 | return dequeue();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * This is called by reference to enqueue itself on this queue.
|
|---|
| 86 | * @param ref the reference that should be enqueued.
|
|---|
| 87 | */
|
|---|
| 88 | synchronized void enqueue(Reference ref)
|
|---|
| 89 | {
|
|---|
| 90 | /* last reference will point to itself */
|
|---|
| 91 | ref.nextOnQueue = first == null ? ref : first;
|
|---|
| 92 | first = ref;
|
|---|
| 93 | /* this wakes only one remove thread. */
|
|---|
| 94 | notify();
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Remove a reference from the queue, if there is one.
|
|---|
| 99 | * @return the first element of the queue, or null if there isn't any.
|
|---|
| 100 | */
|
|---|
| 101 | private Reference dequeue()
|
|---|
| 102 | {
|
|---|
| 103 | if (first == null)
|
|---|
| 104 | return null;
|
|---|
| 105 |
|
|---|
| 106 | Reference result = first;
|
|---|
| 107 | first = (first == first.nextOnQueue) ? null : first.nextOnQueue;
|
|---|
| 108 | result.nextOnQueue = null;
|
|---|
| 109 | return result;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Removes a reference from the queue, blocking for <code>timeout</code>
|
|---|
| 114 | * until a reference is enqueued.
|
|---|
| 115 | * @param timeout the timeout period in milliseconds, <code>0</code> means
|
|---|
| 116 | * wait forever.
|
|---|
| 117 | * @return the reference removed from the queue, or
|
|---|
| 118 | * <code>null</code> if timeout period expired.
|
|---|
| 119 | * @exception InterruptedException if the wait was interrupted.
|
|---|
| 120 | */
|
|---|
| 121 | public synchronized Reference remove(long timeout)
|
|---|
| 122 | throws InterruptedException
|
|---|
| 123 | {
|
|---|
| 124 | if (first == null)
|
|---|
| 125 | {
|
|---|
| 126 | wait(timeout);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | return dequeue();
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Removes a reference from the queue, blocking until a reference is
|
|---|
| 135 | * enqueued.
|
|---|
| 136 | *
|
|---|
| 137 | * @return the reference removed from the queue.
|
|---|
| 138 | * @exception InterruptedException if the wait was interrupted.
|
|---|
| 139 | */
|
|---|
| 140 | public Reference remove()
|
|---|
| 141 | throws InterruptedException
|
|---|
| 142 | {
|
|---|
| 143 | return remove(0L);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|