emul/compact/src/main/java/java/lang/ref/ReferenceQueue.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Jan 2013 18:12:47 +0100
branchjdk7-b147
changeset 601 5198affdb915
child 604 3fcc279c921b
permissions -rw-r--r--
Adding ObjectInputStream and ObjectOutputStream (but without implementation). Adding PropertyChange related classes.
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
                if (r instanceof FinalReference) {
jaroslav@601
    66
                    sun.misc.VM.addFinalRefCount(1);
jaroslav@601
    67
                }
jaroslav@601
    68
                lock.notifyAll();
jaroslav@601
    69
                return true;
jaroslav@601
    70
            }
jaroslav@601
    71
        }
jaroslav@601
    72
    }
jaroslav@601
    73
jaroslav@601
    74
    private Reference<? extends T> reallyPoll() {       /* Must hold lock */
jaroslav@601
    75
        if (head != null) {
jaroslav@601
    76
            Reference<? extends T> r = head;
jaroslav@601
    77
            head = (r.next == r) ? null : r.next;
jaroslav@601
    78
            r.queue = NULL;
jaroslav@601
    79
            r.next = r;
jaroslav@601
    80
            queueLength--;
jaroslav@601
    81
            if (r instanceof FinalReference) {
jaroslav@601
    82
                sun.misc.VM.addFinalRefCount(-1);
jaroslav@601
    83
            }
jaroslav@601
    84
            return r;
jaroslav@601
    85
        }
jaroslav@601
    86
        return null;
jaroslav@601
    87
    }
jaroslav@601
    88
jaroslav@601
    89
    /**
jaroslav@601
    90
     * Polls this queue to see if a reference object is available.  If one is
jaroslav@601
    91
     * available without further delay then it is removed from the queue and
jaroslav@601
    92
     * returned.  Otherwise this method immediately returns <tt>null</tt>.
jaroslav@601
    93
     *
jaroslav@601
    94
     * @return  A reference object, if one was immediately available,
jaroslav@601
    95
     *          otherwise <code>null</code>
jaroslav@601
    96
     */
jaroslav@601
    97
    public Reference<? extends T> poll() {
jaroslav@601
    98
        if (head == null)
jaroslav@601
    99
            return null;
jaroslav@601
   100
        synchronized (lock) {
jaroslav@601
   101
            return reallyPoll();
jaroslav@601
   102
        }
jaroslav@601
   103
    }
jaroslav@601
   104
jaroslav@601
   105
    /**
jaroslav@601
   106
     * Removes the next reference object in this queue, blocking until either
jaroslav@601
   107
     * one becomes available or the given timeout period expires.
jaroslav@601
   108
     *
jaroslav@601
   109
     * <p> This method does not offer real-time guarantees: It schedules the
jaroslav@601
   110
     * timeout as if by invoking the {@link Object#wait(long)} method.
jaroslav@601
   111
     *
jaroslav@601
   112
     * @param  timeout  If positive, block for up to <code>timeout</code>
jaroslav@601
   113
     *                  milliseconds while waiting for a reference to be
jaroslav@601
   114
     *                  added to this queue.  If zero, block indefinitely.
jaroslav@601
   115
     *
jaroslav@601
   116
     * @return  A reference object, if one was available within the specified
jaroslav@601
   117
     *          timeout period, otherwise <code>null</code>
jaroslav@601
   118
     *
jaroslav@601
   119
     * @throws  IllegalArgumentException
jaroslav@601
   120
     *          If the value of the timeout argument is negative
jaroslav@601
   121
     *
jaroslav@601
   122
     * @throws  InterruptedException
jaroslav@601
   123
     *          If the timeout wait is interrupted
jaroslav@601
   124
     */
jaroslav@601
   125
    public Reference<? extends T> remove(long timeout)
jaroslav@601
   126
        throws IllegalArgumentException, InterruptedException
jaroslav@601
   127
    {
jaroslav@601
   128
        if (timeout < 0) {
jaroslav@601
   129
            throw new IllegalArgumentException("Negative timeout value");
jaroslav@601
   130
        }
jaroslav@601
   131
        synchronized (lock) {
jaroslav@601
   132
            Reference<? extends T> r = reallyPoll();
jaroslav@601
   133
            if (r != null) return r;
jaroslav@601
   134
            for (;;) {
jaroslav@601
   135
                lock.wait(timeout);
jaroslav@601
   136
                r = reallyPoll();
jaroslav@601
   137
                if (r != null) return r;
jaroslav@601
   138
                if (timeout != 0) return null;
jaroslav@601
   139
            }
jaroslav@601
   140
        }
jaroslav@601
   141
    }
jaroslav@601
   142
jaroslav@601
   143
    /**
jaroslav@601
   144
     * Removes the next reference object in this queue, blocking until one
jaroslav@601
   145
     * becomes available.
jaroslav@601
   146
     *
jaroslav@601
   147
     * @return A reference object, blocking until one becomes available
jaroslav@601
   148
     * @throws  InterruptedException  If the wait is interrupted
jaroslav@601
   149
     */
jaroslav@601
   150
    public Reference<? extends T> remove() throws InterruptedException {
jaroslav@601
   151
        return remove(0);
jaroslav@601
   152
    }
jaroslav@601
   153
jaroslav@601
   154
}