ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Exchanger.java
Revision: 1.76
Committed: Thu Jun 2 13:40:42 2016 UTC (8 years ago) by jsr166
Branch: MAIN
Changes since 1.75: +1 -0 lines
Log Message:
whitespace

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 jsr166 1.76
10 dl 1.75 import java.lang.invoke.MethodHandles;
11     import java.lang.invoke.VarHandle;
12     import java.util.concurrent.locks.LockSupport;
13 jsr166 1.66
14 tim 1.1 /**
15 dl 1.28 * A synchronization point at which threads can pair and swap elements
16 jsr166 1.39 * within pairs. Each thread presents some object on entry to the
17 dl 1.28 * {@link #exchange exchange} method, matches with a partner thread,
18 jsr166 1.39 * and receives its partner's object on return. An Exchanger may be
19     * viewed as a bidirectional form of a {@link SynchronousQueue}.
20     * Exchangers may be useful in applications such as genetic algorithms
21     * and pipeline designs.
22 tim 1.1 *
23     * <p><b>Sample Usage:</b>
24 jsr166 1.29 * Here are the highlights of a class that uses an {@code Exchanger}
25     * to swap buffers between threads so that the thread filling the
26     * buffer gets a freshly emptied one when it needs it, handing off the
27     * filled one to the thread emptying the buffer.
28 jsr166 1.71 * <pre> {@code
29 tim 1.1 * class FillAndEmpty {
30 jsr166 1.65 * Exchanger<DataBuffer> exchanger = new Exchanger<>();
31 dl 1.9 * DataBuffer initialEmptyBuffer = ... a made-up type
32     * DataBuffer initialFullBuffer = ...
33 tim 1.1 *
34     * class FillingLoop implements Runnable {
35     * public void run() {
36 dl 1.9 * DataBuffer currentBuffer = initialEmptyBuffer;
37 tim 1.1 * try {
38     * while (currentBuffer != null) {
39     * addToBuffer(currentBuffer);
40 dl 1.30 * if (currentBuffer.isFull())
41 tim 1.1 * currentBuffer = exchanger.exchange(currentBuffer);
42     * }
43 tim 1.7 * } catch (InterruptedException ex) { ... handle ... }
44 tim 1.1 * }
45     * }
46     *
47     * class EmptyingLoop implements Runnable {
48     * public void run() {
49 dl 1.9 * DataBuffer currentBuffer = initialFullBuffer;
50 tim 1.1 * try {
51     * while (currentBuffer != null) {
52     * takeFromBuffer(currentBuffer);
53 dl 1.30 * if (currentBuffer.isEmpty())
54 tim 1.1 * currentBuffer = exchanger.exchange(currentBuffer);
55     * }
56 tim 1.7 * } catch (InterruptedException ex) { ... handle ...}
57 tim 1.1 * }
58     * }
59     *
60     * void start() {
61     * new Thread(new FillingLoop()).start();
62     * new Thread(new EmptyingLoop()).start();
63     * }
64 jsr166 1.50 * }}</pre>
65 tim 1.1 *
66 jsr166 1.27 * <p>Memory consistency effects: For each pair of threads that
67     * successfully exchange objects via an {@code Exchanger}, actions
68     * prior to the {@code exchange()} in each thread
69     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
70     * those subsequent to a return from the corresponding {@code exchange()}
71     * in the other thread.
72 brian 1.22 *
73 tim 1.1 * @since 1.5
74 dl 1.16 * @author Doug Lea and Bill Scherer and Michael Scott
75 dl 1.11 * @param <V> The type of objects that may be exchanged
76 tim 1.1 */
77     public class Exchanger<V> {
78 dl 1.55
79 dl 1.16 /*
80 jsr166 1.57 * Overview: The core algorithm is, for an exchange "slot",
81 dl 1.55 * and a participant (caller) with an item:
82 dl 1.16 *
83 jsr166 1.61 * for (;;) {
84     * if (slot is empty) { // offer
85     * place item in a Node;
86     * if (can CAS slot from empty to node) {
87 jsr166 1.62 * wait for release;
88     * return matching item in node;
89 dl 1.55 * }
90 jsr166 1.61 * }
91     * else if (can CAS slot from node to empty) { // release
92     * get the item in node;
93     * set matching item in node;
94     * release waiting thread;
95     * }
96     * // else retry on CAS failure
97     * }
98 dl 1.55 *
99     * This is among the simplest forms of a "dual data structure" --
100     * see Scott and Scherer's DISC 04 paper and
101     * http://www.cs.rochester.edu/research/synchronization/pseudocode/duals.html
102     *
103     * This works great in principle. But in practice, like many
104     * algorithms centered on atomic updates to a single location, it
105     * scales horribly when there are more than a few participants
106     * using the same Exchanger. So the implementation instead uses a
107     * form of elimination arena, that spreads out this contention by
108     * arranging that some threads typically use different slots,
109     * while still ensuring that eventually, any two parties will be
110     * able to exchange items. That is, we cannot completely partition
111     * across threads, but instead give threads arena indices that
112     * will on average grow under contention and shrink under lack of
113     * contention. We approach this by defining the Nodes that we need
114     * anyway as ThreadLocals, and include in them per-thread index
115     * and related bookkeeping state. (We can safely reuse per-thread
116     * nodes rather than creating them fresh each time because slots
117     * alternate between pointing to a node vs null, so cannot
118 jsr166 1.57 * encounter ABA problems. However, we do need some care in
119 dl 1.55 * resetting them between uses.)
120     *
121     * Implementing an effective arena requires allocating a bunch of
122     * space, so we only do so upon detecting contention (except on
123     * uniprocessors, where they wouldn't help, so aren't used).
124     * Otherwise, exchanges use the single-slot slotExchange method.
125     * On contention, not only must the slots be in different
126     * locations, but the locations must not encounter memory
127     * contention due to being on the same cache line (or more
128     * generally, the same coherence unit). Because, as of this
129     * writing, there is no way to determine cacheline size, we define
130     * a value that is enough for common platforms. Additionally,
131     * extra care elsewhere is taken to avoid other false/unintended
132 dl 1.64 * sharing and to enhance locality, including adding padding (via
133 dl 1.75 * @Contended) to Nodes, embedding "bound" as an Exchanger field.
134 dl 1.55 *
135     * The arena starts out with only one used slot. We expand the
136     * effective arena size by tracking collisions; i.e., failed CASes
137     * while trying to exchange. By nature of the above algorithm, the
138     * only kinds of collision that reliably indicate contention are
139     * when two attempted releases collide -- one of two attempted
140     * offers can legitimately fail to CAS without indicating
141     * contention by more than one other thread. (Note: it is possible
142     * but not worthwhile to more precisely detect contention by
143     * reading slot values after CAS failures.) When a thread has
144     * collided at each slot within the current arena bound, it tries
145     * to expand the arena size by one. We track collisions within
146     * bounds by using a version (sequence) number on the "bound"
147     * field, and conservatively reset collision counts when a
148     * participant notices that bound has been updated (in either
149     * direction).
150     *
151     * The effective arena size is reduced (when there is more than
152     * one slot) by giving up on waiting after a while and trying to
153     * decrement the arena size on expiration. The value of "a while"
154     * is an empirical matter. We implement by piggybacking on the
155     * use of spin->yield->block that is essential for reasonable
156     * waiting performance anyway -- in a busy exchanger, offers are
157     * usually almost immediately released, in which case context
158     * switching on multiprocessors is extremely slow/wasteful. Arena
159     * waits just omit the blocking part, and instead cancel. The spin
160     * count is empirically chosen to be a value that avoids blocking
161     * 99% of the time under maximum sustained exchange rates on a
162     * range of test machines. Spins and yields entail some limited
163     * randomness (using a cheap xorshift) to avoid regular patterns
164     * that can induce unproductive grow/shrink cycles. (Using a
165     * pseudorandom also helps regularize spin cycle duration by
166     * making branches unpredictable.) Also, during an offer, a
167     * waiter can "know" that it will be released when its slot has
168     * changed, but cannot yet proceed until match is set. In the
169     * mean time it cannot cancel the offer, so instead spins/yields.
170     * Note: It is possible to avoid this secondary check by changing
171     * the linearization point to be a CAS of the match field (as done
172     * in one case in the Scott & Scherer DISC paper), which also
173     * increases asynchrony a bit, at the expense of poorer collision
174     * detection and inability to always reuse per-thread nodes. So
175     * the current scheme is typically a better tradeoff.
176     *
177     * On collisions, indices traverse the arena cyclically in reverse
178     * order, restarting at the maximum index (which will tend to be
179     * sparsest) when bounds change. (On expirations, indices instead
180     * are halved until reaching 0.) It is possible (and has been
181     * tried) to use randomized, prime-value-stepped, or double-hash
182     * style traversal instead of simple cyclic traversal to reduce
183     * bunching. But empirically, whatever benefits these may have
184     * don't overcome their added overhead: We are managing operations
185     * that occur very quickly unless there is sustained contention,
186     * so simpler/faster control policies work better than more
187     * accurate but slower ones.
188     *
189     * Because we use expiration for arena size control, we cannot
190     * throw TimeoutExceptions in the timed version of the public
191     * exchange method until the arena size has shrunken to zero (or
192     * the arena isn't enabled). This may delay response to timeout
193     * but is still within spec.
194     *
195     * Essentially all of the implementation is in methods
196     * slotExchange and arenaExchange. These have similar overall
197     * structure, but differ in too many details to combine. The
198     * slotExchange method uses the single Exchanger field "slot"
199     * rather than arena array elements. However, it still needs
200     * minimal collision detection to trigger arena construction.
201     * (The messiest part is making sure interrupt status and
202     * InterruptedExceptions come out right during transitions when
203     * both methods may be called. This is done by using null return
204     * as a sentinel to recheck interrupt status.)
205     *
206 jsr166 1.57 * As is too common in this sort of code, methods are monolithic
207 dl 1.55 * because most of the logic relies on reads of fields that are
208     * maintained as local variables so can't be nicely factored --
209     * mainly, here, bulky spin->yield->block/cancel code), and
210 dl 1.75 * heavily dependent on intrinsics (VarHandles) to use inlined
211 jsr166 1.57 * embedded CAS and related memory access operations (that tend
212 dl 1.55 * not to be as readily inlined by dynamic compilers when they are
213     * hidden behind other methods that would more nicely name and
214     * encapsulate the intended effects). This includes the use of
215 jsr166 1.74 * putXRelease to clear fields of the per-thread Nodes between
216 dl 1.55 * uses. Note that field Node.item is not declared as volatile
217     * even though it is read by releasing threads, because they only
218 jsr166 1.57 * do so after CAS operations that must precede access, and all
219 dl 1.55 * uses by the owning thread are otherwise acceptably ordered by
220     * other operations. (Because the actual points of atomicity are
221     * slot CASes, it would also be legal for the write to Node.match
222     * in a release to be weaker than a full volatile write. However,
223     * this is not done because it could allow further postponement of
224     * the write, delaying progress.)
225     */
226    
227     /**
228     * The byte distance (as a shift value) between any two used slots
229     * in the arena. 1 << ASHIFT should be at least cacheline size.
230     */
231     private static final int ASHIFT = 7;
232    
233     /**
234     * The maximum supported arena index. The maximum allocatable
235     * arena size is MMASK + 1. Must be a power of two minus one, less
236     * than (1<<(31-ASHIFT)). The cap of 255 (0xff) more than suffices
237     * for the expected scaling limits of the main algorithms.
238 dl 1.16 */
239 jsr166 1.59 private static final int MMASK = 0xff;
240 dl 1.55
241     /**
242     * Unit for sequence/version bits of bound field. Each successful
243     * change to the bound also adds SEQ.
244     */
245 jsr166 1.59 private static final int SEQ = MMASK + 1;
246 dl 1.2
247 dl 1.32 /** The number of CPUs, for sizing and spin control */
248 dl 1.37 private static final int NCPU = Runtime.getRuntime().availableProcessors();
249 dl 1.32
250 jsr166 1.17 /**
251 dl 1.55 * The maximum slot index of the arena: The number of slots that
252     * can in principle hold all threads without contention, or at
253     * most the maximum indexable value.
254 dl 1.37 */
255 dl 1.55 static final int FULL = (NCPU >= (MMASK << 1)) ? MMASK : NCPU >>> 1;
256 dl 1.37
257     /**
258 dl 1.55 * The bound for spins while waiting for a match. The actual
259     * number of iterations will on average be about twice this value
260     * due to randomization. Note: Spinning is disabled when NCPU==1.
261 dl 1.37 */
262 dl 1.55 private static final int SPINS = 1 << 10;
263 dl 1.37
264     /**
265 dl 1.55 * Value representing null arguments/returns from public
266     * methods. Needed because the API originally didn't disallow null
267     * arguments, which it should have.
268 dl 1.16 */
269 dl 1.55 private static final Object NULL_ITEM = new Object();
270 dl 1.34
271     /**
272 dl 1.55 * Sentinel value returned by internal exchange methods upon
273     * timeout, to avoid need for separate timed versions of these
274     * methods.
275 dl 1.34 */
276 dl 1.55 private static final Object TIMED_OUT = new Object();
277 dl 1.34
278     /**
279 dl 1.55 * Nodes hold partially exchanged data, plus other per-thread
280 jsr166 1.73 * bookkeeping. Padded via @Contended to reduce memory contention.
281 dl 1.34 */
282 jsr166 1.73 @jdk.internal.vm.annotation.Contended static final class Node {
283 dl 1.55 int index; // Arena index
284     int bound; // Last recorded value of Exchanger.bound
285     int collides; // Number of CAS failures at current bound
286     int hash; // Pseudo-random for spins
287     Object item; // This thread's current item
288     volatile Object match; // Item provided by releasing thread
289     volatile Thread parked; // Set to this thread when parked, else null
290     }
291    
292     /** The corresponding thread local class */
293     static final class Participant extends ThreadLocal<Node> {
294     public Node initialValue() { return new Node(); }
295     }
296 dl 1.32
297     /**
298 jsr166 1.72 * Per-thread state.
299 dl 1.32 */
300 dl 1.55 private final Participant participant;
301 dl 1.32
302     /**
303 dl 1.55 * Elimination array; null until enabled (within slotExchange).
304     * Element accesses use emulation of volatile gets and CAS.
305     */
306     private volatile Node[] arena;
307 dl 1.5
308 dl 1.34 /**
309 dl 1.55 * Slot used until contention detected.
310 dl 1.34 */
311 dl 1.55 private volatile Node slot;
312 dl 1.5
313 dl 1.16 /**
314 dl 1.55 * The index of the largest valid arena position, OR'ed with SEQ
315     * number in high bits, incremented on each update. The initial
316     * update from 0 to SEQ is used to ensure that the arena array is
317     * constructed only once.
318 dl 1.16 */
319 dl 1.55 private volatile int bound;
320 dl 1.2
321 dl 1.16 /**
322 dl 1.55 * Exchange function when arenas enabled. See above for explanation.
323 dl 1.30 *
324 jsr166 1.60 * @param item the (non-null) item to exchange
325 dl 1.30 * @param timed true if the wait is timed
326 jsr166 1.58 * @param ns if timed, the maximum wait time, else 0L
327 dl 1.55 * @return the other thread's item; or null if interrupted; or
328     * TIMED_OUT if timed and timed out
329     */
330     private final Object arenaExchange(Object item, boolean timed, long ns) {
331     Node[] a = arena;
332 dl 1.75 int alen = a.length;
333 dl 1.55 Node p = participant.get();
334     for (int i = p.index;;) { // access slot at i
335 dl 1.75 int b, m, c;
336     int j = (i << ASHIFT) + ((1 << ASHIFT) - 1);
337     if (j < 0 || j >= alen)
338     j = alen - 1;
339     Node q = (Node)AA.get(a, j);
340     if (q != null && AA.compareAndSet(a, j, q, null)) {
341 dl 1.55 Object v = q.item; // release
342     q.match = item;
343     Thread w = q.parked;
344     if (w != null)
345 dl 1.75 LockSupport.unpark(w);
346 dl 1.55 return v;
347 dl 1.37 }
348 dl 1.55 else if (i <= (m = (b = bound) & MMASK) && q == null) {
349     p.item = item; // offer
350 dl 1.75 if (AA.compareAndSet(a, j, null, p)) {
351 jsr166 1.56 long end = (timed && m == 0) ? System.nanoTime() + ns : 0L;
352 dl 1.55 Thread t = Thread.currentThread(); // wait
353     for (int h = p.hash, spins = SPINS;;) {
354     Object v = p.match;
355     if (v != null) {
356 dl 1.75 MATCH.setRelease(p, null);
357 dl 1.55 p.item = null; // clear for next use
358     p.hash = h;
359     return v;
360     }
361     else if (spins > 0) {
362     h ^= h << 1; h ^= h >>> 3; h ^= h << 10; // xorshift
363     if (h == 0) // initialize hash
364     h = SPINS | (int)t.getId();
365     else if (h < 0 && // approx 50% true
366     (--spins & ((SPINS >>> 1) - 1)) == 0)
367     Thread.yield(); // two yields per wait
368     }
369 dl 1.75 else if (AA.getVolatile(a, j) != p)
370 dl 1.55 spins = SPINS; // releaser hasn't set match yet
371     else if (!t.isInterrupted() && m == 0 &&
372     (!timed ||
373     (ns = end - System.nanoTime()) > 0L)) {
374     p.parked = t; // minimize window
375 dl 1.75 if (AA.getVolatile(a, j) == p) {
376     if (ns == 0L)
377     LockSupport.park(this);
378     else
379     LockSupport.parkNanos(this, ns);
380     }
381 dl 1.55 p.parked = null;
382     }
383 dl 1.75 else if (AA.getVolatile(a, j) == p &&
384     AA.compareAndSet(a, j, p, null)) {
385 dl 1.55 if (m != 0) // try to shrink
386 dl 1.75 BOUND.compareAndSet(this, b, b + SEQ - 1);
387 dl 1.55 p.item = null;
388     p.hash = h;
389     i = p.index >>>= 1; // descend
390     if (Thread.interrupted())
391     return null;
392     if (timed && m == 0 && ns <= 0L)
393     return TIMED_OUT;
394     break; // expired; restart
395     }
396     }
397     }
398     else
399     p.item = null; // clear offer
400 dl 1.2 }
401 dl 1.55 else {
402     if (p.bound != b) { // stale; reset
403     p.bound = b;
404     p.collides = 0;
405     i = (i != m || m == 0) ? m : m - 1;
406     }
407     else if ((c = p.collides) < m || m == FULL ||
408 dl 1.75 !BOUND.compareAndSet(this, b, b + SEQ + 1)) {
409 dl 1.55 p.collides = c + 1;
410     i = (i == 0) ? m : i - 1; // cyclically traverse
411     }
412     else
413     i = m + 1; // grow
414     p.index = i;
415 dl 1.34 }
416 dl 1.37 }
417     }
418 dl 1.2
419 dl 1.37 /**
420 dl 1.55 * Exchange function used until arenas enabled. See above for explanation.
421     *
422     * @param item the item to exchange
423     * @param timed true if the wait is timed
424 jsr166 1.58 * @param ns if timed, the maximum wait time, else 0L
425 dl 1.55 * @return the other thread's item; or null if either the arena
426     * was enabled or the thread was interrupted before completion; or
427     * TIMED_OUT if timed and timed out
428     */
429     private final Object slotExchange(Object item, boolean timed, long ns) {
430     Node p = participant.get();
431     Thread t = Thread.currentThread();
432     if (t.isInterrupted()) // preserve interrupt status so caller can recheck
433     return null;
434    
435     for (Node q;;) {
436     if ((q = slot) != null) {
437 dl 1.75 if (SLOT.compareAndSet(this, q, null)) {
438 dl 1.55 Object v = q.item;
439     q.match = item;
440     Thread w = q.parked;
441     if (w != null)
442 dl 1.75 LockSupport.unpark(w);
443 dl 1.55 return v;
444     }
445     // create arena on contention, but continue until slot null
446     if (NCPU > 1 && bound == 0 &&
447 dl 1.75 BOUND.compareAndSet(this, 0, SEQ))
448 dl 1.55 arena = new Node[(FULL + 2) << ASHIFT];
449     }
450     else if (arena != null)
451     return null; // caller must reroute to arenaExchange
452     else {
453     p.item = item;
454 dl 1.75 if (SLOT.compareAndSet(this, null, p))
455 dl 1.55 break;
456     p.item = null;
457     }
458 dl 1.16 }
459    
460 dl 1.55 // await release
461     int h = p.hash;
462 jsr166 1.56 long end = timed ? System.nanoTime() + ns : 0L;
463 dl 1.55 int spins = (NCPU > 1) ? SPINS : 1;
464     Object v;
465     while ((v = p.match) == null) {
466     if (spins > 0) {
467     h ^= h << 1; h ^= h >>> 3; h ^= h << 10;
468     if (h == 0)
469     h = SPINS | (int)t.getId();
470     else if (h < 0 && (--spins & ((SPINS >>> 1) - 1)) == 0)
471     Thread.yield();
472 jsr166 1.53 }
473 dl 1.55 else if (slot != p)
474     spins = SPINS;
475     else if (!t.isInterrupted() && arena == null &&
476     (!timed || (ns = end - System.nanoTime()) > 0L)) {
477     p.parked = t;
478 dl 1.75 if (slot == p) {
479     if (ns == 0L)
480     LockSupport.park(this);
481     else
482     LockSupport.parkNanos(this, ns);
483     }
484 dl 1.55 p.parked = null;
485 dl 1.37 }
486 dl 1.75 else if (SLOT.compareAndSet(this, p, null)) {
487 dl 1.55 v = timed && ns <= 0L && !t.isInterrupted() ? TIMED_OUT : null;
488     break;
489 dl 1.16 }
490     }
491 dl 1.75 MATCH.setRelease(p, null);
492 dl 1.55 p.item = null;
493     p.hash = h;
494     return v;
495 dl 1.37 }
496    
497     /**
498     * Creates a new Exchanger.
499     */
500     public Exchanger() {
501 dl 1.55 participant = new Participant();
502 tim 1.1 }
503    
504     /**
505     * Waits for another thread to arrive at this exchange point (unless
506 jsr166 1.44 * the current thread is {@linkplain Thread#interrupt interrupted}),
507 tim 1.1 * and then transfers the given object to it, receiving its object
508     * in return.
509 jsr166 1.17 *
510 tim 1.1 * <p>If another thread is already waiting at the exchange point then
511     * it is resumed for thread scheduling purposes and receives the object
512 jsr166 1.39 * passed in by the current thread. The current thread returns immediately,
513 tim 1.1 * receiving the object passed to the exchange by that other thread.
514 jsr166 1.17 *
515 jsr166 1.15 * <p>If no other thread is already waiting at the exchange then the
516 tim 1.1 * current thread is disabled for thread scheduling purposes and lies
517     * dormant until one of two things happens:
518     * <ul>
519     * <li>Some other thread enters the exchange; or
520 jsr166 1.45 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
521     * the current thread.
522 tim 1.1 * </ul>
523     * <p>If the current thread:
524     * <ul>
525 jsr166 1.15 * <li>has its interrupted status set on entry to this method; or
526 jsr166 1.44 * <li>is {@linkplain Thread#interrupt interrupted} while waiting
527 jsr166 1.15 * for the exchange,
528 tim 1.1 * </ul>
529 jsr166 1.15 * then {@link InterruptedException} is thrown and the current thread's
530     * interrupted status is cleared.
531 tim 1.1 *
532     * @param x the object to exchange
533 dl 1.30 * @return the object provided by the other thread
534     * @throws InterruptedException if the current thread was
535     * interrupted while waiting
536 jsr166 1.15 */
537 jsr166 1.52 @SuppressWarnings("unchecked")
538 tim 1.1 public V exchange(V x) throws InterruptedException {
539 dl 1.55 Object v;
540 dl 1.75 Node[] a;
541 jsr166 1.56 Object item = (x == null) ? NULL_ITEM : x; // translate null args
542 dl 1.75 if (((a = arena) != null ||
543 dl 1.55 (v = slotExchange(item, false, 0L)) == null) &&
544     ((Thread.interrupted() || // disambiguates null return
545     (v = arenaExchange(item, false, 0L)) == null)))
546     throw new InterruptedException();
547 jsr166 1.56 return (v == NULL_ITEM) ? null : (V)v;
548 tim 1.1 }
549    
550     /**
551     * Waits for another thread to arrive at this exchange point (unless
552 jsr166 1.44 * the current thread is {@linkplain Thread#interrupt interrupted} or
553 jsr166 1.31 * the specified waiting time elapses), and then transfers the given
554     * object to it, receiving its object in return.
555 tim 1.1 *
556     * <p>If another thread is already waiting at the exchange point then
557     * it is resumed for thread scheduling purposes and receives the object
558 jsr166 1.39 * passed in by the current thread. The current thread returns immediately,
559 tim 1.1 * receiving the object passed to the exchange by that other thread.
560     *
561 jsr166 1.15 * <p>If no other thread is already waiting at the exchange then the
562 tim 1.1 * current thread is disabled for thread scheduling purposes and lies
563     * dormant until one of three things happens:
564     * <ul>
565     * <li>Some other thread enters the exchange; or
566 jsr166 1.44 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
567     * the current thread; or
568 tim 1.1 * <li>The specified waiting time elapses.
569     * </ul>
570     * <p>If the current thread:
571     * <ul>
572 jsr166 1.15 * <li>has its interrupted status set on entry to this method; or
573 jsr166 1.44 * <li>is {@linkplain Thread#interrupt interrupted} while waiting
574 jsr166 1.15 * for the exchange,
575 tim 1.1 * </ul>
576 jsr166 1.15 * then {@link InterruptedException} is thrown and the current thread's
577     * interrupted status is cleared.
578 tim 1.1 *
579 dl 1.37 * <p>If the specified waiting time elapses then {@link
580     * TimeoutException} is thrown. If the time is less than or equal
581     * to zero, the method will not wait at all.
582 tim 1.1 *
583     * @param x the object to exchange
584     * @param timeout the maximum time to wait
585 jsr166 1.63 * @param unit the time unit of the {@code timeout} argument
586 dl 1.30 * @return the object provided by the other thread
587     * @throws InterruptedException if the current thread was
588     * interrupted while waiting
589     * @throws TimeoutException if the specified waiting time elapses
590     * before another thread enters the exchange
591 jsr166 1.15 */
592 jsr166 1.52 @SuppressWarnings("unchecked")
593 jsr166 1.15 public V exchange(V x, long timeout, TimeUnit unit)
594 tim 1.1 throws InterruptedException, TimeoutException {
595 dl 1.55 Object v;
596 jsr166 1.56 Object item = (x == null) ? NULL_ITEM : x;
597 dl 1.55 long ns = unit.toNanos(timeout);
598     if ((arena != null ||
599     (v = slotExchange(item, true, ns)) == null) &&
600     ((Thread.interrupted() ||
601     (v = arenaExchange(item, true, ns)) == null)))
602     throw new InterruptedException();
603     if (v == TIMED_OUT)
604     throw new TimeoutException();
605 jsr166 1.56 return (v == NULL_ITEM) ? null : (V)v;
606 dl 1.55 }
607    
608 dl 1.75 // VarHandle mechanics
609     private static final VarHandle BOUND;
610     private static final VarHandle SLOT;
611     private static final VarHandle MATCH;
612     private static final VarHandle AA;
613 dl 1.55 static {
614     try {
615 dl 1.75 MethodHandles.Lookup l = MethodHandles.lookup();
616     BOUND = l.findVarHandle(Exchanger.class, "bound", int.class);
617     SLOT = l.findVarHandle(Exchanger.class, "slot", Node.class);
618     MATCH = l.findVarHandle(Node.class, "match", Object.class);
619     AA = MethodHandles.arrayElementVarHandle(Node[].class);
620 jsr166 1.68 } catch (ReflectiveOperationException e) {
621 dl 1.55 throw new Error(e);
622 dl 1.34 }
623     }
624 dl 1.55
625 tim 1.1 }