emul/compact/src/main/java/java/lang/ref/ReferenceQueue.java
branchjdk7-b147
changeset 601 5198affdb915
child 604 3fcc279c921b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/compact/src/main/java/java/lang/ref/ReferenceQueue.java	Mon Jan 28 18:12:47 2013 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.ref;
    1.30 +
    1.31 +/**
    1.32 + * Reference queues, to which registered reference objects are appended by the
    1.33 + * garbage collector after the appropriate reachability changes are detected.
    1.34 + *
    1.35 + * @author   Mark Reinhold
    1.36 + * @since    1.2
    1.37 + */
    1.38 +
    1.39 +public class ReferenceQueue<T> {
    1.40 +
    1.41 +    /**
    1.42 +     * Constructs a new reference-object queue.
    1.43 +     */
    1.44 +    public ReferenceQueue() { }
    1.45 +
    1.46 +    private static class Null extends ReferenceQueue {
    1.47 +        boolean enqueue(Reference r) {
    1.48 +            return false;
    1.49 +        }
    1.50 +    }
    1.51 +
    1.52 +    static ReferenceQueue NULL = new Null();
    1.53 +    static ReferenceQueue ENQUEUED = new Null();
    1.54 +
    1.55 +    static private class Lock { };
    1.56 +    private Lock lock = new Lock();
    1.57 +    private volatile Reference<? extends T> head = null;
    1.58 +    private long queueLength = 0;
    1.59 +
    1.60 +    boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
    1.61 +        synchronized (r) {
    1.62 +            if (r.queue == ENQUEUED) return false;
    1.63 +            synchronized (lock) {
    1.64 +                r.queue = ENQUEUED;
    1.65 +                r.next = (head == null) ? r : head;
    1.66 +                head = r;
    1.67 +                queueLength++;
    1.68 +                if (r instanceof FinalReference) {
    1.69 +                    sun.misc.VM.addFinalRefCount(1);
    1.70 +                }
    1.71 +                lock.notifyAll();
    1.72 +                return true;
    1.73 +            }
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    private Reference<? extends T> reallyPoll() {       /* Must hold lock */
    1.78 +        if (head != null) {
    1.79 +            Reference<? extends T> r = head;
    1.80 +            head = (r.next == r) ? null : r.next;
    1.81 +            r.queue = NULL;
    1.82 +            r.next = r;
    1.83 +            queueLength--;
    1.84 +            if (r instanceof FinalReference) {
    1.85 +                sun.misc.VM.addFinalRefCount(-1);
    1.86 +            }
    1.87 +            return r;
    1.88 +        }
    1.89 +        return null;
    1.90 +    }
    1.91 +
    1.92 +    /**
    1.93 +     * Polls this queue to see if a reference object is available.  If one is
    1.94 +     * available without further delay then it is removed from the queue and
    1.95 +     * returned.  Otherwise this method immediately returns <tt>null</tt>.
    1.96 +     *
    1.97 +     * @return  A reference object, if one was immediately available,
    1.98 +     *          otherwise <code>null</code>
    1.99 +     */
   1.100 +    public Reference<? extends T> poll() {
   1.101 +        if (head == null)
   1.102 +            return null;
   1.103 +        synchronized (lock) {
   1.104 +            return reallyPoll();
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Removes the next reference object in this queue, blocking until either
   1.110 +     * one becomes available or the given timeout period expires.
   1.111 +     *
   1.112 +     * <p> This method does not offer real-time guarantees: It schedules the
   1.113 +     * timeout as if by invoking the {@link Object#wait(long)} method.
   1.114 +     *
   1.115 +     * @param  timeout  If positive, block for up to <code>timeout</code>
   1.116 +     *                  milliseconds while waiting for a reference to be
   1.117 +     *                  added to this queue.  If zero, block indefinitely.
   1.118 +     *
   1.119 +     * @return  A reference object, if one was available within the specified
   1.120 +     *          timeout period, otherwise <code>null</code>
   1.121 +     *
   1.122 +     * @throws  IllegalArgumentException
   1.123 +     *          If the value of the timeout argument is negative
   1.124 +     *
   1.125 +     * @throws  InterruptedException
   1.126 +     *          If the timeout wait is interrupted
   1.127 +     */
   1.128 +    public Reference<? extends T> remove(long timeout)
   1.129 +        throws IllegalArgumentException, InterruptedException
   1.130 +    {
   1.131 +        if (timeout < 0) {
   1.132 +            throw new IllegalArgumentException("Negative timeout value");
   1.133 +        }
   1.134 +        synchronized (lock) {
   1.135 +            Reference<? extends T> r = reallyPoll();
   1.136 +            if (r != null) return r;
   1.137 +            for (;;) {
   1.138 +                lock.wait(timeout);
   1.139 +                r = reallyPoll();
   1.140 +                if (r != null) return r;
   1.141 +                if (timeout != 0) return null;
   1.142 +            }
   1.143 +        }
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Removes the next reference object in this queue, blocking until one
   1.148 +     * becomes available.
   1.149 +     *
   1.150 +     * @return A reference object, blocking until one becomes available
   1.151 +     * @throws  InterruptedException  If the wait is interrupted
   1.152 +     */
   1.153 +    public Reference<? extends T> remove() throws InterruptedException {
   1.154 +        return remove(0);
   1.155 +    }
   1.156 +
   1.157 +}