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 |
dl |
1.14 |
* http://creativecommons.org/licenses/publicdomain |
6 |
dl |
1.2 |
*/ |
7 |
|
|
|
8 |
tim |
1.1 |
package java.util.concurrent; |
9 |
jsr166 |
1.21 |
import java.util.concurrent.*; // for javadoc (till 6280605 is fixed) |
10 |
dl |
1.4 |
import java.util.concurrent.locks.*; |
11 |
dl |
1.16 |
import java.util.concurrent.atomic.*; |
12 |
|
|
import java.util.Random; |
13 |
tim |
1.1 |
|
14 |
|
|
/** |
15 |
dl |
1.12 |
* A synchronization point at which two threads can exchange objects. |
16 |
|
|
* Each thread presents some object on entry to the {@link #exchange |
17 |
|
|
* exchange} method, and receives the object presented by the other |
18 |
|
|
* thread on return. |
19 |
tim |
1.1 |
* |
20 |
|
|
* <p><b>Sample Usage:</b> |
21 |
|
|
* Here are the highlights of a class that uses an <tt>Exchanger</tt> to |
22 |
|
|
* swap buffers between threads so that the thread filling the |
23 |
|
|
* buffer gets a freshly |
24 |
|
|
* emptied one when it needs it, handing off the filled one to |
25 |
|
|
* the thread emptying the buffer. |
26 |
|
|
* <pre> |
27 |
|
|
* class FillAndEmpty { |
28 |
dl |
1.9 |
* Exchanger<DataBuffer> exchanger = new Exchanger(); |
29 |
|
|
* 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 |
|
|
* if (currentBuffer.full()) |
39 |
|
|
* 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 |
|
|
* if (currentBuffer.empty()) |
52 |
|
|
* 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 |
|
|
* } |
63 |
|
|
* </pre> |
64 |
|
|
* |
65 |
brian |
1.22 |
* <p> Memory visibility effects: Actions in a thread prior to calling |
66 |
|
|
* <tt>exchange()</tt> <a |
67 |
|
|
* href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
68 |
|
|
* those subsequent to the matching <tt>exchange()</tt> in another thread. |
69 |
|
|
* |
70 |
tim |
1.1 |
* @since 1.5 |
71 |
dl |
1.16 |
* @author Doug Lea and Bill Scherer and Michael Scott |
72 |
dl |
1.11 |
* @param <V> The type of objects that may be exchanged |
73 |
tim |
1.1 |
*/ |
74 |
|
|
public class Exchanger<V> { |
75 |
dl |
1.16 |
/* |
76 |
|
|
* The underlying idea is to use a stack to hold nodes containing |
77 |
|
|
* pairs of items to be exchanged. Except that: |
78 |
|
|
* |
79 |
|
|
* * Only one element of the pair is known on creation by a |
80 |
|
|
* first-arriving thread; the other is a "hole" waiting to be |
81 |
|
|
* filled in. This is a degenerate form of the dual stacks |
82 |
|
|
* described in "Nonblocking Concurrent Objects with Condition |
83 |
|
|
* Synchronization", by W. N. Scherer III and M. L. Scott. |
84 |
|
|
* 18th Annual Conf. on Distributed Computing, Oct. 2004. |
85 |
|
|
* It is "degenerate" in that both the items and the holes |
86 |
|
|
* are shared in the same nodes. |
87 |
|
|
* |
88 |
|
|
* * There isn't really a stack here! There can't be -- if two |
89 |
|
|
* nodes were both in the stack, they should cancel themselves |
90 |
|
|
* out by combining. So that's what we do. The 0th element of |
91 |
|
|
* the "arena" array serves only as the top of stack. The |
92 |
|
|
* remainder of the array is a form of the elimination backoff |
93 |
|
|
* collision array described in "A Scalable Lock-free Stack |
94 |
|
|
* Algorithm", by D. Hendler, N. Shavit, and L. Yerushalmi. |
95 |
|
|
* 16th ACM Symposium on Parallelism in Algorithms and |
96 |
|
|
* Architectures, June 2004. Here, threads spin (using short |
97 |
|
|
* timed waits with exponential backoff) looking for each |
98 |
|
|
* other. If they fail to find others waiting, they try the |
99 |
|
|
* top spot again. As shown in that paper, this always |
100 |
|
|
* converges. |
101 |
|
|
* |
102 |
|
|
* The backoff elimination mechanics never come into play in |
103 |
|
|
* common usages where only two threads ever meet to exchange |
104 |
|
|
* items, but they prevent contention bottlenecks when an |
105 |
|
|
* exchanger is used by a large number of threads. |
106 |
|
|
*/ |
107 |
dl |
1.2 |
|
108 |
jsr166 |
1.17 |
/** |
109 |
dl |
1.16 |
* Size of collision space. Using a size of half the number of |
110 |
|
|
* CPUs provides enough space for threads to find each other but |
111 |
|
|
* not so much that it would always require one or more to time |
112 |
|
|
* out to become unstuck. Note that the arena array holds SIZE+1 |
113 |
|
|
* elements, to include the top-of-stack slot. |
114 |
|
|
*/ |
115 |
jsr166 |
1.17 |
private static final int SIZE = |
116 |
dl |
1.16 |
(Runtime.getRuntime().availableProcessors() + 1) / 2; |
117 |
jsr166 |
1.15 |
|
118 |
dl |
1.2 |
/** |
119 |
dl |
1.16 |
* Base unit in nanoseconds for backoffs. Must be a power of two. |
120 |
|
|
* Should be small because backoffs exponentially increase from |
121 |
|
|
* base. |
122 |
dl |
1.2 |
*/ |
123 |
dl |
1.16 |
private static final long BACKOFF_BASE = 128L; |
124 |
dl |
1.2 |
|
125 |
jsr166 |
1.17 |
/** |
126 |
dl |
1.16 |
* Sentinel item representing cancellation. This value is placed |
127 |
|
|
* in holes on cancellation, and used as a return value from Node |
128 |
|
|
* methods to indicate failure to set or get hole. |
129 |
jsr166 |
1.17 |
*/ |
130 |
dl |
1.16 |
static final Object FAIL = new Object(); |
131 |
|
|
|
132 |
jsr166 |
1.17 |
/** |
133 |
dl |
1.16 |
* The collision arena. arena[0] is used as the top of the stack. |
134 |
|
|
* The remainder is used as the collision elimination space. |
135 |
jsr166 |
1.17 |
* Each slot holds an AtomicReference<Node>, but this cannot be |
136 |
dl |
1.16 |
* expressed for arrays, so elements are casted on each use. |
137 |
dl |
1.3 |
*/ |
138 |
dl |
1.16 |
private final AtomicReference[] arena; |
139 |
dl |
1.5 |
|
140 |
dl |
1.16 |
/** Generator for random backoffs and delays. */ |
141 |
|
|
private final Random random = new Random(); |
142 |
dl |
1.5 |
|
143 |
dl |
1.16 |
/** |
144 |
|
|
* Creates a new Exchanger. |
145 |
|
|
*/ |
146 |
|
|
public Exchanger() { |
147 |
|
|
arena = new AtomicReference[SIZE + 1]; |
148 |
|
|
for (int i = 0; i < arena.length; ++i) |
149 |
|
|
arena[i] = new AtomicReference(); |
150 |
|
|
} |
151 |
dl |
1.2 |
|
152 |
dl |
1.16 |
/** |
153 |
|
|
* Main exchange function, handling the different policy variants. |
154 |
|
|
* Uses Object, not "V" as argument and return value to simplify |
155 |
|
|
* handling of internal sentinel values. Callers from public |
156 |
|
|
* methods cast accordingly. |
157 |
|
|
* @param item the item to exchange. |
158 |
|
|
* @param timed true if the wait is timed. |
159 |
|
|
* @param nanos if timed, the maximum wait time. |
160 |
|
|
* @return the other thread's item. |
161 |
|
|
*/ |
162 |
|
|
private Object doExchange(Object item, boolean timed, long nanos) |
163 |
|
|
throws InterruptedException, TimeoutException { |
164 |
|
|
Node me = new Node(item); |
165 |
|
|
long lastTime = (timed)? System.nanoTime() : 0; |
166 |
|
|
int idx = 0; // start out at slot representing top |
167 |
|
|
int backoff = 0; // increases on failure to occupy a slot |
168 |
|
|
|
169 |
|
|
for (;;) { |
170 |
|
|
AtomicReference<Node> slot = (AtomicReference<Node>)arena[idx]; |
171 |
|
|
|
172 |
|
|
// If this slot is already occupied, there is a waiting item... |
173 |
|
|
Node you = slot.get(); |
174 |
|
|
if (you != null) { |
175 |
|
|
Object v = you.fillHole(item); |
176 |
|
|
slot.compareAndSet(you, null); |
177 |
|
|
if (v != FAIL) // ... unless it was cancelled |
178 |
|
|
return v; |
179 |
dl |
1.2 |
} |
180 |
|
|
|
181 |
dl |
1.16 |
// Try to occupy this slot |
182 |
|
|
if (slot.compareAndSet(null, me)) { |
183 |
|
|
// If this is top slot, use regular wait, else backoff-wait |
184 |
|
|
Object v = ((idx == 0)? |
185 |
|
|
me.waitForHole(timed, nanos) : |
186 |
|
|
me.waitForHole(true, randomDelay(backoff))); |
187 |
|
|
slot.compareAndSet(me, null); |
188 |
|
|
if (v != FAIL) |
189 |
|
|
return v; |
190 |
|
|
if (Thread.interrupted()) |
191 |
|
|
throw new InterruptedException(); |
192 |
|
|
if (timed) { |
193 |
|
|
long now = System.nanoTime(); |
194 |
|
|
nanos -= now - lastTime; |
195 |
|
|
lastTime = now; |
196 |
|
|
if (nanos <= 0) |
197 |
|
|
throw new TimeoutException(); |
198 |
|
|
} |
199 |
dl |
1.2 |
|
200 |
dl |
1.16 |
me = new Node(item); // Throw away nodes on failure |
201 |
|
|
if (backoff < SIZE - 1) // Increase or stay saturated |
202 |
|
|
++backoff; |
203 |
|
|
idx = 0; // Restart at top |
204 |
dl |
1.2 |
} |
205 |
|
|
|
206 |
dl |
1.16 |
else // Retry with a random non-top slot <= backoff |
207 |
|
|
idx = 1 + random.nextInt(backoff + 1); |
208 |
dl |
1.2 |
|
209 |
|
|
} |
210 |
|
|
} |
211 |
tim |
1.1 |
|
212 |
|
|
/** |
213 |
dl |
1.16 |
* Returns a random delay less than (base times (2 raised to backoff)) |
214 |
|
|
*/ |
215 |
|
|
private long randomDelay(int backoff) { |
216 |
|
|
return ((BACKOFF_BASE << backoff) - 1) & random.nextInt(); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
/** |
220 |
|
|
* Nodes hold partially exchanged data. This class |
221 |
|
|
* opportunistically subclasses AtomicReference to represent the |
222 |
|
|
* hole. So get() returns hole, and compareAndSet CAS'es value |
223 |
|
|
* into hole. Note that this class cannot be parameterized as V |
224 |
|
|
* because the sentinel value FAIL is only of type Object. |
225 |
jsr166 |
1.15 |
*/ |
226 |
dl |
1.16 |
static final class Node extends AtomicReference<Object> { |
227 |
dl |
1.20 |
private static final long serialVersionUID = -3221313401284163686L; |
228 |
jsr166 |
1.21 |
|
229 |
dl |
1.16 |
/** The element offered by the Thread creating this node. */ |
230 |
|
|
final Object item; |
231 |
|
|
/** The Thread creating this node. */ |
232 |
|
|
final Thread waiter; |
233 |
|
|
|
234 |
|
|
/** |
235 |
|
|
* Creates node with given item and empty hole. |
236 |
|
|
* @param item the item. |
237 |
|
|
*/ |
238 |
|
|
Node(Object item) { |
239 |
|
|
this.item = item; |
240 |
|
|
waiter = Thread.currentThread(); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/** |
244 |
|
|
* Tries to fill in hole. On success, wakes up the waiter. |
245 |
|
|
* @param val the value to place in hole. |
246 |
|
|
* @return on success, the item; on failure, FAIL. |
247 |
|
|
*/ |
248 |
|
|
Object fillHole(Object val) { |
249 |
|
|
if (compareAndSet(null, val)) { |
250 |
|
|
LockSupport.unpark(waiter); |
251 |
|
|
return item; |
252 |
|
|
} |
253 |
|
|
return FAIL; |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
/** |
257 |
jsr166 |
1.17 |
* Waits for and gets the hole filled in by another thread. |
258 |
|
|
* Fails if timed out or interrupted before hole filled. |
259 |
dl |
1.16 |
* @param timed true if the wait is timed. |
260 |
|
|
* @param nanos if timed, the maximum wait time. |
261 |
|
|
* @return on success, the hole; on failure, FAIL. |
262 |
|
|
*/ |
263 |
|
|
Object waitForHole(boolean timed, long nanos) { |
264 |
|
|
long lastTime = (timed)? System.nanoTime() : 0; |
265 |
dl |
1.18 |
Object h; |
266 |
|
|
while ((h = get()) == null) { |
267 |
|
|
// If interrupted or timed out, try to cancel by |
268 |
|
|
// CASing FAIL as hole value. |
269 |
|
|
if (Thread.currentThread().isInterrupted() || |
270 |
jsr166 |
1.19 |
(timed && nanos <= 0)) |
271 |
dl |
1.18 |
compareAndSet(null, FAIL); |
272 |
|
|
else if (!timed) |
273 |
dl |
1.16 |
LockSupport.park(); |
274 |
|
|
else { |
275 |
|
|
LockSupport.parkNanos(nanos); |
276 |
|
|
long now = System.nanoTime(); |
277 |
|
|
nanos -= now - lastTime; |
278 |
|
|
lastTime = now; |
279 |
|
|
} |
280 |
|
|
} |
281 |
|
|
return h; |
282 |
|
|
} |
283 |
tim |
1.1 |
} |
284 |
|
|
|
285 |
|
|
/** |
286 |
|
|
* Waits for another thread to arrive at this exchange point (unless |
287 |
|
|
* it is {@link Thread#interrupt interrupted}), |
288 |
|
|
* and then transfers the given object to it, receiving its object |
289 |
|
|
* in return. |
290 |
jsr166 |
1.17 |
* |
291 |
tim |
1.1 |
* <p>If another thread is already waiting at the exchange point then |
292 |
|
|
* it is resumed for thread scheduling purposes and receives the object |
293 |
|
|
* passed in by the current thread. The current thread returns immediately, |
294 |
|
|
* receiving the object passed to the exchange by that other thread. |
295 |
jsr166 |
1.17 |
* |
296 |
jsr166 |
1.15 |
* <p>If no other thread is already waiting at the exchange then the |
297 |
tim |
1.1 |
* current thread is disabled for thread scheduling purposes and lies |
298 |
|
|
* dormant until one of two things happens: |
299 |
|
|
* <ul> |
300 |
|
|
* <li>Some other thread enters the exchange; or |
301 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
302 |
|
|
* thread. |
303 |
|
|
* </ul> |
304 |
|
|
* <p>If the current thread: |
305 |
|
|
* <ul> |
306 |
jsr166 |
1.15 |
* <li>has its interrupted status set on entry to this method; or |
307 |
tim |
1.1 |
* <li>is {@link Thread#interrupt interrupted} while waiting |
308 |
jsr166 |
1.15 |
* for the exchange, |
309 |
tim |
1.1 |
* </ul> |
310 |
jsr166 |
1.15 |
* then {@link InterruptedException} is thrown and the current thread's |
311 |
|
|
* interrupted status is cleared. |
312 |
tim |
1.1 |
* |
313 |
|
|
* @param x the object to exchange |
314 |
|
|
* @return the object provided by the other thread. |
315 |
jsr166 |
1.15 |
* @throws InterruptedException if current thread was interrupted |
316 |
dl |
1.3 |
* while waiting |
317 |
jsr166 |
1.15 |
*/ |
318 |
tim |
1.1 |
public V exchange(V x) throws InterruptedException { |
319 |
dl |
1.2 |
try { |
320 |
dl |
1.16 |
return (V)doExchange(x, false, 0); |
321 |
jsr166 |
1.15 |
} catch (TimeoutException cannotHappen) { |
322 |
dl |
1.2 |
throw new Error(cannotHappen); |
323 |
|
|
} |
324 |
tim |
1.1 |
} |
325 |
|
|
|
326 |
|
|
/** |
327 |
|
|
* Waits for another thread to arrive at this exchange point (unless |
328 |
|
|
* it is {@link Thread#interrupt interrupted}, or the specified waiting |
329 |
|
|
* time elapses), |
330 |
|
|
* and then transfers the given object to it, receiving its object |
331 |
|
|
* in return. |
332 |
|
|
* |
333 |
|
|
* <p>If another thread is already waiting at the exchange point then |
334 |
|
|
* it is resumed for thread scheduling purposes and receives the object |
335 |
|
|
* passed in by the current thread. The current thread returns immediately, |
336 |
|
|
* receiving the object passed to the exchange by that other thread. |
337 |
|
|
* |
338 |
jsr166 |
1.15 |
* <p>If no other thread is already waiting at the exchange then the |
339 |
tim |
1.1 |
* current thread is disabled for thread scheduling purposes and lies |
340 |
|
|
* dormant until one of three things happens: |
341 |
|
|
* <ul> |
342 |
|
|
* <li>Some other thread enters the exchange; or |
343 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
344 |
|
|
* thread; or |
345 |
|
|
* <li>The specified waiting time elapses. |
346 |
|
|
* </ul> |
347 |
|
|
* <p>If the current thread: |
348 |
|
|
* <ul> |
349 |
jsr166 |
1.15 |
* <li>has its interrupted status set on entry to this method; or |
350 |
tim |
1.1 |
* <li>is {@link Thread#interrupt interrupted} while waiting |
351 |
jsr166 |
1.15 |
* for the exchange, |
352 |
tim |
1.1 |
* </ul> |
353 |
jsr166 |
1.15 |
* then {@link InterruptedException} is thrown and the current thread's |
354 |
|
|
* interrupted status is cleared. |
355 |
tim |
1.1 |
* |
356 |
|
|
* <p>If the specified waiting time elapses then {@link TimeoutException} |
357 |
|
|
* is thrown. |
358 |
jsr166 |
1.15 |
* If the time is |
359 |
tim |
1.1 |
* less than or equal to zero, the method will not wait at all. |
360 |
|
|
* |
361 |
|
|
* @param x the object to exchange |
362 |
|
|
* @param timeout the maximum time to wait |
363 |
dl |
1.2 |
* @param unit the time unit of the <tt>timeout</tt> argument. |
364 |
tim |
1.1 |
* @return the object provided by the other thread. |
365 |
dl |
1.3 |
* @throws InterruptedException if current thread was interrupted |
366 |
|
|
* while waiting |
367 |
tim |
1.1 |
* @throws TimeoutException if the specified waiting time elapses before |
368 |
|
|
* another thread enters the exchange. |
369 |
jsr166 |
1.15 |
*/ |
370 |
|
|
public V exchange(V x, long timeout, TimeUnit unit) |
371 |
tim |
1.1 |
throws InterruptedException, TimeoutException { |
372 |
dl |
1.16 |
return (V)doExchange(x, true, unit.toNanos(timeout)); |
373 |
tim |
1.1 |
} |
374 |
|
|
} |