src/java.base/share/classes/java/lang/ref/ReferenceQueue.java
author kbarrett
Sat, 26 May 2018 03:11:50 -0400
changeset 50277 f84ae8aa5d88
parent 47216 71c04702a3d5
child 50301 fe42de5250f3
permissions -rw-r--r--
8203028: Simplify reference processing in light of JDK-8175797 Summary: Removed special handling of Reference.next Reviewed-by: tschatzl, sjohanss, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50277
f84ae8aa5d88 8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2442
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
32100
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
    28
import java.util.function.Consumer;
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 34774
diff changeset
    29
import jdk.internal.misc.VM;
32100
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Reference queues, to which registered reference objects are appended by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * garbage collector after the appropriate reachability changes are detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ReferenceQueue<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Constructs a new reference-object queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public ReferenceQueue() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
19053
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
    46
    private static class Null<S> extends ReferenceQueue<S> {
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
    47
        boolean enqueue(Reference<? extends S> r) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
19053
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
    52
    static ReferenceQueue<Object> NULL = new Null<>();
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
    53
    static ReferenceQueue<Object> ENQUEUED = new Null<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32221
diff changeset
    55
    private static class Lock { };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Lock lock = new Lock();
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 32649
diff changeset
    57
    private volatile Reference<? extends T> head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private long queueLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    61
        synchronized (lock) {
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    62
            // Check that since getting the lock this reference hasn't already been
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    63
            // enqueued (and even then removed)
19053
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
    64
            ReferenceQueue<?> queue = r.queue;
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    65
            if ((queue == NULL) || (queue == ENQUEUED)) {
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    66
                return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    68
            assert queue == this;
50277
f84ae8aa5d88 8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents: 47216
diff changeset
    69
            // Self-loop end, so if a FinalReference it remains inactive.
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    70
            r.next = (head == null) ? r : head;
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    71
            head = r;
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    72
            queueLength++;
32219
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    73
            // Update r.queue *after* adding to list, to avoid race
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    74
            // with concurrent enqueued checks and fast-path poll().
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    75
            // Volatiles ensure ordering.
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    76
            r.queue = ENQUEUED;
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    77
            if (r instanceof FinalReference) {
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 34774
diff changeset
    78
                VM.addFinalRefCount(1);
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    79
            }
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    80
            lock.notifyAll();
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    81
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private Reference<? extends T> reallyPoll() {       /* Must hold lock */
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    86
        Reference<? extends T> r = head;
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 5506
diff changeset
    87
        if (r != null) {
32219
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    88
            r.queue = NULL;
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    89
            // Update r.queue *before* removing from list, to avoid
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    90
            // race with concurrent enqueued checks and fast-path
e613f4863973 8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents: 32100
diff changeset
    91
            // poll().  Volatiles ensure ordering.
32100
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
    92
            @SuppressWarnings("unchecked")
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
    93
            Reference<? extends T> rn = r.next;
50277
f84ae8aa5d88 8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents: 47216
diff changeset
    94
            // Handle self-looped next as end of list designator.
32100
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
    95
            head = (rn == r) ? null : rn;
50277
f84ae8aa5d88 8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents: 47216
diff changeset
    96
            // Self-loop next rather than setting to null, so if a
f84ae8aa5d88 8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents: 47216
diff changeset
    97
            // FinalReference it remains inactive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            r.next = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            queueLength--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (r instanceof FinalReference) {
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 34774
diff changeset
   101
                VM.addFinalRefCount(-1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Polls this queue to see if a reference object is available.  If one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * available without further delay then it is removed from the queue and
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   111
     * returned.  Otherwise this method immediately returns {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return  A reference object, if one was immediately available,
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   114
     *          otherwise {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public Reference<? extends T> poll() {
2442
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
   117
        if (head == null)
f8bb207f9458 6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents: 2
diff changeset
   118
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return reallyPoll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Removes the next reference object in this queue, blocking until either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * one becomes available or the given timeout period expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p> This method does not offer real-time guarantees: It schedules the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * timeout as if by invoking the {@link Object#wait(long)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   131
     * @param  timeout  If positive, block for up to {@code timeout}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *                  milliseconds while waiting for a reference to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *                  added to this queue.  If zero, block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return  A reference object, if one was available within the specified
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   136
     *          timeout period, otherwise {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *          If the value of the timeout argument is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @throws  InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *          If the timeout wait is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public Reference<? extends T> remove(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        throws IllegalArgumentException, InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (timeout < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new IllegalArgumentException("Negative timeout value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            Reference<? extends T> r = reallyPoll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (r != null) return r;
23027
07b4bf8a9026 6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents: 19053
diff changeset
   153
            long start = (timeout == 0) ? 0 : System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                lock.wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                r = reallyPoll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (r != null) return r;
23027
07b4bf8a9026 6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents: 19053
diff changeset
   158
                if (timeout != 0) {
07b4bf8a9026 6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents: 19053
diff changeset
   159
                    long end = System.nanoTime();
07b4bf8a9026 6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents: 19053
diff changeset
   160
                    timeout -= (end - start) / 1000_000;
07b4bf8a9026 6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents: 19053
diff changeset
   161
                    if (timeout <= 0) return null;
07b4bf8a9026 6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents: 19053
diff changeset
   162
                    start = end;
07b4bf8a9026 6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents: 19053
diff changeset
   163
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Removes the next reference object in this queue, blocking until one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * becomes available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return A reference object, blocking until one becomes available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @throws  InterruptedException  If the wait is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public Reference<? extends T> remove() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
32100
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   179
    /**
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   180
     * Iterate queue and invoke given action with each Reference.
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   181
     * Suitable for diagnostic purposes.
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   182
     * WARNING: any use of this method should make sure to not
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   183
     * retain the referents of iterated references (in case of
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   184
     * FinalReference(s)) so that their life is not prolonged more
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   185
     * than necessary.
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   186
     */
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   187
    void forEach(Consumer<? super Reference<? extends T>> action) {
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   188
        for (Reference<? extends T> r = head; r != null;) {
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   189
            action.accept(r);
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   190
            @SuppressWarnings("unchecked")
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   191
            Reference<? extends T> rn = r.next;
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   192
            if (rn == r) {
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   193
                if (r.queue == ENQUEUED) {
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   194
                    // still enqueued -> we reached end of chain
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   195
                    r = null;
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   196
                } else {
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   197
                    // already dequeued: r.queue == NULL; ->
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   198
                    // restart from head when overtaken by queue poller(s)
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   199
                    r = head;
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   200
                }
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   201
            } else {
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   202
                // next in chain
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   203
                r = rn;
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   204
            }
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   205
        }
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 25859
diff changeset
   206
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}