add debugs for now
[IRC.git] / Robust / src / Benchmarks / Recovery / Game / recovery / Barrier.java
1 public class Barrier extends Thread {
2   threadinfo[] tinfo;
3   int numthreads;
4
5   public Barrier(int n, threadinfo[] tinfo) {
6     numthreads=n;
7     this.tinfo=tinfo;
8   }
9
10   /**
11    ** Update the age of all trees in a given map
12    ** @param land The map to be searched
13    ** @param maxage The maxage of a tree
14    ** @param rows The number of rows in the map
15    ** @param cols The number of columns in the map
16    **/
17   public void updateAge(GameMap[][] land, int maxage, int rows, int cols) {
18     int countTrees = 0;
19     for(int i = 0; i<rows; i++) {
20       for(int j = 0; j<cols; j++) {
21         if(land[i][j].tree != null) {
22           if(land[i][j].tree.getage() > maxage) {
23             land[i][j].tree = null;
24           } else {
25             land[i][j].tree.incrementage();
26           }
27           countTrees++;
28         }
29       }
30     }
31     /* Debugging-> System.println("Tree count=  "+countTrees); */
32   }
33
34   public static void enterBarrier(int threadid, threadinfo[] tinfo, int numthreads) {
35     int x;
36     atomic {
37       tinfo[threadid].counter++;
38       x = tinfo[threadid].counter;
39     }
40
41     for(int i=0; i<numthreads; i++) {
42       if(threadid == i) {
43         continue;
44       }
45       boolean check = false;
46       atomic {
47         if(tinfo[i].counter >= tinfo[threadid].counter)  {
48           check = true;
49         }
50       }
51       if(!check) {
52         int status = Thread.getStatus(i);
53         if(status==-1) {//Thread is dead
54           //System.out.println("DEBUG -> Dead\n");
55           continue;
56         }
57         int y;
58         atomic {
59           y=tinfo[i].counter;
60         }
61
62         //System.out.println("i= " + i + " i's count= " + y + " threadid= " + threadid + " mycount= " + x);
63
64         while(y!=x) {
65           //Wait for 100 microseconds
66           sleep(100);
67           atomic {
68             y = tinfo[i].counter;
69           }
70         }
71       }
72     }
73     return;
74   }
75 }
76
77 public class threadinfo {
78   int counter;
79   int id;
80   public threadinfo() {
81     counter = 0;
82   }
83 }
84