1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain. Use, modify, and |
4 |
|
|
* redistribute this code in any way without acknowledgement. |
5 |
|
|
*/ |
6 |
|
|
|
7 |
tim |
1.1 |
package java.util.concurrent; |
8 |
|
|
|
9 |
|
|
/** |
10 |
dl |
1.3 |
* A synchronization aid that allows a set threads to all wait for |
11 |
brian |
1.4 |
* each other to reach a common barrier point. CyclicBarriers are |
12 |
dl |
1.3 |
* useful in programs involving a fixed sized party of threads that |
13 |
brian |
1.4 |
* must occasionally wait for each other. The barrier is called |
14 |
dl |
1.3 |
* <em>cyclic</em> because it can be re-used after the waiting threads |
15 |
|
|
* are released. |
16 |
tim |
1.1 |
* |
17 |
|
|
* <p>A <tt>CyclicBarrier</tt> supports an optional {@link Runnable} command |
18 |
|
|
* that is run once per barrier point, after the last thread in the party |
19 |
|
|
* arrives, but before any threads are released. |
20 |
|
|
* This <em>barrier action</em> is useful |
21 |
|
|
* for updating shared-state before any of the parties continue. |
22 |
|
|
* |
23 |
brian |
1.4 |
* <p><b>Sample usage:</b> Here is an example of |
24 |
tim |
1.1 |
* using a barrier in a parallel decomposition design: |
25 |
|
|
* <pre> |
26 |
|
|
* class Solver { |
27 |
|
|
* final int N; |
28 |
|
|
* final float[][] data; |
29 |
|
|
* final CyclicBarrier barrier; |
30 |
|
|
* |
31 |
|
|
* class Worker implements Runnable { |
32 |
|
|
* int myRow; |
33 |
|
|
* Worker(int row) { myRow = row; } |
34 |
|
|
* public void run() { |
35 |
|
|
* while (!done()) { |
36 |
|
|
* processRow(myRow); |
37 |
|
|
* |
38 |
|
|
* try { |
39 |
|
|
* barrier.await(); |
40 |
|
|
* } |
41 |
|
|
* catch (InterruptedException ex) { return; } |
42 |
|
|
* catch (BrokenBarrierException ex) { return; } |
43 |
|
|
* } |
44 |
|
|
* } |
45 |
|
|
* } |
46 |
|
|
* |
47 |
|
|
* public Solver(float[][] matrix) { |
48 |
|
|
* data = matrix; |
49 |
|
|
* N = matrix.length; |
50 |
|
|
* barrier = new CyclicBarrier(N, |
51 |
|
|
* new Runnable() { |
52 |
|
|
* public void run() { |
53 |
|
|
* mergeRows(...); |
54 |
|
|
* } |
55 |
|
|
* }); |
56 |
|
|
* for (int i = 0; i < N; ++i) |
57 |
|
|
* new Thread(new Worker(i)).start(); |
58 |
|
|
* |
59 |
|
|
* waitUntilDone(); |
60 |
|
|
* } |
61 |
|
|
* } |
62 |
|
|
* </pre> |
63 |
|
|
* Here, each worker thread processes a row of the matrix then waits at the |
64 |
|
|
* barrier until all rows have been processed. When all rows are processed |
65 |
|
|
* the supplied {@link Runnable} barrier action is executed and merges the |
66 |
|
|
* rows. If the merger |
67 |
|
|
* determines that a solution has been found then <tt>done()</tt> will return |
68 |
|
|
* <tt>true</tt> and each worker will terminate. |
69 |
|
|
* |
70 |
|
|
* <p>If the barrier action does not rely on the parties being suspended when |
71 |
|
|
* it is executed, then any of the threads in the party could execute that |
72 |
|
|
* action when it is released. To facilitate this, each invocation of |
73 |
|
|
* {@link #await} returns the arrival index of that thread at the barrier. |
74 |
|
|
* You can then choose which thread should execute the barrier action, for |
75 |
|
|
* example: |
76 |
|
|
* <pre> if (barrier.await() == 0) { |
77 |
|
|
* // log the completion of this iteration |
78 |
|
|
* }</pre> |
79 |
|
|
* |
80 |
dl |
1.2 |
* <p>The <tt>CyclicBarrier</tt> uses an all-or-none breakage model |
81 |
|
|
* for failed synchronization attempts: If a thread leaves a barrier |
82 |
|
|
* point prematurely because of interruption or timeout, all others |
83 |
|
|
* will also leave abnormally (via {@link BrokenBarrierException}), |
84 |
|
|
* until the barrier is {@link #reset}. This is usually the simplest |
85 |
|
|
* and best strategy for sharing knowledge about failures among |
86 |
|
|
* cooperating threads in the most common usage contexts of barriers. |
87 |
tim |
1.1 |
* |
88 |
|
|
* <h3>Implementation Considerations</h3> |
89 |
|
|
* <p>This implementation has the property that interruptions among newly |
90 |
|
|
* arriving threads can cause as-yet-unresumed threads from a previous |
91 |
|
|
* barrier cycle to return out as broken. This transmits breakage as |
92 |
|
|
* early as possible, but with the possible byproduct that only some |
93 |
|
|
* threads returning out of a barrier will realize that it is newly |
94 |
|
|
* broken. (Others will not realize this until a future cycle.) |
95 |
|
|
* |
96 |
|
|
* |
97 |
|
|
* |
98 |
|
|
* @since 1.5 |
99 |
|
|
* @spec JSR-166 |
100 |
dl |
1.5 |
* @revised $Date: 2003/06/23 02:26:16 $ |
101 |
|
|
* @editor $Author: brian $ |
102 |
brian |
1.4 |
* @see CountDownLatch |
103 |
tim |
1.1 |
* |
104 |
|
|
* @fixme Is the above property actually true in this implementation? |
105 |
|
|
* @fixme Should we have a timeout version of await()? |
106 |
dl |
1.5 |
* @author Doug Lea |
107 |
tim |
1.1 |
*/ |
108 |
|
|
public class CyclicBarrier { |
109 |
dl |
1.5 |
/** The lock for guarding barrier entry */ |
110 |
dl |
1.2 |
private final ReentrantLock lock = new ReentrantLock(); |
111 |
dl |
1.5 |
/** Condition to wait on until tripped */ |
112 |
dl |
1.2 |
private final Condition trip = lock.newCondition(); |
113 |
dl |
1.5 |
/** The number of parties */ |
114 |
dl |
1.2 |
private final int parties; |
115 |
dl |
1.5 |
/* The command to run when tripped */ |
116 |
dl |
1.2 |
private Runnable barrierCommand; |
117 |
|
|
|
118 |
|
|
/** |
119 |
|
|
* The generation number. Incremented mod Integer.MAX_VALUE every |
120 |
|
|
* time barrier tripped. Starts at 1 to simplify handling of |
121 |
|
|
* breakage indicator |
122 |
|
|
*/ |
123 |
|
|
private int generation = 1; |
124 |
|
|
|
125 |
|
|
/** |
126 |
|
|
* Breakage indicator: last generation of breakage, propagated |
127 |
|
|
* across barrier generations until reset. |
128 |
|
|
*/ |
129 |
|
|
private int broken = 0; |
130 |
|
|
|
131 |
|
|
/** |
132 |
|
|
* Number of parties still waiting. Counts down from parties to 0 |
133 |
|
|
* on each cycle. |
134 |
|
|
*/ |
135 |
|
|
private int count; |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* Update state on barrier trip. |
139 |
|
|
*/ |
140 |
|
|
private void nextGeneration() { |
141 |
|
|
count = parties; |
142 |
|
|
int g = generation; |
143 |
|
|
// avoid generation == 0 |
144 |
|
|
if (++generation < 0) generation = 1; |
145 |
|
|
// propagate breakage |
146 |
|
|
if (broken == g) broken = generation; |
147 |
|
|
} |
148 |
|
|
|
149 |
dl |
1.5 |
/** |
150 |
|
|
* Main barrier code, covering the various pilicies. |
151 |
|
|
*/ |
152 |
dl |
1.2 |
private int dowait(boolean timed, long nanos) throws InterruptedException, BrokenBarrierException, TimeoutException { |
153 |
|
|
lock.lock(); |
154 |
|
|
try { |
155 |
|
|
int index = --count; |
156 |
|
|
int g = generation; |
157 |
|
|
|
158 |
|
|
if (broken == g) |
159 |
|
|
throw new BrokenBarrierException(); |
160 |
|
|
|
161 |
|
|
if (Thread.interrupted()) { |
162 |
|
|
broken = g; |
163 |
|
|
trip.signalAll(); |
164 |
|
|
throw new InterruptedException(); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
if (index == 0) { // tripped |
168 |
|
|
nextGeneration(); |
169 |
|
|
trip.signalAll(); |
170 |
|
|
try { |
171 |
|
|
if (barrierCommand != null) |
172 |
|
|
barrierCommand.run(); |
173 |
|
|
return 0; |
174 |
|
|
} |
175 |
|
|
catch (RuntimeException ex) { |
176 |
|
|
broken = generation; // next generation is broken |
177 |
|
|
throw ex; |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
while (generation == g) { |
182 |
|
|
try { |
183 |
|
|
if (!timed) |
184 |
|
|
trip.await(); |
185 |
|
|
else if (nanos > 0) |
186 |
|
|
nanos = trip.awaitNanos(nanos); |
187 |
|
|
} |
188 |
|
|
catch (InterruptedException ex) { |
189 |
|
|
// Only claim that broken if interrupted before reset |
190 |
|
|
if (generation == g) { |
191 |
|
|
broken = g; |
192 |
|
|
trip.signalAll(); |
193 |
|
|
throw ex; |
194 |
|
|
} |
195 |
|
|
else { |
196 |
|
|
Thread.currentThread().interrupt(); // propagate |
197 |
|
|
break; |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
if (timed && nanos <= 0) { |
202 |
|
|
broken = g; |
203 |
|
|
trip.signalAll(); |
204 |
|
|
throw new TimeoutException(); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
if (broken == generation) |
208 |
|
|
throw new BrokenBarrierException(); |
209 |
|
|
|
210 |
|
|
} |
211 |
|
|
return index; |
212 |
|
|
|
213 |
|
|
} |
214 |
|
|
finally { |
215 |
|
|
lock.unlock(); |
216 |
|
|
} |
217 |
|
|
} |
218 |
tim |
1.1 |
|
219 |
|
|
/** |
220 |
|
|
* Create a new <tt>CyclicBarrier</tt> that will trip when the |
221 |
|
|
* given number of parties (threads) are waiting upon it, and which |
222 |
|
|
* will execute the given barrier action when the barrier is tripped. |
223 |
|
|
* |
224 |
|
|
* @param parties the number of threads that must invoke {@link #await} |
225 |
|
|
* before the barrier is tripped. |
226 |
|
|
* @param barrierAction the command to execute when the barrier is |
227 |
|
|
* tripped. |
228 |
|
|
* |
229 |
|
|
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
230 |
|
|
*/ |
231 |
|
|
public CyclicBarrier(int parties, Runnable barrierAction) { |
232 |
dl |
1.2 |
if (parties <= 0) throw new IllegalArgumentException(); |
233 |
|
|
this.parties = parties; |
234 |
|
|
this.count = parties; |
235 |
|
|
this.barrierCommand = barrierAction; |
236 |
tim |
1.1 |
} |
237 |
|
|
|
238 |
|
|
/** |
239 |
|
|
* Create a new <tt>CyclicBarrier</tt> that will trip when the |
240 |
|
|
* given number of parties (threads) are waiting upon it. |
241 |
|
|
* |
242 |
|
|
* <p>This is equivalent to <tt>CyclicBarrier(parties, null)</tt>. |
243 |
|
|
* |
244 |
|
|
* @param parties the number of threads that must invoke {@link #await} |
245 |
|
|
* before the barrier is tripped. |
246 |
|
|
* |
247 |
|
|
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
248 |
|
|
*/ |
249 |
|
|
public CyclicBarrier(int parties) { |
250 |
dl |
1.2 |
this(parties, null); |
251 |
tim |
1.1 |
} |
252 |
|
|
|
253 |
|
|
/** |
254 |
|
|
* Return the number of parties required to trip this barrier. |
255 |
|
|
* @return the number of parties required to trip this barrier. |
256 |
|
|
**/ |
257 |
|
|
public int getParties() { |
258 |
dl |
1.2 |
return parties; |
259 |
tim |
1.1 |
} |
260 |
|
|
|
261 |
|
|
/** |
262 |
|
|
* Wait until all {@link #getParties parties} have invoked <tt>await</tt> |
263 |
|
|
* on this barrier. |
264 |
|
|
* |
265 |
|
|
* <p>If the current thread is not the last to arrive then it is |
266 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
267 |
dl |
1.2 |
* one of following things happens: |
268 |
tim |
1.1 |
* <ul> |
269 |
|
|
* <li>The last thread arrives; or |
270 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
271 |
|
|
* thread; or |
272 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} one of the |
273 |
|
|
* other waiting threads; or |
274 |
dl |
1.2 |
* <li>Some other thread times out while waiting for barrier; or |
275 |
tim |
1.1 |
* <li>Some other thread invokes {@link #reset} on this barrier. |
276 |
|
|
* </ul> |
277 |
|
|
* <p>If the current thread: |
278 |
|
|
* <ul> |
279 |
|
|
* <li>has its interrupted status set on entry to this method; or |
280 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
281 |
|
|
* </ul> |
282 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
283 |
|
|
* interrupted status is cleared. |
284 |
|
|
* |
285 |
|
|
* <p>If the barrier is {@link #reset} while any thread is waiting, or if |
286 |
|
|
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked |
287 |
|
|
* then {@link BrokenBarrierException} is thrown. |
288 |
|
|
* |
289 |
|
|
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting, |
290 |
|
|
* then all other waiting threads will throw |
291 |
|
|
* {@link BrokenBarrierException} and the barrier is placed in the broken |
292 |
|
|
* state. |
293 |
|
|
* |
294 |
|
|
* <p>If the current thread is the last thread to arrive, and a |
295 |
|
|
* non-null barrier action was supplied in the constructor, then the |
296 |
|
|
* current thread runs the action before allowing the other threads to |
297 |
|
|
* continue. |
298 |
|
|
* If an exception occurs during the barrier action then that exception |
299 |
|
|
* will be propagated in the current thread. |
300 |
|
|
* |
301 |
|
|
* @return the arrival index of the current thread, where index |
302 |
|
|
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and |
303 |
|
|
* zero indicates the last to arrive. |
304 |
|
|
* |
305 |
|
|
* @throws InterruptedException if the current thread was interrupted |
306 |
|
|
* while waiting |
307 |
|
|
* @throws BrokenBarrierException if <em>another</em> thread was |
308 |
|
|
* interrupted while the current thread was waiting, or the barrier was |
309 |
|
|
* reset, or the barrier was broken when <tt>await</tt> was called. |
310 |
|
|
*/ |
311 |
|
|
public int await() throws InterruptedException, BrokenBarrierException { |
312 |
dl |
1.2 |
try { |
313 |
|
|
return dowait(false, 0); |
314 |
|
|
} |
315 |
|
|
catch (TimeoutException toe) { |
316 |
|
|
throw new Error(toe); // cannot happen; |
317 |
|
|
} |
318 |
|
|
} |
319 |
|
|
|
320 |
|
|
/** |
321 |
|
|
* Wait until all {@link #getParties parties} have invoked <tt>await</tt> |
322 |
|
|
* on this barrier. |
323 |
|
|
* |
324 |
|
|
* <p>If the current thread is not the last to arrive then it is |
325 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
326 |
|
|
* one of the following things happens: |
327 |
|
|
* <ul> |
328 |
|
|
* <li>The last thread arrives; or |
329 |
|
|
* <li>The speceified timeout elapses; or |
330 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
331 |
|
|
* thread; or |
332 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} one of the |
333 |
|
|
* other waiting threads; or |
334 |
|
|
* <li>Some other thread times out while waiting for barrier; or |
335 |
|
|
* <li>Some other thread invokes {@link #reset} on this barrier. |
336 |
|
|
* </ul> |
337 |
|
|
* <p>If the current thread: |
338 |
|
|
* <ul> |
339 |
|
|
* <li>has its interrupted status set on entry to this method; or |
340 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
341 |
|
|
* </ul> |
342 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
343 |
|
|
* interrupted status is cleared. |
344 |
|
|
* |
345 |
|
|
* <p>If the barrier is {@link #reset} while any thread is waiting, or if |
346 |
|
|
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked |
347 |
|
|
* then {@link BrokenBarrierException} is thrown. |
348 |
|
|
* |
349 |
|
|
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting, |
350 |
|
|
* then all other waiting threads will throw |
351 |
|
|
* {@link BrokenBarrierException} and the barrier is placed in the broken |
352 |
|
|
* state. |
353 |
|
|
* |
354 |
|
|
* <p>If the current thread is the last thread to arrive, and a |
355 |
|
|
* non-null barrier action was supplied in the constructor, then the |
356 |
|
|
* current thread runs the action before allowing the other threads to |
357 |
|
|
* continue. |
358 |
|
|
* If an exception occurs during the barrier action then that exception |
359 |
|
|
* will be propagated in the current thread. |
360 |
|
|
* |
361 |
dl |
1.5 |
* @param timeout the time to wait for the barrier |
362 |
|
|
* @param unit the time unit of the timeout parameter |
363 |
dl |
1.2 |
* @return the arrival index of the current thread, where index |
364 |
|
|
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and |
365 |
|
|
* zero indicates the last to arrive. |
366 |
|
|
* |
367 |
|
|
* @throws InterruptedException if the current thread was interrupted |
368 |
|
|
* while waiting |
369 |
|
|
* @throws TimeoutException if the specified timeout elapses. |
370 |
|
|
* @throws BrokenBarrierException if <em>another</em> thread was |
371 |
|
|
* interrupted while the current thread was waiting, or the barrier was |
372 |
|
|
* reset, or the barrier was broken when <tt>await</tt> was called. |
373 |
|
|
*/ |
374 |
|
|
public int await(long timeout, TimeUnit unit) throws InterruptedException, BrokenBarrierException, TimeoutException { |
375 |
|
|
return dowait(true, unit.toNanos(timeout)); |
376 |
tim |
1.1 |
} |
377 |
|
|
|
378 |
|
|
/** |
379 |
|
|
* Query if this barrier is in a broken state. |
380 |
|
|
* @return <tt>true</tt> if one or more parties broke out of this |
381 |
dl |
1.2 |
* barrier due to interruption or timeout since construction or |
382 |
|
|
* the last reset; and <tt>false</tt> otherwise. |
383 |
tim |
1.1 |
*/ |
384 |
|
|
public boolean isBroken() { |
385 |
dl |
1.2 |
lock.lock(); |
386 |
|
|
try { |
387 |
|
|
return broken >= generation; |
388 |
|
|
} |
389 |
|
|
finally { |
390 |
|
|
lock.unlock(); |
391 |
|
|
} |
392 |
tim |
1.1 |
} |
393 |
|
|
|
394 |
|
|
/** |
395 |
|
|
* Reset the barrier to its initial state. If any parties are |
396 |
|
|
* currently waiting at the barrier, they will return with a |
397 |
|
|
* {@link BrokenBarrierException}. |
398 |
|
|
*/ |
399 |
|
|
public void reset() { |
400 |
dl |
1.2 |
lock.lock(); |
401 |
|
|
try { |
402 |
|
|
int g = generation; |
403 |
|
|
nextGeneration(); |
404 |
|
|
broken = g; // cause brokenness setting to stop at previous gen. |
405 |
|
|
trip.signalAll(); |
406 |
|
|
} |
407 |
|
|
finally { |
408 |
|
|
lock.unlock(); |
409 |
|
|
} |
410 |
tim |
1.1 |
} |
411 |
|
|
|
412 |
|
|
/** |
413 |
|
|
* Return the number of parties currently waiting at the barrier. |
414 |
|
|
* This method is primarily useful for debugging and assertions. |
415 |
|
|
* |
416 |
|
|
* @return the number of parties currently blocked in {@link #await} |
417 |
|
|
**/ |
418 |
|
|
public int getNumberWaiting() { |
419 |
dl |
1.2 |
lock.lock(); |
420 |
|
|
try { |
421 |
|
|
return parties - count; |
422 |
|
|
} |
423 |
|
|
finally { |
424 |
|
|
lock.unlock(); |
425 |
|
|
} |
426 |
tim |
1.1 |
} |
427 |
|
|
|
428 |
|
|
} |
429 |
|
|
|
430 |
|
|
|