ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Exchanger.java
Revision: 1.51
Committed: Fri Dec 2 13:00:25 2011 UTC (12 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.50: +14 -8 lines
Log Message:
javac warning removal

File Contents

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