start of new file
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / dsm / Barrier.java
1 public class Barrier {
2   int numthreads;
3   int entercount;
4   boolean cleared;
5
6   public Barrier(int n) {
7     numthreads=n;
8     cleared = false;
9     entercount = 0;
10   }
11
12   public Barrier() {
13   }
14
15   public void reset() {
16     cleared = false;
17     entercount = 0;
18   }
19
20   public static void enterBarrier(Barrier b) {
21     int tmp;
22     boolean retry=true, ret1=false, ret2=true;;
23
24     do {
25       atomic {
26         if (!b.cleared) {
27           b.entercount++;
28           tmp = b.entercount;
29           if (tmp==b.numthreads) {
30             if(b.numthreads > 1)
31               b.cleared=true;
32             b.entercount--;
33             ret1 = true;
34           }
35           retry=false;
36         }
37       }
38     } while(retry);
39     if (ret1) {
40       return;
41     }
42     while(ret2) {
43       atomic {
44         if (b.cleared) {
45           b.entercount--;
46           int count = b.entercount;
47           if (count==0)
48             b.cleared=false;
49           ret2=false;
50         }
51       }
52     }
53   }
54 }