author | darcy |
Wed, 16 Oct 2019 16:55:52 -0700 | |
changeset 58657 | 6252605fb005 |
parent 50764 | 5637aca18f1d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
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 | 34 |
*/ |
35 |
||
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 | 49 |
import java.util.function.Consumer; |
50 |
import java.util.function.Predicate; |
|
2 | 51 |
|
52 |
/** |
|
53 |
* A bounded {@linkplain BlockingQueue blocking queue} backed by an |
|
54 |
* array. This queue orders elements FIFO (first-in-first-out). The |
|
55 |
* <em>head</em> of the queue is that element that has been on the |
|
56 |
* queue the longest time. The <em>tail</em> of the queue is that |
|
57 |
* element that has been on the queue the shortest time. New elements |
|
58 |
* are inserted at the tail of the queue, and the queue retrieval |
|
59 |
* operations obtain elements at the head of the queue. |
|
60 |
* |
|
61 |
* <p>This is a classic "bounded buffer", in which a |
|
62 |
* fixed-sized array holds elements inserted by producers and |
|
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 | 66 |
* element from an empty queue will similarly block. |
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 | 69 |
* waiting producer and consumer threads. By default, this ordering |
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 | 72 |
* generally decreases throughput but reduces variability and avoids |
73 |
* starvation. |
|
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 | 77 |
* |
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 | 80 |
* Java Collections Framework</a>. |
81 |
* |
|
82 |
* @since 1.5 |
|
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 | 85 |
*/ |
86 |
public class ArrayBlockingQueue<E> extends AbstractQueue<E> |
|
87 |
implements BlockingQueue<E>, java.io.Serializable { |
|
88 |
||
42319 | 89 |
/* |
90 |
* Much of the implementation mechanics, especially the unusual |
|
91 |
* nested loops, are shared and co-maintained with ArrayDeque. |
|
92 |
*/ |
|
93 |
||
2 | 94 |
/** |
95 |
* Serialization ID. This class relies on default serialization |
|
96 |
* even for the items array, which is default-serialized, even if |
|
97 |
* it is empty. Otherwise it could not be declared final, which is |
|
98 |
* necessary here. |
|
99 |
*/ |
|
100 |
private static final long serialVersionUID = -817911632652898426L; |
|
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 | 114 |
|
115 |
/* |
|
116 |
* Concurrency control uses the classic two-condition algorithm |
|
117 |
* found in any textbook. |
|
118 |
*/ |
|
119 |
||
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 | 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 | 125 |
private final Condition notEmpty; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
126 |
|
2 | 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 | 129 |
private final Condition notFull; |
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 | 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 | 141 |
* Increments i, mod modulus. |
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 | 144 |
static final int inc(int i, int modulus) { |
145 |
if (++i >= modulus) i = 0; |
|
146 |
return i; |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* Decrements i, mod modulus. |
|
151 |
* Precondition and postcondition: 0 <= i < modulus. |
|
152 |
*/ |
|
153 |
static final int dec(int i, int modulus) { |
|
154 |
if (--i < 0) i = modulus - 1; |
|
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 | 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 | 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 | 167 |
* Returns element at array index i. |
168 |
* This is a slight abuse of generics, accepted by javac. |
|
169 |
*/ |
|
170 |
@SuppressWarnings("unchecked") |
|
171 |
static <E> E itemAt(Object[] items, int i) { |
|
172 |
return (E) items[i]; |
|
173 |
} |
|
174 |
||
175 |
/** |
|
2 | 176 |
* Inserts element at current put position, advances, and signals. |
177 |
* Call only when holding lock. |
|
178 |
*/ |
|
42319 | 179 |
private void enqueue(E e) { |
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 | 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 | 187 |
notEmpty.signal(); |
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* Extracts element at current take position, advances, and signals. |
|
192 |
* Call only when holding lock. |
|
193 |
*/ |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
194 |
private E dequeue() { |
42319 | 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 | 199 |
@SuppressWarnings("unchecked") |
42319 | 200 |
E e = (E) items[takeIndex]; |
2 | 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 | 206 |
notFull.signal(); |
42319 | 207 |
return e; |
2 | 208 |
} |
209 |
||
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 | 213 |
* Call only when holding lock. |
214 |
*/ |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
215 |
void removeAt(final int removeIndex) { |
42319 | 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 | 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 | 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 | 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 | 238 |
break; |
239 |
} |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
240 |
items[pred] = items[i]; |
2 | 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 | 245 |
} |
246 |
notFull.signal(); |
|
247 |
} |
|
248 |
||
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 | 251 |
* capacity and default access policy. |
252 |
* |
|
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 | 255 |
*/ |
256 |
public ArrayBlockingQueue(int capacity) { |
|
257 |
this(capacity, false); |
|
258 |
} |
|
259 |
||
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 | 262 |
* capacity and the specified access policy. |
263 |
* |
|
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 | 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 | 269 |
*/ |
270 |
public ArrayBlockingQueue(int capacity, boolean fair) { |
|
271 |
if (capacity <= 0) |
|
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 | 274 |
lock = new ReentrantLock(fair); |
275 |
notEmpty = lock.newCondition(); |
|
276 |
notFull = lock.newCondition(); |
|
277 |
} |
|
278 |
||
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 | 281 |
* capacity, the specified access policy and initially containing the |
282 |
* elements of the given collection, |
|
283 |
* added in traversal order of the collection's iterator. |
|
284 |
* |
|
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 | 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 | 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 | 292 |
* @throws NullPointerException if the specified collection or any |
293 |
* of its elements are null |
|
294 |
*/ |
|
295 |
public ArrayBlockingQueue(int capacity, boolean fair, |
|
296 |
Collection<? extends E> c) { |
|
297 |
this(capacity, fair); |
|
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 | 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 | 315 |
} |
316 |
||
317 |
/** |
|
318 |
* Inserts the specified element at the tail of this queue if it is |
|
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 | 322 |
* |
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 | 325 |
* @throws IllegalStateException if this queue is full |
326 |
* @throws NullPointerException if the specified element is null |
|
327 |
*/ |
|
328 |
public boolean add(E e) { |
|
329 |
return super.add(e); |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* Inserts the specified element at the tail of this queue if it is |
|
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 | 336 |
* is full. This method is generally preferable to method {@link #add}, |
337 |
* which can fail to insert an element only by throwing an exception. |
|
338 |
* |
|
339 |
* @throws NullPointerException if the specified element is null |
|
340 |
*/ |
|
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 | 343 |
final ReentrantLock lock = this.lock; |
344 |
lock.lock(); |
|
345 |
try { |
|
346 |
if (count == items.length) |
|
347 |
return false; |
|
348 |
else { |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
349 |
enqueue(e); |
2 | 350 |
return true; |
351 |
} |
|
352 |
} finally { |
|
353 |
lock.unlock(); |
|
354 |
} |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Inserts the specified element at the tail of this queue, waiting |
|
359 |
* for space to become available if the queue is full. |
|
360 |
* |
|
361 |
* @throws InterruptedException {@inheritDoc} |
|
362 |
* @throws NullPointerException {@inheritDoc} |
|
363 |
*/ |
|
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 | 366 |
final ReentrantLock lock = this.lock; |
367 |
lock.lockInterruptibly(); |
|
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 | 372 |
} finally { |
373 |
lock.unlock(); |
|
374 |
} |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Inserts the specified element at the tail of this queue, waiting |
|
379 |
* up to the specified wait time for space to become available if |
|
380 |
* the queue is full. |
|
381 |
* |
|
382 |
* @throws InterruptedException {@inheritDoc} |
|
383 |
* @throws NullPointerException {@inheritDoc} |
|
384 |
*/ |
|
385 |
public boolean offer(E e, long timeout, TimeUnit unit) |
|
386 |
throws InterruptedException { |
|
387 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
388 |
Objects.requireNonNull(e); |
2 | 389 |
long nanos = unit.toNanos(timeout); |
390 |
final ReentrantLock lock = this.lock; |
|
391 |
lock.lockInterruptibly(); |
|
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 | 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 | 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 | 400 |
} finally { |
401 |
lock.unlock(); |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
public E poll() { |
|
406 |
final ReentrantLock lock = this.lock; |
|
407 |
lock.lock(); |
|
408 |
try { |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
409 |
return (count == 0) ? null : dequeue(); |
2 | 410 |
} finally { |
411 |
lock.unlock(); |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
public E take() throws InterruptedException { |
|
416 |
final ReentrantLock lock = this.lock; |
|
417 |
lock.lockInterruptibly(); |
|
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 | 422 |
} finally { |
423 |
lock.unlock(); |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
|
428 |
long nanos = unit.toNanos(timeout); |
|
429 |
final ReentrantLock lock = this.lock; |
|
430 |
lock.lockInterruptibly(); |
|
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 | 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 | 436 |
} |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
437 |
return dequeue(); |
2 | 438 |
} finally { |
439 |
lock.unlock(); |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
public E peek() { |
|
444 |
final ReentrantLock lock = this.lock; |
|
445 |
lock.lock(); |
|
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 | 448 |
} finally { |
449 |
lock.unlock(); |
|
450 |
} |
|
451 |
} |
|
452 |
||
453 |
// this doc comment is overridden to remove the reference to collections |
|
454 |
// greater in size than Integer.MAX_VALUE |
|
455 |
/** |
|
456 |
* Returns the number of elements in this queue. |
|
457 |
* |
|
458 |
* @return the number of elements in this queue |
|
459 |
*/ |
|
460 |
public int size() { |
|
461 |
final ReentrantLock lock = this.lock; |
|
462 |
lock.lock(); |
|
463 |
try { |
|
464 |
return count; |
|
465 |
} finally { |
|
466 |
lock.unlock(); |
|
467 |
} |
|
468 |
} |
|
469 |
||
470 |
// this doc comment is a modified copy of the inherited doc comment, |
|
471 |
// without the reference to unlimited queues. |
|
472 |
/** |
|
473 |
* Returns the number of additional elements that this queue can ideally |
|
474 |
* (in the absence of memory or resource constraints) accept without |
|
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 | 477 |
* |
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 | 480 |
* because it may be the case that another thread is about to |
481 |
* insert or remove an element. |
|
482 |
*/ |
|
483 |
public int remainingCapacity() { |
|
484 |
final ReentrantLock lock = this.lock; |
|
485 |
lock.lock(); |
|
486 |
try { |
|
487 |
return items.length - count; |
|
488 |
} finally { |
|
489 |
lock.unlock(); |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
/** |
|
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 | 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 | 499 |
* (or equivalently, if this queue changed as a result of the call). |
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 | 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 | 509 |
*/ |
510 |
public boolean remove(Object o) { |
|
511 |
if (o == null) return false; |
|
512 |
final ReentrantLock lock = this.lock; |
|
513 |
lock.lock(); |
|
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 | 517 |
for (int i = takeIndex, end = putIndex, |
518 |
to = (i < end) ? end : items.length; |
|
519 |
; i = 0, to = end) { |
|
520 |
for (; i < to; i++) |
|
521 |
if (o.equals(items[i])) { |
|
522 |
removeAt(i); |
|
523 |
return true; |
|
524 |
} |
|
525 |
if (to == end) break; |
|
526 |
} |
|
2 | 527 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
528 |
return false; |
2 | 529 |
} finally { |
530 |
lock.unlock(); |
|
531 |
} |
|
532 |
} |
|
533 |
||
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 | 538 |
* |
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 | 541 |
*/ |
542 |
public boolean contains(Object o) { |
|
543 |
if (o == null) return false; |
|
544 |
final ReentrantLock lock = this.lock; |
|
545 |
lock.lock(); |
|
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 | 549 |
for (int i = takeIndex, end = putIndex, |
550 |
to = (i < end) ? end : items.length; |
|
551 |
; i = 0, to = end) { |
|
552 |
for (; i < to; i++) |
|
553 |
if (o.equals(items[i])) |
|
554 |
return true; |
|
555 |
if (to == end) break; |
|
556 |
} |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
557 |
} |
2 | 558 |
return false; |
559 |
} finally { |
|
560 |
lock.unlock(); |
|
561 |
} |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* Returns an array containing all of the elements in this queue, in |
|
566 |
* proper sequence. |
|
567 |
* |
|
568 |
* <p>The returned array will be "safe" in that no references to it are |
|
569 |
* maintained by this queue. (In other words, this method must allocate |
|
570 |
* a new array). The caller is thus free to modify the returned array. |
|
571 |
* |
|
572 |
* <p>This method acts as bridge between array-based and collection-based |
|
573 |
* APIs. |
|
574 |
* |
|
575 |
* @return an array containing all of the elements in this queue |
|
576 |
*/ |
|
577 |
public Object[] toArray() { |
|
578 |
final ReentrantLock lock = this.lock; |
|
579 |
lock.lock(); |
|
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 | 587 |
} finally { |
588 |
lock.unlock(); |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
/** |
|
593 |
* Returns an array containing all of the elements in this queue, in |
|
594 |
* proper sequence; the runtime type of the returned array is that of |
|
595 |
* the specified array. If the queue fits in the specified array, it |
|
596 |
* is returned therein. Otherwise, a new array is allocated with the |
|
597 |
* runtime type of the specified array and the size of this queue. |
|
598 |
* |
|
599 |
* <p>If this queue fits in the specified array with room to spare |
|
600 |
* (i.e., the array has more elements than this queue), the element in |
|
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 | 603 |
* |
604 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
605 |
* array-based and collection-based APIs. Further, this method allows |
|
606 |
* precise control over the runtime type of the output array, and may, |
|
607 |
* under certain circumstances, be used to save allocation costs. |
|
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 | 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 | 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 | 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 | 617 |
* |
618 |
* @param a the array into which the elements of the queue are to |
|
619 |
* be stored, if it is big enough; otherwise, a new array of the |
|
620 |
* same runtime type is allocated for this purpose |
|
621 |
* @return an array containing all of the elements in this queue |
|
622 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
623 |
* is not a supertype of the runtime type of every element in |
|
624 |
* this queue |
|
625 |
* @throws NullPointerException if the specified array is null |
|
626 |
*/ |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
627 |
@SuppressWarnings("unchecked") |
2 | 628 |
public <T> T[] toArray(T[] a) { |
629 |
final ReentrantLock lock = this.lock; |
|
630 |
lock.lock(); |
|
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 | 646 |
} finally { |
647 |
lock.unlock(); |
|
648 |
} |
|
649 |
} |
|
650 |
||
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 | 653 |
} |
654 |
||
655 |
/** |
|
656 |
* Atomically removes all of the elements from this queue. |
|
657 |
* The queue will be empty after this call returns. |
|
658 |
*/ |
|
659 |
public void clear() { |
|
660 |
final ReentrantLock lock = this.lock; |
|
661 |
lock.lock(); |
|
662 |
try { |
|
42319 | 663 |
int k; |
664 |
if ((k = count) > 0) { |
|
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 | 673 |
} finally { |
674 |
lock.unlock(); |
|
675 |
} |
|
676 |
} |
|
677 |
||
678 |
/** |
|
42319 | 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 | 681 |
*/ |
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 | 685 |
for (int to = (i < end) ? end : items.length; |
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 | 688 |
if (to == end) break; |
689 |
} |
|
690 |
} |
|
691 |
||
692 |
/** |
|
2 | 693 |
* @throws UnsupportedOperationException {@inheritDoc} |
694 |
* @throws ClassCastException {@inheritDoc} |
|
695 |
* @throws NullPointerException {@inheritDoc} |
|
696 |
* @throws IllegalArgumentException {@inheritDoc} |
|
697 |
*/ |
|
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 | 700 |
} |
701 |
||
702 |
/** |
|
703 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
704 |
* @throws ClassCastException {@inheritDoc} |
|
705 |
* @throws NullPointerException {@inheritDoc} |
|
706 |
* @throws IllegalArgumentException {@inheritDoc} |
|
707 |
*/ |
|
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 | 710 |
if (c == this) |
711 |
throw new IllegalArgumentException(); |
|
712 |
if (maxElements <= 0) |
|
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 | 715 |
final ReentrantLock lock = this.lock; |
716 |
lock.lock(); |
|
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 | 724 |
E e = (E) items[take]; |
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 | 745 |
} |
746 |
} finally { |
|
747 |
lock.unlock(); |
|
748 |
} |
|
749 |
} |
|
750 |
||
751 |
/** |
|
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 | 757 |
* |
758 |
* @return an iterator over the elements in this queue in proper sequence |
|
759 |
*/ |
|
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 | 762 |
} |
763 |
||
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 | 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 | 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 | 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 | 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 | 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 | 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 | 1023 |
*/ |
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 | 1057 |
|
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 | 1087 |
} |
1088 |
} |
|
1089 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
1090 |
boolean isDetached() { |
42319 | 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 | 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 | 1114 |
} |
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 | 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 | 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 | 1191 |
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
1192 |
lock.lock(); |
|
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 | 1207 |
} finally { |
1208 |
lock.unlock(); |
|
1209 |
} |
|
1210 |
} |
|
1211 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
1212 |
public E next() { |
42319 | 1213 |
final E e = nextItem; |
1214 |
if (e == null) |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
1215 |
throw new NoSuchElementException(); |
2 | 1216 |
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
1217 |
lock.lock(); |
|
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 | 1234 |
} finally { |
1235 |
lock.unlock(); |
|
1236 |
} |
|
42319 | 1237 |
return e; |
1238 |
} |
|
1239 |
||
1240 |
public void forEachRemaining(Consumer<? super E> action) { |
|
1241 |
Objects.requireNonNull(action); |
|
1242 |
final ReentrantLock lock = ArrayBlockingQueue.this.lock; |
|
1243 |
lock.lock(); |
|
1244 |
try { |
|
1245 |
final E e = nextItem; |
|
1246 |
if (e == null) return; |
|
1247 |
if (!isDetached()) |
|
1248 |
incorporateDequeues(); |
|
1249 |
action.accept(e); |
|
1250 |
if (isDetached() || cursor < 0) return; |
|
1251 |
final Object[] items = ArrayBlockingQueue.this.items; |
|
1252 |
for (int i = cursor, end = putIndex, |
|
1253 |
to = (i < end) ? end : items.length; |
|
1254 |
; i = 0, to = end) { |
|
1255 |
for (; i < to; i++) |
|
1256 |
action.accept(itemAt(items, i)); |
|
1257 |
if (to == end) break; |
|
1258 |
} |
|
1259 |
} finally { |
|
1260 |
// Calling forEachRemaining is a strong hint that this |
|
1261 |
// iteration is surely over; supporting remove() after |
|
1262 |
// forEachRemaining() is more trouble than it's worth |
|
1263 |
cursor = nextIndex = lastRet = NONE; |
|
1264 |
nextItem = lastItem = null; |
|
1265 |
detach(); |
|
1266 |
lock.unlock(); |
|
1267 |
} |
|
2 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 1448 |
public void forEach(Consumer<? super E> action) { |
1449 |
Objects.requireNonNull(action); |
|
1450 |
final ReentrantLock lock = this.lock; |
|
1451 |
lock.lock(); |
|
1452 |
try { |
|
1453 |
if (count > 0) { |
|
1454 |
final Object[] items = this.items; |
|
1455 |
for (int i = takeIndex, end = putIndex, |
|
1456 |
to = (i < end) ? end : items.length; |
|
1457 |
; i = 0, to = end) { |
|
1458 |
for (; i < to; i++) |
|
1459 |
action.accept(itemAt(items, i)); |
|
1460 |
if (to == end) break; |
|
1461 |
} |
|
1462 |
} |
|
1463 |
} finally { |
|
1464 |
lock.unlock(); |
|
1465 |
} |
|
1466 |
} |
|
1467 |
||
1468 |
/** |
|
1469 |
* @throws NullPointerException {@inheritDoc} |
|
1470 |
*/ |
|
1471 |
public boolean removeIf(Predicate<? super E> filter) { |
|
1472 |
Objects.requireNonNull(filter); |
|
1473 |
return bulkRemove(filter); |
|
1474 |
} |
|
1475 |
||
1476 |
/** |
|
1477 |
* @throws NullPointerException {@inheritDoc} |
|
1478 |
*/ |
|
1479 |
public boolean removeAll(Collection<?> c) { |
|
1480 |
Objects.requireNonNull(c); |
|
1481 |
return bulkRemove(e -> c.contains(e)); |
|
1482 |
} |
|
1483 |
||
1484 |
/** |
|
1485 |
* @throws NullPointerException {@inheritDoc} |
|
1486 |
*/ |
|
1487 |
public boolean retainAll(Collection<?> c) { |
|
1488 |
Objects.requireNonNull(c); |
|
1489 |
return bulkRemove(e -> !c.contains(e)); |
|
1490 |
} |
|
1491 |
||
1492 |
/** Implementation of bulk remove methods. */ |
|
1493 |
private boolean bulkRemove(Predicate<? super E> filter) { |
|
1494 |
final ReentrantLock lock = this.lock; |
|
1495 |
lock.lock(); |
|
1496 |
try { |
|
1497 |
if (itrs == null) { // check for active iterators |
|
1498 |
if (count > 0) { |
|
1499 |
final Object[] items = this.items; |
|
1500 |
// Optimize for initial run of survivors |
|
1501 |
for (int i = takeIndex, end = putIndex, |
|
1502 |
to = (i < end) ? end : items.length; |
|
1503 |
; i = 0, to = end) { |
|
1504 |
for (; i < to; i++) |
|
1505 |
if (filter.test(itemAt(items, i))) |
|
1506 |
return bulkRemoveModified(filter, i); |
|
1507 |
if (to == end) break; |
|
1508 |
} |
|
1509 |
} |
|
1510 |
return false; |
|
1511 |
} |
|
1512 |
} finally { |
|
1513 |
lock.unlock(); |
|
1514 |
} |
|
1515 |
// Active iterators are too hairy! |
|
1516 |
// Punting (for now) to the slow n^2 algorithm ... |
|
1517 |
return super.removeIf(filter); |
|
1518 |
} |
|
1519 |
||
1520 |
// A tiny bit set implementation |
|
1521 |
||
1522 |
private static long[] nBits(int n) { |
|
1523 |
return new long[((n - 1) >> 6) + 1]; |
|
1524 |
} |
|
1525 |
private static void setBit(long[] bits, int i) { |
|
1526 |
bits[i >> 6] |= 1L << i; |
|
1527 |
} |
|
1528 |
private static boolean isClear(long[] bits, int i) { |
|
1529 |
return (bits[i >> 6] & (1L << i)) == 0; |
|
1530 |
} |
|
1531 |
||
1532 |
/** |
|
1533 |
* Returns circular distance from i to j, disambiguating i == j to |
|
1534 |
* items.length; never returns 0. |
|
1535 |
*/ |
|
1536 |
private int distanceNonEmpty(int i, int j) { |
|
1537 |
if ((j -= i) <= 0) j += items.length; |
|
1538 |
return j; |
|
1539 |
} |
|
1540 |
||
1541 |
/** |
|
1542 |
* Helper for bulkRemove, in case of at least one deletion. |
|
1543 |
* Tolerate predicates that reentrantly access the collection for |
|
1544 |
* read (but not write), so traverse once to find elements to |
|
1545 |
* delete, a second pass to physically expunge. |
|
1546 |
* |
|
1547 |
* @param beg valid index of first element to be deleted |
|
1548 |
*/ |
|
1549 |
private boolean bulkRemoveModified( |
|
1550 |
Predicate<? super E> filter, final int beg) { |
|
1551 |
final Object[] es = items; |
|
1552 |
final int capacity = items.length; |
|
1553 |
final int end = putIndex; |
|
1554 |
final long[] deathRow = nBits(distanceNonEmpty(beg, putIndex)); |
|
1555 |
deathRow[0] = 1L; // set bit 0 |
|
1556 |
for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg; |
|
1557 |
; i = 0, to = end, k -= capacity) { |
|
1558 |
for (; i < to; i++) |
|
1559 |
if (filter.test(itemAt(es, i))) |
|
1560 |
setBit(deathRow, i - k); |
|
1561 |
if (to == end) break; |
|
1562 |
} |
|
1563 |
// a two-finger traversal, with hare i reading, tortoise w writing |
|
1564 |
int w = beg; |
|
1565 |
for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg; |
|
1566 |
; w = 0) { // w rejoins i on second leg |
|
1567 |
// In this loop, i and w are on the same leg, with i > w |
|
1568 |
for (; i < to; i++) |
|
1569 |
if (isClear(deathRow, i - k)) |
|
1570 |
es[w++] = es[i]; |
|
1571 |
if (to == end) break; |
|
1572 |
// In this loop, w is on the first leg, i on the second |
|
1573 |
for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++) |
|
1574 |
if (isClear(deathRow, i - k)) |
|
1575 |
es[w++] = es[i]; |
|
1576 |
if (i >= to) { |
|
1577 |
if (w == capacity) w = 0; // "corner" case |
|
1578 |
break; |
|
1579 |
} |
|
1580 |
} |
|
1581 |
count -= distanceNonEmpty(w, end); |
|
1582 |
circularClear(es, putIndex = w, end); |
|
1583 |
return true; |
|
1584 |
} |
|
1585 |
||
1586 |
/** debugging */ |
|
1587 |
void checkInvariants() { |
|
1588 |
// meta-assertions |
|
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 | 1597 |
} |
1598 |
} |
|
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 | 1638 |
} |