File: CyclicBarrier.java

package info (click to toggle)
concurrent-dfsg 1.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 976 kB
  • ctags: 2,018
  • sloc: java: 10,704; xml: 49; makefile: 12
file content (284 lines) | stat: -rw-r--r-- 9,578 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
  File: CyclicBarrier.java

  Originally written by Doug Lea and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
  Thanks for the assistance and support of Sun Microsystems Labs,
  and everyone contributing, testing, and using this code.

  History:
  Date       Who                What
  11Jul1998  dl               Create public version
  28Aug1998  dl               minor code simplification
*/

package EDU.oswego.cs.dl.util.concurrent;

/**
 * A cyclic barrier is a reasonable choice for a barrier in contexts
 * involving a fixed sized group of threads that
 * must occasionally wait for each other. 
 * (A Rendezvous better handles applications in which
 * any number of threads meet, n-at-a-time.)
 * <p>
 * CyclicBarriers use an all-or-none breakage model
 * for failed synchronization attempts: If threads
 * leave a barrier point prematurely because of timeout
 * or interruption, others will also leave abnormally
 * (via BrokenBarrierException), until
 * the barrier is <code>restart</code>ed. This is usually
 * the simplest and best strategy for sharing knowledge
 * about failures among cooperating threads in the most
 * common usages contexts of Barriers.
 * This implementation  has the property that interruptions
 * among newly arriving threads can cause as-yet-unresumed
 * threads from a previous barrier cycle to return out
 * as broken. This transmits breakage
 * as early as possible, but with the possible byproduct that
 * only some threads returning out of a barrier will realize
 * that it is newly broken. (Others will not realize this until a
 * future cycle.) (The Rendezvous class has a more uniform, but
 * sometimes less desirable policy.)
 * <p>
 * Barriers support an optional Runnable command
 * that is run once per barrier point.
 * <p>
 * <b>Sample usage</b> Here is a code sketch of 
 *  a  barrier in a parallel decomposition design.
 * <pre>
 * class Solver {
 *   final int N;
 *   final float[][] data;
 *   final CyclicBarrier barrier;
 *   
 *   class Worker implements Runnable {
 *      int myRow;
 *      Worker(int row) { myRow = row; }
 *      public void run() {
 *         while (!done()) {
 *            processRow(myRow);
 *
 *            try {
 *              barrier.barrier(); 
 *            }
 *            catch (InterruptedException ex) { return; }
 *            catch (BrokenBarrierException ex) { return; }
 *         }
 *      }
 *   }
 *
 *   public Solver(float[][] matrix) {
 *     data = matrix;
 *     N = matrix.length;
 *     barrier = new CyclicBarrier(N);
 *     barrier.setBarrierCommand(new Runnable() {
 *       public void run() { mergeRows(...); }
 *     });
 *     for (int i = 0; i < N; ++i) {
 *       new Thread(new Worker(i)).start();
 *     waitUntilDone();
 *    }
 * }
 * </pre>
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]

 **/
public class CyclicBarrier implements Barrier {

  protected final int parties_;
  protected boolean broken_ = false;
  protected Runnable barrierCommand_ = null;
  protected int count_; // number of parties still waiting
  protected int resets_ = 0; // incremented on each release

  /** 
   * Create a CyclicBarrier for the indicated number of parties,
   * and no command to run at each barrier.
   * @exception IllegalArgumentException if parties less than or equal to zero.
   **/

  public CyclicBarrier(int parties) { this(parties, null); }

  /** 
   * Create a CyclicBarrier for the indicated number of parties.
   * and the given command to run at each barrier point.
   * @exception IllegalArgumentException if parties less than or equal to zero.
   **/

  public CyclicBarrier(int parties, Runnable command) { 
    if (parties <= 0) throw new IllegalArgumentException();
    parties_ = parties; 
    count_ = parties;
    barrierCommand_ = command;
  }

  /**
   * Set the command to run at the point at which all threads reach the
   * barrier. This command is run exactly once, by the thread
   * that trips the barrier. The command is not run if the barrier is
   * broken.
   * @param command the command to run. If null, no command is run.
   * @return the previous command
   **/

  public synchronized Runnable setBarrierCommand(Runnable command) {
    Runnable old = barrierCommand_;
    barrierCommand_ = command;
    return old;
  }

  public synchronized boolean broken() { return broken_; }

