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 |
brian |
1.4 |
* @revised $Date: 2003/06/07 18:20:20 $ |
101 |
dl |
1.3 |
* @editor $Author: dl $ |
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 |
|
|
*/ |
107 |
|
|
public class CyclicBarrier { |
108 |
dl |
1.2 |
private final ReentrantLock lock = new ReentrantLock(); |
109 |
|
|
private final Condition trip = lock.newCondition(); |
110 |
|
|
private final int parties; |
111 |
|
|
private Runnable barrierCommand; |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* The generation number. Incremented mod Integer.MAX_VALUE every |
115 |
|
|
* time barrier tripped. Starts at 1 to simplify handling of |
116 |
|
|
* breakage indicator |
117 |
|
|
*/ |
118 |
|
|
private int generation = 1; |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* Breakage indicator: last generation of breakage, propagated |
122 |
|
|
* across barrier generations until reset. |
123 |
|
|
*/ |
124 |
|
|
private int broken = 0; |
125 |
|
|
|
126 |
|
|
/** |
127 |
|
|
* Number of parties still waiting. Counts down from parties to 0 |
128 |
|
|
* on each cycle. |
129 |
|
|
*/ |
130 |
|
|
private int count; |
131 |
|
|
|
132 |
|
|
/** |
133 |
|
|
* Update state on barrier trip. |
134 |
|
|
*/ |
135 |
|
|
private void nextGeneration() { |
136 |
|
|
count = parties; |
137 |
|
|
int g = generation; |
138 |
|
|
// avoid generation == 0 |
139 |
|
|
if (++generation < 0) generation = 1; |
140 |
|
|
// propagate breakage |
141 |
|
|
if (broken == g) broken = generation; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
private int dowait(boolean timed, long nanos) throws InterruptedException, BrokenBarrierException, TimeoutException { |
145 |
|
|
lock.lock(); |
146 |
|
|
try { |
147 |
|
|
int index = --count; |
148 |
|
|
int g = generation; |
149 |
|
|
|
150 |
|
|
if (broken == g) |
151 |
|
|
throw new BrokenBarrierException(); |
152 |
|
|
|
153 |
|
|
if (Thread.interrupted()) { |
154 |
|
|
broken = g; |
155 |
|
|
trip.signalAll(); |
156 |
|
|
throw new InterruptedException(); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
if (index == 0) { // tripped |
160 |
|
|
nextGeneration(); |
161 |
|
|
trip.signalAll(); |
162 |
|
|
try { |
163 |
|
|
if (barrierCommand != null) |
164 |
|
|
barrierCommand.run(); |
165 |
|
|
return 0; |
166 |
|
|
} |
167 |
|
|
catch (RuntimeException ex) { |
168 |
|
|
broken = generation; // next generation is broken |
169 |
|
|
throw ex; |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
while (generation == g) { |
174 |
|
|
try { |
175 |
|
|
if (!timed) |
176 |
|
|
trip.await(); |
177 |
|
|
else if (nanos > 0) |
178 |
|
|
nanos = trip.awaitNanos(nanos); |
179 |
|
|
} |
180 |
|
|
catch (InterruptedException ex) { |
181 |
|
|
// Only claim that broken if interrupted before reset |
182 |
|
|
if (generation == g) { |
183 |
|
|
broken = g; |
184 |
|
|
trip.signalAll(); |
185 |
|
|
throw ex; |
186 |
|
|
} |
187 |
|
|
else { |
188 |
|
|
Thread.currentThread().interrupt(); // propagate |
189 |
|
|
break; |
190 |
|
|
} |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
if (timed && nanos <= 0) { |
194 |
|
|
broken = g; |
195 |
|
|
trip.signalAll(); |
196 |
|
|
throw new TimeoutException(); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
if (broken == generation) |
200 |
|
|
throw new BrokenBarrierException(); |
201 |
|
|
|
202 |
|
|
} |
203 |
|
|
return index; |
204 |
|
|
|
205 |
|
|
} |
206 |
|
|
finally { |
207 |
|
|
lock.unlock(); |
208 |
|
|
} |
209 |
|
|
} |
210 |
tim |
1.1 |
|
211 |
|
|
/** |
212 |
|
|
* Create a new <tt>CyclicBarrier</tt> that will trip when the |
213 |
|
|
* given number of parties (threads) are waiting upon it, and which |
214 |
|
|
* will execute the given barrier action when the barrier is tripped. |
215 |
|
|
* |
216 |
|
|
* @param parties the number of threads that must invoke {@link #await} |
217 |
|
|
* before the barrier is tripped. |
218 |
|
|
* @param barrierAction the command to execute when the barrier is |
219 |
|
|
* tripped. |
220 |
|
|
* |
221 |
|
|
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
222 |
|
|
*/ |
223 |
|
|
public CyclicBarrier(int parties, Runnable barrierAction) { |
224 |
dl |
1.2 |
if (parties <= 0) throw new IllegalArgumentException(); |
225 |
|
|
this.parties = parties; |
226 |
|
|
this.count = parties; |
227 |
|
|
this.barrierCommand = barrierAction; |
228 |
tim |
1.1 |
} |
229 |
|
|
|
230 |
|
|
/** |
231 |
|
|
* Create a new <tt>CyclicBarrier</tt> that will trip when the |
232 |
|
|
* given number of parties (threads) are waiting upon it. |
233 |
|
|
* |
234 |
|
|
* <p>This is equivalent to <tt>CyclicBarrier(parties, null)</tt>. |
235 |
|
|
* |
236 |
|
|
* @param parties the number of threads that must invoke {@link #await} |
237 |
|
|
* before the barrier is tripped. |
238 |
|
|
* |
239 |
|
|
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
240 |
|
|
*/ |
241 |
|
|
public CyclicBarrier(int parties) { |
242 |
dl |
1.2 |
this(parties, null); |
243 |
tim |
1.1 |
} |
244 |
|
|
|
245 |
|
|
/** |
246 |
|
|
* Return the number of parties required to trip this barrier. |
247 |
|
|
* @return the number of parties required to trip this barrier. |
248 |
|
|
**/ |
249 |
|
|
public int getParties() { |
250 |
dl |
1.2 |
return parties; |
251 |
tim |
1.1 |
} |
252 |
|
|
|
253 |
|
|
/** |
254 |
|
|
* Wait until all {@link #getParties parties} have invoked <tt>await</tt> |
255 |
|
|
* on this barrier. |
256 |
|
|
* |
257 |
|
|
* <p>If the current thread is not the last to arrive then it is |
258 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
259 |
dl |
1.2 |
* one of following things happens: |
260 |
tim |
1.1 |
* <ul> |
261 |
|
|
* <li>The last thread arrives; or |
262 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
263 |
|
|
* thread; or |
264 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} one of the |
265 |
|
|
* other waiting threads; or |
266 |
dl |
1.2 |
* <li>Some other thread times out while waiting for barrier; or |
267 |
tim |
1.1 |
* <li>Some other thread invokes {@link #reset} on this barrier. |
268 |
|
|
* </ul> |
269 |
|
|
* <p>If the current thread: |
270 |
|
|
* <ul> |
271 |
|
|
* <li>has its interrupted status set on entry to this method; or |
272 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
273 |
|
|
* </ul> |
274 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
275 |
|
|
* interrupted status is cleared. |
276 |
|
|
* |
277 |
|
|
* <p>If the barrier is {@link #reset} while any thread is waiting, or if |
278 |
|
|
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked |
279 |
|
|
* then {@link BrokenBarrierException} is thrown. |
280 |
|
|
* |
281 |
|
|
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting, |
282 |
|
|
* then all other waiting threads will throw |
283 |
|
|
* {@link BrokenBarrierException} and the barrier is placed in the broken |
284 |
|
|
* state. |
285 |
|
|
* |
286 |
|
|
* <p>If the current thread is the last thread to arrive, and a |
287 |
|
|
* non-null barrier action was supplied in the constructor, then the |
288 |
|
|
* current thread runs the action before allowing the other threads to |
289 |
|
|
* continue. |
290 |
|
|
* If an exception occurs during the barrier action then that exception |
291 |
|
|
* will be propagated in the current thread. |
292 |
|
|
* |
293 |
|
|
* @return the arrival index of the current thread, where index |
294 |
|
|
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and |
295 |
|
|
* zero indicates the last to arrive. |
296 |
|
|
* |
297 |
|
|
* @throws InterruptedException if the current thread was interrupted |
298 |
|
|
* while waiting |
299 |
|
|
* @throws BrokenBarrierException if <em>another</em> thread was |
300 |
|
|
* interrupted while the current thread was waiting, or the barrier was |
301 |
|
|
* reset, or the barrier was broken when <tt>await</tt> was called. |
302 |
|
|
*/ |
303 |
|
|
public int await() throws InterruptedException, BrokenBarrierException { |
304 |
dl |
1.2 |
try { |
305 |
|
|
return dowait(false, 0); |
306 |
|
|
} |
307 |
|
|
catch (TimeoutException toe) { |
308 |
|
|
throw new Error(toe); // cannot happen; |
309 |
|
|
} |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
/** |
313 |
|
|
* Wait until all {@link #getParties parties} have invoked <tt>await</tt> |
314 |
|
|
* on this barrier. |
315 |
|
|
* |
316 |
|
|
* <p>If the current thread is not the last to arrive then it is |
317 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
318 |
|
|
* one of the following things happens: |
319 |
|
|
* <ul> |
320 |
|
|
* <li>The last thread arrives; or |
321 |
|
|
* <li>The speceified timeout elapses; or |
322 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
323 |
|
|
* thread; or |
324 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} one of the |
325 |
|
|
* other waiting threads; or |
326 |
|
|
* <li>Some other thread times out while waiting for barrier; or |
327 |
|
|
* <li>Some other thread invokes {@link #reset} on this barrier. |
328 |
|
|
* </ul> |
329 |
|
|
* <p>If the current thread: |
330 |
|
|
* <ul> |
331 |
|
|
* <li>has its interrupted status set on entry to this method; or |
332 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
333 |
|
|
* </ul> |
334 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
335 |
|
|
* interrupted status is cleared. |
336 |
|
|
* |
337 |
|
|
* <p>If the barrier is {@link #reset} while any thread is waiting, or if |
338 |
|
|
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked |
339 |
|
|
* then {@link BrokenBarrierException} is thrown. |
340 |
|
|
* |
341 |
|
|
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting, |
342 |
|
|
* then all other waiting threads will throw |
343 |
|
|
* {@link BrokenBarrierException} and the barrier is placed in the broken |
344 |
|
|
* state. |
345 |
|
|
* |
346 |
|
|
* <p>If the current thread is the last thread to arrive, and a |
347 |
|
|
* non-null barrier action was supplied in the constructor, then the |
348 |
|
|
* current thread runs the action before allowing the other threads to |
349 |
|
|
* continue. |
350 |
|
|
* If an exception occurs during the barrier action then that exception |
351 |
|
|
* will be propagated in the current thread. |
352 |
|
|
* |
353 |
|
|
* @return the arrival index of the current thread, where index |
354 |
|
|
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and |
355 |
|
|
* zero indicates the last to arrive. |
356 |
|
|
* |
357 |
|
|
* @throws InterruptedException if the current thread was interrupted |
358 |
|
|
* while waiting |
359 |
|
|
* @throws TimeoutException if the specified timeout elapses. |
360 |
|
|
* @throws BrokenBarrierException if <em>another</em> thread was |
361 |
|
|
* interrupted while the current thread was waiting, or the barrier was |
362 |
|
|
* reset, or the barrier was broken when <tt>await</tt> was called. |
363 |
|
|
*/ |
364 |
|
|
public int await(long timeout, TimeUnit unit) throws InterruptedException, BrokenBarrierException, TimeoutException { |
365 |
|
|
return dowait(true, unit.toNanos(timeout)); |
366 |
tim |
1.1 |
} |
367 |
|
|
|
368 |
|
|
/** |
369 |
|
|
* Query if this barrier is in a broken state. |
370 |
|
|
* @return <tt>true</tt> if one or more parties broke out of this |
371 |
dl |
1.2 |
* barrier due to interruption or timeout since construction or |
372 |
|
|
* the last reset; and <tt>false</tt> otherwise. |
373 |
tim |
1.1 |
*/ |
374 |
|
|
public boolean isBroken() { |
375 |
dl |
1.2 |
lock.lock(); |
376 |
|
|
try { |
377 |
|
|
return broken >= generation; |
378 |
|
|
} |
379 |
|
|
finally { |
380 |
|
|
lock.unlock(); |
381 |
|
|
} |
382 |
tim |
1.1 |
} |
383 |
|
|
|
384 |
|
|
/** |
385 |
|
|
* Reset the barrier to its initial state. If any parties are |
386 |
|
|
* currently waiting at the barrier, they will return with a |
387 |
|
|
* {@link BrokenBarrierException}. |
388 |
|
|
*/ |
389 |
|
|
public void reset() { |
390 |
dl |
1.2 |
lock.lock(); |
391 |
|
|
try { |
392 |
|
|
int g = generation; |
393 |
|
|
nextGeneration(); |
394 |
|
|
broken = g; // cause brokenness setting to stop at previous gen. |
395 |
|
|
trip.signalAll(); |
396 |
|
|
} |
397 |
|
|
finally { |
398 |
|
|
lock.unlock(); |
399 |
|
|
} |
400 |
tim |
1.1 |
} |
401 |
|
|
|
402 |
|
|
/** |
403 |
|
|
* Return the number of parties currently waiting at the barrier. |
404 |
|
|
* This method is primarily useful for debugging and assertions. |
405 |
|
|
* |
406 |
|
|
* @return the number of parties currently blocked in {@link #await} |
407 |
|
|
**/ |
408 |
|
|
public int getNumberWaiting() { |
409 |
dl |
1.2 |
lock.lock(); |
410 |
|
|
try { |
411 |
|
|
return parties - count; |
412 |
|
|
} |
413 |
|
|
finally { |
414 |
|
|
lock.unlock(); |
415 |
|
|
} |
416 |
tim |
1.1 |
} |
417 |
|
|
|
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
|