src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44743 jdk/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java@f0bbd698c486
child 48586 8877e857fdd7
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7976
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    37
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    38
import java.lang.ref.WeakReference;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    39
import java.util.AbstractQueue;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    40
import java.util.Arrays;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    41
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    42
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    43
import java.util.NoSuchElementException;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    44
import java.util.Objects;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    45
import java.util.Spliterator;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
    46
import java.util.Spliterators;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    47
import java.util.concurrent.locks.Condition;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    48
import java.util.concurrent.locks.ReentrantLock;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    49
import java.util.function.Consumer;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    50
import java.util.function.Predicate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * A bounded {@linkplain BlockingQueue blocking queue} backed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * array.  This queue orders elements FIFO (first-in-first-out).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * queue the longest time.  The <em>tail</em> of the queue is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * element that has been on the queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * operations obtain elements at the head of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>This is a classic &quot;bounded buffer&quot;, in which a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * fixed-sized array holds elements inserted by producers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * extracted by consumers.  Once created, the capacity cannot be
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    64
 * changed.  Attempts to {@code put} an element into a full queue
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    65
 * will result in the operation blocking; attempts to {@code take} an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * element from an empty queue will similarly block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    68
 * <p>This class supports an optional fairness policy for ordering
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * waiting producer and consumer threads.  By default, this ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * is not guaranteed. However, a queue constructed with fairness set
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    71
 * to {@code true} grants threads access in FIFO order. Fairness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * generally decreases throughput but reduces variability and avoids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * starvation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
    75
 * <p>This class and its iterator implement all of the <em>optional</em>
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
    76
 * methods of the {@link Collection} and {@link Iterator} interfaces.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>This class is a member of the
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 44039
diff changeset
    79
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    84
 * @param <E> the type of elements held in this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    89
    /*
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    90
     * Much of the implementation mechanics, especially the unusual
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    91
     * nested loops, are shared and co-maintained with ArrayDeque.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    92
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
    93
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Serialization ID. This class relies on default serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * even for the items array, which is default-serialized, even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * it is empty. Otherwise it could not be declared final, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * necessary here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final long serialVersionUID = -817911632652898426L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   102
    /** The queued items */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   103
    final Object[] items;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   104
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   105
    /** items index for next take, poll, peek or remove */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   106
    int takeIndex;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   107
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   108
    /** items index for next put, offer, or add */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   109
    int putIndex;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   110
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   111
    /** Number of elements in the queue */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   112
    int count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Concurrency control uses the classic two-condition algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * found in any textbook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** Main lock guarding all access */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   120
    final ReentrantLock lock;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   121
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /** Condition for waiting takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private final Condition notEmpty;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   124
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** Condition for waiting puts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private final Condition notFull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   128
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   129
     * Shared state for currently active iterators, or null if there
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   130
     * are known not to be any.  Allows queue operations to update
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   131
     * iterator state.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   132
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   133
    transient Itrs itrs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   135
    // Internal helper methods
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   136
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   137
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   138
     * Increments i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   139
     * Precondition and postcondition: 0 <= i < modulus.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   140
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   141
    static final int inc(int i, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   142
        if (++i >= modulus) i = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   143
        return i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   144
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   145
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   146
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   147
     * Decrements i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   148
     * Precondition and postcondition: 0 <= i < modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   149
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   150
    static final int dec(int i, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   151
        if (--i < 0) i = modulus - 1;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   152
        return i;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   153
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   154
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   155
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   156
     * Returns item at index i.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   157
     */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   158
    @SuppressWarnings("unchecked")
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   159
    final E itemAt(int i) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   160
        return (E) items[i];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   161
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   162
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   163
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   164
     * Returns element at array index i.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   165
     * This is a slight abuse of generics, accepted by javac.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   166
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   167
    @SuppressWarnings("unchecked")
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   168
    static <E> E itemAt(Object[] items, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   169
        return (E) items[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   170
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   171
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   172
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Inserts element at current put position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   176
    private void enqueue(E e) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   177
        // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   178
        // assert lock.getHoldCount() == 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   179
        // assert items[putIndex] == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   180
        final Object[] items = this.items;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   181
        items[putIndex] = e;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   182
        if (++putIndex == items.length) putIndex = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   183
        count++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Extracts element at current take position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   191
    private E dequeue() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   192
        // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   193
        // assert lock.getHoldCount() == 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   194
        // assert items[takeIndex] != null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   195
        final Object[] items = this.items;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   196
        @SuppressWarnings("unchecked")
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   197
        E e = (E) items[takeIndex];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        items[takeIndex] = null;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   199
        if (++takeIndex == items.length) takeIndex = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   200
        count--;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   201
        if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   202
            itrs.elementDequeued();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        notFull.signal();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   204
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   208
     * Deletes item at array index removeIndex.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   209
     * Utility for remove(Object) and iterator.remove.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   212
    void removeAt(final int removeIndex) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   213
        // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   214
        // assert lock.getHoldCount() == 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   215
        // assert items[removeIndex] != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   216
        // assert removeIndex >= 0 && removeIndex < items.length;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   217
        final Object[] items = this.items;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   218
        if (removeIndex == takeIndex) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   219
            // removing front item; just advance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            items[takeIndex] = null;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   221
            if (++takeIndex == items.length) takeIndex = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   222
            count--;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   223
            if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   224
                itrs.elementDequeued();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   226
            // an "interior" remove
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   227
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            // slide over all others up through putIndex.
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   229
            for (int i = removeIndex, putIndex = this.putIndex;;) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   230
                int pred = i;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   231
                if (++i == items.length) i = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   232
                if (i == putIndex) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   233
                    items[pred] = null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   234
                    this.putIndex = pred;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   237
                items[pred] = items[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   239
            count--;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   240
            if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   241
                itrs.removedAt(removeIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   247
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * capacity and default access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param capacity the capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   251
     * @throws IllegalArgumentException if {@code capacity < 1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public ArrayBlockingQueue(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        this(capacity, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   258
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * capacity and the specified access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param capacity the capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   262
     * @param fair if {@code true} then queue accesses for threads blocked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *        on insertion or removal, are processed in FIFO order;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   264
     *        if {@code false} the access order is unspecified.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   265
     * @throws IllegalArgumentException if {@code capacity < 1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public ArrayBlockingQueue(int capacity, boolean fair) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (capacity <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new IllegalArgumentException();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   270
        this.items = new Object[capacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        lock = new ReentrantLock(fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        notEmpty = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        notFull =  lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   277
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * capacity, the specified access policy and initially containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * elements of the given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * added in traversal order of the collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param capacity the capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   283
     * @param fair if {@code true} then queue accesses for threads blocked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *        on insertion or removal, are processed in FIFO order;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   285
     *        if {@code false} the access order is unspecified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param c the collection of elements to initially contain
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   287
     * @throws IllegalArgumentException if {@code capacity} is less than
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   288
     *         {@code c.size()}, or less than 1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public ArrayBlockingQueue(int capacity, boolean fair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                              Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        this(capacity, fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   296
        final ReentrantLock lock = this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   297
        lock.lock(); // Lock only for visibility, not mutual exclusion
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   298
        try {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   299
            final Object[] items = this.items;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   300
            int i = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   301
            try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   302
                for (E e : c)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   303
                    items[i++] = Objects.requireNonNull(e);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   304
            } catch (ArrayIndexOutOfBoundsException ex) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   305
                throw new IllegalArgumentException();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   306
            }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   307
            count = i;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   308
            putIndex = (i == capacity) ? 0 : i;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   309
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   310
            lock.unlock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   311
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * possible to do so immediately without exceeding the queue's capacity,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   317
     * returning {@code true} upon success and throwing an
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   318
     * {@code IllegalStateException} if this queue is full.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param e the element to add
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   321
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @throws IllegalStateException if this queue is full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return super.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * possible to do so immediately without exceeding the queue's capacity,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   332
     * returning {@code true} upon success and {@code false} if this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * is full.  This method is generally preferable to method {@link #add},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * which can fail to insert an element only by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public boolean offer(E e) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   339
        Objects.requireNonNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (count == items.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   346
                enqueue(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * for space to become available if the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public void put(E e) throws InterruptedException {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   362
        Objects.requireNonNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   366
            while (count == items.length)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   367
                notFull.await();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   368
            enqueue(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * up to the specified wait time for space to become available if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   385
        Objects.requireNonNull(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   390
            while (count == items.length) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   391
                if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    return false;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   393
                nanos = notFull.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   395
            enqueue(e);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   396
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   406
            return (count == 0) ? null : dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   416
            while (count == 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   417
                notEmpty.await();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   418
            return dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   429
            while (count == 0) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   430
                if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    return null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   432
                nanos = notEmpty.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   434
            return dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   444
            return itemAt(takeIndex); // null when queue is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    // this doc comment is overridden to remove the reference to collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // greater in size than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Returns the number of elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    // this doc comment is a modified copy of the inherited doc comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // without the reference to unlimited queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Returns the number of additional elements that this queue can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * blocking. This is always equal to the initial capacity of this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   473
     * less the current {@code size} of this queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <p>Note that you <em>cannot</em> always tell if an attempt to insert
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   476
     * an element will succeed by inspecting {@code remainingCapacity}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return items.length - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Removes a single instance of the specified element from this queue,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   492
     * if it is present.  More formally, removes an element {@code e} such
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   493
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * elements.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   495
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   498
     * <p>Removal of interior elements in circular array based queues
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   499
     * is an intrinsically slow and disruptive operation, so should
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   500
     * be undertaken only in exceptional circumstances, ideally
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   501
     * only when the queue is known not to be accessible by other
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   502
     * threads.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   503
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param o element to be removed from this queue, if present
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   505
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   512
            if (count > 0) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   513
                final Object[] items = this.items;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   514
                for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   515
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   516
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   517
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   518
                        if (o.equals(items[i])) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   519
                            removeAt(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   520
                            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   521
                        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   522
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   523
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   525
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   532
     * Returns {@code true} if this queue contains the specified element.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   533
     * More formally, returns {@code true} if and only if this queue contains
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   534
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param o object to be checked for containment in this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   537
     * @return {@code true} if this queue contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   544
            if (count > 0) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   545
                final Object[] items = this.items;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   546
                for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   547
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   548
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   549
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   550
                        if (o.equals(items[i]))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   551
                            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   552
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   553
                }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   554
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   578
            final Object[] items = this.items;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   579
            final int end = takeIndex + count;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   580
            final Object[] a = Arrays.copyOfRange(items, takeIndex, end);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   581
            if (end != putIndex)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   582
                System.arraycopy(items, 0, a, items.length - takeIndex, putIndex);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   583
            return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * the array immediately following the end of the queue is set to
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   599
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   606
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * The following code can be used to dump the queue into a newly
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   608
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   610
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   612
     * Note that {@code toArray(new Object[0])} is identical in function to
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   613
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   624
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   629
            final Object[] items = this.items;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   630
            final int count = this.count;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   631
            final int firstLeg = Math.min(items.length - takeIndex, count);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   632
            if (a.length < count) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   633
                a = (T[]) Arrays.copyOfRange(items, takeIndex, takeIndex + count,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   634
                                             a.getClass());
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   635
            } else {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   636
                System.arraycopy(items, takeIndex, a, 0, firstLeg);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   637
                if (a.length > count)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   638
                    a[count] = null;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   639
            }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   640
            if (firstLeg < count)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   641
                System.arraycopy(items, 0, a, firstLeg, putIndex);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   642
            return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public String toString() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   649
        return Helpers.collectionToString(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        try {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   660
            int k;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   661
            if ((k = count) > 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   662
                circularClear(items, takeIndex, putIndex);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   663
                takeIndex = putIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   664
                count = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   665
                if (itrs != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   666
                    itrs.queueIsEmpty();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   667
                for (; k > 0 && lock.hasWaiters(notFull); k--)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   668
                    notFull.signal();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   669
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   676
     * Nulls out slots starting at array index i, upto index end.
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   677
     * Condition i == end means "full" - the entire array is cleared.
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   678
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   679
    private static void circularClear(Object[] items, int i, int end) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   680
        // assert 0 <= i && i < items.length;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   681
        // assert 0 <= end && end < items.length;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   682
        for (int to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   683
             ; i = 0, to = end) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   684
            for (; i < to; i++) items[i] = null;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   685
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   686
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   687
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   688
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   689
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    public int drainTo(Collection<? super E> c) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   696
        return drainTo(c, Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public int drainTo(Collection<? super E> c, int maxElements) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   706
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return 0;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   711
        final Object[] items = this.items;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   715
            int n = Math.min(maxElements, count);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   716
            int take = takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   717
            int i = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   718
            try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   719
                while (i < n) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   720
                    @SuppressWarnings("unchecked")
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   721
                    E e = (E) items[take];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   722
                    c.add(e);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   723
                    items[take] = null;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   724
                    if (++take == items.length) take = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   725
                    i++;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   726
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   727
                return n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   728
            } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   729
                // Restore invariants even if c.add() threw
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   730
                if (i > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   731
                    count -= i;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   732
                    takeIndex = take;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   733
                    if (itrs != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   734
                        if (count == 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   735
                            itrs.queueIsEmpty();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   736
                        else if (i > take)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   737
                            itrs.takeIndexWrapped();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   738
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   739
                    for (; i > 0 && lock.hasWaiters(notFull); i--)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   740
                        notFull.signal();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   741
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Returns an iterator over the elements in this queue in proper sequence.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   750
     * The elements will be returned in order from first (head) to last (tail).
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   751
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   752
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   753
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public Iterator<E> iterator() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   758
        return new Itr();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   762
     * Shared data between iterators and their queue, allowing queue
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   763
     * modifications to update iterators when elements are removed.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   764
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   765
     * This adds a lot of complexity for the sake of correctly
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   766
     * handling some uncommon operations, but the combination of
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   767
     * circular-arrays and supporting interior removes (i.e., those
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   768
     * not at head) would cause iterators to sometimes lose their
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   769
     * places and/or (re)report elements they shouldn't.  To avoid
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   770
     * this, when a queue has one or more iterators, it keeps iterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   771
     * state consistent by:
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   772
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   773
     * (1) keeping track of the number of "cycles", that is, the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   774
     *     number of times takeIndex has wrapped around to 0.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   775
     * (2) notifying all iterators via the callback removedAt whenever
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   776
     *     an interior element is removed (and thus other elements may
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   777
     *     be shifted).
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   778
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   779
     * These suffice to eliminate iterator inconsistencies, but
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   780
     * unfortunately add the secondary responsibility of maintaining
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   781
     * the list of iterators.  We track all active iterators in a
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   782
     * simple linked list (accessed only when the queue's lock is
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   783
     * held) of weak references to Itr.  The list is cleaned up using
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   784
     * 3 different mechanisms:
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   785
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   786
     * (1) Whenever a new iterator is created, do some O(1) checking for
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   787
     *     stale list elements.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   788
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   789
     * (2) Whenever takeIndex wraps around to 0, check for iterators
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   790
     *     that have been unused for more than one wrap-around cycle.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   791
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   792
     * (3) Whenever the queue becomes empty, all iterators are notified
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   793
     *     and this entire data structure is discarded.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   794
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   795
     * So in addition to the removedAt callback that is necessary for
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   796
     * correctness, iterators have the shutdown and takeIndexWrapped
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   797
     * callbacks that help remove stale iterators from the list.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   798
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   799
     * Whenever a list element is examined, it is expunged if either
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   800
     * the GC has determined that the iterator is discarded, or if the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   801
     * iterator reports that it is "detached" (does not need any
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   802
     * further state updates).  Overhead is maximal when takeIndex
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   803
     * never advances, iterators are discarded before they are
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   804
     * exhausted, and all removals are interior removes, in which case
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   805
     * all stale iterators are discovered by the GC.  But even in this
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   806
     * case we don't increase the amortized complexity.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   807
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   808
     * Care must be taken to keep list sweeping methods from
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   809
     * reentrantly invoking another such method, causing subtle
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   810
     * corruption bugs.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   811
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   812
    class Itrs {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   813
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   814
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   815
         * Node in a linked list of weak iterator references.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   816
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   817
        private class Node extends WeakReference<Itr> {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   818
            Node next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   819
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   820
            Node(Itr iterator, Node next) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   821
                super(iterator);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   822
                this.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   823
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   824
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   825
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   826
        /** Incremented whenever takeIndex wraps around to 0 */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   827
        int cycles;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   828
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   829
        /** Linked list of weak iterator references */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   830
        private Node head;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   831
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   832
        /** Used to expunge stale iterators */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   833
        private Node sweeper;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   834
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   835
        private static final int SHORT_SWEEP_PROBES = 4;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   836
        private static final int LONG_SWEEP_PROBES = 16;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   837
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   838
        Itrs(Itr initial) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   839
            register(initial);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   840
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   841
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   842
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   843
         * Sweeps itrs, looking for and expunging stale iterators.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   844
         * If at least one was found, tries harder to find more.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   845
         * Called only from iterating thread.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   846
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   847
         * @param tryHarder whether to start in try-harder mode, because
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   848
         * there is known to be at least one iterator to collect
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   849
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   850
        void doSomeSweeping(boolean tryHarder) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   851
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   852
            // assert head != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   853
            int probes = tryHarder ? LONG_SWEEP_PROBES : SHORT_SWEEP_PROBES;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   854
            Node o, p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   855
            final Node sweeper = this.sweeper;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   856
            boolean passedGo;   // to limit search to one full sweep
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   857
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   858
            if (sweeper == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   859
                o = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   860
                p = head;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   861
                passedGo = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   862
            } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   863
                o = sweeper;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   864
                p = o.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   865
                passedGo = false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   866
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   867
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   868
            for (; probes > 0; probes--) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   869
                if (p == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   870
                    if (passedGo)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   871
                        break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   872
                    o = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   873
                    p = head;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   874
                    passedGo = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   875
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   876
                final Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   877
                final Node next = p.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   878
                if (it == null || it.isDetached()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   879
                    // found a discarded/exhausted iterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   880
                    probes = LONG_SWEEP_PROBES; // "try harder"
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   881
                    // unlink p
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   882
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   883
                    p.next = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   884
                    if (o == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   885
                        head = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   886
                        if (next == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   887
                            // We've run out of iterators to track; retire
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   888
                            itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   889
                            return;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   890
                        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   891
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   892
                    else
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   893
                        o.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   894
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   895
                    o = p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   896
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   897
                p = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   898
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   899
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   900
            this.sweeper = (p == null) ? null : o;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   901
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   902
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   903
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   904
         * Adds a new iterator to the linked list of tracked iterators.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   905
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   906
        void register(Itr itr) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   907
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   908
            head = new Node(itr, head);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   909
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   910
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   911
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   912
         * Called whenever takeIndex wraps around to 0.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   913
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   914
         * Notifies all iterators, and expunges any that are now stale.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   915
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   916
        void takeIndexWrapped() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   917
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   918
            cycles++;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   919
            for (Node o = null, p = head; p != null;) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   920
                final Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   921
                final Node next = p.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   922
                if (it == null || it.takeIndexWrapped()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   923
                    // unlink p
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   924
                    // assert it == null || it.isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   925
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   926
                    p.next = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   927
                    if (o == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   928
                        head = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   929
                    else
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   930
                        o.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   931
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   932
                    o = p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   933
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   934
                p = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   935
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   936
            if (head == null)   // no more iterators to track
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   937
                itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   938
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   939
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   940
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19428
diff changeset
   941
         * Called whenever an interior remove (not at takeIndex) occurred.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   942
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   943
         * Notifies all iterators, and expunges any that are now stale.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   944
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   945
        void removedAt(int removedIndex) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   946
            for (Node o = null, p = head; p != null;) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   947
                final Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   948
                final Node next = p.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   949
                if (it == null || it.removedAt(removedIndex)) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   950
                    // unlink p
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   951
                    // assert it == null || it.isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   952
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   953
                    p.next = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   954
                    if (o == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   955
                        head = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   956
                    else
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   957
                        o.next = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   958
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   959
                    o = p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   960
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   961
                p = next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   962
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   963
            if (head == null)   // no more iterators to track
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   964
                itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   965
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   966
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   967
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   968
         * Called whenever the queue becomes empty.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   969
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   970
         * Notifies all active iterators that the queue is empty,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   971
         * clears all weak refs, and unlinks the itrs datastructure.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   972
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   973
        void queueIsEmpty() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   974
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   975
            for (Node p = head; p != null; p = p.next) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   976
                Itr it = p.get();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   977
                if (it != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   978
                    p.clear();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   979
                    it.shutdown();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   980
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   981
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   982
            head = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   983
            itrs = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   984
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   985
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   986
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   987
         * Called whenever an element has been dequeued (at takeIndex).
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   988
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   989
        void elementDequeued() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
   990
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   991
            if (count == 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   992
                queueIsEmpty();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   993
            else if (takeIndex == 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   994
                takeIndexWrapped();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   995
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   996
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   997
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   998
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
   999
     * Iterator for ArrayBlockingQueue.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1000
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1001
     * To maintain weak consistency with respect to puts and takes, we
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1002
     * read ahead one slot, so as to not report hasNext true but then
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1003
     * not have an element to return.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1004
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1005
     * We switch into "detached" mode (allowing prompt unlinking from
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1006
     * itrs without help from the GC) when all indices are negative, or
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1007
     * when hasNext returns false for the first time.  This allows the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1008
     * iterator to track concurrent updates completely accurately,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1009
     * except for the corner case of the user calling Iterator.remove()
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1010
     * after hasNext() returned false.  Even in this case, we ensure
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1011
     * that we don't remove the wrong element by keeping track of the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1012
     * expected element to remove, in lastItem.  Yes, we may fail to
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1013
     * remove lastItem from the queue if it moved due to an interleaved
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1014
     * interior remove while in detached mode.
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1015
     *
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1016
     * Method forEachRemaining, added in Java 8, is treated similarly
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1017
     * to hasNext returning false, in that we switch to detached mode,
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1018
     * but we regard it as an even stronger request to "close" this
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1019
     * iteration, and don't bother supporting subsequent remove().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    private class Itr implements Iterator<E> {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1022
        /** Index to look for new nextItem; NONE at end */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1023
        private int cursor;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1024
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1025
        /** Element to be returned by next call to next(); null if none */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1026
        private E nextItem;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1027
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1028
        /** Index of nextItem; NONE if none, REMOVED if removed elsewhere */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1029
        private int nextIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1030
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1031
        /** Last element returned; null if none or not detached. */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1032
        private E lastItem;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1033
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1034
        /** Index of lastItem, NONE if none, REMOVED if removed elsewhere */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1035
        private int lastRet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1036
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1037
        /** Previous value of takeIndex, or DETACHED when detached */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1038
        private int prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1039
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1040
        /** Previous value of iters.cycles */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1041
        private int prevCycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1042
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1043
        /** Special index value indicating "not available" or "undefined" */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1044
        private static final int NONE = -1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1045
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1046
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1047
         * Special index value indicating "removed elsewhere", that is,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1048
         * removed by some operation other than a call to this.remove().
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1049
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1050
        private static final int REMOVED = -2;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1051
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1052
        /** Special value for prevTakeIndex indicating "detached mode" */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1053
        private static final int DETACHED = -3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        Itr() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1056
            lastRet = NONE;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1057
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1058
            lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1059
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1060
                if (count == 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1061
                    // assert itrs == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1062
                    cursor = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1063
                    nextIndex = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1064
                    prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1065
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1066
                    final int takeIndex = ArrayBlockingQueue.this.takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1067
                    prevTakeIndex = takeIndex;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1068
                    nextItem = itemAt(nextIndex = takeIndex);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1069
                    cursor = incCursor(takeIndex);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1070
                    if (itrs == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1071
                        itrs = new Itrs(this);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1072
                    } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1073
                        itrs.register(this); // in this order
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1074
                        itrs.doSomeSweeping(false);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1075
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1076
                    prevCycles = itrs.cycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1077
                    // assert takeIndex >= 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1078
                    // assert prevTakeIndex == takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1079
                    // assert nextIndex >= 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1080
                    // assert nextItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1081
                }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1082
            } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1083
                lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1087
        boolean isDetached() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1088
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1089
            return prevTakeIndex < 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1090
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1091
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1092
        private int incCursor(int index) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1093
            // assert lock.isHeldByCurrentThread();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1094
            if (++index == items.length) index = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1095
            if (index == putIndex) index = NONE;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1096
            return index;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1097
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1098
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1099
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1100
         * Returns true if index is invalidated by the given number of
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1101
         * dequeues, starting from prevTakeIndex.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1102
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1103
        private boolean invalidated(int index, int prevTakeIndex,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1104
                                    long dequeues, int length) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1105
            if (index < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1106
                return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1107
            int distance = index - prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1108
            if (distance < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1109
                distance += length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1110
            return dequeues > distance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1113
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1114
         * Adjusts indices to incorporate all dequeues since the last
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1115
         * operation on this iterator.  Call only from iterating thread.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1116
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1117
        private void incorporateDequeues() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1118
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1119
            // assert itrs != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1120
            // assert !isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1121
            // assert count > 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1122
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1123
            final int cycles = itrs.cycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1124
            final int takeIndex = ArrayBlockingQueue.this.takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1125
            final int prevCycles = this.prevCycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1126
            final int prevTakeIndex = this.prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1127
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1128
            if (cycles != prevCycles || takeIndex != prevTakeIndex) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1129
                final int len = items.length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1130
                // how far takeIndex has advanced since the previous
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1131
                // operation of this iterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1132
                long dequeues = (cycles - prevCycles) * len
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1133
                    + (takeIndex - prevTakeIndex);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1134
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1135
                // Check indices for invalidation
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1136
                if (invalidated(lastRet, prevTakeIndex, dequeues, len))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1137
                    lastRet = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1138
                if (invalidated(nextIndex, prevTakeIndex, dequeues, len))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1139
                    nextIndex = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1140
                if (invalidated(cursor, prevTakeIndex, dequeues, len))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1141
                    cursor = takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1142
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1143
                if (cursor < 0 && nextIndex < 0 && lastRet < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1144
                    detach();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1145
                else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1146
                    this.prevCycles = cycles;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1147
                    this.prevTakeIndex = takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1148
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1149
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1150
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1151
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1152
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1153
         * Called when itrs should stop tracking this iterator, either
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1154
         * because there are no more indices to update (cursor < 0 &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1155
         * nextIndex < 0 && lastRet < 0) or as a special exception, when
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1156
         * lastRet >= 0, because hasNext() is about to return false for the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1157
         * first time.  Call only from iterating thread.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1158
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1159
        private void detach() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1160
            // Switch to detached mode
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1161
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1162
            // assert cursor == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1163
            // assert nextIndex < 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1164
            // assert lastRet < 0 || nextItem == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1165
            // assert lastRet < 0 ^ lastItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1166
            if (prevTakeIndex >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1167
                // assert itrs != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1168
                prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1169
                // try to unlink from itrs (but not too hard)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1170
                itrs.doSomeSweeping(true);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1171
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1172
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1173
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1174
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1175
         * For performance reasons, we would like not to acquire a lock in
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1176
         * hasNext in the common case.  To allow for this, we only access
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1177
         * fields (i.e. nextItem) that are not modified by update operations
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1178
         * triggered by queue modifications.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1179
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1180
        public boolean hasNext() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1181
            if (nextItem != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1182
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1183
            noNext();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1184
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1185
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1186
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1187
        private void noNext() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1191
                // assert cursor == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1192
                // assert nextIndex == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1193
                if (!isDetached()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1194
                    // assert lastRet >= 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1195
                    incorporateDequeues(); // might update lastRet
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1196
                    if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1197
                        lastItem = itemAt(lastRet);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1198
                        // assert lastItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1199
                        detach();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1200
                    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1201
                }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1202
                // assert isDetached();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1203
                // assert lastRet < 0 ^ lastItem != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1209
        public E next() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1210
            final E e = nextItem;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1211
            if (e == null)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1212
                throw new NoSuchElementException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            try {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1216
                if (!isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1217
                    incorporateDequeues();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1218
                // assert nextIndex != NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1219
                // assert lastItem == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1220
                lastRet = nextIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1221
                final int cursor = this.cursor;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1222
                if (cursor >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1223
                    nextItem = itemAt(nextIndex = cursor);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1224
                    // assert nextItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1225
                    this.cursor = incCursor(cursor);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1226
                } else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1227
                    nextIndex = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1228
                    nextItem = null;
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43522
diff changeset
  1229
                    if (lastRet == REMOVED) detach();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1230
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1234
            return e;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1235
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1236
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1237
        public void forEachRemaining(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1238
            Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1239
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1240
            lock.lock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1241
            try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1242
                final E e = nextItem;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1243
                if (e == null) return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1244
                if (!isDetached())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1245
                    incorporateDequeues();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1246
                action.accept(e);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1247
                if (isDetached() || cursor < 0) return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1248
                final Object[] items = ArrayBlockingQueue.this.items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1249
                for (int i = cursor, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1250
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1251
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1252
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1253
                        action.accept(itemAt(items, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1254
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1255
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1256
            } finally {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1257
                // Calling forEachRemaining is a strong hint that this
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1258
                // iteration is surely over; supporting remove() after
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1259
                // forEachRemaining() is more trouble than it's worth
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1260
                cursor = nextIndex = lastRet = NONE;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1261
                nextItem = lastItem = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1262
                detach();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1263
                lock.unlock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1264
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1266
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1267
        public void remove() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1268
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1269
            lock.lock();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1270
            // assert lock.getHoldCount() == 1;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1271
            try {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1272
                if (!isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1273
                    incorporateDequeues(); // might update lastRet or detach
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1274
                final int lastRet = this.lastRet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1275
                this.lastRet = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1276
                if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1277
                    if (!isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1278
                        removeAt(lastRet);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1279
                    else {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1280
                        final E lastItem = this.lastItem;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1281
                        // assert lastItem != null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1282
                        this.lastItem = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1283
                        if (itemAt(lastRet) == lastItem)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1284
                            removeAt(lastRet);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1285
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1286
                } else if (lastRet == NONE)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1287
                    throw new IllegalStateException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1288
                // else lastRet == REMOVED and the last returned element was
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1289
                // previously asynchronously removed via an operation other
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1290
                // than this.remove(), so nothing to do.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1291
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1292
                if (cursor < 0 && nextIndex < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1293
                    detach();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1294
            } finally {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1295
                lock.unlock();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1296
                // assert lastRet == NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1297
                // assert lastItem == null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1298
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1299
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1300
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1301
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1302
         * Called to notify the iterator that the queue is empty, or that it
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1303
         * has fallen hopelessly behind, so that it should abandon any
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1304
         * further iteration, except possibly to return one more element
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1305
         * from next(), as promised by returning true from hasNext().
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1306
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1307
        void shutdown() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1308
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1309
            cursor = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1310
            if (nextIndex >= 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1311
                nextIndex = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1312
            if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1313
                lastRet = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1314
                lastItem = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1315
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1316
            prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1317
            // Don't set nextItem to null because we must continue to be
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1318
            // able to return it on next().
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1319
            //
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1320
            // Caller will unlink from itrs when convenient.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1321
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1322
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1323
        private int distance(int index, int prevTakeIndex, int length) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1324
            int distance = index - prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1325
            if (distance < 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1326
                distance += length;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1327
            return distance;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1328
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1329
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1330
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19428
diff changeset
  1331
         * Called whenever an interior remove (not at takeIndex) occurred.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1332
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1333
         * @return true if this iterator should be unlinked from itrs
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1334
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1335
        boolean removedAt(int removedIndex) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1336
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1337
            if (isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1338
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1339
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1340
            final int takeIndex = ArrayBlockingQueue.this.takeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1341
            final int prevTakeIndex = this.prevTakeIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1342
            final int len = items.length;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1343
            // distance from prevTakeIndex to removedIndex
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1344
            final int removedDistance =
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1345
                len * (itrs.cycles - this.prevCycles
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1346
                       + ((removedIndex < takeIndex) ? 1 : 0))
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1347
                + (removedIndex - prevTakeIndex);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1348
            // assert itrs.cycles - this.prevCycles >= 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1349
            // assert itrs.cycles - this.prevCycles <= 1;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1350
            // assert removedDistance > 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1351
            // assert removedIndex != takeIndex;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1352
            int cursor = this.cursor;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1353
            if (cursor >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1354
                int x = distance(cursor, prevTakeIndex, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1355
                if (x == removedDistance) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1356
                    if (cursor == putIndex)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1357
                        this.cursor = cursor = NONE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1358
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1359
                else if (x > removedDistance) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1360
                    // assert cursor != prevTakeIndex;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1361
                    this.cursor = cursor = dec(cursor, len);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1362
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1363
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1364
            int lastRet = this.lastRet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1365
            if (lastRet >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1366
                int x = distance(lastRet, prevTakeIndex, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1367
                if (x == removedDistance)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1368
                    this.lastRet = lastRet = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1369
                else if (x > removedDistance)
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1370
                    this.lastRet = lastRet = dec(lastRet, len);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1371
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1372
            int nextIndex = this.nextIndex;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1373
            if (nextIndex >= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1374
                int x = distance(nextIndex, prevTakeIndex, len);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1375
                if (x == removedDistance)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1376
                    this.nextIndex = nextIndex = REMOVED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1377
                else if (x > removedDistance)
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1378
                    this.nextIndex = nextIndex = dec(nextIndex, len);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1379
            }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1380
            if (cursor < 0 && nextIndex < 0 && lastRet < 0) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1381
                this.prevTakeIndex = DETACHED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1382
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1383
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1384
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1385
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1386
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1387
        /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1388
         * Called whenever takeIndex wraps around to zero.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1389
         *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1390
         * @return true if this iterator should be unlinked from itrs
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1391
         */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1392
        boolean takeIndexWrapped() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1393
            // assert lock.isHeldByCurrentThread();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1394
            if (isDetached())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1395
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1396
            if (itrs.cycles - prevCycles > 1) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1397
                // All the elements that existed at the time of the last
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1398
                // operation are gone, so abandon further iteration.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1399
                shutdown();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1400
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1401
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1402
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1403
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1404
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1405
//         /** Uncomment for debugging. */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1406
//         public String toString() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1407
//             return ("cursor=" + cursor + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1408
//                     "nextIndex=" + nextIndex + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1409
//                     "lastRet=" + lastRet + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1410
//                     "nextItem=" + nextItem + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1411
//                     "lastItem=" + lastItem + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1412
//                     "prevCycles=" + prevCycles + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1413
//                     "prevTakeIndex=" + prevTakeIndex + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1414
//                     "size()=" + size() + " " +
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1415
//                     "remainingCapacity()=" + remainingCapacity());
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1416
//         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1418
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1419
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1420
     * Returns a {@link Spliterator} over the elements in this queue.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1421
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1422
     * <p>The returned spliterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1423
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1424
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1425
     * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1426
     * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1427
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1428
     * @implNote
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1429
     * The {@code Spliterator} implements {@code trySplit} to permit limited
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1430
     * parallelism.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1431
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1432
     * @return a {@code Spliterator} over the elements in this queue
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1433
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1434
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1435
    public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1436
        return Spliterators.spliterator
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1437
            (this, (Spliterator.ORDERED |
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1438
                    Spliterator.NONNULL |
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1439
                    Spliterator.CONCURRENT));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 11279
diff changeset
  1440
    }
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1441
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1442
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1443
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1444
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1445
    public void forEach(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1446
        Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1447
        final ReentrantLock lock = this.lock;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1448
        lock.lock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1449
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1450
            if (count > 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1451
                final Object[] items = this.items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1452
                for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1453
                         to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1454
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1455
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1456
                        action.accept(itemAt(items, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1457
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1458
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1459
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1460
        } finally {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1461
            lock.unlock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1462
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1463
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1464
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1465
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1466
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1467
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1468
    public boolean removeIf(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1469
        Objects.requireNonNull(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1470
        return bulkRemove(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1471
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1472
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1473
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1474
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1475
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1476
    public boolean removeAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1477
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1478
        return bulkRemove(e -> c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1479
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1480
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1481
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1482
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1483
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1484
    public boolean retainAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1485
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1486
        return bulkRemove(e -> !c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1487
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1488
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1489
    /** Implementation of bulk remove methods. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1490
    private boolean bulkRemove(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1491
        final ReentrantLock lock = this.lock;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1492
        lock.lock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1493
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1494
            if (itrs == null) { // check for active iterators
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1495
                if (count > 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1496
                    final Object[] items = this.items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1497
                    // Optimize for initial run of survivors
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1498
                    for (int i = takeIndex, end = putIndex,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1499
                             to = (i < end) ? end : items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1500
                         ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1501
                        for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1502
                            if (filter.test(itemAt(items, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1503
                                return bulkRemoveModified(filter, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1504
                        if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1505
                    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1506
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1507
                return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1508
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1509
        } finally {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1510
            lock.unlock();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1511
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1512
        // Active iterators are too hairy!
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1513
        // Punting (for now) to the slow n^2 algorithm ...
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1514
        return super.removeIf(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1515
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1516
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1517
    // A tiny bit set implementation
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1518
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1519
    private static long[] nBits(int n) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1520
        return new long[((n - 1) >> 6) + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1521
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1522
    private static void setBit(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1523
        bits[i >> 6] |= 1L << i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1524
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1525
    private static boolean isClear(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1526
        return (bits[i >> 6] & (1L << i)) == 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1527
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1528
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1529
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1530
     * Returns circular distance from i to j, disambiguating i == j to
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1531
     * items.length; never returns 0.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1532
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1533
    private int distanceNonEmpty(int i, int j) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1534
        if ((j -= i) <= 0) j += items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1535
        return j;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1536
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1537
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1538
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1539
     * Helper for bulkRemove, in case of at least one deletion.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1540
     * Tolerate predicates that reentrantly access the collection for
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1541
     * read (but not write), so traverse once to find elements to
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1542
     * delete, a second pass to physically expunge.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1543
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1544
     * @param beg valid index of first element to be deleted
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1545
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1546
    private boolean bulkRemoveModified(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1547
        Predicate<? super E> filter, final int beg) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1548
        final Object[] es = items;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1549
        final int capacity = items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1550
        final int end = putIndex;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1551
        final long[] deathRow = nBits(distanceNonEmpty(beg, putIndex));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1552
        deathRow[0] = 1L;   // set bit 0
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1553
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1554
             ; i = 0, to = end, k -= capacity) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1555
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1556
                if (filter.test(itemAt(es, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1557
                    setBit(deathRow, i - k);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1558
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1559
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1560
        // a two-finger traversal, with hare i reading, tortoise w writing
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1561
        int w = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1562
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1563
             ; w = 0) { // w rejoins i on second leg
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1564
            // In this loop, i and w are on the same leg, with i > w
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1565
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1566
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1567
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1568
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1569
            // In this loop, w is on the first leg, i on the second
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1570
            for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1571
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1572
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1573
            if (i >= to) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1574
                if (w == capacity) w = 0; // "corner" case
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1575
                break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1576
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1577
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1578
        count -= distanceNonEmpty(w, end);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1579
        circularClear(es, putIndex = w, end);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1580
        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1581
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1582
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1583
    /** debugging */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1584
    void checkInvariants() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1585
        // meta-assertions
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1586
        // assert lock.isHeldByCurrentThread();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1587
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1588
            // Unlike ArrayDeque, we have a count field but no spare slot.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1589
            // We prefer ArrayDeque's strategy (and the names of its fields!),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1590
            // but our field layout is baked into the serial form, and so is
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1591
            // too annoying to change.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1592
            //
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1593
            // putIndex == takeIndex must be disambiguated by checking count.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1594
            int capacity = items.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1595
            // assert capacity > 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1596
            // assert takeIndex >= 0 && takeIndex < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1597
            // assert putIndex >= 0 && putIndex < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1598
            // assert count <= capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1599
            // assert takeIndex == putIndex || items[takeIndex] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1600
            // assert count == capacity || items[putIndex] == null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1601
            // assert takeIndex == putIndex || items[dec(putIndex, capacity)] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1602
        } catch (Throwable t) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1603
            System.err.printf("takeIndex=%d putIndex=%d count=%d capacity=%d%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1604
                              takeIndex, putIndex, count, items.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1605
            System.err.printf("items=%s%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1606
                              Arrays.toString(items));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1607
            throw t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1608
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1609
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 32991
diff changeset
  1610
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
}