rt/emul/compact/src/main/java/java/lang/ref/ReferenceQueue.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 604 emul/compact/src/main/java/java/lang/ref/ReferenceQueue.java@3fcc279c921b
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@601
     1
/*
jaroslav@601
     2
 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
jaroslav@601
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@601
     4
 *
jaroslav@601
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@601
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@601
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@601
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@601
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@601
    10
 *
jaroslav@601
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@601
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@601
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@601
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@601
    15
 * accompanied this code).
jaroslav@601
    16
 *
jaroslav@601
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@601
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@601
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@601
    20
 *
jaroslav@601
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@601
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@601
    23
 * questions.
jaroslav@601
    24
 */
jaroslav@601
    25
jaroslav@601
    26
package java.lang.ref;
jaroslav@601
    27
jaroslav@601
    28
/**
jaroslav@601
    29
 * Reference queues, to which registered reference objects are appended by the
jaroslav@601
    30
 * garbage collector after the appropriate reachability changes are detected.
jaroslav@601
    31
 *
jaroslav@601
    32
 * @author   Mark Reinhold
jaroslav@601
    33
 * @since    1.2
jaroslav@601
    34
 */
jaroslav@601
    35
jaroslav@601
    36
public class ReferenceQueue<T> {
jaroslav@601
    37
jaroslav@601
    38
    /**
jaroslav@601
    39
     * Constructs a new reference-object queue.
jaroslav@601
    40
     */
jaroslav@601
    41
    public ReferenceQueue() { }
jaroslav@601
    42
jaroslav@601
    43
    private static class Null extends ReferenceQueue {
jaroslav@601
    44
        boolean enqueue(Reference r) {
jaroslav@601
    45
            return false;
jaroslav@601
    46
        }
jaroslav@601
    47
    }
jaroslav@601
    48
jaroslav@601
    49
    static ReferenceQueue NULL = new Null();
jaroslav@601
    50
    static ReferenceQueue ENQUEUED = new Null();
jaroslav@601
    51
jaroslav@601
    52
    static private class Lock { };
jaroslav@601
    53
    private Lock lock = new Lock();
jaroslav@601
    54
    private volatile Reference<? extends T> head = null;
jaroslav@601
    55
    private long queueLength = 0;
jaroslav@601
    56
jaroslav@601
    57
    boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
jaroslav@601
    58
        synchronized (r) {
jaroslav@601
    59
            if (r.queue == ENQUEUED) return false;
jaroslav@601
    60
            synchronized (lock) {
jaroslav@601
    61
                r.queue = ENQUEUED;
jaroslav@601
    62
                r.next = (head == null) ? r : head;
jaroslav@601
    63
                head = r;
jaroslav@601
    64
                queueLength++;
jaroslav@601
    65
                lock.notifyAll();
jaroslav@601
    66
                return true;
jaroslav@601
    67
            }
jaroslav@601
    68
        }
jaroslav@601
    69
    }
jaroslav@601
    70
jaroslav@601
    71
    private Reference<? extends T> reallyPoll() {       /* Must hold lock */
jaroslav@601
    72
        if (head != null) {
jaroslav@601
    73
            Reference<? extends T> r = head;
jaroslav@601
    74
            head = (r.next == r) ? null : r.next;
jaroslav@601
    75
            r.queue = NULL;
jaroslav@601
    76
            r.next = r;
jaroslav@601
    77
            queueLength--;
jaroslav@601
    78
            return r;
jaroslav@601
    79
        }
jaroslav@601
    80
        return null;
jaroslav@601
    81
    }
jaroslav@601
    82
jaroslav@601
    83
    /**
jaroslav@601
    84
     * Polls this queue to see if a reference object is available.  If one is
jaroslav@601
    85
     * available without further delay then it is removed from the queue and
jaroslav@601
    86
     * returned.  Otherwise this method immediately returns <tt>null</tt>.
jaroslav@601
    87
     *
jaroslav@601
    88
     * @return  A reference object, if one was immediately available,
jaroslav@601
    89
     *          otherwise <code>null</code>
jaroslav@601
    90
     */
jaroslav@601
    91
    public Reference<? extends T> poll() {
jaroslav@601
    92
        if (head == null)
jaroslav@601
    93
            return null;
jaroslav@601
    94
        synchronized (lock) {
jaroslav@601
    95
            return reallyPoll();
jaroslav@601
    96
        }
jaroslav@601
    97
    }
jaroslav@601
    98
jaroslav@601
    99
    /**
jaroslav@601
   100
     * Removes the next reference object in this queue, blocking until either
jaroslav@601
   101
     * one becomes available or the given timeout period expires.
jaroslav@601
   102
     *
jaroslav@601
   103
     * <p> This method does not offer real-time guarantees: It schedules the
jaroslav@601
   104
     * timeout as if by invoking the {@link Object#wait(long)} method.
jaroslav@601
   105
     *
jaroslav@601
   106
     * @param  timeout  If positive, block for up to <code>timeout</code>
jaroslav@601
   107
     *                  milliseconds while waiting for a reference to be
jaroslav@601
   108
     *                  added to this queue.  If zero, block indefinitely.
jaroslav@601
   109
     *
jaroslav@601
   110
     * @return  A reference object, if one was available within the specified
jaroslav@601
   111
     *          timeout period, otherwise <code>null</code>
jaroslav@601
   112
     *
jaroslav@601
   113
     * @throws  IllegalArgumentException
jaroslav@601
   114
     *          If the value of the timeout argument is negative
jaroslav@601
   115
     *
jaroslav@601
   116
     * @throws  InterruptedException
jaroslav@601
   117
     *          If the timeout wait is interrupted
jaroslav@601
   118
     */
jaroslav@601
   119
    public Reference<? extends T> remove(long timeout)
jaroslav@601
   120
        throws IllegalArgumentException, InterruptedException
jaroslav@601
   121
    {
jaroslav@601
   122
        if (timeout < 0) {
jaroslav@601
   123
            throw new IllegalArgumentException("Negative timeout value");
jaroslav@601
   124
        }
jaroslav@601
   125
        synchronized (lock) {
jaroslav@601
   126
            Reference<? extends T> r = reallyPoll();
jaroslav@601
   127
            if (r != null) return r;
jaroslav@601
   128
            for (;;) {
jaroslav@601
   129
                lock.wait(timeout);
jaroslav@601
   130
                r = reallyPoll();
jaroslav@601
   131
                if (r != null) return r;
jaroslav@601
   132
                if (timeout != 0) return null;
jaroslav@601
   133
            }
jaroslav@601
   134
        }
jaroslav@601
   135
    }
jaroslav@601
   136
jaroslav@601
   137
    /**
jaroslav@601
   138
     * Removes the next reference object in this queue, blocking until one
jaroslav@601
   139
     * becomes available.
jaroslav@601
   140
     *
jaroslav@601
   141
     * @return A reference object, blocking until one becomes available
jaroslav@601
   142
     * @throws  InterruptedException  If the wait is interrupted
jaroslav@601
   143
     */
jaroslav@601
   144
    public Reference<? extends T> remove() throws InterruptedException {
jaroslav@601
   145
        return remove(0);
jaroslav@601
   146
    }
jaroslav@601
   147
jaroslav@601
   148
}