author | dl |
Tue, 16 Jun 2015 13:13:05 +0200 | |
changeset 31151 | 5535c077def0 |
parent 25859 | 3317bb8137f4 |
child 32991 | b27c76b82713 |
permissions | -rw-r--r-- |
4110 | 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 |
4110 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
4110 | 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. |
|
4110 | 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:
8544
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
4110 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
38 |
import java.util.AbstractQueue; |
|
39 |
import java.util.Collection; |
|
40 |
import java.util.Iterator; |
|
41 |
import java.util.NoSuchElementException; |
|
42 |
import java.util.Queue; |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
43 |
import java.util.concurrent.TimeUnit; |
4110 | 44 |
import java.util.concurrent.locks.LockSupport; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
45 |
import java.util.Spliterator; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
46 |
import java.util.Spliterators; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
47 |
import java.util.function.Consumer; |
6543 | 48 |
|
4110 | 49 |
/** |
50 |
* An unbounded {@link TransferQueue} based on linked nodes. |
|
51 |
* This queue orders elements FIFO (first-in-first-out) with respect |
|
52 |
* to any given producer. The <em>head</em> of the queue is that |
|
53 |
* element that has been on the queue the longest time for some |
|
54 |
* producer. The <em>tail</em> of the queue is that element that has |
|
55 |
* been on the queue the shortest time for some producer. |
|
56 |
* |
|
9515
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
57 |
* <p>Beware that, unlike in most collections, the {@code size} method |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
58 |
* is <em>NOT</em> a constant-time operation. Because of the |
4110 | 59 |
* asynchronous nature of these queues, determining the current number |
9515
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
60 |
* of elements requires a traversal of the elements, and so may report |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
61 |
* inaccurate results if this collection is modified during traversal. |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
62 |
* Additionally, the bulk operations {@code addAll}, |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
63 |
* {@code removeAll}, {@code retainAll}, {@code containsAll}, |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
64 |
* {@code equals}, and {@code toArray} are <em>not</em> guaranteed |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
65 |
* to be performed atomically. For example, an iterator operating |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
66 |
* concurrently with an {@code addAll} operation might view only some |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
67 |
* of the added elements. |
4110 | 68 |
* |
69 |
* <p>This class and its iterator implement all of the |
|
70 |
* <em>optional</em> methods of the {@link Collection} and {@link |
|
71 |
* Iterator} interfaces. |
|
72 |
* |
|
73 |
* <p>Memory consistency effects: As with other concurrent |
|
74 |
* collections, actions in a thread prior to placing an object into a |
|
75 |
* {@code LinkedTransferQueue} |
|
76 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
|
77 |
* actions subsequent to the access or removal of that element from |
|
78 |
* the {@code LinkedTransferQueue} in another thread. |
|
79 |
* |
|
80 |
* <p>This class is a member of the |
|
81 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
82 |
* Java Collections Framework</a>. |
|
83 |
* |
|
84 |
* @since 1.7 |
|
85 |
* @author Doug Lea |
|
86 |
* @param <E> the type of elements held in this collection |
|
87 |
*/ |
|
88 |
public class LinkedTransferQueue<E> extends AbstractQueue<E> |
|
89 |
implements TransferQueue<E>, java.io.Serializable { |
|
90 |
private static final long serialVersionUID = -3223113410248163686L; |
|
91 |
||
92 |
/* |
|
93 |
* *** Overview of Dual Queues with Slack *** |
|
94 |
* |
|
95 |
* Dual Queues, introduced by Scherer and Scott |
|
96 |
* (http://www.cs.rice.edu/~wns1/papers/2004-DISC-DDS.pdf) are |
|
97 |
* (linked) queues in which nodes may represent either data or |
|
98 |
* requests. When a thread tries to enqueue a data node, but |
|
99 |
* encounters a request node, it instead "matches" and removes it; |
|
100 |
* and vice versa for enqueuing requests. Blocking Dual Queues |
|
101 |
* arrange that threads enqueuing unmatched requests block until |
|
102 |
* other threads provide the match. Dual Synchronous Queues (see |
|
103 |
* Scherer, Lea, & Scott |
|
104 |
* http://www.cs.rochester.edu/u/scott/papers/2009_Scherer_CACM_SSQ.pdf) |
|
105 |
* additionally arrange that threads enqueuing unmatched data also |
|
106 |
* block. Dual Transfer Queues support all of these modes, as |
|
107 |
* dictated by callers. |
|
108 |
* |
|
109 |
* A FIFO dual queue may be implemented using a variation of the |
|
110 |
* Michael & Scott (M&S) lock-free queue algorithm |
|
111 |
* (http://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf). |
|
112 |
* It maintains two pointer fields, "head", pointing to a |
|
113 |
* (matched) node that in turn points to the first actual |
|
114 |
* (unmatched) queue node (or null if empty); and "tail" that |
|
115 |
* points to the last node on the queue (or again null if |
|
116 |
* empty). For example, here is a possible queue with four data |
|
117 |
* elements: |
|
118 |
* |
|
119 |
* head tail |
|
120 |
* | | |
|
121 |
* v v |
|
122 |
* M -> U -> U -> U -> U |
|
123 |
* |
|
124 |
* The M&S queue algorithm is known to be prone to scalability and |
|
125 |
* overhead limitations when maintaining (via CAS) these head and |
|
126 |
* tail pointers. This has led to the development of |
|
127 |
* contention-reducing variants such as elimination arrays (see |
|
128 |
* Moir et al http://portal.acm.org/citation.cfm?id=1074013) and |
|
129 |
* optimistic back pointers (see Ladan-Mozes & Shavit |
|
130 |
* http://people.csail.mit.edu/edya/publications/OptimisticFIFOQueue-journal.pdf). |
|
131 |
* However, the nature of dual queues enables a simpler tactic for |
|
132 |
* improving M&S-style implementations when dual-ness is needed. |
|
133 |
* |
|
134 |
* In a dual queue, each node must atomically maintain its match |
|
135 |
* status. While there are other possible variants, we implement |
|
136 |
* this here as: for a data-mode node, matching entails CASing an |
|
137 |
* "item" field from a non-null data value to null upon match, and |
|
138 |
* vice-versa for request nodes, CASing from null to a data |
|
139 |
* value. (Note that the linearization properties of this style of |
|
140 |
* queue are easy to verify -- elements are made available by |
|
141 |
* linking, and unavailable by matching.) Compared to plain M&S |
|
142 |
* queues, this property of dual queues requires one additional |
|
143 |
* successful atomic operation per enq/deq pair. But it also |
|
144 |
* enables lower cost variants of queue maintenance mechanics. (A |
|
145 |
* variation of this idea applies even for non-dual queues that |
|
146 |
* support deletion of interior elements, such as |
|
147 |
* j.u.c.ConcurrentLinkedQueue.) |
|
148 |
* |
|
149 |
* Once a node is matched, its match status can never again |
|
150 |
* change. We may thus arrange that the linked list of them |
|
151 |
* contain a prefix of zero or more matched nodes, followed by a |
|
152 |
* suffix of zero or more unmatched nodes. (Note that we allow |
|
153 |
* both the prefix and suffix to be zero length, which in turn |
|
154 |
* means that we do not use a dummy header.) If we were not |
|
155 |
* concerned with either time or space efficiency, we could |
|
156 |
* correctly perform enqueue and dequeue operations by traversing |
|
157 |
* from a pointer to the initial node; CASing the item of the |
|
158 |
* first unmatched node on match and CASing the next field of the |
|
159 |
* trailing node on appends. (Plus some special-casing when |
|
160 |
* initially empty). While this would be a terrible idea in |
|
161 |
* itself, it does have the benefit of not requiring ANY atomic |
|
162 |
* updates on head/tail fields. |
|
163 |
* |
|
164 |
* We introduce here an approach that lies between the extremes of |
|
165 |
* never versus always updating queue (head and tail) pointers. |
|
166 |
* This offers a tradeoff between sometimes requiring extra |
|
167 |
* traversal steps to locate the first and/or last unmatched |
|
168 |
* nodes, versus the reduced overhead and contention of fewer |
|
169 |
* updates to queue pointers. For example, a possible snapshot of |
|
170 |
* a queue is: |
|
171 |
* |
|
172 |
* head tail |
|
173 |
* | | |
|
174 |
* v v |
|
175 |
* M -> M -> U -> U -> U -> U |
|
176 |
* |
|
177 |
* The best value for this "slack" (the targeted maximum distance |
|
178 |
* between the value of "head" and the first unmatched node, and |
|
179 |
* similarly for "tail") is an empirical matter. We have found |
|
180 |
* that using very small constants in the range of 1-3 work best |
|
181 |
* over a range of platforms. Larger values introduce increasing |
|
182 |
* costs of cache misses and risks of long traversal chains, while |
|
183 |
* smaller values increase CAS contention and overhead. |
|
184 |
* |
|
185 |
* Dual queues with slack differ from plain M&S dual queues by |
|
186 |
* virtue of only sometimes updating head or tail pointers when |
|
187 |
* matching, appending, or even traversing nodes; in order to |
|
188 |
* maintain a targeted slack. The idea of "sometimes" may be |
|
189 |
* operationalized in several ways. The simplest is to use a |
|
190 |
* per-operation counter incremented on each traversal step, and |
|
191 |
* to try (via CAS) to update the associated queue pointer |
|
192 |
* whenever the count exceeds a threshold. Another, that requires |
|
193 |
* more overhead, is to use random number generators to update |
|
194 |
* with a given probability per traversal step. |
|
195 |
* |
|
196 |
* In any strategy along these lines, because CASes updating |
|
197 |
* fields may fail, the actual slack may exceed targeted |
|
198 |
* slack. However, they may be retried at any time to maintain |
|
199 |
* targets. Even when using very small slack values, this |
|
200 |
* approach works well for dual queues because it allows all |
|
201 |
* operations up to the point of matching or appending an item |
|
202 |
* (hence potentially allowing progress by another thread) to be |
|
203 |
* read-only, thus not introducing any further contention. As |
|
204 |
* described below, we implement this by performing slack |
|
205 |
* maintenance retries only after these points. |
|
206 |
* |
|
207 |
* As an accompaniment to such techniques, traversal overhead can |
|
208 |
* be further reduced without increasing contention of head |
|
209 |
* pointer updates: Threads may sometimes shortcut the "next" link |
|
210 |
* path from the current "head" node to be closer to the currently |
|
211 |
* known first unmatched node, and similarly for tail. Again, this |
|
212 |
* may be triggered with using thresholds or randomization. |
|
213 |
* |
|
214 |
* These ideas must be further extended to avoid unbounded amounts |
|
215 |
* of costly-to-reclaim garbage caused by the sequential "next" |
|
216 |
* links of nodes starting at old forgotten head nodes: As first |
|
217 |
* described in detail by Boehm |
|
218 |
* (http://portal.acm.org/citation.cfm?doid=503272.503282) if a GC |
|
219 |
* delays noticing that any arbitrarily old node has become |
|
220 |
* garbage, all newer dead nodes will also be unreclaimed. |
|
221 |
* (Similar issues arise in non-GC environments.) To cope with |
|
222 |
* this in our implementation, upon CASing to advance the head |
|
223 |
* pointer, we set the "next" link of the previous head to point |
|
224 |
* only to itself; thus limiting the length of connected dead lists. |
|
225 |
* (We also take similar care to wipe out possibly garbage |
|
226 |
* retaining values held in other Node fields.) However, doing so |
|
227 |
* adds some further complexity to traversal: If any "next" |
|
228 |
* pointer links to itself, it indicates that the current thread |
|
229 |
* has lagged behind a head-update, and so the traversal must |
|
230 |
* continue from the "head". Traversals trying to find the |
|
231 |
* current tail starting from "tail" may also encounter |
|
232 |
* self-links, in which case they also continue at "head". |
|
233 |
* |
|
234 |
* It is tempting in slack-based scheme to not even use CAS for |
|
235 |
* updates (similarly to Ladan-Mozes & Shavit). However, this |
|
236 |
* cannot be done for head updates under the above link-forgetting |
|
237 |
* mechanics because an update may leave head at a detached node. |
|
238 |
* And while direct writes are possible for tail updates, they |
|
239 |
* increase the risk of long retraversals, and hence long garbage |
|
240 |
* chains, which can be much more costly than is worthwhile |
|
241 |
* considering that the cost difference of performing a CAS vs |
|
242 |
* write is smaller when they are not triggered on each operation |
|
243 |
* (especially considering that writes and CASes equally require |
|
244 |
* additional GC bookkeeping ("write barriers") that are sometimes |
|
245 |
* more costly than the writes themselves because of contention). |
|
246 |
* |
|
247 |
* *** Overview of implementation *** |
|
248 |
* |
|
249 |
* We use a threshold-based approach to updates, with a slack |
|
250 |
* threshold of two -- that is, we update head/tail when the |
|
251 |
* current pointer appears to be two or more steps away from the |
|
252 |
* first/last node. The slack value is hard-wired: a path greater |
|
253 |
* than one is naturally implemented by checking equality of |
|
254 |
* traversal pointers except when the list has only one element, |
|
255 |
* in which case we keep slack threshold at one. Avoiding tracking |
|
256 |
* explicit counts across method calls slightly simplifies an |
|
257 |
* already-messy implementation. Using randomization would |
|
258 |
* probably work better if there were a low-quality dirt-cheap |
|
259 |
* per-thread one available, but even ThreadLocalRandom is too |
|
260 |
* heavy for these purposes. |
|
261 |
* |
|
6543 | 262 |
* With such a small slack threshold value, it is not worthwhile |
263 |
* to augment this with path short-circuiting (i.e., unsplicing |
|
264 |
* interior nodes) except in the case of cancellation/removal (see |
|
265 |
* below). |
|
4110 | 266 |
* |
267 |
* We allow both the head and tail fields to be null before any |
|
268 |
* nodes are enqueued; initializing upon first append. This |
|
269 |
* simplifies some other logic, as well as providing more |
|
270 |
* efficient explicit control paths instead of letting JVMs insert |
|
271 |
* implicit NullPointerExceptions when they are null. While not |
|
272 |
* currently fully implemented, we also leave open the possibility |
|
273 |
* of re-nulling these fields when empty (which is complicated to |
|
274 |
* arrange, for little benefit.) |
|
275 |
* |
|
276 |
* All enqueue/dequeue operations are handled by the single method |
|
277 |
* "xfer" with parameters indicating whether to act as some form |
|
278 |
* of offer, put, poll, take, or transfer (each possibly with |
|
279 |
* timeout). The relative complexity of using one monolithic |
|
280 |
* method outweighs the code bulk and maintenance problems of |
|
281 |
* using separate methods for each case. |
|
282 |
* |
|
283 |
* Operation consists of up to three phases. The first is |
|
284 |
* implemented within method xfer, the second in tryAppend, and |
|
285 |
* the third in method awaitMatch. |
|
286 |
* |
|
287 |
* 1. Try to match an existing node |
|
288 |
* |
|
289 |
* Starting at head, skip already-matched nodes until finding |
|
290 |
* an unmatched node of opposite mode, if one exists, in which |
|
291 |
* case matching it and returning, also if necessary updating |
|
292 |
* head to one past the matched node (or the node itself if the |
|
293 |
* list has no other unmatched nodes). If the CAS misses, then |
|
294 |
* a loop retries advancing head by two steps until either |
|
295 |
* success or the slack is at most two. By requiring that each |
|
296 |
* attempt advances head by two (if applicable), we ensure that |
|
297 |
* the slack does not grow without bound. Traversals also check |
|
298 |
* if the initial head is now off-list, in which case they |
|
299 |
* start at the new head. |
|
300 |
* |
|
301 |
* If no candidates are found and the call was untimed |
|
302 |
* poll/offer, (argument "how" is NOW) return. |
|
303 |
* |
|
304 |
* 2. Try to append a new node (method tryAppend) |
|
305 |
* |
|
306 |
* Starting at current tail pointer, find the actual last node |
|
307 |
* and try to append a new node (or if head was null, establish |
|
308 |
* the first node). Nodes can be appended only if their |
|
309 |
* predecessors are either already matched or are of the same |
|
310 |
* mode. If we detect otherwise, then a new node with opposite |
|
311 |
* mode must have been appended during traversal, so we must |
|
312 |
* restart at phase 1. The traversal and update steps are |
|
313 |
* otherwise similar to phase 1: Retrying upon CAS misses and |
|
314 |
* checking for staleness. In particular, if a self-link is |
|
315 |
* encountered, then we can safely jump to a node on the list |
|
316 |
* by continuing the traversal at current head. |
|
317 |
* |
|
318 |
* On successful append, if the call was ASYNC, return. |
|
319 |
* |
|
320 |
* 3. Await match or cancellation (method awaitMatch) |
|
321 |
* |
|
322 |
* Wait for another thread to match node; instead cancelling if |
|
323 |
* the current thread was interrupted or the wait timed out. On |
|
324 |
* multiprocessors, we use front-of-queue spinning: If a node |
|
325 |
* appears to be the first unmatched node in the queue, it |
|
326 |
* spins a bit before blocking. In either case, before blocking |
|
327 |
* it tries to unsplice any nodes between the current "head" |
|
328 |
* and the first unmatched node. |
|
329 |
* |
|
330 |
* Front-of-queue spinning vastly improves performance of |
|
331 |
* heavily contended queues. And so long as it is relatively |
|
332 |
* brief and "quiet", spinning does not much impact performance |
|
333 |
* of less-contended queues. During spins threads check their |
|
334 |
* interrupt status and generate a thread-local random number |
|
335 |
* to decide to occasionally perform a Thread.yield. While |
|
11279 | 336 |
* yield has underdefined specs, we assume that it might help, |
337 |
* and will not hurt, in limiting impact of spinning on busy |
|
4110 | 338 |
* systems. We also use smaller (1/2) spins for nodes that are |
339 |
* not known to be front but whose predecessors have not |
|
340 |
* blocked -- these "chained" spins avoid artifacts of |
|
341 |
* front-of-queue rules which otherwise lead to alternating |
|
342 |
* nodes spinning vs blocking. Further, front threads that |
|
343 |
* represent phase changes (from data to request node or vice |
|
344 |
* versa) compared to their predecessors receive additional |
|
345 |
* chained spins, reflecting longer paths typically required to |
|
346 |
* unblock threads during phase changes. |
|
6543 | 347 |
* |
348 |
* |
|
349 |
* ** Unlinking removed interior nodes ** |
|
350 |
* |
|
351 |
* In addition to minimizing garbage retention via self-linking |
|
352 |
* described above, we also unlink removed interior nodes. These |
|
353 |
* may arise due to timed out or interrupted waits, or calls to |
|
354 |
* remove(x) or Iterator.remove. Normally, given a node that was |
|
355 |
* at one time known to be the predecessor of some node s that is |
|
356 |
* to be removed, we can unsplice s by CASing the next field of |
|
357 |
* its predecessor if it still points to s (otherwise s must |
|
358 |
* already have been removed or is now offlist). But there are two |
|
359 |
* situations in which we cannot guarantee to make node s |
|
360 |
* unreachable in this way: (1) If s is the trailing node of list |
|
361 |
* (i.e., with null next), then it is pinned as the target node |
|
362 |
* for appends, so can only be removed later after other nodes are |
|
363 |
* appended. (2) We cannot necessarily unlink s given a |
|
364 |
* predecessor node that is matched (including the case of being |
|
365 |
* cancelled): the predecessor may already be unspliced, in which |
|
366 |
* case some previous reachable node may still point to s. |
|
367 |
* (For further explanation see Herlihy & Shavit "The Art of |
|
368 |
* Multiprocessor Programming" chapter 9). Although, in both |
|
369 |
* cases, we can rule out the need for further action if either s |
|
370 |
* or its predecessor are (or can be made to be) at, or fall off |
|
371 |
* from, the head of list. |
|
372 |
* |
|
373 |
* Without taking these into account, it would be possible for an |
|
374 |
* unbounded number of supposedly removed nodes to remain |
|
375 |
* reachable. Situations leading to such buildup are uncommon but |
|
376 |
* can occur in practice; for example when a series of short timed |
|
377 |
* calls to poll repeatedly time out but never otherwise fall off |
|
378 |
* the list because of an untimed call to take at the front of the |
|
379 |
* queue. |
|
380 |
* |
|
381 |
* When these cases arise, rather than always retraversing the |
|
382 |
* entire list to find an actual predecessor to unlink (which |
|
383 |
* won't help for case (1) anyway), we record a conservative |
|
384 |
* estimate of possible unsplice failures (in "sweepVotes"). |
|
385 |
* We trigger a full sweep when the estimate exceeds a threshold |
|
386 |
* ("SWEEP_THRESHOLD") indicating the maximum number of estimated |
|
387 |
* removal failures to tolerate before sweeping through, unlinking |
|
388 |
* cancelled nodes that were not unlinked upon initial removal. |
|
389 |
* We perform sweeps by the thread hitting threshold (rather than |
|
390 |
* background threads or by spreading work to other threads) |
|
391 |
* because in the main contexts in which removal occurs, the |
|
392 |
* caller is already timed-out, cancelled, or performing a |
|
393 |
* potentially O(n) operation (e.g. remove(x)), none of which are |
|
394 |
* time-critical enough to warrant the overhead that alternatives |
|
395 |
* would impose on other threads. |
|
396 |
* |
|
397 |
* Because the sweepVotes estimate is conservative, and because |
|
398 |
* nodes become unlinked "naturally" as they fall off the head of |
|
399 |
* the queue, and because we allow votes to accumulate even while |
|
400 |
* sweeps are in progress, there are typically significantly fewer |
|
401 |
* such nodes than estimated. Choice of a threshold value |
|
402 |
* balances the likelihood of wasted effort and contention, versus |
|
403 |
* providing a worst-case bound on retention of interior nodes in |
|
404 |
* quiescent queues. The value defined below was chosen |
|
405 |
* empirically to balance these under various timeout scenarios. |
|
406 |
* |
|
407 |
* Note that we cannot self-link unlinked interior nodes during |
|
408 |
* sweeps. However, the associated garbage chains terminate when |
|
409 |
* some successor ultimately falls off the head of the list and is |
|
410 |
* self-linked. |
|
4110 | 411 |
*/ |
412 |
||
413 |
/** True if on multiprocessor */ |
|
414 |
private static final boolean MP = |
|
415 |
Runtime.getRuntime().availableProcessors() > 1; |
|
416 |
||
417 |
/** |
|
418 |
* The number of times to spin (with randomly interspersed calls |
|
419 |
* to Thread.yield) on multiprocessor before blocking when a node |
|
420 |
* is apparently the first waiter in the queue. See above for |
|
421 |
* explanation. Must be a power of two. The value is empirically |
|
422 |
* derived -- it works pretty well across a variety of processors, |
|
423 |
* numbers of CPUs, and OSes. |
|
424 |
*/ |
|
425 |
private static final int FRONT_SPINS = 1 << 7; |
|
426 |
||
427 |
/** |
|
428 |
* The number of times to spin before blocking when a node is |
|
429 |
* preceded by another node that is apparently spinning. Also |
|
430 |
* serves as an increment to FRONT_SPINS on phase changes, and as |
|
431 |
* base average frequency for yielding during spins. Must be a |
|
432 |
* power of two. |
|
433 |
*/ |
|
434 |
private static final int CHAINED_SPINS = FRONT_SPINS >>> 1; |
|
435 |
||
436 |
/** |
|
6543 | 437 |
* The maximum number of estimated removal failures (sweepVotes) |
438 |
* to tolerate before sweeping through the queue unlinking |
|
439 |
* cancelled nodes that were not unlinked upon initial |
|
440 |
* removal. See above for explanation. The value must be at least |
|
441 |
* two to avoid useless sweeps when removing trailing nodes. |
|
442 |
*/ |
|
443 |
static final int SWEEP_THRESHOLD = 32; |
|
444 |
||
445 |
/** |
|
4110 | 446 |
* Queue nodes. Uses Object, not E, for items to allow forgetting |
447 |
* them after use. Relies heavily on Unsafe mechanics to minimize |
|
6543 | 448 |
* unnecessary ordering constraints: Writes that are intrinsically |
449 |
* ordered wrt other accesses or CASes use simple relaxed forms. |
|
4110 | 450 |
*/ |
451 |
static final class Node { |
|
452 |
final boolean isData; // false if this is a request node |
|
453 |
volatile Object item; // initially non-null if isData; CASed to match |
|
454 |
volatile Node next; |
|
455 |
volatile Thread waiter; // null until waiting |
|
456 |
||
457 |
// CAS methods for fields |
|
458 |
final boolean casNext(Node cmp, Node val) { |
|
459 |
return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); |
|
460 |
} |
|
461 |
||
462 |
final boolean casItem(Object cmp, Object val) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
463 |
// assert cmp == null || cmp.getClass() != Node.class; |
4110 | 464 |
return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); |
465 |
} |
|
466 |
||
467 |
/** |
|
6543 | 468 |
* Constructs a new node. Uses relaxed write because item can |
469 |
* only be seen after publication via casNext. |
|
4110 | 470 |
*/ |
471 |
Node(Object item, boolean isData) { |
|
472 |
UNSAFE.putObject(this, itemOffset, item); // relaxed write |
|
473 |
this.isData = isData; |
|
474 |
} |
|
475 |
||
476 |
/** |
|
477 |
* Links node to itself to avoid garbage retention. Called |
|
478 |
* only after CASing head field, so uses relaxed write. |
|
479 |
*/ |
|
480 |
final void forgetNext() { |
|
481 |
UNSAFE.putObject(this, nextOffset, this); |
|
482 |
} |
|
483 |
||
484 |
/** |
|
6543 | 485 |
* Sets item to self and waiter to null, to avoid garbage |
486 |
* retention after matching or cancelling. Uses relaxed writes |
|
487 |
* because order is already constrained in the only calling |
|
488 |
* contexts: item is forgotten only after volatile/atomic |
|
489 |
* mechanics that extract items. Similarly, clearing waiter |
|
490 |
* follows either CAS or return from park (if ever parked; |
|
491 |
* else we don't care). |
|
4110 | 492 |
*/ |
493 |
final void forgetContents() { |
|
6543 | 494 |
UNSAFE.putObject(this, itemOffset, this); |
495 |
UNSAFE.putObject(this, waiterOffset, null); |
|
4110 | 496 |
} |
497 |
||
498 |
/** |
|
499 |
* Returns true if this node has been matched, including the |
|
500 |
* case of artificial matches due to cancellation. |
|
501 |
*/ |
|
502 |
final boolean isMatched() { |
|
503 |
Object x = item; |
|
504 |
return (x == this) || ((x == null) == isData); |
|
505 |
} |
|
506 |
||
507 |
/** |
|
508 |
* Returns true if this is an unmatched request node. |
|
509 |
*/ |
|
510 |
final boolean isUnmatchedRequest() { |
|
511 |
return !isData && item == null; |
|
512 |
} |
|
513 |
||
514 |
/** |
|
515 |
* Returns true if a node with the given mode cannot be |
|
516 |
* appended to this node because this node is unmatched and |
|
517 |
* has opposite data mode. |
|
518 |
*/ |
|
519 |
final boolean cannotPrecede(boolean haveData) { |
|
520 |
boolean d = isData; |
|
521 |
Object x; |
|
522 |
return d != haveData && (x = item) != this && (x != null) == d; |
|
523 |
} |
|
524 |
||
525 |
/** |
|
526 |
* Tries to artificially match a data node -- used by remove. |
|
527 |
*/ |
|
528 |
final boolean tryMatchData() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
529 |
// assert isData; |
4110 | 530 |
Object x = item; |
531 |
if (x != null && x != this && casItem(x, null)) { |
|
532 |
LockSupport.unpark(waiter); |
|
533 |
return true; |
|
534 |
} |
|
535 |
return false; |
|
536 |
} |
|
537 |
||
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
538 |
private static final long serialVersionUID = -3375979862319811754L; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
539 |
|
4110 | 540 |
// Unsafe mechanics |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
541 |
private static final sun.misc.Unsafe UNSAFE; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
542 |
private static final long itemOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
543 |
private static final long nextOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
544 |
private static final long waiterOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
545 |
static { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
546 |
try { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
547 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
11279 | 548 |
Class<?> k = Node.class; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
549 |
itemOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
550 |
(k.getDeclaredField("item")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
551 |
nextOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
552 |
(k.getDeclaredField("next")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
553 |
waiterOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
554 |
(k.getDeclaredField("waiter")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
555 |
} catch (Exception e) { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
556 |
throw new Error(e); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
557 |
} |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
558 |
} |
4110 | 559 |
} |
560 |
||
561 |
/** head of the queue; null until first enqueue */ |
|
562 |
transient volatile Node head; |
|
563 |
||
564 |
/** tail of the queue; null until first append */ |
|
565 |
private transient volatile Node tail; |
|
566 |
||
6543 | 567 |
/** The number of apparent failures to unsplice removed nodes */ |
568 |
private transient volatile int sweepVotes; |
|
569 |
||
4110 | 570 |
// CAS methods for fields |
571 |
private boolean casTail(Node cmp, Node val) { |
|
572 |
return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val); |
|
573 |
} |
|
574 |
||
575 |
private boolean casHead(Node cmp, Node val) { |
|
576 |
return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val); |
|
577 |
} |
|
578 |
||
6543 | 579 |
private boolean casSweepVotes(int cmp, int val) { |
580 |
return UNSAFE.compareAndSwapInt(this, sweepVotesOffset, cmp, val); |
|
4110 | 581 |
} |
582 |
||
583 |
/* |
|
584 |
* Possible values for "how" argument in xfer method. |
|
585 |
*/ |
|
586 |
private static final int NOW = 0; // for untimed poll, tryTransfer |
|
587 |
private static final int ASYNC = 1; // for offer, put, add |
|
588 |
private static final int SYNC = 2; // for transfer, take |
|
589 |
private static final int TIMED = 3; // for timed poll, tryTransfer |
|
590 |
||
591 |
@SuppressWarnings("unchecked") |
|
592 |
static <E> E cast(Object item) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
593 |
// assert item == null || item.getClass() != Node.class; |
4110 | 594 |
return (E) item; |
595 |
} |
|
596 |
||
597 |
/** |
|
598 |
* Implements all queuing methods. See above for explanation. |
|
599 |
* |
|
600 |
* @param e the item or null for take |
|
601 |
* @param haveData true if this is a put, else a take |
|
602 |
* @param how NOW, ASYNC, SYNC, or TIMED |
|
603 |
* @param nanos timeout in nanosecs, used only if mode is TIMED |
|
604 |
* @return an item if matched, else e |
|
605 |
* @throws NullPointerException if haveData mode but e is null |
|
606 |
*/ |
|
607 |
private E xfer(E e, boolean haveData, int how, long nanos) { |
|
608 |
if (haveData && (e == null)) |
|
609 |
throw new NullPointerException(); |
|
610 |
Node s = null; // the node to append, if needed |
|
611 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
612 |
retry: |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
613 |
for (;;) { // restart on append race |
4110 | 614 |
|
615 |
for (Node h = head, p = h; p != null;) { // find & match first node |
|
616 |
boolean isData = p.isData; |
|
617 |
Object item = p.item; |
|
618 |
if (item != p && (item != null) == isData) { // unmatched |
|
619 |
if (isData == haveData) // can't match |
|
620 |
break; |
|
621 |
if (p.casItem(item, e)) { // match |
|
622 |
for (Node q = p; q != h;) { |
|
6543 | 623 |
Node n = q.next; // update by 2 unless singleton |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
624 |
if (head == h && casHead(h, n == null ? q : n)) { |
4110 | 625 |
h.forgetNext(); |
626 |
break; |
|
627 |
} // advance and retry |
|
628 |
if ((h = head) == null || |
|
629 |
(q = h.next) == null || !q.isMatched()) |
|
630 |
break; // unless slack < 2 |
|
631 |
} |
|
632 |
LockSupport.unpark(p.waiter); |
|
11279 | 633 |
return LinkedTransferQueue.<E>cast(item); |
4110 | 634 |
} |
635 |
} |
|
636 |
Node n = p.next; |
|
637 |
p = (p != n) ? n : (h = head); // Use head if p offlist |
|
638 |
} |
|
639 |
||
640 |
if (how != NOW) { // No matches available |
|
641 |
if (s == null) |
|
642 |
s = new Node(e, haveData); |
|
643 |
Node pred = tryAppend(s, haveData); |
|
644 |
if (pred == null) |
|
645 |
continue retry; // lost race vs opposite mode |
|
646 |
if (how != ASYNC) |
|
647 |
return awaitMatch(s, pred, e, (how == TIMED), nanos); |
|
648 |
} |
|
649 |
return e; // not waiting |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* Tries to append node s as tail. |
|
655 |
* |
|
656 |
* @param s the node to append |
|
657 |
* @param haveData true if appending in data mode |
|
658 |
* @return null on failure due to losing race with append in |
|
659 |
* different mode, else s's predecessor, or s itself if no |
|
660 |
* predecessor |
|
661 |
*/ |
|
662 |
private Node tryAppend(Node s, boolean haveData) { |
|
663 |
for (Node t = tail, p = t;;) { // move p to last node and append |
|
664 |
Node n, u; // temps for reads of next & tail |
|
665 |
if (p == null && (p = head) == null) { |
|
666 |
if (casHead(null, s)) |
|
667 |
return s; // initialize |
|
668 |
} |
|
669 |
else if (p.cannotPrecede(haveData)) |
|
670 |
return null; // lost race vs opposite mode |
|
671 |
else if ((n = p.next) != null) // not last; keep traversing |
|
672 |
p = p != t && t != (u = tail) ? (t = u) : // stale tail |
|
673 |
(p != n) ? n : null; // restart if off list |
|
674 |
else if (!p.casNext(null, s)) |
|
675 |
p = p.next; // re-read on CAS failure |
|
676 |
else { |
|
677 |
if (p != t) { // update if slack now >= 2 |
|
678 |
while ((tail != t || !casTail(t, s)) && |
|
679 |
(t = tail) != null && |
|
680 |
(s = t.next) != null && // advance and retry |
|
681 |
(s = s.next) != null && s != t); |
|
682 |
} |
|
683 |
return p; |
|
684 |
} |
|
685 |
} |
|
686 |
} |
|
687 |
||
688 |
/** |
|
689 |
* Spins/yields/blocks until node s is matched or caller gives up. |
|
690 |
* |
|
691 |
* @param s the waiting node |
|
692 |
* @param pred the predecessor of s, or s itself if it has no |
|
693 |
* predecessor, or null if unknown (the null case does not occur |
|
694 |
* in any current calls but may in possible future extensions) |
|
695 |
* @param e the comparison value for checking match |
|
696 |
* @param timed if true, wait only until timeout elapses |
|
697 |
* @param nanos timeout in nanosecs, used only if timed is true |
|
698 |
* @return matched item, or e if unmatched on interrupt or timeout |
|
699 |
*/ |
|
700 |
private E awaitMatch(Node s, Node pred, E e, boolean timed, long nanos) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
701 |
final long deadline = timed ? System.nanoTime() + nanos : 0L; |
4110 | 702 |
Thread w = Thread.currentThread(); |
703 |
int spins = -1; // initialized after first item and cancel checks |
|
704 |
ThreadLocalRandom randomYields = null; // bound if needed |
|
705 |
||
706 |
for (;;) { |
|
707 |
Object item = s.item; |
|
708 |
if (item != e) { // matched |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
709 |
// assert item != s; |
4110 | 710 |
s.forgetContents(); // avoid garbage |
11279 | 711 |
return LinkedTransferQueue.<E>cast(item); |
4110 | 712 |
} |
713 |
if ((w.isInterrupted() || (timed && nanos <= 0)) && |
|
6543 | 714 |
s.casItem(e, s)) { // cancel |
4110 | 715 |
unsplice(pred, s); |
716 |
return e; |
|
717 |
} |
|
718 |
||
719 |
if (spins < 0) { // establish spins at/near front |
|
720 |
if ((spins = spinsFor(pred, s.isData)) > 0) |
|
721 |
randomYields = ThreadLocalRandom.current(); |
|
722 |
} |
|
723 |
else if (spins > 0) { // spin |
|
6543 | 724 |
--spins; |
725 |
if (randomYields.nextInt(CHAINED_SPINS) == 0) |
|
4110 | 726 |
Thread.yield(); // occasionally yield |
727 |
} |
|
728 |
else if (s.waiter == null) { |
|
729 |
s.waiter = w; // request unpark then recheck |
|
730 |
} |
|
731 |
else if (timed) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
732 |
nanos = deadline - System.nanoTime(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
733 |
if (nanos > 0L) |
4110 | 734 |
LockSupport.parkNanos(this, nanos); |
735 |
} |
|
736 |
else { |
|
737 |
LockSupport.park(this); |
|
738 |
} |
|
739 |
} |
|
740 |
} |
|
741 |
||
742 |
/** |
|
743 |
* Returns spin/yield value for a node with given predecessor and |
|
744 |
* data mode. See above for explanation. |
|
745 |
*/ |
|
746 |
private static int spinsFor(Node pred, boolean haveData) { |
|
747 |
if (MP && pred != null) { |
|
748 |
if (pred.isData != haveData) // phase change |
|
749 |
return FRONT_SPINS + CHAINED_SPINS; |
|
750 |
if (pred.isMatched()) // probably at front |
|
751 |
return FRONT_SPINS; |
|
752 |
if (pred.waiter == null) // pred apparently spinning |
|
753 |
return CHAINED_SPINS; |
|
754 |
} |
|
755 |
return 0; |
|
756 |
} |
|
757 |
||
758 |
/* -------------- Traversal methods -------------- */ |
|
759 |
||
760 |
/** |
|
761 |
* Returns the successor of p, or the head node if p.next has been |
|
762 |
* linked to self, which will only be true if traversing with a |
|
763 |
* stale pointer that is now off the list. |
|
764 |
*/ |
|
765 |
final Node succ(Node p) { |
|
766 |
Node next = p.next; |
|
767 |
return (p == next) ? head : next; |
|
768 |
} |
|
769 |
||
770 |
/** |
|
771 |
* Returns the first unmatched node of the given mode, or null if |
|
772 |
* none. Used by methods isEmpty, hasWaitingConsumer. |
|
773 |
*/ |
|
774 |
private Node firstOfMode(boolean isData) { |
|
775 |
for (Node p = head; p != null; p = succ(p)) { |
|
776 |
if (!p.isMatched()) |
|
777 |
return (p.isData == isData) ? p : null; |
|
778 |
} |
|
779 |
return null; |
|
780 |
} |
|
781 |
||
782 |
/** |
|
31151
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
783 |
* Version of firstOfMode used by Spliterator. Callers must |
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
784 |
* recheck if the returned node's item field is null or |
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
785 |
* self-linked before using. |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
786 |
*/ |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
787 |
final Node firstDataNode() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
788 |
for (Node p = head; p != null;) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
789 |
Object item = p.item; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
790 |
if (p.isData) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
791 |
if (item != null && item != p) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
792 |
return p; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
793 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
794 |
else if (item == null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
795 |
break; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
796 |
if (p == (p = p.next)) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
797 |
p = head; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
798 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
799 |
return null; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
800 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
801 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
802 |
/** |
4110 | 803 |
* Returns the item in the first unmatched node with isData; or |
804 |
* null if none. Used by peek. |
|
805 |
*/ |
|
806 |
private E firstDataItem() { |
|
807 |
for (Node p = head; p != null; p = succ(p)) { |
|
808 |
Object item = p.item; |
|
809 |
if (p.isData) { |
|
810 |
if (item != null && item != p) |
|
11279 | 811 |
return LinkedTransferQueue.<E>cast(item); |
4110 | 812 |
} |
813 |
else if (item == null) |
|
814 |
return null; |
|
815 |
} |
|
816 |
return null; |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* Traverses and counts unmatched nodes of the given mode. |
|
821 |
* Used by methods size and getWaitingConsumerCount. |
|
822 |
*/ |
|
823 |
private int countOfMode(boolean data) { |
|
824 |
int count = 0; |
|
825 |
for (Node p = head; p != null; ) { |
|
826 |
if (!p.isMatched()) { |
|
827 |
if (p.isData != data) |
|
828 |
return 0; |
|
829 |
if (++count == Integer.MAX_VALUE) // saturated |
|
830 |
break; |
|
831 |
} |
|
832 |
Node n = p.next; |
|
833 |
if (n != p) |
|
834 |
p = n; |
|
835 |
else { |
|
836 |
count = 0; |
|
837 |
p = head; |
|
838 |
} |
|
839 |
} |
|
840 |
return count; |
|
841 |
} |
|
842 |
||
843 |
final class Itr implements Iterator<E> { |
|
844 |
private Node nextNode; // next node to return item for |
|
845 |
private E nextItem; // the corresponding item |
|
846 |
private Node lastRet; // last returned node, to support remove |
|
847 |
private Node lastPred; // predecessor to unlink lastRet |
|
848 |
||
849 |
/** |
|
850 |
* Moves to next node after prev, or first node if prev null. |
|
851 |
*/ |
|
852 |
private void advance(Node prev) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
853 |
/* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
854 |
* To track and avoid buildup of deleted nodes in the face |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
855 |
* of calls to both Queue.remove and Itr.remove, we must |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
856 |
* include variants of unsplice and sweep upon each |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
857 |
* advance: Upon Itr.remove, we may need to catch up links |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
858 |
* from lastPred, and upon other removes, we might need to |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
859 |
* skip ahead from stale nodes and unsplice deleted ones |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
860 |
* found while advancing. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
861 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
862 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
863 |
Node r, b; // reset lastPred upon possible deletion of lastRet |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
864 |
if ((r = lastRet) != null && !r.isMatched()) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
865 |
lastPred = r; // next lastPred is old lastRet |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
866 |
else if ((b = lastPred) == null || b.isMatched()) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
867 |
lastPred = null; // at start of list |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
868 |
else { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
869 |
Node s, n; // help with removal of lastPred.next |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
870 |
while ((s = b.next) != null && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
871 |
s != b && s.isMatched() && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
872 |
(n = s.next) != null && n != s) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
873 |
b.casNext(s, n); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
874 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
875 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
876 |
this.lastRet = prev; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
877 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
878 |
for (Node p = prev, s, n;;) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
879 |
s = (p == null) ? head : p.next; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
880 |
if (s == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
881 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
882 |
else if (s == p) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
883 |
p = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
884 |
continue; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
885 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
886 |
Object item = s.item; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
887 |
if (s.isData) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
888 |
if (item != null && item != s) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
889 |
nextItem = LinkedTransferQueue.<E>cast(item); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
890 |
nextNode = s; |
4110 | 891 |
return; |
892 |
} |
|
893 |
} |
|
894 |
else if (item == null) |
|
895 |
break; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
896 |
// assert s.isMatched(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
897 |
if (p == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
898 |
p = s; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
899 |
else if ((n = s.next) == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
900 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
901 |
else if (s == n) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
902 |
p = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
903 |
else |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
904 |
p.casNext(s, n); |
4110 | 905 |
} |
906 |
nextNode = null; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
907 |
nextItem = null; |
4110 | 908 |
} |
909 |
||
910 |
Itr() { |
|
911 |
advance(null); |
|
912 |
} |
|
913 |
||
914 |
public final boolean hasNext() { |
|
915 |
return nextNode != null; |
|
916 |
} |
|
917 |
||
918 |
public final E next() { |
|
919 |
Node p = nextNode; |
|
920 |
if (p == null) throw new NoSuchElementException(); |
|
921 |
E e = nextItem; |
|
922 |
advance(p); |
|
923 |
return e; |
|
924 |
} |
|
925 |
||
926 |
public final void remove() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
927 |
final Node lastRet = this.lastRet; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
928 |
if (lastRet == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
929 |
throw new IllegalStateException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
930 |
this.lastRet = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
931 |
if (lastRet.tryMatchData()) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
932 |
unsplice(lastPred, lastRet); |
4110 | 933 |
} |
934 |
} |
|
935 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
936 |
/** A customized variant of Spliterators.IteratorSpliterator */ |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
937 |
static final class LTQSpliterator<E> implements Spliterator<E> { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
938 |
static final int MAX_BATCH = 1 << 25; // max batch array size; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
939 |
final LinkedTransferQueue<E> queue; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
940 |
Node current; // current node; null until initialized |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
941 |
int batch; // batch size for splits |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
942 |
boolean exhausted; // true when no more nodes |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
943 |
LTQSpliterator(LinkedTransferQueue<E> queue) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
944 |
this.queue = queue; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
945 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
946 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
947 |
public Spliterator<E> trySplit() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
948 |
Node p; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
949 |
final LinkedTransferQueue<E> q = this.queue; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
950 |
int b = batch; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
951 |
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
952 |
if (!exhausted && |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
953 |
((p = current) != null || (p = q.firstDataNode()) != null) && |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
954 |
p.next != null) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
955 |
Object[] a = new Object[n]; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
956 |
int i = 0; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
957 |
do { |
31151
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
958 |
Object e = p.item; |
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
959 |
if (e != p && (a[i] = e) != null) |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
960 |
++i; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
961 |
if (p == (p = p.next)) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
962 |
p = q.firstDataNode(); |
31151
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
963 |
} while (p != null && i < n && p.isData); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
964 |
if ((current = p) == null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
965 |
exhausted = true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
966 |
if (i > 0) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
967 |
batch = i; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
968 |
return Spliterators.spliterator |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
969 |
(a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL | |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
970 |
Spliterator.CONCURRENT); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
971 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
972 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
973 |
return null; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
974 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
975 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
976 |
@SuppressWarnings("unchecked") |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
977 |
public void forEachRemaining(Consumer<? super E> action) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
978 |
Node p; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
979 |
if (action == null) throw new NullPointerException(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
980 |
final LinkedTransferQueue<E> q = this.queue; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
981 |
if (!exhausted && |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
982 |
((p = current) != null || (p = q.firstDataNode()) != null)) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
983 |
exhausted = true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
984 |
do { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
985 |
Object e = p.item; |
31151
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
986 |
if (e != null && e != p) |
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
987 |
action.accept((E)e); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
988 |
if (p == (p = p.next)) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
989 |
p = q.firstDataNode(); |
31151
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
990 |
} while (p != null && p.isData); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
991 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
992 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
993 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
994 |
@SuppressWarnings("unchecked") |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
995 |
public boolean tryAdvance(Consumer<? super E> action) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
996 |
Node p; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
997 |
if (action == null) throw new NullPointerException(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
998 |
final LinkedTransferQueue<E> q = this.queue; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
999 |
if (!exhausted && |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1000 |
((p = current) != null || (p = q.firstDataNode()) != null)) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1001 |
Object e; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1002 |
do { |
31151
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
1003 |
if ((e = p.item) == p) |
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
1004 |
e = null; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1005 |
if (p == (p = p.next)) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1006 |
p = q.firstDataNode(); |
31151
5535c077def0
8085978: LinkedTransferQueue<T>.spliterator can report LTQ.Node object, not T
dl
parents:
25859
diff
changeset
|
1007 |
} while (e == null && p != null && p.isData); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1008 |
if ((current = p) == null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1009 |
exhausted = true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1010 |
if (e != null) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1011 |
action.accept((E)e); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1012 |
return true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1013 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1014 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1015 |
return false; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1016 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1017 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1018 |
public long estimateSize() { return Long.MAX_VALUE; } |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1019 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1020 |
public int characteristics() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1021 |
return Spliterator.ORDERED | Spliterator.NONNULL | |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1022 |
Spliterator.CONCURRENT; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1023 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1024 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1025 |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1026 |
/** |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1027 |
* 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
|
1028 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1029 |
* <p>The returned spliterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1030 |
* <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
|
1031 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1032 |
* <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
|
1033 |
* {@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
|
1034 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1035 |
* @implNote |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1036 |
* 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
|
1037 |
* parallelism. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1038 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1039 |
* @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
|
1040 |
* @since 1.8 |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1041 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1042 |
public Spliterator<E> spliterator() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1043 |
return new LTQSpliterator<E>(this); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1044 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1045 |
|
4110 | 1046 |
/* -------------- Removal methods -------------- */ |
1047 |
||
1048 |
/** |
|
1049 |
* Unsplices (now or later) the given deleted/cancelled node with |
|
1050 |
* the given predecessor. |
|
1051 |
* |
|
6543 | 1052 |
* @param pred a node that was at one time known to be the |
1053 |
* predecessor of s, or null or s itself if s is/was at head |
|
4110 | 1054 |
* @param s the node to be unspliced |
1055 |
*/ |
|
6543 | 1056 |
final void unsplice(Node pred, Node s) { |
1057 |
s.forgetContents(); // forget unneeded fields |
|
4110 | 1058 |
/* |
6543 | 1059 |
* See above for rationale. Briefly: if pred still points to |
1060 |
* s, try to unlink s. If s cannot be unlinked, because it is |
|
1061 |
* trailing node or pred might be unlinked, and neither pred |
|
1062 |
* nor s are head or offlist, add to sweepVotes, and if enough |
|
1063 |
* votes have accumulated, sweep. |
|
4110 | 1064 |
*/ |
6543 | 1065 |
if (pred != null && pred != s && pred.next == s) { |
1066 |
Node n = s.next; |
|
1067 |
if (n == null || |
|
1068 |
(n != s && pred.casNext(s, n) && pred.isMatched())) { |
|
1069 |
for (;;) { // check if at, or could be, head |
|
1070 |
Node h = head; |
|
1071 |
if (h == pred || h == s || h == null) |
|
1072 |
return; // at head or list empty |
|
1073 |
if (!h.isMatched()) |
|
1074 |
break; |
|
1075 |
Node hn = h.next; |
|
1076 |
if (hn == null) |
|
1077 |
return; // now empty |
|
1078 |
if (hn != h && casHead(h, hn)) |
|
1079 |
h.forgetNext(); // advance head |
|
4110 | 1080 |
} |
6543 | 1081 |
if (pred.next != pred && s.next != s) { // recheck if offlist |
1082 |
for (;;) { // sweep now if enough votes |
|
1083 |
int v = sweepVotes; |
|
1084 |
if (v < SWEEP_THRESHOLD) { |
|
1085 |
if (casSweepVotes(v, v + 1)) |
|
1086 |
break; |
|
1087 |
} |
|
1088 |
else if (casSweepVotes(v, 0)) { |
|
1089 |
sweep(); |
|
1090 |
break; |
|
1091 |
} |
|
1092 |
} |
|
4110 | 1093 |
} |
1094 |
} |
|
1095 |
} |
|
1096 |
} |
|
1097 |
||
1098 |
/** |
|
6543 | 1099 |
* Unlinks matched (typically cancelled) nodes encountered in a |
1100 |
* traversal from head. |
|
4110 | 1101 |
*/ |
6543 | 1102 |
private void sweep() { |
1103 |
for (Node p = head, s, n; p != null && (s = p.next) != null; ) { |
|
1104 |
if (!s.isMatched()) |
|
1105 |
// Unmatched nodes are never self-linked |
|
1106 |
p = s; |
|
1107 |
else if ((n = s.next) == null) // trailing node is pinned |
|
4110 | 1108 |
break; |
6543 | 1109 |
else if (s == n) // stale |
1110 |
// No need to also check for p == s, since that implies s == n |
|
1111 |
p = head; |
|
1112 |
else |
|
1113 |
p.casNext(s, n); |
|
4110 | 1114 |
} |
1115 |
} |
|
1116 |
||
1117 |
/** |
|
1118 |
* Main implementation of remove(Object) |
|
1119 |
*/ |
|
1120 |
private boolean findAndRemove(Object e) { |
|
1121 |
if (e != null) { |
|
1122 |
for (Node pred = null, p = head; p != null; ) { |
|
1123 |
Object item = p.item; |
|
1124 |
if (p.isData) { |
|
1125 |
if (item != null && item != p && e.equals(item) && |
|
1126 |
p.tryMatchData()) { |
|
1127 |
unsplice(pred, p); |
|
1128 |
return true; |
|
1129 |
} |
|
1130 |
} |
|
1131 |
else if (item == null) |
|
1132 |
break; |
|
1133 |
pred = p; |
|
1134 |
if ((p = p.next) == pred) { // stale |
|
1135 |
pred = null; |
|
1136 |
p = head; |
|
1137 |
} |
|
1138 |
} |
|
1139 |
} |
|
1140 |
return false; |
|
1141 |
} |
|
1142 |
||
1143 |
/** |
|
1144 |
* Creates an initially empty {@code LinkedTransferQueue}. |
|
1145 |
*/ |
|
1146 |
public LinkedTransferQueue() { |
|
1147 |
} |
|
1148 |
||
1149 |
/** |
|
1150 |
* Creates a {@code LinkedTransferQueue} |
|
1151 |
* initially containing the elements of the given collection, |
|
1152 |
* added in traversal order of the collection's iterator. |
|
1153 |
* |
|
1154 |
* @param c the collection of elements to initially contain |
|
1155 |
* @throws NullPointerException if the specified collection or any |
|
1156 |
* of its elements are null |
|
1157 |
*/ |
|
1158 |
public LinkedTransferQueue(Collection<? extends E> c) { |
|
1159 |
this(); |
|
1160 |
addAll(c); |
|
1161 |
} |
|
1162 |
||
1163 |
/** |
|
1164 |
* Inserts the specified element at the tail of this queue. |
|
1165 |
* As the queue is unbounded, this method will never block. |
|
1166 |
* |
|
1167 |
* @throws NullPointerException if the specified element is null |
|
1168 |
*/ |
|
1169 |
public void put(E e) { |
|
1170 |
xfer(e, true, ASYNC, 0); |
|
1171 |
} |
|
1172 |
||
1173 |
/** |
|
1174 |
* Inserts the specified element at the tail of this queue. |
|
1175 |
* As the queue is unbounded, this method will never block or |
|
1176 |
* return {@code false}. |
|
1177 |
* |
|
1178 |
* @return {@code true} (as specified by |
|
11279 | 1179 |
* {@link java.util.concurrent.BlockingQueue#offer(Object,long,TimeUnit) |
1180 |
* BlockingQueue.offer}) |
|
4110 | 1181 |
* @throws NullPointerException if the specified element is null |
1182 |
*/ |
|
1183 |
public boolean offer(E e, long timeout, TimeUnit unit) { |
|
1184 |
xfer(e, true, ASYNC, 0); |
|
1185 |
return true; |
|
1186 |
} |
|
1187 |
||
1188 |
/** |
|
1189 |
* Inserts the specified element at the tail of this queue. |
|
1190 |
* As the queue is unbounded, this method will never return {@code false}. |
|
1191 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1192 |
* @return {@code true} (as specified by {@link Queue#offer}) |
4110 | 1193 |
* @throws NullPointerException if the specified element is null |
1194 |
*/ |
|
1195 |
public boolean offer(E e) { |
|
1196 |
xfer(e, true, ASYNC, 0); |
|
1197 |
return true; |
|
1198 |
} |
|
1199 |
||
1200 |
/** |
|
1201 |
* Inserts the specified element at the tail of this queue. |
|
1202 |
* As the queue is unbounded, this method will never throw |
|
1203 |
* {@link IllegalStateException} or return {@code false}. |
|
1204 |
* |
|
1205 |
* @return {@code true} (as specified by {@link Collection#add}) |
|
1206 |
* @throws NullPointerException if the specified element is null |
|
1207 |
*/ |
|
1208 |
public boolean add(E e) { |
|
1209 |
xfer(e, true, ASYNC, 0); |
|
1210 |
return true; |
|
1211 |
} |
|
1212 |
||
1213 |
/** |
|
1214 |
* Transfers the element to a waiting consumer immediately, if possible. |
|
1215 |
* |
|
1216 |
* <p>More precisely, transfers the specified element immediately |
|
1217 |
* if there exists a consumer already waiting to receive it (in |
|
1218 |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
|
1219 |
* otherwise returning {@code false} without enqueuing the element. |
|
1220 |
* |
|
1221 |
* @throws NullPointerException if the specified element is null |
|
1222 |
*/ |
|
1223 |
public boolean tryTransfer(E e) { |
|
1224 |
return xfer(e, true, NOW, 0) == null; |
|
1225 |
} |
|
1226 |
||
1227 |
/** |
|
1228 |
* Transfers the element to a consumer, waiting if necessary to do so. |
|
1229 |
* |
|
1230 |
* <p>More precisely, transfers the specified element immediately |
|
1231 |
* if there exists a consumer already waiting to receive it (in |
|
1232 |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
|
1233 |
* else inserts the specified element at the tail of this queue |
|
1234 |
* and waits until the element is received by a consumer. |
|
1235 |
* |
|
1236 |
* @throws NullPointerException if the specified element is null |
|
1237 |
*/ |
|
1238 |
public void transfer(E e) throws InterruptedException { |
|
1239 |
if (xfer(e, true, SYNC, 0) != null) { |
|
1240 |
Thread.interrupted(); // failure possible only due to interrupt |
|
1241 |
throw new InterruptedException(); |
|
1242 |
} |
|
1243 |
} |
|
1244 |
||
1245 |
/** |
|
1246 |
* Transfers the element to a consumer if it is possible to do so |
|
1247 |
* before the timeout elapses. |
|
1248 |
* |
|
1249 |
* <p>More precisely, transfers the specified element immediately |
|
1250 |
* if there exists a consumer already waiting to receive it (in |
|
1251 |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
|
1252 |
* else inserts the specified element at the tail of this queue |
|
1253 |
* and waits until the element is received by a consumer, |
|
1254 |
* returning {@code false} if the specified wait time elapses |
|
1255 |
* before the element can be transferred. |
|
1256 |
* |
|
1257 |
* @throws NullPointerException if the specified element is null |
|
1258 |
*/ |
|
1259 |
public boolean tryTransfer(E e, long timeout, TimeUnit unit) |
|
1260 |
throws InterruptedException { |
|
1261 |
if (xfer(e, true, TIMED, unit.toNanos(timeout)) == null) |
|
1262 |
return true; |
|
1263 |
if (!Thread.interrupted()) |
|
1264 |
return false; |
|
1265 |
throw new InterruptedException(); |
|
1266 |
} |
|
1267 |
||
1268 |
public E take() throws InterruptedException { |
|
1269 |
E e = xfer(null, false, SYNC, 0); |
|
1270 |
if (e != null) |
|
1271 |
return e; |
|
1272 |
Thread.interrupted(); |
|
1273 |
throw new InterruptedException(); |
|
1274 |
} |
|
1275 |
||
1276 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
|
1277 |
E e = xfer(null, false, TIMED, unit.toNanos(timeout)); |
|
1278 |
if (e != null || !Thread.interrupted()) |
|
1279 |
return e; |
|
1280 |
throw new InterruptedException(); |
|
1281 |
} |
|
1282 |
||
1283 |
public E poll() { |
|
1284 |
return xfer(null, false, NOW, 0); |
|
1285 |
} |
|
1286 |
||
1287 |
/** |
|
1288 |
* @throws NullPointerException {@inheritDoc} |
|
1289 |
* @throws IllegalArgumentException {@inheritDoc} |
|
1290 |
*/ |
|
1291 |
public int drainTo(Collection<? super E> c) { |
|
1292 |
if (c == null) |
|
1293 |
throw new NullPointerException(); |
|
1294 |
if (c == this) |
|
1295 |
throw new IllegalArgumentException(); |
|
1296 |
int n = 0; |
|
11279 | 1297 |
for (E e; (e = poll()) != null;) { |
4110 | 1298 |
c.add(e); |
1299 |
++n; |
|
1300 |
} |
|
1301 |
return n; |
|
1302 |
} |
|
1303 |
||
1304 |
/** |
|
1305 |
* @throws NullPointerException {@inheritDoc} |
|
1306 |
* @throws IllegalArgumentException {@inheritDoc} |
|
1307 |
*/ |
|
1308 |
public int drainTo(Collection<? super E> c, int maxElements) { |
|
1309 |
if (c == null) |
|
1310 |
throw new NullPointerException(); |
|
1311 |
if (c == this) |
|
1312 |
throw new IllegalArgumentException(); |
|
1313 |
int n = 0; |
|
11279 | 1314 |
for (E e; n < maxElements && (e = poll()) != null;) { |
4110 | 1315 |
c.add(e); |
1316 |
++n; |
|
1317 |
} |
|
1318 |
return n; |
|
1319 |
} |
|
1320 |
||
1321 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1322 |
* Returns an iterator over the elements in this queue in proper sequence. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1323 |
* The elements will be returned in order from first (head) to last (tail). |
4110 | 1324 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1325 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1326 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
4110 | 1327 |
* |
1328 |
* @return an iterator over the elements in this queue in proper sequence |
|
1329 |
*/ |
|
1330 |
public Iterator<E> iterator() { |
|
1331 |
return new Itr(); |
|
1332 |
} |
|
1333 |
||
1334 |
public E peek() { |
|
1335 |
return firstDataItem(); |
|
1336 |
} |
|
1337 |
||
1338 |
/** |
|
1339 |
* Returns {@code true} if this queue contains no elements. |
|
1340 |
* |
|
1341 |
* @return {@code true} if this queue contains no elements |
|
1342 |
*/ |
|
1343 |
public boolean isEmpty() { |
|
6543 | 1344 |
for (Node p = head; p != null; p = succ(p)) { |
1345 |
if (!p.isMatched()) |
|
1346 |
return !p.isData; |
|
1347 |
} |
|
1348 |
return true; |
|
4110 | 1349 |
} |
1350 |
||
1351 |
public boolean hasWaitingConsumer() { |
|
1352 |
return firstOfMode(false) != null; |
|
1353 |
} |
|
1354 |
||
1355 |
/** |
|
1356 |
* Returns the number of elements in this queue. If this queue |
|
1357 |
* contains more than {@code Integer.MAX_VALUE} elements, returns |
|
1358 |
* {@code Integer.MAX_VALUE}. |
|
1359 |
* |
|
1360 |
* <p>Beware that, unlike in most collections, this method is |
|
1361 |
* <em>NOT</em> a constant-time operation. Because of the |
|
1362 |
* asynchronous nature of these queues, determining the current |
|
1363 |
* number of elements requires an O(n) traversal. |
|
1364 |
* |
|
1365 |
* @return the number of elements in this queue |
|
1366 |
*/ |
|
1367 |
public int size() { |
|
1368 |
return countOfMode(true); |
|
1369 |
} |
|
1370 |
||
1371 |
public int getWaitingConsumerCount() { |
|
1372 |
return countOfMode(false); |
|
1373 |
} |
|
1374 |
||
1375 |
/** |
|
1376 |
* Removes a single instance of the specified element from this queue, |
|
1377 |
* if it is present. More formally, removes an element {@code e} such |
|
1378 |
* that {@code o.equals(e)}, if this queue contains one or more such |
|
1379 |
* elements. |
|
1380 |
* Returns {@code true} if this queue contained the specified element |
|
1381 |
* (or equivalently, if this queue changed as a result of the call). |
|
1382 |
* |
|
1383 |
* @param o element to be removed from this queue, if present |
|
1384 |
* @return {@code true} if this queue changed as a result of the call |
|
1385 |
*/ |
|
1386 |
public boolean remove(Object o) { |
|
1387 |
return findAndRemove(o); |
|
1388 |
} |
|
1389 |
||
1390 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1391 |
* Returns {@code true} if this queue contains the specified element. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1392 |
* 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:
6543
diff
changeset
|
1393 |
* at least one element {@code e} such that {@code o.equals(e)}. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1394 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1395 |
* @param o object to be checked for containment in this queue |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1396 |
* @return {@code true} if this queue contains the specified element |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1397 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1398 |
public boolean contains(Object o) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1399 |
if (o == null) return false; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1400 |
for (Node p = head; p != null; p = succ(p)) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1401 |
Object item = p.item; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1402 |
if (p.isData) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1403 |
if (item != null && item != p && o.equals(item)) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1404 |
return true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1405 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1406 |
else if (item == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1407 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1408 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1409 |
return false; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1410 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1411 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1412 |
/** |
4110 | 1413 |
* Always returns {@code Integer.MAX_VALUE} because a |
1414 |
* {@code LinkedTransferQueue} is not capacity constrained. |
|
1415 |
* |
|
1416 |
* @return {@code Integer.MAX_VALUE} (as specified by |
|
11279 | 1417 |
* {@link java.util.concurrent.BlockingQueue#remainingCapacity() |
1418 |
* BlockingQueue.remainingCapacity}) |
|
4110 | 1419 |
*/ |
1420 |
public int remainingCapacity() { |
|
1421 |
return Integer.MAX_VALUE; |
|
1422 |
} |
|
1423 |
||
1424 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1425 |
* Saves this queue to a stream (that is, serializes it). |
4110 | 1426 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1427 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1428 |
* @throws java.io.IOException if an I/O error occurs |
4110 | 1429 |
* @serialData All of the elements (each an {@code E}) in |
1430 |
* the proper order, followed by a null |
|
1431 |
*/ |
|
1432 |
private void writeObject(java.io.ObjectOutputStream s) |
|
1433 |
throws java.io.IOException { |
|
1434 |
s.defaultWriteObject(); |
|
1435 |
for (E e : this) |
|
1436 |
s.writeObject(e); |
|
1437 |
// Use trailing null as sentinel |
|
1438 |
s.writeObject(null); |
|
1439 |
} |
|
1440 |
||
1441 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1442 |
* Reconstitutes this queue from a stream (that is, deserializes it). |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1443 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1444 |
* @throws ClassNotFoundException if the class of a serialized object |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1445 |
* could not be found |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1446 |
* @throws java.io.IOException if an I/O error occurs |
4110 | 1447 |
*/ |
1448 |
private void readObject(java.io.ObjectInputStream s) |
|
1449 |
throws java.io.IOException, ClassNotFoundException { |
|
1450 |
s.defaultReadObject(); |
|
1451 |
for (;;) { |
|
11279 | 1452 |
@SuppressWarnings("unchecked") |
1453 |
E item = (E) s.readObject(); |
|
4110 | 1454 |
if (item == null) |
1455 |
break; |
|
1456 |
else |
|
1457 |
offer(item); |
|
1458 |
} |
|
1459 |
} |
|
1460 |
||
1461 |
// Unsafe mechanics |
|
1462 |
||
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1463 |
private static final sun.misc.Unsafe UNSAFE; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1464 |
private static final long headOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1465 |
private static final long tailOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1466 |
private static final long sweepVotesOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1467 |
static { |
4110 | 1468 |
try { |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1469 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
11279 | 1470 |
Class<?> k = LinkedTransferQueue.class; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1471 |
headOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1472 |
(k.getDeclaredField("head")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1473 |
tailOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1474 |
(k.getDeclaredField("tail")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1475 |
sweepVotesOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1476 |
(k.getDeclaredField("sweepVotes")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1477 |
} catch (Exception e) { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1478 |
throw new Error(e); |
4110 | 1479 |
} |
1480 |
} |
|
1481 |
} |