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