  /**
   * Reset to initial state. Clears both the broken status
   * and any record of waiting threads, and releases all
   * currently waiting threads with indeterminate return status.
   * This method is intended only for use in recovery actions
   * in which it is somehow known
   * that no thread could possibly be relying on the
   * the synchronization properties of this barrier.
   **/

  public synchronized void restart() { 
    broken_ = false; 
    ++resets_;
    count_ = parties_;
    notifyAll();
  }
  
 
  public int parties() { return parties_; }

  /**
   * Enter barrier and wait for the other parties()-1 threads.
   * @return the arrival index: the number of other parties 
   * that were still waiting
   * upon entry. This is a unique value from zero to parties()-1.
   * If it is zero, then the current
   * thread was the last party to hit barrier point
   * and so was responsible for releasing the others. 
   * @exception BrokenBarrierException if any other thread
   * in any previous or current barrier 
   * since either creation or the last <code>restart</code>
   * operation left the barrier
   * prematurely due to interruption or time-out. (If so,
   * the <code>broken</code> status is also set.)
   * Threads that are notified to have been
   * interrupted <em>after</em> being released are not considered
   * to have broken the barrier.
   * In all cases, the interruption
   * status of the current thread is preserved, so can be tested
   * by checking <code>Thread.interrupted</code>. 
   * @exception InterruptedException if this thread was interrupted
   * during the barrier, and was the one causing breakage. 
   * If so, <code>broken</code> status is also set.
   **/

  public int barrier() throws InterruptedException, BrokenBarrierException {
    return doBarrier(false, 0);
  }

  /**
   * Enter barrier and wait at most msecs for the other parties()-1 threads.
   * @return if not timed out, the arrival index: the number of other parties 
   * that were still waiting
   * upon entry. This is a unique value from zero to parties()-1.
   * If it is zero, then the current
   * thread was the last party to hit barrier point
   * and so was responsible for releasing the others. 
   * @exception BrokenBarrierException 
   * if any other thread
   * in any previous or current barrier 
   * since either creation or the last <code>restart</code>
   * operation left the barrier
   * prematurely due to interruption or time-out. (If so,
   * the <code>broken</code> status is also set.) 
   * Threads that are noticed to have been
   * interrupted <em>after</em> being released are not considered
   * to have broken the barrier.
   * In all cases, the interruption
   * status of the current thread is preserved, so can be tested
   * by checking <code>Thread.interrupted</code>. 
   * @exception InterruptedException if this thread was interrupted
   * during the barrier. If so, <code>broken</code> status is also set.
   * @exception TimeoutException if this thread timed out waiting for
   *  the barrier. If the timeout occured while already in the
   * barrier, <code>broken</code> status is also set.
   **/

  public int attemptBarrier(long msecs) 
    throws InterruptedException, TimeoutException, BrokenBarrierException {
    return doBarrier(true, msecs);
  }

  protected synchronized int doBarrier(boolean timed, long msecs) 
    throws InterruptedException, TimeoutException, BrokenBarrierException  { 
    
    int index = --count_;

    if (broken_) {
      throw new BrokenBarrierException(index);
    }
    else if (Thread.interrupted()) {
      broken_ = true;
      notifyAll();
      throw new InterruptedException();
    }
    else if (index == 0) {  // tripped
      count_ = parties_;
      ++resets_;
      notifyAll();
      try {
        if (barrierCommand_ != null)
          barrierCommand_.run();
        return 0;
      }
      catch (RuntimeException ex) {
        broken_ = true;
        return 0;
      }
    }
    else if (timed && msecs <= 0) {
      broken_ = true;
      notifyAll();
      throw new TimeoutException(msecs);
    }
    else {                   // wait until next reset
      int r = resets_;      
      long startTime = (timed)? System.currentTimeMillis() : 0;
      long waitTime = msecs;
      for (;;) {
        try {
          wait(waitTime);
        }
        catch (InterruptedException ex) {
          // Only claim that broken if interrupted before reset
          if (resets_ == r) { 
            broken_ = true;
            notifyAll();
            throw ex;
          }
          else {
            Thread.currentThread().interrupt(); // propagate
          }
        }

        if (broken_) 
          throw new BrokenBarrierException(index);

        else if (r != resets_)
          return index;

        else if (timed) {
          waitTime = msecs - (System.currentTimeMillis() - startTime);
          if  (waitTime <= 0) {
            broken_ = true;
            notifyAll();
            throw new TimeoutException(msecs);
          }
        }
      }
    }
  }

}