1 |
/* |
2 |
* Written by Doug Lea, Bill Scherer, and Michael Scott with |
3 |
* assistance from members of JCP JSR-166 Expert Group and released to |
4 |
* the public domain, as explained at |
5 |
* http://creativecommons.org/licenses/publicdomain |
6 |
*/ |
7 |
|
8 |
package java.util.concurrent; |
9 |
import java.util.concurrent.atomic.*; |
10 |
import java.util.concurrent.locks.LockSupport; |
11 |
|
12 |
/** |
13 |
* A synchronization point at which threads can pair and swap elements |
14 |
* within pairs. Each thread presents some object on entry to the |
15 |
* {@link #exchange exchange} method, matches with a partner thread, |
16 |
* and receives its partner's object on return. An Exchanger may be |
17 |
* viewed as a bidirectional form of a {@link |
18 |
* SynchronousQueue}. Exchangers may be useful in applications such as |
19 |
* genetic algorithms and pipeline designs. |
20 |
* |
21 |
* <p><b>Sample Usage:</b> |
22 |
* Here are the highlights of a class that uses an {@code Exchanger} |
23 |
* to swap buffers between threads so that the thread filling the |
24 |
* buffer gets a freshly emptied one when it needs it, handing off the |
25 |
* filled one to the thread emptying the buffer. |
26 |
* <pre>{@code |
27 |
* class FillAndEmpty { |
28 |
* Exchanger<DataBuffer> exchanger = new Exchanger<DataBuffer>(); |
29 |
* DataBuffer initialEmptyBuffer = ... a made-up type |
30 |
* DataBuffer initialFullBuffer = ... |
31 |
* |
32 |
* class FillingLoop implements Runnable { |
33 |
* public void run() { |
34 |
* DataBuffer currentBuffer = initialEmptyBuffer; |
35 |
* try { |
36 |
* while (currentBuffer != null) { |
37 |
* addToBuffer(currentBuffer); |
38 |
* if (currentBuffer.isFull()) |
39 |
* currentBuffer = exchanger.exchange(currentBuffer); |
40 |
* } |
41 |
* } catch (InterruptedException ex) { ... handle ... } |
42 |
* } |
43 |
* } |
44 |
* |
45 |
* class EmptyingLoop implements Runnable { |
46 |
* public void run() { |
47 |
* DataBuffer currentBuffer = initialFullBuffer; |
48 |
* try { |
49 |
* while (currentBuffer != null) { |
50 |
* takeFromBuffer(currentBuffer); |
51 |
* if (currentBuffer.isEmpty()) |
52 |
* currentBuffer = exchanger.exchange(currentBuffer); |
53 |
* } |
54 |
* } catch (InterruptedException ex) { ... handle ...} |
55 |
* } |
56 |
* } |
57 |
* |
58 |
* void start() { |
59 |
* new Thread(new FillingLoop()).start(); |
60 |
* new Thread(new EmptyingLoop()).start(); |
61 |
* } |
62 |
* } |
63 |
* }</pre> |
64 |
* |
65 |
* <p>Memory consistency effects: For each pair of threads that |
66 |
* successfully exchange objects via an {@code Exchanger}, actions |
67 |
* prior to the {@code exchange()} in each thread |
68 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
69 |
* those subsequent to a return from the corresponding {@code exchange()} |
70 |
* in the other thread. |
71 |
* |
72 |
* @since 1.5 |
73 |
* @author Doug Lea and Bill Scherer and Michael Scott |
74 |
* @param <V> The type of objects that may be exchanged |
75 |
*/ |
76 |
public class Exchanger<V> { |
77 |
/* |
78 |
* Algorithm Description: |
79 |
* |
80 |
* The basic idea is to maintain a "slot", which is a reference to |
81 |
* a Node containing both an Item to offer and a "hole" waiting to |
82 |
* get filled in.. If an incoming "occupying" thread sees that the |
83 |
* slot is null, it CAS'es (compareAndSets) a Node there and waits |
84 |
* for another to invoke exchange. That second "fulfilling" thread |
85 |
* sees that the slot is non-null, and so CASes it back to null, |
86 |
* also exchanging items by CASing the hole, plus waking up the |
87 |
* occupying thread if it is blocked. In each case CAS'es may |
88 |
* fail because a slot at first appears non-null but is null upon |
89 |
* CAS, or vice-versa. So threads may need to retry these |
90 |
* actions. |
91 |
* |
92 |
* This simple approach works great when there are only a few |
93 |
* threads using an Exchanger, but performance rapidly |
94 |
* deteriorates due to CAS contention on the single slot when |
95 |
* there are lots of threads using an exchanger. So instead we use |
96 |
* an "arena"; basically a kind of hash table with a dynamically |
97 |
* varying number of of slots, any one of which can be used by |
98 |
* threads performing an exchange. Incoming threads pick slots |
99 |
* based on a hash of their Thread ids. If an incoming thread |
100 |
* fails to CAS in its chosen slot, it picks an alternative slot |
101 |
* instead. And similarly from there. If a thread successfully |
102 |
* CASes into a slot but no other thread arrives, it tries |
103 |
* another, heading toward the zero slot, which always exists even |
104 |
* if the table shrinks. The particular mechanics controlling this |
105 |
* are as follows: |
106 |
* |
107 |
* Waiting: Slot zero is special in that it is the only slot that |
108 |
* exists when there is no contention. A thread occupying slot |
109 |
* zero will block if no thread fulfills it after a short spin. In |
110 |
* other cases, occupying threads eventually give up and try |
111 |
* another slot. Waiting threads spin for a while (a period that |
112 |
* should be a little less than a typical context-switch time) |
113 |
* before either blocking (if slot zero) or giving up (if other |
114 |
* slots) and restarting. There is no reason for threads to block |
115 |
* unless there are unlikely to be any other threads |
116 |
* present. Occupants are mainly avoiding memory contention so sit |
117 |
* there quietly polling for a shorter period than it would take |
118 |
* to block and then unblock them. Non-slot-zero waits that elapse |
119 |
* because of lack of other threads waste around one extra |
120 |
* context-switch time per try, which is still on average much |
121 |
* faster than alternative approaches. |
122 |
* |
123 |
* Sizing: Usually, using only a few slots suffices to reduce |
124 |
* contention. Especially with small numbers of threads, using |
125 |
* too many slots can lead to just as poor performance as using |
126 |
* too few of them, and there's not much room for error. The |
127 |
* variable "max" maintains the number of slots actually in |
128 |
* use. It is increased when a thread sees too many CAS |
129 |
* failures. (This is analogous to resizing a regular hash table |
130 |
* based on a target load factor, except here, growth steps are |
131 |
* just one-by one rather than proportional.) Growth requires |
132 |
* contention failures in each of three tried slots. Requiring |
133 |
* multiple failures for expansion copes with the fact that some |
134 |
* failed CASes are not due to contention but instead to simple |
135 |
* races between two threads or thread pre-emptions occurring |
136 |
* between reading and CASing. Also, very transient peak |
137 |
* contention can be much higher than the average sustainable |
138 |
* levels. The max limit is decreased on average 50% of the times |
139 |
* that a non-slot-zero wait elapses without being fulfilled. |
140 |
* Threads experiencing elapsed waits move closer to zero, so |
141 |
* eventually find existing (or future) threads even if the table |
142 |
* has been shrunk due to inactivity. The chosen mechanics and |
143 |
* thresholds for growing and shrinking are intrinsically |
144 |
* entangled with indexing and hashing inside the exchange code, |
145 |
* and can't be nicely abstracted out. |
146 |
* |
147 |
* Hashing: Each thread picks its initial slot to use in accord |
148 |
* with a simple hashcode. The sequence is the same on each |
149 |
* encounter by any given thread, but effectively random across |
150 |
* threads. Using arenas encounters the classic cost vs quality |
151 |
* tradeoffs of all hash tables. Here, we use a one-step FNV-1a |
152 |
* hash code based on the current thread's Thread.getId(), along |
153 |
* with a cheap approximation to a mod operation to select an |
154 |
* index. The downside of optimizing index selection in this way |
155 |
* is that the code is hardwired to use a maximum table size of |
156 |
* 32. But this value more than suffices for known platforms and |
157 |
* applications. |
158 |
* |
159 |
* Probing: On sensed contention of a selected slot, we probe |
160 |
* sequentially through the table, analogously to linear probing |
161 |
* after collision in a hash table. (We move circularly, in |
162 |
* reverse order to mesh best with table growth and shrinkage |
163 |
* rules.) Except that to minimize the effects of false-alarms |
164 |
* and cache thrashing, we try the first selected slot twice |
165 |
* before moving. |
166 |
* |
167 |
* Padding: Even with contention management, slots are heavily |
168 |
* contended, so use cache-padding to avoid poor memory |
169 |
* performance. Because of this, slots are lazily constructed only |
170 |
* when used, to avoid wasting this space unnecessarily. While |
171 |
* isolation of locations is not much of an issue at first in an |
172 |
* application, as time goes on and garbage-collectors perform |
173 |
* compaction, slots are very likely to be moved adjacent to each |
174 |
* other, which can cause much thrashing of cache lines on MPs |
175 |
* unless padding is employed. |
176 |
* |
177 |
* This is an improvement of the algorithm described in the paper |
178 |
* "A Scalable Elimination-based Exchange Channel" by William |
179 |
* Scherer, Doug Lea, and Michael Scott in Proceedings of SCOOL05 |
180 |
* workshop. Available at: http://hdl.handle.net/1802/2104 |
181 |
*/ |
182 |
|
183 |
/** The number of CPUs, for sizing and spin control */ |
184 |
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
185 |
|
186 |
/** |
187 |
* The capacity of the arena. Set to a value that provides more |
188 |
* than enough space to handle contention. On small machines most |
189 |
* slots won't be used, but it is still not wasted because the |
190 |
* extra space provides some machine-level address padding to |
191 |
* minimize interference with heavily CAS'ed Slot locations. And |
192 |
* on very large machines, performance eventually becomes bounded |
193 |
* by memory bandwidth, not numbers of threads/CPUs. This |
194 |
* constant cannot be changed without also modifying indexing and |
195 |
* hashing algorithms. |
196 |
*/ |
197 |
private static final int CAPACITY = 32; |
198 |
|
199 |
/** |
200 |
* The value of "max" that will hold all threads without |
201 |
* contention. When this value is less than CAPACITY, some |
202 |
* otherwise wasted expansion can be avoided. |
203 |
*/ |
204 |
private static final int FULL = |
205 |
Math.max(0, Math.min(CAPACITY, NCPU / 2) - 1); |
206 |
|
207 |
/** |
208 |
* The number of times to spin (doing nothing except polling a |
209 |
* memory location) before blocking or giving up while waiting to |
210 |
* be fulfilled. Should be zero on uniprocessors. On |
211 |
* multiprocessors, this value should be large enough so that two |
212 |
* threads exchanging items as fast as possible block only when |
213 |
* one of them is stalled (due to GC or preemption), but not much |
214 |
* longer, to avoid wasting CPU resources. Seen differently, this |
215 |
* value is a little over half the number of cycles of an average |
216 |
* context switch time on most systems. The value here is |
217 |
* approximately the average of those across a range of tested |
218 |
* systems. |
219 |
*/ |
220 |
private static final int SPINS = (NCPU == 1) ? 0 : 2000; |
221 |
|
222 |
/** |
223 |
* The number of times to spin before blocking in timed waits. |
224 |
* Timed waits spin more slowly because checking the time takes |
225 |
* time. The best value relies mainly on the relative rate of |
226 |
* System.nanoTime vs memory accesses. The value is empirically |
227 |
* derived to work well across a variety of systems. |
228 |
*/ |
229 |
private static final int TIMED_SPINS = SPINS / 20; |
230 |
|
231 |
/** |
232 |
* Sentinel item representing cancellation of a wait due to |
233 |
* interruption, timeout, or elapsed spin-waits. This value is |
234 |
* placed in holes on cancellation, and used as a return value |
235 |
* from waiting methods to indicate failure to set or get hole. |
236 |
*/ |
237 |
private static final Object CANCEL = new Object(); |
238 |
|
239 |
/** |
240 |
* Value representing null arguments/returns from public |
241 |
* methods. This disambiguates from internal requirement that |
242 |
* holes start out as null to mean they are not yet set. |
243 |
*/ |
244 |
private static final Object NULL_ITEM = new Object(); |
245 |
|
246 |
/** |
247 |
* Nodes hold partially exchanged data. This class |
248 |
* opportunistically subclasses AtomicReference to represent the |
249 |
* hole. So get() returns hole, and compareAndSet CAS'es value |
250 |
* into hole. This class cannot be parameterized as "V" because of |
251 |
* the use of non-V CANCEL sentinels. |
252 |
*/ |
253 |
private static final class Node extends AtomicReference<Object> { |
254 |
/** The element offered by the Thread creating this node. */ |
255 |
public final Object item; |
256 |
|
257 |
/** The Thread waiting to be signalled; null until waiting. */ |
258 |
public volatile Thread waiter; |
259 |
|
260 |
/** |
261 |
* Creates node with given item and empty hole. |
262 |
* @param item the item |
263 |
*/ |
264 |
public Node(Object item) { |
265 |
this.item = item; |
266 |
} |
267 |
} |
268 |
|
269 |
/** |
270 |
* A Slot is an AtomicReference with heuristic padding to lessen |
271 |
* cache effects of this heavily CAS'ed location. While the |
272 |
* padding adds noticeable space, all slots are created only on |
273 |
* demand, and there will be more than one of them only when it |
274 |
* would improve throughput more than enough to outweigh using |
275 |
* extra space. |
276 |
*/ |
277 |
private static final class Slot extends AtomicReference<Object> { |
278 |
// Improve likelihood of isolation on <= 64 byte cache lines |
279 |
long q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, qa, qb, qc, qd, qe; |
280 |
} |
281 |
|
282 |
/** |
283 |
* Slot array. Elements are lazily initialized when needed. |
284 |
* Declared volatile to enable double-checked lazy construction. |
285 |
*/ |
286 |
private volatile Slot[] arena = new Slot[CAPACITY]; |
287 |
|
288 |
/** |
289 |
* The maximum slot index being used. The value sometimes |
290 |
* increases when a thread experiences too many CAS contentions, |
291 |
* and sometimes decreases when a backoff wait elapses. Changes |
292 |
* are performed only via compareAndSet, to avoid stale values |
293 |
* when a thread happens to stall right before setting. |
294 |
*/ |
295 |
private final AtomicInteger max = new AtomicInteger(); |
296 |
|
297 |
/** |
298 |
* Main exchange function, handling the different policy variants. |
299 |
* Uses Object, not "V" as argument and return value to simplify |
300 |
* handling of sentinel values. Callers from public methods decode |
301 |
* and cast accordingly. |
302 |
* |
303 |
* @param item the (nonnull) item to exchange |
304 |
* @param timed true if the wait is timed |
305 |
* @param nanos if timed, the maximum wait time |
306 |
* @return the other thread's item, or CANCEL if interrupted or timed out. |
307 |
*/ |
308 |
private Object doExchange(Object item, boolean timed, long nanos) { |
309 |
Node me = new Node(item); // Create in case occupying |
310 |
int index = hashIndex(); // Index of current slot |
311 |
int fails = 0; // Number of CAS failures |
312 |
|
313 |
for (;;) { |
314 |
Object y; // Contents of current slot |
315 |
Slot slot = arena[index]; |
316 |
if (slot == null) // Lazily initialize slots |
317 |
createSlot(index); // Continue loop to reread |
318 |
else if ((y = slot.get()) != null && // Try to fulfill |
319 |
slot.compareAndSet(y, null)) { |
320 |
Node you = (Node)y; // Transfer item |
321 |
if (you.compareAndSet(null, me.item)) { |
322 |
LockSupport.unpark(you.waiter); |
323 |
return you.item; |
324 |
} // Else cancelled; continue |
325 |
} |
326 |
else if (y == null && // Try to occupy |
327 |
slot.compareAndSet(null, me)) { |
328 |
if (index == 0) // Blocking wait for slot 0 |
329 |
return timed? awaitNanos(me, slot, nanos): await(me, slot); |
330 |
Object v = spinWait(me, slot); // Spin wait for non-0 |
331 |
if (v != CANCEL) |
332 |
return v; |
333 |
me = new Node(me.item); // Throw away cancelled node |
334 |
int m = max.get(); |
335 |
if (m > (index >>>= 1)) // Decrease index |
336 |
max.compareAndSet(m, m - 1); // Maybe shrink table |
337 |
} |
338 |
else if (++fails > 1) { // Allow 2 fails on 1st slot |
339 |
int m = max.get(); |
340 |
if (fails > 3 && m < FULL && max.compareAndSet(m, m + 1)) |
341 |
index = m + 1; // Grow on 3rd failed slot |
342 |
else if (--index < 0) |
343 |
index = m; // Circularly traverse |
344 |
} |
345 |
} |
346 |
} |
347 |
|
348 |
/** |
349 |
* Returns a hash index for current thread. Uses a one-step |
350 |
* FNV-1a hash code (http://www.isthe.com/chongo/tech/comp/fnv/) |
351 |
* based on the current thread's Thread.getId(). These hash codes |
352 |
* have more uniform distribution properties with respect to small |
353 |
* moduli (here 1-31) than do other simple hashing functions. To |
354 |
* return an index between 0 and max, we use a cheap approximation |
355 |
* to a mod operation, that also corrects for bias due to |
356 |
* non-power-of-2 remaindering (see {@link |
357 |
* java.util.Random#nextInt}). Bits of the hashcode are masked |
358 |
* with "nbits", the ceiling power of two of table size (looked up |
359 |
* in a table packed into three ints). If too large, this is |
360 |
* retried after rotating the hash by nbits bits, while forcing |
361 |
* new top bit to 0, which guarantees eventual termination |
362 |
* (although with a non-random-bias). This requires an average of |
363 |
* less than 2 tries for all table sizes, and has a maximum 2% |
364 |
* difference from perfectly uniform slot probabilities when |
365 |
* applied to all possible hash codes for sizes less than 32. |
366 |
* |
367 |
* @return a per-thread-random index, 0 <= index < max |
368 |
*/ |
369 |
private final int hashIndex() { |
370 |
long id = Thread.currentThread().getId(); |
371 |
int hash = (((int)(id ^ (id >>> 32))) ^ 0x811c9dc5) * 0x01000193; |
372 |
|
373 |
int m = max.get(); |
374 |
int nbits = (((0xfffffc00 >> m) & 4) | // Compute ceil(log2(m+1)) |
375 |
((0x000001f8 >>> m) & 2) | // The constants hold |
376 |
((0xffff00f2 >>> m) & 1)); // a lookup table |
377 |
int index; |
378 |
while ((index = hash & ((1 << nbits) - 1)) > m) // May retry on |
379 |
hash = (hash >>> nbits) | (hash << (33 - nbits)); // non-power-2 m |
380 |
return index; |
381 |
} |
382 |
|
383 |
/** |
384 |
* Creates a new slot at given index. Called only when the slot |
385 |
* appears to be null. Relies on double-check using builtin locks, |
386 |
* since they rarely contend. |
387 |
* |
388 |
* @param index the index to add slot at |
389 |
*/ |
390 |
private void createSlot(int index) { |
391 |
// Create slot outside of lock to narrow sync region |
392 |
Slot newSlot = new Slot(); |
393 |
Slot[] a = arena; |
394 |
synchronized(a) { |
395 |
if (a[index] == null) |
396 |
a[index] = newSlot; |
397 |
} |
398 |
} |
399 |
|
400 |
/** |
401 |
* Try to cancel a wait for the given node waiting in the given |
402 |
* slot, if so, helping clear the node from its slot to avoid |
403 |
* garbage retention. |
404 |
* |
405 |
* @param node the waiting node |
406 |
* @param the slot it is waiting in |
407 |
* @return true if successfully cancelled |
408 |
*/ |
409 |
private static boolean tryCancel(Node node, Slot slot) { |
410 |
if (!node.compareAndSet(null, CANCEL)) |
411 |
return false; |
412 |
if (slot.get() == node) |
413 |
slot.compareAndSet(node, null); |
414 |
return true; |
415 |
} |
416 |
|
417 |
// Three forms of waiting. Each just different enough not to merge |
418 |
// code with others. |
419 |
|
420 |
/** |
421 |
* Spin-waits for hole for a non-0 slot. Fails if spin elapses |
422 |
* before hole filled. Does not check interrupt, relying on check |
423 |
* in public exchange method to abort if interrupted on entry. |
424 |
* |
425 |
* @param node the waiting node |
426 |
* @return on success, the hole; on failure, CANCEL |
427 |
*/ |
428 |
private static Object spinWait(Node node, Slot slot) { |
429 |
int spins = SPINS; |
430 |
for (;;) { |
431 |
Object v = node.get(); |
432 |
if (v != null) |
433 |
return v; |
434 |
else if (spins > 0) |
435 |
--spins; |
436 |
else |
437 |
tryCancel(node, slot); |
438 |
} |
439 |
} |
440 |
|
441 |
/** |
442 |
* Waits for (by spinning and/or blocking) and gets the hole |
443 |
* filled in by another thread. Fails if or interrupted before |
444 |
* hole filled. |
445 |
* |
446 |
* When a node/thread is about to block, it sets its waiter field |
447 |
* and then rechecks state at least one more time before actually |
448 |
* parking, thus covering race vs fulfiller noticing that waiter |
449 |
* is non-null so should be woken. |
450 |
* |
451 |
* Thread interruption status is checked only surrounding calls to |
452 |
* park. The caller is assumed to have checked interrupt status |
453 |
* on entry. |
454 |
* |
455 |
* @param node the waiting node |
456 |
* @return on success, the hole; on failure, CANCEL |
457 |
*/ |
458 |
private static Object await(Node node, Slot slot) { |
459 |
Thread w = Thread.currentThread(); |
460 |
int spins = SPINS; |
461 |
for (;;) { |
462 |
Object v = node.get(); |
463 |
if (v != null) |
464 |
return v; |
465 |
else if (spins > 0) // Spin-wait phase |
466 |
--spins; |
467 |
else if (node.waiter == null) // Set up to block next |
468 |
node.waiter = w; |
469 |
else if (w.isInterrupted()) // Abort on interrupt |
470 |
tryCancel(node, slot); |
471 |
else // Block |
472 |
LockSupport.park(node); |
473 |
} |
474 |
} |
475 |
|
476 |
/** |
477 |
* Waits for (at index 0) and gets the hole filled in by another |
478 |
* thread. Fails if timed out or interrupted before hole filled. |
479 |
* Same basic logic as untimed version, but a bit messier. |
480 |
* |
481 |
* @param node the waiting node |
482 |
* @param nanos the wait time |
483 |
* @return on success, the hole; on failure, CANCEL |
484 |
*/ |
485 |
private Object awaitNanos(Node node, Slot slot, long nanos) { |
486 |
int spins = TIMED_SPINS; |
487 |
long lastTime = 0; |
488 |
Thread w = null; |
489 |
for (;;) { |
490 |
Object v = node.get(); |
491 |
if (v != null) |
492 |
return v; |
493 |
long now = System.nanoTime(); |
494 |
if (w == null) |
495 |
w = Thread.currentThread(); |
496 |
else |
497 |
nanos -= now - lastTime; |
498 |
lastTime = now; |
499 |
if (nanos > 0) { |
500 |
if (spins > 0) |
501 |
--spins; |
502 |
else if (node.waiter == null) |
503 |
node.waiter = w; |
504 |
else if (w.isInterrupted()) |
505 |
tryCancel(node, slot); |
506 |
else |
507 |
LockSupport.parkNanos(node, nanos); |
508 |
} |
509 |
else if (tryCancel(node, slot) && !w.isInterrupted()) |
510 |
return scanOnTimeout(node); |
511 |
} |
512 |
} |
513 |
|
514 |
/** |
515 |
* Sweeps through arena checking for any waiting threads. Called |
516 |
* only upon return from timeout while waiting in slot 0. When a |
517 |
* thread gives up on a timed wait, it is possible that a |
518 |
* previously-entered thread is still waiting in some other |
519 |
* slot. So we scan to check for any. This is almost always |
520 |
* overkill, but decreases the likelihood of timeouts when there |
521 |
* are other threads present to far less than that in lock-based |
522 |
* exchangers in which earlier-arriving threads may still be |
523 |
* waiting on entry locks. |
524 |
* |
525 |
* @param node the waiting node |
526 |
* @return another thread's item, or CANCEL |
527 |
*/ |
528 |
private Object scanOnTimeout(Node node) { |
529 |
Object y; |
530 |
for (int j = arena.length - 1; j >= 0; --j) { |
531 |
Slot slot = arena[j]; |
532 |
if (slot != null) { |
533 |
while ((y = slot.get()) != null) { |
534 |
if (slot.compareAndSet(y, null)) { |
535 |
Node you = (Node)y; |
536 |
if (you.compareAndSet(null, node.item)) { |
537 |
LockSupport.unpark(you.waiter); |
538 |
return you.item; |
539 |
} |
540 |
} |
541 |
} |
542 |
} |
543 |
} |
544 |
return CANCEL; |
545 |
} |
546 |
|
547 |
/** |
548 |
* Creates a new Exchanger. |
549 |
*/ |
550 |
public Exchanger() { |
551 |
} |
552 |
|
553 |
/** |
554 |
* Waits for another thread to arrive at this exchange point (unless |
555 |
* the current thread is {@link Thread#interrupt interrupted}), |
556 |
* and then transfers the given object to it, receiving its object |
557 |
* in return. |
558 |
* |
559 |
* <p>If another thread is already waiting at the exchange point then |
560 |
* it is resumed for thread scheduling purposes and receives the object |
561 |
* passed in by the current thread. The current thread returns immediately, |
562 |
* receiving the object passed to the exchange by that other thread. |
563 |
* |
564 |
* <p>If no other thread is already waiting at the exchange then the |
565 |
* current thread is disabled for thread scheduling purposes and lies |
566 |
* dormant until one of two things happens: |
567 |
* <ul> |
568 |
* <li>Some other thread enters the exchange; or |
569 |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
570 |
* thread. |
571 |
* </ul> |
572 |
* <p>If the current thread: |
573 |
* <ul> |
574 |
* <li>has its interrupted status set on entry to this method; or |
575 |
* <li>is {@link Thread#interrupt interrupted} while waiting |
576 |
* for the exchange, |
577 |
* </ul> |
578 |
* then {@link InterruptedException} is thrown and the current thread's |
579 |
* interrupted status is cleared. |
580 |
* |
581 |
* @param x the object to exchange |
582 |
* @return the object provided by the other thread |
583 |
* @throws InterruptedException if the current thread was |
584 |
* interrupted while waiting |
585 |
*/ |
586 |
public V exchange(V x) throws InterruptedException { |
587 |
if (!Thread.interrupted()) { |
588 |
Object v = doExchange(x == null? NULL_ITEM : x, false, 0); |
589 |
if (v == NULL_ITEM) |
590 |
return null; |
591 |
if (v != CANCEL) |
592 |
return (V)v; |
593 |
Thread.interrupted(); // Clear interrupt status on IE throw |
594 |
} |
595 |
throw new InterruptedException(); |
596 |
} |
597 |
|
598 |
/** |
599 |
* Waits for another thread to arrive at this exchange point (unless |
600 |
* the current thread is {@link Thread#interrupt interrupted} or |
601 |
* the specified waiting time elapses), and then transfers the given |
602 |
* object to it, receiving its object in return. |
603 |
* |
604 |
* <p>If another thread is already waiting at the exchange point then |
605 |
* it is resumed for thread scheduling purposes and receives the object |
606 |
* passed in by the current thread. The current thread returns immediately, |
607 |
* receiving the object passed to the exchange by that other thread. |
608 |
* |
609 |
* <p>If no other thread is already waiting at the exchange then the |
610 |
* current thread is disabled for thread scheduling purposes and lies |
611 |
* dormant until one of three things happens: |
612 |
* <ul> |
613 |
* <li>Some other thread enters the exchange; or |
614 |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
615 |
* thread; or |
616 |
* <li>The specified waiting time elapses. |
617 |
* </ul> |
618 |
* <p>If the current thread: |
619 |
* <ul> |
620 |
* <li>has its interrupted status set on entry to this method; or |
621 |
* <li>is {@link Thread#interrupt interrupted} while waiting |
622 |
* for the exchange, |
623 |
* </ul> |
624 |
* then {@link InterruptedException} is thrown and the current thread's |
625 |
* interrupted status is cleared. |
626 |
* |
627 |
* <p>If the specified waiting time elapses then {@link |
628 |
* TimeoutException} is thrown. If the time is less than or equal |
629 |
* to zero, the method will not wait at all. |
630 |
* |
631 |
* @param x the object to exchange |
632 |
* @param timeout the maximum time to wait |
633 |
* @param unit the time unit of the <tt>timeout</tt> argument |
634 |
* @return the object provided by the other thread |
635 |
* @throws InterruptedException if the current thread was |
636 |
* interrupted while waiting |
637 |
* @throws TimeoutException if the specified waiting time elapses |
638 |
* before another thread enters the exchange |
639 |
*/ |
640 |
public V exchange(V x, long timeout, TimeUnit unit) |
641 |
throws InterruptedException, TimeoutException { |
642 |
if (!Thread.interrupted()) { |
643 |
Object v = doExchange(x == null? NULL_ITEM : x, |
644 |
true, unit.toNanos(timeout)); |
645 |
if (v == NULL_ITEM) |
646 |
return null; |
647 |
if (v != CANCEL) |
648 |
return (V)v; |
649 |
if (!Thread.interrupted()) |
650 |
throw new TimeoutException(); |
651 |
} |
652 |
throw new InterruptedException(); |
653 |
} |
654 |
} |