author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 25859 | 3317bb8137f4 |
child 32991 | b27c76b82713 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea, Bill Scherer, and Michael Scott with |
|
32 |
* assistance from members of JCP JSR-166 Expert Group and released to |
|
33 |
* the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
8544
diff
changeset
|
34 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 35 |
*/ |
36 |
||
37 |
package java.util.concurrent; |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
38 |
import java.util.concurrent.locks.LockSupport; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
39 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 40 |
import java.util.*; |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
41 |
import java.util.Spliterator; |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
42 |
import java.util.Spliterators; |
2 | 43 |
|
44 |
/** |
|
45 |
* A {@linkplain BlockingQueue blocking queue} in which each insert |
|
46 |
* operation must wait for a corresponding remove operation by another |
|
47 |
* thread, and vice versa. A synchronous queue does not have any |
|
48 |
* internal capacity, not even a capacity of one. You cannot |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
49 |
* {@code peek} at a synchronous queue because an element is only |
2 | 50 |
* present when you try to remove it; you cannot insert an element |
51 |
* (using any method) unless another thread is trying to remove it; |
|
52 |
* you cannot iterate as there is nothing to iterate. The |
|
53 |
* <em>head</em> of the queue is the element that the first queued |
|
54 |
* inserting thread is trying to add to the queue; if there is no such |
|
55 |
* queued thread then no element is available for removal and |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
56 |
* {@code poll()} will return {@code null}. For purposes of other |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
57 |
* {@code Collection} methods (for example {@code contains}), a |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
58 |
* {@code SynchronousQueue} acts as an empty collection. This queue |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
59 |
* does not permit {@code null} elements. |
2 | 60 |
* |
61 |
* <p>Synchronous queues are similar to rendezvous channels used in |
|
62 |
* CSP and Ada. They are well suited for handoff designs, in which an |
|
63 |
* object running in one thread must sync up with an object running |
|
64 |
* in another thread in order to hand it some information, event, or |
|
65 |
* task. |
|
66 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
67 |
* <p>This class supports an optional fairness policy for ordering |
2 | 68 |
* waiting producer and consumer threads. By default, this ordering |
69 |
* is not guaranteed. However, a queue constructed with fairness set |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
70 |
* to {@code true} grants threads access in FIFO order. |
2 | 71 |
* |
72 |
* <p>This class and its iterator implement all of the |
|
73 |
* <em>optional</em> methods of the {@link Collection} and {@link |
|
74 |
* Iterator} interfaces. |
|
75 |
* |
|
76 |
* <p>This class is a member of the |
|
77 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
78 |
* Java Collections Framework</a>. |
|
79 |
* |
|
80 |
* @since 1.5 |
|
81 |
* @author Doug Lea and Bill Scherer and Michael Scott |
|
82 |
* @param <E> the type of elements held in this collection |
|
83 |
*/ |
|
84 |
public class SynchronousQueue<E> extends AbstractQueue<E> |
|
85 |
implements BlockingQueue<E>, java.io.Serializable { |
|
86 |
private static final long serialVersionUID = -3223113410248163686L; |
|
87 |
||
88 |
/* |
|
89 |
* This class implements extensions of the dual stack and dual |
|
90 |
* queue algorithms described in "Nonblocking Concurrent Objects |
|
91 |
* with Condition Synchronization", by W. N. Scherer III and |
|
92 |
* M. L. Scott. 18th Annual Conf. on Distributed Computing, |
|
93 |
* Oct. 2004 (see also |
|
94 |
* http://www.cs.rochester.edu/u/scott/synchronization/pseudocode/duals.html). |
|
95 |
* The (Lifo) stack is used for non-fair mode, and the (Fifo) |
|
96 |
* queue for fair mode. The performance of the two is generally |
|
97 |
* similar. Fifo usually supports higher throughput under |
|
98 |
* contention but Lifo maintains higher thread locality in common |
|
99 |
* applications. |
|
100 |
* |
|
101 |
* A dual queue (and similarly stack) is one that at any given |
|
102 |
* time either holds "data" -- items provided by put operations, |
|
103 |
* or "requests" -- slots representing take operations, or is |
|
104 |
* empty. A call to "fulfill" (i.e., a call requesting an item |
|
105 |
* from a queue holding data or vice versa) dequeues a |
|
106 |
* complementary node. The most interesting feature of these |
|
107 |
* queues is that any operation can figure out which mode the |
|
108 |
* queue is in, and act accordingly without needing locks. |
|
109 |
* |
|
110 |
* Both the queue and stack extend abstract class Transferer |
|
111 |
* defining the single method transfer that does a put or a |
|
112 |
* take. These are unified into a single method because in dual |
|
113 |
* data structures, the put and take operations are symmetrical, |
|
114 |
* so nearly all code can be combined. The resulting transfer |
|
115 |
* methods are on the long side, but are easier to follow than |
|
116 |
* they would be if broken up into nearly-duplicated parts. |
|
117 |
* |
|
118 |
* The queue and stack data structures share many conceptual |
|
119 |
* similarities but very few concrete details. For simplicity, |
|
120 |
* they are kept distinct so that they can later evolve |
|
121 |
* separately. |
|
122 |
* |
|
123 |
* The algorithms here differ from the versions in the above paper |
|
124 |
* in extending them for use in synchronous queues, as well as |
|
125 |
* dealing with cancellation. The main differences include: |
|
126 |
* |
|
127 |
* 1. The original algorithms used bit-marked pointers, but |
|
128 |
* the ones here use mode bits in nodes, leading to a number |
|
129 |
* of further adaptations. |
|
130 |
* 2. SynchronousQueues must block threads waiting to become |
|
131 |
* fulfilled. |
|
132 |
* 3. Support for cancellation via timeout and interrupts, |
|
133 |
* including cleaning out cancelled nodes/threads |
|
134 |
* from lists to avoid garbage retention and memory depletion. |
|
135 |
* |
|
136 |
* Blocking is mainly accomplished using LockSupport park/unpark, |
|
137 |
* except that nodes that appear to be the next ones to become |
|
138 |
* fulfilled first spin a bit (on multiprocessors only). On very |
|
139 |
* busy synchronous queues, spinning can dramatically improve |
|
140 |
* throughput. And on less busy ones, the amount of spinning is |
|
141 |
* small enough not to be noticeable. |
|
142 |
* |
|
143 |
* Cleaning is done in different ways in queues vs stacks. For |
|
144 |
* queues, we can almost always remove a node immediately in O(1) |
|
145 |
* time (modulo retries for consistency checks) when it is |
|
146 |
* cancelled. But if it may be pinned as the current tail, it must |
|
147 |
* wait until some subsequent cancellation. For stacks, we need a |
|
148 |
* potentially O(n) traversal to be sure that we can remove the |
|
149 |
* node, but this can run concurrently with other threads |
|
150 |
* accessing the stack. |
|
151 |
* |
|
152 |
* While garbage collection takes care of most node reclamation |
|
153 |
* issues that otherwise complicate nonblocking algorithms, care |
|
154 |
* is taken to "forget" references to data, other nodes, and |
|
155 |
* threads that might be held on to long-term by blocked |
|
156 |
* threads. In cases where setting to null would otherwise |
|
157 |
* conflict with main algorithms, this is done by changing a |
|
158 |
* node's link to now point to the node itself. This doesn't arise |
|
159 |
* much for Stack nodes (because blocked threads do not hang on to |
|
160 |
* old head pointers), but references in Queue nodes must be |
|
161 |
* aggressively forgotten to avoid reachability of everything any |
|
162 |
* node has ever referred to since arrival. |
|
163 |
*/ |
|
164 |
||
165 |
/** |
|
166 |
* Shared internal API for dual stacks and queues. |
|
167 |
*/ |
|
11279 | 168 |
abstract static class Transferer<E> { |
2 | 169 |
/** |
170 |
* Performs a put or take. |
|
171 |
* |
|
172 |
* @param e if non-null, the item to be handed to a consumer; |
|
173 |
* if null, requests that transfer return an item |
|
174 |
* offered by producer. |
|
175 |
* @param timed if this operation should timeout |
|
176 |
* @param nanos the timeout, in nanoseconds |
|
177 |
* @return if non-null, the item provided or received; if null, |
|
178 |
* the operation failed due to timeout or interrupt -- |
|
179 |
* the caller can distinguish which of these occurred |
|
180 |
* by checking Thread.interrupted. |
|
181 |
*/ |
|
11279 | 182 |
abstract E transfer(E e, boolean timed, long nanos); |
2 | 183 |
} |
184 |
||
185 |
/** The number of CPUs, for spin control */ |
|
186 |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
|
187 |
||
188 |
/** |
|
189 |
* The number of times to spin before blocking in timed waits. |
|
190 |
* The value is empirically derived -- it works well across a |
|
191 |
* variety of processors and OSes. Empirically, the best value |
|
192 |
* seems not to vary with number of CPUs (beyond 2) so is just |
|
193 |
* a constant. |
|
194 |
*/ |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
195 |
static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32; |
2 | 196 |
|
197 |
/** |
|
198 |
* The number of times to spin before blocking in untimed waits. |
|
199 |
* This is greater than timed value because untimed waits spin |
|
200 |
* faster since they don't need to check times on each spin. |
|
201 |
*/ |
|
202 |
static final int maxUntimedSpins = maxTimedSpins * 16; |
|
203 |
||
204 |
/** |
|
205 |
* The number of nanoseconds for which it is faster to spin |
|
206 |
* rather than to use timed park. A rough estimate suffices. |
|
207 |
*/ |
|
208 |
static final long spinForTimeoutThreshold = 1000L; |
|
209 |
||
210 |
/** Dual stack */ |
|
11279 | 211 |
static final class TransferStack<E> extends Transferer<E> { |
2 | 212 |
/* |
213 |
* This extends Scherer-Scott dual stack algorithm, differing, |
|
214 |
* among other ways, by using "covering" nodes rather than |
|
215 |
* bit-marked pointers: Fulfilling operations push on marker |
|
216 |
* nodes (with FULFILLING bit set in mode) to reserve a spot |
|
217 |
* to match a waiting node. |
|
218 |
*/ |
|
219 |
||
220 |
/* Modes for SNodes, ORed together in node fields */ |
|
221 |
/** Node represents an unfulfilled consumer */ |
|
222 |
static final int REQUEST = 0; |
|
223 |
/** Node represents an unfulfilled producer */ |
|
224 |
static final int DATA = 1; |
|
225 |
/** Node is fulfilling another unfulfilled DATA or REQUEST */ |
|
226 |
static final int FULFILLING = 2; |
|
227 |
||
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
228 |
/** Returns true if m has fulfilling bit set. */ |
2 | 229 |
static boolean isFulfilling(int m) { return (m & FULFILLING) != 0; } |
230 |
||
231 |
/** Node class for TransferStacks. */ |
|
232 |
static final class SNode { |
|
233 |
volatile SNode next; // next node in stack |
|
234 |
volatile SNode match; // the node matched to this |
|
235 |
volatile Thread waiter; // to control park/unpark |
|
236 |
Object item; // data; or null for REQUESTs |
|
237 |
int mode; |
|
238 |
// Note: item and mode fields don't need to be volatile |
|
239 |
// since they are always written before, and read after, |
|
240 |
// other volatile/atomic operations. |
|
241 |
||
242 |
SNode(Object item) { |
|
243 |
this.item = item; |
|
244 |
} |
|
245 |
||
246 |
boolean casNext(SNode cmp, SNode val) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
247 |
return cmp == next && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
248 |
UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); |
2 | 249 |
} |
250 |
||
251 |
/** |
|
252 |
* Tries to match node s to this node, if so, waking up thread. |
|
253 |
* Fulfillers call tryMatch to identify their waiters. |
|
254 |
* Waiters block until they have been matched. |
|
255 |
* |
|
256 |
* @param s the node to match |
|
257 |
* @return true if successfully matched to s |
|
258 |
*/ |
|
259 |
boolean tryMatch(SNode s) { |
|
260 |
if (match == null && |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
261 |
UNSAFE.compareAndSwapObject(this, matchOffset, null, s)) { |
2 | 262 |
Thread w = waiter; |
263 |
if (w != null) { // waiters need at most one unpark |
|
264 |
waiter = null; |
|
265 |
LockSupport.unpark(w); |
|
266 |
} |
|
267 |
return true; |
|
268 |
} |
|
269 |
return match == s; |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* Tries to cancel a wait by matching node to itself. |
|
274 |
*/ |
|
275 |
void tryCancel() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
276 |
UNSAFE.compareAndSwapObject(this, matchOffset, null, this); |
2 | 277 |
} |
278 |
||
279 |
boolean isCancelled() { |
|
280 |
return match == this; |
|
281 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
282 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
283 |
// Unsafe mechanics |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
284 |
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
|
285 |
private static final long matchOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
286 |
private static final long nextOffset; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
287 |
|
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
288 |
static { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
289 |
try { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
290 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
11279 | 291 |
Class<?> k = SNode.class; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
292 |
matchOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
293 |
(k.getDeclaredField("match")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
294 |
nextOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
295 |
(k.getDeclaredField("next")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
296 |
} catch (Exception e) { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
297 |
throw new Error(e); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
298 |
} |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
299 |
} |
2 | 300 |
} |
301 |
||
302 |
/** The head (top) of the stack */ |
|
303 |
volatile SNode head; |
|
304 |
||
305 |
boolean casHead(SNode h, SNode nh) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
306 |
return h == head && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
307 |
UNSAFE.compareAndSwapObject(this, headOffset, h, nh); |
2 | 308 |
} |
309 |
||
310 |
/** |
|
311 |
* Creates or resets fields of a node. Called only from transfer |
|
312 |
* where the node to push on stack is lazily created and |
|
313 |
* reused when possible to help reduce intervals between reads |
|
314 |
* and CASes of head and to avoid surges of garbage when CASes |
|
315 |
* to push nodes fail due to contention. |
|
316 |
*/ |
|
317 |
static SNode snode(SNode s, Object e, SNode next, int mode) { |
|
318 |
if (s == null) s = new SNode(e); |
|
319 |
s.mode = mode; |
|
320 |
s.next = next; |
|
321 |
return s; |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* Puts or takes an item. |
|
326 |
*/ |
|
11279 | 327 |
@SuppressWarnings("unchecked") |
328 |
E transfer(E e, boolean timed, long nanos) { |
|
2 | 329 |
/* |
330 |
* Basic algorithm is to loop trying one of three actions: |
|
331 |
* |
|
332 |
* 1. If apparently empty or already containing nodes of same |
|
333 |
* mode, try to push node on stack and wait for a match, |
|
334 |
* returning it, or null if cancelled. |
|
335 |
* |
|
336 |
* 2. If apparently containing node of complementary mode, |
|
337 |
* try to push a fulfilling node on to stack, match |
|
338 |
* with corresponding waiting node, pop both from |
|
339 |
* stack, and return matched item. The matching or |
|
340 |
* unlinking might not actually be necessary because of |
|
341 |
* other threads performing action 3: |
|
342 |
* |
|
343 |
* 3. If top of stack already holds another fulfilling node, |
|
344 |
* help it out by doing its match and/or pop |
|
345 |
* operations, and then continue. The code for helping |
|
346 |
* is essentially the same as for fulfilling, except |
|
347 |
* that it doesn't return the item. |
|
348 |
*/ |
|
349 |
||
350 |
SNode s = null; // constructed/reused as needed |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
351 |
int mode = (e == null) ? REQUEST : DATA; |
2 | 352 |
|
353 |
for (;;) { |
|
354 |
SNode h = head; |
|
355 |
if (h == null || h.mode == mode) { // empty or same-mode |
|
356 |
if (timed && nanos <= 0) { // can't wait |
|
357 |
if (h != null && h.isCancelled()) |
|
358 |
casHead(h, h.next); // pop cancelled node |
|
359 |
else |
|
360 |
return null; |
|
361 |
} else if (casHead(h, s = snode(s, e, h, mode))) { |
|
362 |
SNode m = awaitFulfill(s, timed, nanos); |
|
363 |
if (m == s) { // wait was cancelled |
|
364 |
clean(s); |
|
365 |
return null; |
|
366 |
} |
|
367 |
if ((h = head) != null && h.next == s) |
|
368 |
casHead(h, s.next); // help s's fulfiller |
|
11279 | 369 |
return (E) ((mode == REQUEST) ? m.item : s.item); |
2 | 370 |
} |
371 |
} else if (!isFulfilling(h.mode)) { // try to fulfill |
|
372 |
if (h.isCancelled()) // already cancelled |
|
373 |
casHead(h, h.next); // pop and retry |
|
374 |
else if (casHead(h, s=snode(s, e, h, FULFILLING|mode))) { |
|
375 |
for (;;) { // loop until matched or waiters disappear |
|
376 |
SNode m = s.next; // m is s's match |
|
377 |
if (m == null) { // all waiters are gone |
|
378 |
casHead(s, null); // pop fulfill node |
|
379 |
s = null; // use new node next time |
|
380 |
break; // restart main loop |
|
381 |
} |
|
382 |
SNode mn = m.next; |
|
383 |
if (m.tryMatch(s)) { |
|
384 |
casHead(s, mn); // pop both s and m |
|
11279 | 385 |
return (E) ((mode == REQUEST) ? m.item : s.item); |
2 | 386 |
} else // lost match |
387 |
s.casNext(m, mn); // help unlink |
|
388 |
} |
|
389 |
} |
|
390 |
} else { // help a fulfiller |
|
391 |
SNode m = h.next; // m is h's match |
|
392 |
if (m == null) // waiter is gone |
|
393 |
casHead(h, null); // pop fulfilling node |
|
394 |
else { |
|
395 |
SNode mn = m.next; |
|
396 |
if (m.tryMatch(h)) // help match |
|
397 |
casHead(h, mn); // pop both h and m |
|
398 |
else // lost match |
|
399 |
h.casNext(m, mn); // help unlink |
|
400 |
} |
|
401 |
} |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Spins/blocks until node s is matched by a fulfill operation. |
|
407 |
* |
|
408 |
* @param s the waiting node |
|
409 |
* @param timed true if timed wait |
|
410 |
* @param nanos timeout value |
|
411 |
* @return matched node, or s if cancelled |
|
412 |
*/ |
|
413 |
SNode awaitFulfill(SNode s, boolean timed, long nanos) { |
|
414 |
/* |
|
415 |
* When a node/thread is about to block, it sets its waiter |
|
416 |
* field and then rechecks state at least one more time |
|
417 |
* before actually parking, thus covering race vs |
|
418 |
* fulfiller noticing that waiter is non-null so should be |
|
419 |
* woken. |
|
420 |
* |
|
421 |
* When invoked by nodes that appear at the point of call |
|
422 |
* to be at the head of the stack, calls to park are |
|
423 |
* preceded by spins to avoid blocking when producers and |
|
424 |
* consumers are arriving very close in time. This can |
|
425 |
* happen enough to bother only on multiprocessors. |
|
426 |
* |
|
427 |
* The order of checks for returning out of main loop |
|
428 |
* reflects fact that interrupts have precedence over |
|
429 |
* normal returns, which have precedence over |
|
430 |
* timeouts. (So, on timeout, one last check for match is |
|
431 |
* done before giving up.) Except that calls from untimed |
|
432 |
* SynchronousQueue.{poll/offer} don't check interrupts |
|
433 |
* and don't wait at all, so are trapped in transfer |
|
434 |
* method rather than calling awaitFulfill. |
|
435 |
*/ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
436 |
final long deadline = timed ? System.nanoTime() + nanos : 0L; |
2 | 437 |
Thread w = Thread.currentThread(); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
438 |
int spins = (shouldSpin(s) ? |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
439 |
(timed ? maxTimedSpins : maxUntimedSpins) : 0); |
2 | 440 |
for (;;) { |
441 |
if (w.isInterrupted()) |
|
442 |
s.tryCancel(); |
|
443 |
SNode m = s.match; |
|
444 |
if (m != null) |
|
445 |
return m; |
|
446 |
if (timed) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
447 |
nanos = deadline - System.nanoTime(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
448 |
if (nanos <= 0L) { |
2 | 449 |
s.tryCancel(); |
450 |
continue; |
|
451 |
} |
|
452 |
} |
|
453 |
if (spins > 0) |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
454 |
spins = shouldSpin(s) ? (spins-1) : 0; |
2 | 455 |
else if (s.waiter == null) |
456 |
s.waiter = w; // establish waiter so can park next iter |
|
457 |
else if (!timed) |
|
458 |
LockSupport.park(this); |
|
459 |
else if (nanos > spinForTimeoutThreshold) |
|
460 |
LockSupport.parkNanos(this, nanos); |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
/** |
|
465 |
* Returns true if node s is at head or there is an active |
|
466 |
* fulfiller. |
|
467 |
*/ |
|
468 |
boolean shouldSpin(SNode s) { |
|
469 |
SNode h = head; |
|
470 |
return (h == s || h == null || isFulfilling(h.mode)); |
|
471 |
} |
|
472 |
||
473 |
/** |
|
474 |
* Unlinks s from the stack. |
|
475 |
*/ |
|
476 |
void clean(SNode s) { |
|
477 |
s.item = null; // forget item |
|
478 |
s.waiter = null; // forget thread |
|
479 |
||
480 |
/* |
|
481 |
* At worst we may need to traverse entire stack to unlink |
|
482 |
* s. If there are multiple concurrent calls to clean, we |
|
483 |
* might not see s if another thread has already removed |
|
484 |
* it. But we can stop when we see any node known to |
|
485 |
* follow s. We use s.next unless it too is cancelled, in |
|
486 |
* which case we try the node one past. We don't check any |
|
487 |
* further because we don't want to doubly traverse just to |
|
488 |
* find sentinel. |
|
489 |
*/ |
|
490 |
||
491 |
SNode past = s.next; |
|
492 |
if (past != null && past.isCancelled()) |
|
493 |
past = past.next; |
|
494 |
||
495 |
// Absorb cancelled nodes at head |
|
496 |
SNode p; |
|
497 |
while ((p = head) != null && p != past && p.isCancelled()) |
|
498 |
casHead(p, p.next); |
|
499 |
||
500 |
// Unsplice embedded nodes |
|
501 |
while (p != null && p != past) { |
|
502 |
SNode n = p.next; |
|
503 |
if (n != null && n.isCancelled()) |
|
504 |
p.casNext(n, n.next); |
|
505 |
else |
|
506 |
p = n; |
|
507 |
} |
|
508 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
509 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
510 |
// Unsafe mechanics |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
511 |
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
|
512 |
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
|
513 |
static { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
514 |
try { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
515 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
11279 | 516 |
Class<?> k = TransferStack.class; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
517 |
headOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
518 |
(k.getDeclaredField("head")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
519 |
} catch (Exception e) { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
520 |
throw new Error(e); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
521 |
} |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
522 |
} |
2 | 523 |
} |
524 |
||
525 |
/** Dual Queue */ |
|
11279 | 526 |
static final class TransferQueue<E> extends Transferer<E> { |
2 | 527 |
/* |
528 |
* This extends Scherer-Scott dual queue algorithm, differing, |
|
529 |
* among other ways, by using modes within nodes rather than |
|
530 |
* marked pointers. The algorithm is a little simpler than |
|
531 |
* that for stacks because fulfillers do not need explicit |
|
532 |
* nodes, and matching is done by CAS'ing QNode.item field |
|
533 |
* from non-null to null (for put) or vice versa (for take). |
|
534 |
*/ |
|
535 |
||
536 |
/** Node class for TransferQueue. */ |
|
537 |
static final class QNode { |
|
538 |
volatile QNode next; // next node in queue |
|
539 |
volatile Object item; // CAS'ed to or from null |
|
540 |
volatile Thread waiter; // to control park/unpark |
|
541 |
final boolean isData; |
|
542 |
||
543 |
QNode(Object item, boolean isData) { |
|
544 |
this.item = item; |
|
545 |
this.isData = isData; |
|
546 |
} |
|
547 |
||
548 |
boolean casNext(QNode cmp, QNode val) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
549 |
return next == cmp && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
550 |
UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); |
2 | 551 |
} |
552 |
||
553 |
boolean casItem(Object cmp, Object val) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
554 |
return item == cmp && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
555 |
UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); |
2 | 556 |
} |
557 |
||
558 |
/** |
|
559 |
* Tries to cancel by CAS'ing ref to this as item. |
|
560 |
*/ |
|
561 |
void tryCancel(Object cmp) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
562 |
UNSAFE.compareAndSwapObject(this, itemOffset, cmp, this); |
2 | 563 |
} |
564 |
||
565 |
boolean isCancelled() { |
|
566 |
return item == this; |
|
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* Returns true if this node is known to be off the queue |
|
571 |
* because its next pointer has been forgotten due to |
|
572 |
* an advanceHead operation. |
|
573 |
*/ |
|
574 |
boolean isOffList() { |
|
575 |
return next == this; |
|
576 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
577 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
578 |
// Unsafe mechanics |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
579 |
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
|
580 |
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
|
581 |
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
|
582 |
|
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
583 |
static { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
584 |
try { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
585 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
11279 | 586 |
Class<?> k = QNode.class; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
587 |
itemOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
588 |
(k.getDeclaredField("item")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
589 |
nextOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
590 |
(k.getDeclaredField("next")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
591 |
} catch (Exception e) { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
592 |
throw new Error(e); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
593 |
} |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
594 |
} |
2 | 595 |
} |
596 |
||
597 |
/** Head of queue */ |
|
598 |
transient volatile QNode head; |
|
599 |
/** Tail of queue */ |
|
600 |
transient volatile QNode tail; |
|
601 |
/** |
|
602 |
* Reference to a cancelled node that might not yet have been |
|
603 |
* unlinked from queue because it was the last inserted node |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
604 |
* when it was cancelled. |
2 | 605 |
*/ |
606 |
transient volatile QNode cleanMe; |
|
607 |
||
608 |
TransferQueue() { |
|
609 |
QNode h = new QNode(null, false); // initialize to dummy node. |
|
610 |
head = h; |
|
611 |
tail = h; |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* Tries to cas nh as new head; if successful, unlink |
|
616 |
* old head's next node to avoid garbage retention. |
|
617 |
*/ |
|
618 |
void advanceHead(QNode h, QNode nh) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
619 |
if (h == head && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
620 |
UNSAFE.compareAndSwapObject(this, headOffset, h, nh)) |
2 | 621 |
h.next = h; // forget old next |
622 |
} |
|
623 |
||
624 |
/** |
|
625 |
* Tries to cas nt as new tail. |
|
626 |
*/ |
|
627 |
void advanceTail(QNode t, QNode nt) { |
|
628 |
if (tail == t) |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
629 |
UNSAFE.compareAndSwapObject(this, tailOffset, t, nt); |
2 | 630 |
} |
631 |
||
632 |
/** |
|
633 |
* Tries to CAS cleanMe slot. |
|
634 |
*/ |
|
635 |
boolean casCleanMe(QNode cmp, QNode val) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
636 |
return cleanMe == cmp && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
637 |
UNSAFE.compareAndSwapObject(this, cleanMeOffset, cmp, val); |
2 | 638 |
} |
639 |
||
640 |
/** |
|
641 |
* Puts or takes an item. |
|
642 |
*/ |
|
11279 | 643 |
@SuppressWarnings("unchecked") |
644 |
E transfer(E e, boolean timed, long nanos) { |
|
2 | 645 |
/* Basic algorithm is to loop trying to take either of |
646 |
* two actions: |
|
647 |
* |
|
648 |
* 1. If queue apparently empty or holding same-mode nodes, |
|
649 |
* try to add node to queue of waiters, wait to be |
|
650 |
* fulfilled (or cancelled) and return matching item. |
|
651 |
* |
|
652 |
* 2. If queue apparently contains waiting items, and this |
|
653 |
* call is of complementary mode, try to fulfill by CAS'ing |
|
654 |
* item field of waiting node and dequeuing it, and then |
|
655 |
* returning matching item. |
|
656 |
* |
|
657 |
* In each case, along the way, check for and try to help |
|
658 |
* advance head and tail on behalf of other stalled/slow |
|
659 |
* threads. |
|
660 |
* |
|
661 |
* The loop starts off with a null check guarding against |
|
662 |
* seeing uninitialized head or tail values. This never |
|
663 |
* happens in current SynchronousQueue, but could if |
|
664 |
* callers held non-volatile/final ref to the |
|
665 |
* transferer. The check is here anyway because it places |
|
666 |
* null checks at top of loop, which is usually faster |
|
667 |
* than having them implicitly interspersed. |
|
668 |
*/ |
|
669 |
||
670 |
QNode s = null; // constructed/reused as needed |
|
671 |
boolean isData = (e != null); |
|
672 |
||
673 |
for (;;) { |
|
674 |
QNode t = tail; |
|
675 |
QNode h = head; |
|
676 |
if (t == null || h == null) // saw uninitialized value |
|
677 |
continue; // spin |
|
678 |
||
679 |
if (h == t || t.isData == isData) { // empty or same-mode |
|
680 |
QNode tn = t.next; |
|
681 |
if (t != tail) // inconsistent read |
|
682 |
continue; |
|
683 |
if (tn != null) { // lagging tail |
|
684 |
advanceTail(t, tn); |
|
685 |
continue; |
|
686 |
} |
|
687 |
if (timed && nanos <= 0) // can't wait |
|
688 |
return null; |
|
689 |
if (s == null) |
|
690 |
s = new QNode(e, isData); |
|
691 |
if (!t.casNext(null, s)) // failed to link in |
|
692 |
continue; |
|
693 |
||
694 |
advanceTail(t, s); // swing tail and wait |
|
695 |
Object x = awaitFulfill(s, e, timed, nanos); |
|
696 |
if (x == s) { // wait was cancelled |
|
697 |
clean(t, s); |
|
698 |
return null; |
|
699 |
} |
|
700 |
||
701 |
if (!s.isOffList()) { // not already unlinked |
|
702 |
advanceHead(t, s); // unlink if head |
|
703 |
if (x != null) // and forget fields |
|
704 |
s.item = s; |
|
705 |
s.waiter = null; |
|
706 |
} |
|
11279 | 707 |
return (x != null) ? (E)x : e; |
2 | 708 |
|
709 |
} else { // complementary-mode |
|
710 |
QNode m = h.next; // node to fulfill |
|
711 |
if (t != tail || m == null || h != head) |
|
712 |
continue; // inconsistent read |
|
713 |
||
714 |
Object x = m.item; |
|
715 |
if (isData == (x != null) || // m already fulfilled |
|
716 |
x == m || // m cancelled |
|
717 |
!m.casItem(x, e)) { // lost CAS |
|
718 |
advanceHead(h, m); // dequeue and retry |
|
719 |
continue; |
|
720 |
} |
|
721 |
||
722 |
advanceHead(h, m); // successfully fulfilled |
|
723 |
LockSupport.unpark(m.waiter); |
|
11279 | 724 |
return (x != null) ? (E)x : e; |
2 | 725 |
} |
726 |
} |
|
727 |
} |
|
728 |
||
729 |
/** |
|
730 |
* Spins/blocks until node s is fulfilled. |
|
731 |
* |
|
732 |
* @param s the waiting node |
|
733 |
* @param e the comparison value for checking match |
|
734 |
* @param timed true if timed wait |
|
735 |
* @param nanos timeout value |
|
736 |
* @return matched item, or s if cancelled |
|
737 |
*/ |
|
11279 | 738 |
Object awaitFulfill(QNode s, E e, boolean timed, long nanos) { |
2 | 739 |
/* Same idea as TransferStack.awaitFulfill */ |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
740 |
final long deadline = timed ? System.nanoTime() + nanos : 0L; |
2 | 741 |
Thread w = Thread.currentThread(); |
742 |
int spins = ((head.next == s) ? |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
743 |
(timed ? maxTimedSpins : maxUntimedSpins) : 0); |
2 | 744 |
for (;;) { |
745 |
if (w.isInterrupted()) |
|
746 |
s.tryCancel(e); |
|
747 |
Object x = s.item; |
|
748 |
if (x != e) |
|
749 |
return x; |
|
750 |
if (timed) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
751 |
nanos = deadline - System.nanoTime(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
752 |
if (nanos <= 0L) { |
2 | 753 |
s.tryCancel(e); |
754 |
continue; |
|
755 |
} |
|
756 |
} |
|
757 |
if (spins > 0) |
|
758 |
--spins; |
|
759 |
else if (s.waiter == null) |
|
760 |
s.waiter = w; |
|
761 |
else if (!timed) |
|
762 |
LockSupport.park(this); |
|
763 |
else if (nanos > spinForTimeoutThreshold) |
|
764 |
LockSupport.parkNanos(this, nanos); |
|
765 |
} |
|
766 |
} |
|
767 |
||
768 |
/** |
|
769 |
* Gets rid of cancelled node s with original predecessor pred. |
|
770 |
*/ |
|
771 |
void clean(QNode pred, QNode s) { |
|
772 |
s.waiter = null; // forget thread |
|
773 |
/* |
|
774 |
* At any given time, exactly one node on list cannot be |
|
775 |
* deleted -- the last inserted node. To accommodate this, |
|
776 |
* if we cannot delete s, we save its predecessor as |
|
777 |
* "cleanMe", deleting the previously saved version |
|
778 |
* first. At least one of node s or the node previously |
|
779 |
* saved can always be deleted, so this always terminates. |
|
780 |
*/ |
|
781 |
while (pred.next == s) { // Return early if already unlinked |
|
782 |
QNode h = head; |
|
783 |
QNode hn = h.next; // Absorb cancelled first node as head |
|
784 |
if (hn != null && hn.isCancelled()) { |
|
785 |
advanceHead(h, hn); |
|
786 |
continue; |
|
787 |
} |
|
788 |
QNode t = tail; // Ensure consistent read for tail |
|
789 |
if (t == h) |
|
790 |
return; |
|
791 |
QNode tn = t.next; |
|
792 |
if (t != tail) |
|
793 |
continue; |
|
794 |
if (tn != null) { |
|
795 |
advanceTail(t, tn); |
|
796 |
continue; |
|
797 |
} |
|
798 |
if (s != t) { // If not tail, try to unsplice |
|
799 |
QNode sn = s.next; |
|
800 |
if (sn == s || pred.casNext(s, sn)) |
|
801 |
return; |
|
802 |
} |
|
803 |
QNode dp = cleanMe; |
|
804 |
if (dp != null) { // Try unlinking previous cancelled node |
|
805 |
QNode d = dp.next; |
|
806 |
QNode dn; |
|
807 |
if (d == null || // d is gone or |
|
808 |
d == dp || // d is off list or |
|
809 |
!d.isCancelled() || // d not cancelled or |
|
810 |
(d != t && // d not tail and |
|
811 |
(dn = d.next) != null && // has successor |
|
812 |
dn != d && // that is on list |
|
813 |
dp.casNext(d, dn))) // d unspliced |
|
814 |
casCleanMe(dp, null); |
|
815 |
if (dp == pred) |
|
816 |
return; // s is already saved node |
|
817 |
} else if (casCleanMe(null, pred)) |
|
818 |
return; // Postpone cleaning s |
|
819 |
} |
|
820 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
821 |
|
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
822 |
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
|
823 |
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
|
824 |
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
|
825 |
private static final long cleanMeOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
826 |
static { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
827 |
try { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
828 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
11279 | 829 |
Class<?> k = TransferQueue.class; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
830 |
headOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
831 |
(k.getDeclaredField("head")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
832 |
tailOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
833 |
(k.getDeclaredField("tail")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
834 |
cleanMeOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
835 |
(k.getDeclaredField("cleanMe")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
836 |
} catch (Exception e) { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
837 |
throw new Error(e); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
838 |
} |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
839 |
} |
2 | 840 |
} |
841 |
||
842 |
/** |
|
843 |
* The transferer. Set only in constructor, but cannot be declared |
|
844 |
* as final without further complicating serialization. Since |
|
845 |
* this is accessed only at most once per public method, there |
|
846 |
* isn't a noticeable performance penalty for using volatile |
|
847 |
* instead of final here. |
|
848 |
*/ |
|
11279 | 849 |
private transient volatile Transferer<E> transferer; |
2 | 850 |
|
851 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
852 |
* Creates a {@code SynchronousQueue} with nonfair access policy. |
2 | 853 |
*/ |
854 |
public SynchronousQueue() { |
|
855 |
this(false); |
|
856 |
} |
|
857 |
||
858 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
859 |
* Creates a {@code SynchronousQueue} with the specified fairness policy. |
2 | 860 |
* |
861 |
* @param fair if true, waiting threads contend in FIFO order for |
|
862 |
* access; otherwise the order is unspecified. |
|
863 |
*/ |
|
864 |
public SynchronousQueue(boolean fair) { |
|
11279 | 865 |
transferer = fair ? new TransferQueue<E>() : new TransferStack<E>(); |
2 | 866 |
} |
867 |
||
868 |
/** |
|
869 |
* Adds the specified element to this queue, waiting if necessary for |
|
870 |
* another thread to receive it. |
|
871 |
* |
|
872 |
* @throws InterruptedException {@inheritDoc} |
|
873 |
* @throws NullPointerException {@inheritDoc} |
|
874 |
*/ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
875 |
public void put(E e) throws InterruptedException { |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
876 |
if (e == null) throw new NullPointerException(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
877 |
if (transferer.transfer(e, false, 0) == null) { |
2 | 878 |
Thread.interrupted(); |
879 |
throw new InterruptedException(); |
|
880 |
} |
|
881 |
} |
|
882 |
||
883 |
/** |
|
884 |
* Inserts the specified element into this queue, waiting if necessary |
|
885 |
* up to the specified wait time for another thread to receive it. |
|
886 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
887 |
* @return {@code true} if successful, or {@code false} if the |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
888 |
* specified waiting time elapses before a consumer appears |
2 | 889 |
* @throws InterruptedException {@inheritDoc} |
890 |
* @throws NullPointerException {@inheritDoc} |
|
891 |
*/ |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
892 |
public boolean offer(E e, long timeout, TimeUnit unit) |
2 | 893 |
throws InterruptedException { |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
894 |
if (e == null) throw new NullPointerException(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
895 |
if (transferer.transfer(e, true, unit.toNanos(timeout)) != null) |
2 | 896 |
return true; |
897 |
if (!Thread.interrupted()) |
|
898 |
return false; |
|
899 |
throw new InterruptedException(); |
|
900 |
} |
|
901 |
||
902 |
/** |
|
903 |
* Inserts the specified element into this queue, if another thread is |
|
904 |
* waiting to receive it. |
|
905 |
* |
|
906 |
* @param e the element to add |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
907 |
* @return {@code true} if the element was added to this queue, else |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
908 |
* {@code false} |
2 | 909 |
* @throws NullPointerException if the specified element is null |
910 |
*/ |
|
911 |
public boolean offer(E e) { |
|
912 |
if (e == null) throw new NullPointerException(); |
|
913 |
return transferer.transfer(e, true, 0) != null; |
|
914 |
} |
|
915 |
||
916 |
/** |
|
917 |
* Retrieves and removes the head of this queue, waiting if necessary |
|
918 |
* for another thread to insert it. |
|
919 |
* |
|
920 |
* @return the head of this queue |
|
921 |
* @throws InterruptedException {@inheritDoc} |
|
922 |
*/ |
|
923 |
public E take() throws InterruptedException { |
|
11279 | 924 |
E e = transferer.transfer(null, false, 0); |
2 | 925 |
if (e != null) |
11279 | 926 |
return e; |
2 | 927 |
Thread.interrupted(); |
928 |
throw new InterruptedException(); |
|
929 |
} |
|
930 |
||
931 |
/** |
|
932 |
* Retrieves and removes the head of this queue, waiting |
|
933 |
* if necessary up to the specified wait time, for another thread |
|
934 |
* to insert it. |
|
935 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
936 |
* @return the head of this queue, or {@code null} if the |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
937 |
* specified waiting time elapses before an element is present |
2 | 938 |
* @throws InterruptedException {@inheritDoc} |
939 |
*/ |
|
940 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
|
11279 | 941 |
E e = transferer.transfer(null, true, unit.toNanos(timeout)); |
2 | 942 |
if (e != null || !Thread.interrupted()) |
11279 | 943 |
return e; |
2 | 944 |
throw new InterruptedException(); |
945 |
} |
|
946 |
||
947 |
/** |
|
948 |
* Retrieves and removes the head of this queue, if another thread |
|
949 |
* is currently making an element available. |
|
950 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
951 |
* @return the head of this queue, or {@code null} if no |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
952 |
* element is available |
2 | 953 |
*/ |
954 |
public E poll() { |
|
11279 | 955 |
return transferer.transfer(null, true, 0); |
2 | 956 |
} |
957 |
||
958 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
959 |
* Always returns {@code true}. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
960 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 961 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
962 |
* @return {@code true} |
2 | 963 |
*/ |
964 |
public boolean isEmpty() { |
|
965 |
return true; |
|
966 |
} |
|
967 |
||
968 |
/** |
|
969 |
* Always returns zero. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
970 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 971 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
972 |
* @return zero |
2 | 973 |
*/ |
974 |
public int size() { |
|
975 |
return 0; |
|
976 |
} |
|
977 |
||
978 |
/** |
|
979 |
* Always returns zero. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
980 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 981 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
982 |
* @return zero |
2 | 983 |
*/ |
984 |
public int remainingCapacity() { |
|
985 |
return 0; |
|
986 |
} |
|
987 |
||
988 |
/** |
|
989 |
* Does nothing. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
990 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 991 |
*/ |
992 |
public void clear() { |
|
993 |
} |
|
994 |
||
995 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
996 |
* Always returns {@code false}. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
997 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 998 |
* |
999 |
* @param o the element |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1000 |
* @return {@code false} |
2 | 1001 |
*/ |
1002 |
public boolean contains(Object o) { |
|
1003 |
return false; |
|
1004 |
} |
|
1005 |
||
1006 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1007 |
* Always returns {@code false}. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1008 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 1009 |
* |
1010 |
* @param o the element to remove |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1011 |
* @return {@code false} |
2 | 1012 |
*/ |
1013 |
public boolean remove(Object o) { |
|
1014 |
return false; |
|
1015 |
} |
|
1016 |
||
1017 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1018 |
* Returns {@code false} unless the given collection is empty. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1019 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 1020 |
* |
1021 |
* @param c the collection |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1022 |
* @return {@code false} unless given collection is empty |
2 | 1023 |
*/ |
1024 |
public boolean containsAll(Collection<?> c) { |
|
1025 |
return c.isEmpty(); |
|
1026 |
} |
|
1027 |
||
1028 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1029 |
* Always returns {@code false}. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1030 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 1031 |
* |
1032 |
* @param c the collection |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1033 |
* @return {@code false} |
2 | 1034 |
*/ |
1035 |
public boolean removeAll(Collection<?> c) { |
|
1036 |
return false; |
|
1037 |
} |
|
1038 |
||
1039 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1040 |
* Always returns {@code false}. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1041 |
* A {@code SynchronousQueue} has no internal capacity. |
2 | 1042 |
* |
1043 |
* @param c the collection |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1044 |
* @return {@code false} |
2 | 1045 |
*/ |
1046 |
public boolean retainAll(Collection<?> c) { |
|
1047 |
return false; |
|
1048 |
} |
|
1049 |
||
1050 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1051 |
* Always returns {@code null}. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1052 |
* A {@code SynchronousQueue} does not return elements |
2 | 1053 |
* unless actively waited on. |
1054 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1055 |
* @return {@code null} |
2 | 1056 |
*/ |
1057 |
public E peek() { |
|
1058 |
return null; |
|
1059 |
} |
|
1060 |
||
1061 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1062 |
* Returns an empty iterator in which {@code hasNext} always returns |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1063 |
* {@code false}. |
2 | 1064 |
* |
1065 |
* @return an empty iterator |
|
1066 |
*/ |
|
1067 |
public Iterator<E> iterator() { |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1068 |
return Collections.emptyIterator(); |
11279 | 1069 |
} |
1070 |
||
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1071 |
/** |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1072 |
* Returns an empty spliterator in which calls to |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1073 |
* {@link java.util.Spliterator#trySplit()} always return {@code null}. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1074 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1075 |
* @return an empty spliterator |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1076 |
* @since 1.8 |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1077 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1078 |
public Spliterator<E> spliterator() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1079 |
return Spliterators.emptySpliterator(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1080 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1081 |
|
2 | 1082 |
/** |
1083 |
* Returns a zero-length array. |
|
1084 |
* @return a zero-length array |
|
1085 |
*/ |
|
1086 |
public Object[] toArray() { |
|
1087 |
return new Object[0]; |
|
1088 |
} |
|
1089 |
||
1090 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1091 |
* Sets the zeroeth element of the specified array to {@code null} |
2 | 1092 |
* (if the array has non-zero length) and returns it. |
1093 |
* |
|
1094 |
* @param a the array |
|
1095 |
* @return the specified array |
|
1096 |
* @throws NullPointerException if the specified array is null |
|
1097 |
*/ |
|
1098 |
public <T> T[] toArray(T[] a) { |
|
1099 |
if (a.length > 0) |
|
1100 |
a[0] = null; |
|
1101 |
return a; |
|
1102 |
} |
|
1103 |
||
1104 |
/** |
|
1105 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
1106 |
* @throws ClassCastException {@inheritDoc} |
|
1107 |
* @throws NullPointerException {@inheritDoc} |
|
1108 |
* @throws IllegalArgumentException {@inheritDoc} |
|
1109 |
*/ |
|
1110 |
public int drainTo(Collection<? super E> c) { |
|
1111 |
if (c == null) |
|
1112 |
throw new NullPointerException(); |
|
1113 |
if (c == this) |
|
1114 |
throw new IllegalArgumentException(); |
|
1115 |
int n = 0; |
|
11279 | 1116 |
for (E e; (e = poll()) != null;) { |
2 | 1117 |
c.add(e); |
1118 |
++n; |
|
1119 |
} |
|
1120 |
return n; |
|
1121 |
} |
|
1122 |
||
1123 |
/** |
|
1124 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
1125 |
* @throws ClassCastException {@inheritDoc} |
|
1126 |
* @throws NullPointerException {@inheritDoc} |
|
1127 |
* @throws IllegalArgumentException {@inheritDoc} |
|
1128 |
*/ |
|
1129 |
public int drainTo(Collection<? super E> c, int maxElements) { |
|
1130 |
if (c == null) |
|
1131 |
throw new NullPointerException(); |
|
1132 |
if (c == this) |
|
1133 |
throw new IllegalArgumentException(); |
|
1134 |
int n = 0; |
|
11279 | 1135 |
for (E e; n < maxElements && (e = poll()) != null;) { |
2 | 1136 |
c.add(e); |
1137 |
++n; |
|
1138 |
} |
|
1139 |
return n; |
|
1140 |
} |
|
1141 |
||
1142 |
/* |
|
1143 |
* To cope with serialization strategy in the 1.5 version of |
|
1144 |
* SynchronousQueue, we declare some unused classes and fields |
|
1145 |
* that exist solely to enable serializability across versions. |
|
1146 |
* These fields are never used, so are initialized only if this |
|
1147 |
* object is ever serialized or deserialized. |
|
1148 |
*/ |
|
1149 |
||
11279 | 1150 |
@SuppressWarnings("serial") |
2 | 1151 |
static class WaitQueue implements java.io.Serializable { } |
1152 |
static class LifoWaitQueue extends WaitQueue { |
|
1153 |
private static final long serialVersionUID = -3633113410248163686L; |
|
1154 |
} |
|
1155 |
static class FifoWaitQueue extends WaitQueue { |
|
1156 |
private static final long serialVersionUID = -3623113410248163686L; |
|
1157 |
} |
|
1158 |
private ReentrantLock qlock; |
|
1159 |
private WaitQueue waitingProducers; |
|
1160 |
private WaitQueue waitingConsumers; |
|
1161 |
||
1162 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1163 |
* Saves this queue to a stream (that is, serializes it). |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1164 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1165 |
* @throws java.io.IOException if an I/O error occurs |
2 | 1166 |
*/ |
1167 |
private void writeObject(java.io.ObjectOutputStream s) |
|
1168 |
throws java.io.IOException { |
|
1169 |
boolean fair = transferer instanceof TransferQueue; |
|
1170 |
if (fair) { |
|
1171 |
qlock = new ReentrantLock(true); |
|
1172 |
waitingProducers = new FifoWaitQueue(); |
|
1173 |
waitingConsumers = new FifoWaitQueue(); |
|
1174 |
} |
|
1175 |
else { |
|
1176 |
qlock = new ReentrantLock(); |
|
1177 |
waitingProducers = new LifoWaitQueue(); |
|
1178 |
waitingConsumers = new LifoWaitQueue(); |
|
1179 |
} |
|
1180 |
s.defaultWriteObject(); |
|
1181 |
} |
|
1182 |
||
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1183 |
/** |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1184 |
* 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
|
1185 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1186 |
* @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
|
1187 |
* could not be found |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1188 |
* @throws java.io.IOException if an I/O error occurs |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1189 |
*/ |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1190 |
private void readObject(java.io.ObjectInputStream s) |
2 | 1191 |
throws java.io.IOException, ClassNotFoundException { |
1192 |
s.defaultReadObject(); |
|
1193 |
if (waitingProducers instanceof FifoWaitQueue) |
|
11279 | 1194 |
transferer = new TransferQueue<E>(); |
2 | 1195 |
else |
11279 | 1196 |
transferer = new TransferStack<E>(); |
2 | 1197 |
} |
1198 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1199 |
// Unsafe mechanics |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1200 |
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1201 |
String field, Class<?> klazz) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1202 |
try { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1203 |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1204 |
} catch (NoSuchFieldException e) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1205 |
// Convert Exception to corresponding Error |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1206 |
NoSuchFieldError error = new NoSuchFieldError(field); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1207 |
error.initCause(e); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1208 |
throw error; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1209 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1210 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1211 |
|
2 | 1212 |
} |