-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHBState.java
More file actions
149 lines (124 loc) · 4.37 KB
/
HBState.java
File metadata and controls
149 lines (124 loc) · 4.37 KB
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
package engine.racedetectionengine.hb;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import engine.racedetectionengine.State;
import event.Lock;
import event.Thread;
import event.Variable;
import util.vectorclock.VectorClock;
public class HBState extends State{
// Internal data
private HashMap<Thread, Integer> threadToIndex;
private HashMap<Lock, Integer> lockToIndex;
private HashMap<Variable, Integer> variableToIndex;
private int numThreads;
private int numLocks;
private int numVariables;
// Data used for algorithm
public ArrayList<VectorClock> HBThread;
public ArrayList<VectorClock> lastReleaseLock;
public ArrayList<VectorClock> readVariable;
public ArrayList<VectorClock> writeVariable;
public HBState(HashSet<Thread> tSet) {
initInternalData(tSet);
initData(tSet);
}
private void initInternalData(HashSet<Thread> tSet) {
this.threadToIndex = new HashMap<Thread, Integer>();
this.numThreads = 0;
Iterator<Thread> tIter = tSet.iterator();
while (tIter.hasNext()) {
Thread thread = tIter.next();
this.threadToIndex.put(thread, (Integer)this.numThreads);
this.numThreads ++;
}
this.lockToIndex = new HashMap<Lock, Integer>();
this.numLocks = 0;
this.variableToIndex = new HashMap<Variable, Integer>();
this.numVariables = 0;
}
public void initData(HashSet<Thread> tSet) {
// initialize HBThread
this.HBThread = new ArrayList<VectorClock>();
initialize1DArrayOfVectorClocksWithBottom(this.HBThread, this.numThreads, this.numThreads);
for(int i=0; i < this.numThreads; i++){
this.HBThread.get(i).setClockIndex(i, 1);
}
// initialize lastReleaseLock
this.lastReleaseLock = new ArrayList<VectorClock>();
// initialize readVariable
this.readVariable = new ArrayList<VectorClock>();
// initialize writeVariable
this.writeVariable = new ArrayList<VectorClock>();
}
//Access Methods
void initialize1DArrayOfVectorClocksWithBottom(ArrayList<VectorClock> arr, int len, int dim) {
for (int i = 0; i < len; i++) {
arr.add(new VectorClock(dim));
}
}
private int checkAndAddLock(Lock l){
if(!lockToIndex.containsKey(l)){
lockToIndex.put(l, this.numLocks);
this.numLocks ++;
lastReleaseLock.add(new VectorClock(this.numThreads));
}
return lockToIndex.get(l);
}
private int checkAndAddVariable(Variable v){
if(!variableToIndex.containsKey(v)){
variableToIndex.put(v, this.numVariables);
this.numVariables ++;
readVariable .add(new VectorClock(this.numThreads));
writeVariable .add(new VectorClock(this.numThreads));
}
return variableToIndex.get(v);
}
public int getClockThread(Thread t) {
int tIndex = threadToIndex.get(t);
return this.HBThread.get(tIndex).getClockIndex(tIndex);
}
public VectorClock getVectorClockFrom1DArray(ArrayList<VectorClock> arr, int index) {
if (index < 0 || index >= arr.size()) {
throw new IllegalArgumentException("Illegal Out of Bound access");
}
return arr.get(index);
}
public void incClockThread(Thread t) {
int tIndex = threadToIndex.get(t);
int origVal = this.HBThread.get(tIndex).getClockIndex(tIndex);
this.HBThread.get(tIndex).setClockIndex(tIndex, (Integer)(origVal + 1));
}
public VectorClock getVectorClock(ArrayList<VectorClock> arr, Thread t) {
int tIndex = threadToIndex.get(t);
return getVectorClockFrom1DArray(arr, tIndex);
}
public VectorClock getVectorClock(ArrayList<VectorClock> arr, Lock l) {
int lIndex = checkAndAddLock(l);
return getVectorClockFrom1DArray(arr, lIndex);
}
public VectorClock getVectorClock(ArrayList<VectorClock> arr, Variable v) {
int vIndex = checkAndAddVariable(v);
return getVectorClockFrom1DArray(arr, vIndex);
}
public void printThreadClock(){
ArrayList<VectorClock> printVC = new ArrayList<VectorClock>();
for(Thread thread : threadToIndex.keySet()){
VectorClock C_t = getVectorClock(this.HBThread, thread);
printVC.add(C_t);
}
System.out.println(printVC);
System.out.println();
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
}
public boolean isThreadRelevant(Thread t){
return this.threadToIndex.containsKey(t);
}
public void printMemory(){
System.err.println("Number of threads = " + Integer.toString(this.numThreads));
System.err.println("Number of locks = " + Integer.toString(this.numLocks));
System.err.println("Number of variables = " + Integer.toString(this.numVariables));
}
}