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