1.1 --- a/emul/compact/src/main/java/java/lang/ref/ReferenceQueue.java Mon Feb 25 19:00:08 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,148 +0,0 @@
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 - lock.notifyAll();
1.69 - return true;
1.70 - }
1.71 - }
1.72 - }
1.73 -
1.74 - private Reference<? extends T> reallyPoll() { /* Must hold lock */
1.75 - if (head != null) {
1.76 - Reference<? extends T> r = head;
1.77 - head = (r.next == r) ? null : r.next;
1.78 - r.queue = NULL;
1.79 - r.next = r;
1.80 - queueLength--;
1.81 - return r;
1.82 - }
1.83 - return null;
1.84 - }
1.85 -
1.86 - /**
1.87 - * Polls this queue to see if a reference object is available. If one is
1.88 - * available without further delay then it is removed from the queue and
1.89 - * returned. Otherwise this method immediately returns <tt>null</tt>.
1.90 - *
1.91 - * @return A reference object, if one was immediately available,
1.92 - * otherwise <code>null</code>
1.93 - */
1.94 - public Reference<? extends T> poll() {
1.95 - if (head == null)
1.96 - return null;
1.97 - synchronized (lock) {
1.98 - return reallyPoll();
1.99 - }
1.100 - }
1.101 -
1.102 - /**
1.103 - * Removes the next reference object in this queue, blocking until either
1.104 - * one becomes available or the given timeout period expires.
1.105 - *
1.106 - * <p> This method does not offer real-time guarantees: It schedules the
1.107 - * timeout as if by invoking the {@link Object#wait(long)} method.
1.108 - *
1.109 - * @param timeout If positive, block for up to <code>timeout</code>
1.110 - * milliseconds while waiting for a reference to be
1.111 - * added to this queue. If zero, block indefinitely.
1.112 - *
1.113 - * @return A reference object, if one was available within the specified
1.114 - * timeout period, otherwise <code>null</code>
1.115 - *
1.116 - * @throws IllegalArgumentException
1.117 - * If the value of the timeout argument is negative
1.118 - *
1.119 - * @throws InterruptedException
1.120 - * If the timeout wait is interrupted
1.121 - */
1.122 - public Reference<? extends T> remove(long timeout)
1.123 - throws IllegalArgumentException, InterruptedException
1.124 - {
1.125 - if (timeout < 0) {
1.126 - throw new IllegalArgumentException("Negative timeout value");
1.127 - }
1.128 - synchronized (lock) {
1.129 - Reference<? extends T> r = reallyPoll();
1.130 - if (r != null) return r;
1.131 - for (;;) {
1.132 - lock.wait(timeout);
1.133 - r = reallyPoll();
1.134 - if (r != null) return r;
1.135 - if (timeout != 0) return null;
1.136 - }
1.137 - }
1.138 - }
1.139 -
1.140 - /**
1.141 - * Removes the next reference object in this queue, blocking until one
1.142 - * becomes available.
1.143 - *
1.144 - * @return A reference object, blocking until one becomes available
1.145 - * @throws InterruptedException If the wait is interrupted
1.146 - */
1.147 - public Reference<? extends T> remove() throws InterruptedException {
1.148 - return remove(0);
1.149 - }
1.150 -
1.151 -}