ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CyclicBarrier.java
Revision: 1.38
Committed: Sun May 18 23:47:56 2008 UTC (16 years ago) by jsr166
Branch: MAIN
Changes since 1.37: +9 -9 lines
Log Message:
Sync with OpenJDK; untabify

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.22 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.2 */
6    
7 tim 1.1 package java.util.concurrent;
8 dl 1.6 import java.util.concurrent.locks.*;
9 tim 1.1
10     /**
11 tim 1.10 * A synchronization aid that allows a set of threads to all wait for
12 brian 1.4 * each other to reach a common barrier point. CyclicBarriers are
13 dl 1.3 * useful in programs involving a fixed sized party of threads that
14 brian 1.4 * must occasionally wait for each other. The barrier is called
15 dl 1.3 * <em>cyclic</em> because it can be re-used after the waiting threads
16     * are released.
17 tim 1.1 *
18     * <p>A <tt>CyclicBarrier</tt> supports an optional {@link Runnable} command
19     * that is run once per barrier point, after the last thread in the party
20 dl 1.28 * arrives, but before any threads are released.
21 tim 1.1 * This <em>barrier action</em> is useful
22     * for updating shared-state before any of the parties continue.
23 dl 1.28 *
24 brian 1.4 * <p><b>Sample usage:</b> Here is an example of
25 tim 1.1 * using a barrier in a parallel decomposition design:
26     * <pre>
27     * class Solver {
28     * final int N;
29     * final float[][] data;
30     * final CyclicBarrier barrier;
31 dl 1.28 *
32 tim 1.1 * class Worker implements Runnable {
33     * int myRow;
34     * Worker(int row) { myRow = row; }
35     * public void run() {
36     * while (!done()) {
37     * processRow(myRow);
38     *
39     * try {
40 dl 1.28 * barrier.await();
41     * } catch (InterruptedException ex) {
42     * return;
43     * } catch (BrokenBarrierException ex) {
44     * return;
45 tim 1.1 * }
46     * }
47     * }
48     * }
49     *
50     * public Solver(float[][] matrix) {
51     * data = matrix;
52     * N = matrix.length;
53 dl 1.28 * barrier = new CyclicBarrier(N,
54 tim 1.1 * new Runnable() {
55 dl 1.28 * public void run() {
56     * mergeRows(...);
57 tim 1.1 * }
58     * });
59 dl 1.28 * for (int i = 0; i < N; ++i)
60 tim 1.1 * new Thread(new Worker(i)).start();
61     *
62     * waitUntilDone();
63     * }
64     * }
65     * </pre>
66 dl 1.28 * Here, each worker thread processes a row of the matrix then waits at the
67 tim 1.1 * barrier until all rows have been processed. When all rows are processed
68 dl 1.28 * the supplied {@link Runnable} barrier action is executed and merges the
69 tim 1.1 * rows. If the merger
70     * determines that a solution has been found then <tt>done()</tt> will return
71     * <tt>true</tt> and each worker will terminate.
72     *
73     * <p>If the barrier action does not rely on the parties being suspended when
74     * it is executed, then any of the threads in the party could execute that
75     * action when it is released. To facilitate this, each invocation of
76     * {@link #await} returns the arrival index of that thread at the barrier.
77 dl 1.28 * You can then choose which thread should execute the barrier action, for
78 tim 1.1 * example:
79     * <pre> if (barrier.await() == 0) {
80     * // log the completion of this iteration
81     * }</pre>
82     *
83 dl 1.28 * <p>The <tt>CyclicBarrier</tt> uses an all-or-none breakage model
84     * for failed synchronization attempts: If a thread leaves a barrier
85     * point prematurely because of interruption, failure, or timeout, all
86     * other threads waiting at that barrier point will also leave
87     * abnormally via {@link BrokenBarrierException} (or
88 dl 1.31 * {@link InterruptedException} if they too were interrupted at about
89 dl 1.28 * the same time).
90 tim 1.1 *
91 jsr166 1.35 * <p>Memory consistency effects: Actions in a thread prior to calling
92     * {@code await()}
93     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
94 brian 1.33 * actions that are part of the barrier action, which in turn
95 jsr166 1.35 * <i>happen-before</i> actions following a successful return from the
96     * corresponding {@code await()} in other threads.
97 brian 1.33 *
98 tim 1.1 * @since 1.5
99 brian 1.4 * @see CountDownLatch
100 tim 1.1 *
101 dl 1.5 * @author Doug Lea
102 tim 1.1 */
103     public class CyclicBarrier {
104 dl 1.28 /**
105     * Each use of the barrier is represented as a generation instance.
106     * The generation changes whenever the barrier is tripped, or
107     * is reset. There can be many generations associated with threads
108     * using the barrier - due to the non-deterministic way the lock
109     * may be allocated to waiting threads - but only one of these
110     * can be active at a time (the one to which <tt>count</tt> applies)
111     * and all the rest are either broken or tripped.
112     * There need not be an active generation if there has been a break
113     * but no subsequent reset.
114     */
115     private static class Generation {
116     boolean broken = false;
117     }
118    
119 dl 1.5 /** The lock for guarding barrier entry */
120 dl 1.2 private final ReentrantLock lock = new ReentrantLock();
121 dl 1.5 /** Condition to wait on until tripped */
122 dl 1.21 private final Condition trip = lock.newCondition();
123 dl 1.5 /** The number of parties */
124 dl 1.2 private final int parties;
125 dl 1.5 /* The command to run when tripped */
126 dl 1.12 private final Runnable barrierCommand;
127 dl 1.28 /** The current generation */
128     private Generation generation = new Generation();
129 dl 1.2
130     /**
131     * Number of parties still waiting. Counts down from parties to 0
132 dl 1.31 * on each generation. It is reset to parties on each new
133     * generation or when broken.
134 dl 1.2 */
135 dl 1.28 private int count;
136 dl 1.2
137     /**
138 jsr166 1.29 * Updates state on barrier trip and wakes up everyone.
139 dl 1.28 * Called only while holding lock.
140     */
141 dl 1.2 private void nextGeneration() {
142 dl 1.28 // signal completion of last generation
143     trip.signalAll();
144     // set up next generation
145 dl 1.2 count = parties;
146 dl 1.28 generation = new Generation();
147 dl 1.12 }
148    
149     /**
150 jsr166 1.29 * Sets current barrier generation as broken and wakes up everyone.
151 dl 1.28 * Called only while holding lock.
152 dl 1.12 */
153     private void breakBarrier() {
154 dl 1.28 generation.broken = true;
155 jsr166 1.38 count = parties;
156 dl 1.12 trip.signalAll();
157 dl 1.2 }
158    
159 dl 1.5 /**
160 dl 1.7 * Main barrier code, covering the various policies.
161 dl 1.5 */
162 dl 1.28 private int dowait(boolean timed, long nanos)
163     throws InterruptedException, BrokenBarrierException,
164     TimeoutException {
165 dl 1.20 final ReentrantLock lock = this.lock;
166 dl 1.2 lock.lock();
167     try {
168 dl 1.30 final Generation g = generation;
169 dl 1.2
170 dl 1.28 if (g.broken)
171 dl 1.2 throw new BrokenBarrierException();
172    
173     if (Thread.interrupted()) {
174 dl 1.12 breakBarrier();
175 dl 1.2 throw new InterruptedException();
176     }
177    
178 dl 1.30 int index = --count;
179     if (index == 0) { // tripped
180     boolean ranAction = false;
181     try {
182 jsr166 1.38 final Runnable command = barrierCommand;
183 dl 1.31 if (command != null)
184     command.run();
185 dl 1.30 ranAction = true;
186     nextGeneration();
187     return 0;
188     } finally {
189     if (!ranAction)
190     breakBarrier();
191     }
192     }
193 dl 1.2
194 dl 1.28 // loop until tripped, broken, interrupted, or timed out
195 dl 1.12 for (;;) {
196 dl 1.2 try {
197 dl 1.28 if (!timed)
198 dl 1.2 trip.await();
199 dl 1.23 else if (nanos > 0L)
200 dl 1.2 nanos = trip.awaitNanos(nanos);
201 dl 1.12 } catch (InterruptedException ie) {
202 jsr166 1.36 if (g == generation && ! g.broken) {
203     breakBarrier();
204 jsr166 1.38 throw ie;
205     } else {
206     // We're about to finish waiting even if we had not
207     // been interrupted, so this interrupt is deemed to
208     // "belong" to subsequent execution.
209     Thread.currentThread().interrupt();
210     }
211 dl 1.2 }
212 dl 1.28
213 dl 1.30 if (g.broken)
214 dl 1.12 throw new BrokenBarrierException();
215    
216 jsr166 1.36 if (g != generation)
217 dl 1.12 return index;
218    
219 dl 1.23 if (timed && nanos <= 0L) {
220 dl 1.12 breakBarrier();
221 dl 1.2 throw new TimeoutException();
222     }
223     }
224 tim 1.9 } finally {
225 dl 1.2 lock.unlock();
226     }
227     }
228 tim 1.1
229     /**
230 dl 1.25 * Creates a new <tt>CyclicBarrier</tt> that will trip when the
231 tim 1.1 * given number of parties (threads) are waiting upon it, and which
232 dl 1.17 * will execute the given barrier action when the barrier is tripped,
233 dl 1.19 * performed by the last thread entering the barrier.
234 tim 1.1 *
235     * @param parties the number of threads that must invoke {@link #await}
236 jsr166 1.37 * before the barrier is tripped
237 tim 1.1 * @param barrierAction the command to execute when the barrier is
238 jsr166 1.37 * tripped, or {@code null} if there is no action
239     * @throws IllegalArgumentException if {@code parties} is less than 1
240 tim 1.1 */
241     public CyclicBarrier(int parties, Runnable barrierAction) {
242 dl 1.2 if (parties <= 0) throw new IllegalArgumentException();
243 dl 1.28 this.parties = parties;
244 dl 1.2 this.count = parties;
245     this.barrierCommand = barrierAction;
246 tim 1.1 }
247    
248     /**
249 dl 1.25 * Creates a new <tt>CyclicBarrier</tt> that will trip when the
250 dl 1.14 * given number of parties (threads) are waiting upon it, and
251 dl 1.31 * does not perform a predefined action when the barrier is tripped.
252 tim 1.1 *
253     * @param parties the number of threads that must invoke {@link #await}
254 jsr166 1.37 * before the barrier is tripped
255     * @throws IllegalArgumentException if {@code parties} is less than 1
256 tim 1.1 */
257     public CyclicBarrier(int parties) {
258 dl 1.2 this(parties, null);
259 tim 1.1 }
260    
261     /**
262 dl 1.25 * Returns the number of parties required to trip this barrier.
263 jsr166 1.37 *
264     * @return the number of parties required to trip this barrier
265 dl 1.31 */
266 tim 1.1 public int getParties() {
267 dl 1.2 return parties;
268 tim 1.1 }
269    
270     /**
271 jsr166 1.37 * Waits until all {@linkplain #getParties parties} have invoked
272     * <tt>await</tt> on this barrier.
273 tim 1.1 *
274     * <p>If the current thread is not the last to arrive then it is
275     * disabled for thread scheduling purposes and lies dormant until
276 jsr166 1.36 * one of the following things happens:
277 tim 1.1 * <ul>
278     * <li>The last thread arrives; or
279 jsr166 1.37 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
280     * the current thread; or
281     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
282     * one of the other waiting threads; or
283 dl 1.24 * <li>Some other thread times out while waiting for barrier; or
284 tim 1.1 * <li>Some other thread invokes {@link #reset} on this barrier.
285     * </ul>
286 jsr166 1.37 *
287 tim 1.1 * <p>If the current thread:
288     * <ul>
289     * <li>has its interrupted status set on entry to this method; or
290 jsr166 1.37 * <li>is {@linkplain Thread#interrupt interrupted} while waiting
291 tim 1.1 * </ul>
292     * then {@link InterruptedException} is thrown and the current thread's
293     * interrupted status is cleared.
294     *
295 jsr166 1.37 * <p>If the barrier is {@link #reset} while any thread is waiting,
296     * or if the barrier {@linkplain #isBroken is broken} when
297     * <tt>await</tt> is invoked, or while any thread is waiting, then
298     * {@link BrokenBarrierException} is thrown.
299 tim 1.1 *
300 jsr166 1.37 * <p>If any thread is {@linkplain Thread#interrupt interrupted} while waiting,
301 dl 1.28 * then all other waiting threads will throw
302 tim 1.1 * {@link BrokenBarrierException} and the barrier is placed in the broken
303     * state.
304     *
305     * <p>If the current thread is the last thread to arrive, and a
306     * non-null barrier action was supplied in the constructor, then the
307 dl 1.28 * current thread runs the action before allowing the other threads to
308 tim 1.1 * continue.
309     * If an exception occurs during the barrier action then that exception
310 dholmes 1.13 * will be propagated in the current thread and the barrier is placed in
311     * the broken state.
312 tim 1.1 *
313     * @return the arrival index of the current thread, where index
314 jsr166 1.37 * <tt>{@link #getParties()} - 1</tt> indicates the first
315     * to arrive and zero indicates the last to arrive
316 dl 1.28 * @throws InterruptedException if the current thread was interrupted
317 jsr166 1.37 * while waiting
318 tim 1.1 * @throws BrokenBarrierException if <em>another</em> thread was
319 jsr166 1.37 * interrupted or timed out while the current thread was
320     * waiting, or the barrier was reset, or the barrier was
321     * broken when {@code await} was called, or the barrier
322     * action (if present) failed due an exception.
323 tim 1.1 */
324     public int await() throws InterruptedException, BrokenBarrierException {
325 dl 1.2 try {
326 dl 1.23 return dowait(false, 0L);
327 tim 1.9 } catch (TimeoutException toe) {
328 dl 1.2 throw new Error(toe); // cannot happen;
329     }
330     }
331    
332     /**
333 jsr166 1.37 * Waits until all {@linkplain #getParties parties} have invoked
334     * <tt>await</tt> on this barrier, or the specified waiting time elapses.
335 dl 1.2 *
336     * <p>If the current thread is not the last to arrive then it is
337     * disabled for thread scheduling purposes and lies dormant until
338     * one of the following things happens:
339     * <ul>
340     * <li>The last thread arrives; or
341 dholmes 1.13 * <li>The specified timeout elapses; or
342 jsr166 1.37 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
343     * the current thread; or
344     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
345     * one of the other waiting threads; or
346 dl 1.24 * <li>Some other thread times out while waiting for barrier; or
347 dl 1.2 * <li>Some other thread invokes {@link #reset} on this barrier.
348     * </ul>
349 jsr166 1.37 *
350 dl 1.2 * <p>If the current thread:
351     * <ul>
352     * <li>has its interrupted status set on entry to this method; or
353 jsr166 1.37 * <li>is {@linkplain Thread#interrupt interrupted} while waiting
354 dl 1.2 * </ul>
355     * then {@link InterruptedException} is thrown and the current thread's
356     * interrupted status is cleared.
357     *
358 dl 1.26 * <p>If the specified waiting time elapses then {@link TimeoutException}
359     * is thrown. If the time is less than or equal to zero, the
360     * method will not wait at all.
361     *
362 jsr166 1.37 * <p>If the barrier is {@link #reset} while any thread is waiting,
363     * or if the barrier {@linkplain #isBroken is broken} when
364     * <tt>await</tt> is invoked, or while any thread is waiting, then
365     * {@link BrokenBarrierException} is thrown.
366     *
367     * <p>If any thread is {@linkplain Thread#interrupt interrupted} while
368     * waiting, then all other waiting threads will throw {@link
369     * BrokenBarrierException} and the barrier is placed in the broken
370 dl 1.2 * state.
371     *
372     * <p>If the current thread is the last thread to arrive, and a
373     * non-null barrier action was supplied in the constructor, then the
374 dl 1.28 * current thread runs the action before allowing the other threads to
375 dl 1.2 * continue.
376     * If an exception occurs during the barrier action then that exception
377 dholmes 1.13 * will be propagated in the current thread and the barrier is placed in
378     * the broken state.
379 dl 1.2 *
380 dl 1.5 * @param timeout the time to wait for the barrier
381     * @param unit the time unit of the timeout parameter
382 dl 1.2 * @return the arrival index of the current thread, where index
383 jsr166 1.37 * <tt>{@link #getParties()} - 1</tt> indicates the first
384     * to arrive and zero indicates the last to arrive
385 dl 1.28 * @throws InterruptedException if the current thread was interrupted
386 jsr166 1.37 * while waiting
387     * @throws TimeoutException if the specified timeout elapses
388 dl 1.2 * @throws BrokenBarrierException if <em>another</em> thread was
389 jsr166 1.37 * interrupted or timed out while the current thread was
390     * waiting, or the barrier was reset, or the barrier was broken
391     * when {@code await} was called, or the barrier action (if
392     * present) failed due an exception
393 dl 1.28 */
394     public int await(long timeout, TimeUnit unit)
395     throws InterruptedException,
396     BrokenBarrierException,
397     TimeoutException {
398 dl 1.2 return dowait(true, unit.toNanos(timeout));
399 tim 1.1 }
400    
401     /**
402 dl 1.25 * Queries if this barrier is in a broken state.
403 jsr166 1.37 *
404     * @return {@code true} if one or more parties broke out of this
405     * barrier due to interruption or timeout since
406     * construction or the last reset, or a barrier action
407     * failed due to an exception; {@code false} otherwise.
408 tim 1.1 */
409     public boolean isBroken() {
410 dl 1.20 final ReentrantLock lock = this.lock;
411 dl 1.2 lock.lock();
412     try {
413 dl 1.28 return generation.broken;
414 tim 1.9 } finally {
415 dl 1.2 lock.unlock();
416     }
417 tim 1.1 }
418    
419     /**
420 dl 1.25 * Resets the barrier to its initial state. If any parties are
421 tim 1.1 * currently waiting at the barrier, they will return with a
422 dl 1.8 * {@link BrokenBarrierException}. Note that resets <em>after</em>
423 dl 1.12 * a breakage has occurred for other reasons can be complicated to
424     * carry out; threads need to re-synchronize in some other way,
425     * and choose one to perform the reset. It may be preferable to
426     * instead create a new barrier for subsequent use.
427 tim 1.1 */
428     public void reset() {
429 dl 1.20 final ReentrantLock lock = this.lock;
430 dl 1.2 lock.lock();
431     try {
432 dl 1.28 breakBarrier(); // break the current generation
433     nextGeneration(); // start a new generation
434 tim 1.9 } finally {
435 dl 1.2 lock.unlock();
436     }
437 tim 1.1 }
438    
439     /**
440 dl 1.25 * Returns the number of parties currently waiting at the barrier.
441 tim 1.1 * This method is primarily useful for debugging and assertions.
442     *
443 jsr166 1.37 * @return the number of parties currently blocked in {@link #await}
444 jsr166 1.32 */
445 tim 1.1 public int getNumberWaiting() {
446 dl 1.20 final ReentrantLock lock = this.lock;
447 dl 1.2 lock.lock();
448     try {
449 dl 1.31 return parties - count;
450 tim 1.9 } finally {
451 dl 1.2 lock.unlock();
452     }
453 tim 1.1 }
454     }