-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGoldilocksEvent.java
More file actions
195 lines (166 loc) · 5.48 KB
/
GoldilocksEvent.java
File metadata and controls
195 lines (166 loc) · 5.48 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
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
package engine.racedetectionengine.goldilocks;
import java.util.HashMap;
import java.util.HashSet;
import engine.racedetectionengine.RaceDetectionEvent;
import event.Thread;
import event.Lock;
import event.Variable;
public class GoldilocksEvent extends RaceDetectionEvent<GoldilocksState> {
@Override
public boolean Handle(GoldilocksState state, int verbosity) {
return this.HandleSub(state, verbosity);
}
@Override
public void printRaceInfoLockType(GoldilocksState state, int verbosity) {
System.out.println("Dummy method called");
}
@Override
public void printRaceInfoAccessType(GoldilocksState state, int verbosity) {
System.out.println("Dummy method called");
}
@Override
public void printRaceInfoExtremeType(GoldilocksState state, int verbosity) {
System.out.println("Dummy method called");
}
@Override
public boolean HandleSubAcquire(GoldilocksState state, int verbosity) {
for(Variable x: state.writeLockSet.keySet()){
if(state.writeLockSet.get(x).contains(this.getLock())){
state.writeLockSet.get(x).add(state.threadLocks.get(this.getThread())) ;
}
}
for(Thread t: state.threadLocks.keySet()){
Lock tLock = state.threadLocks.get(t);
if(state.readLockSet.containsKey(tLock)){
for(Variable x: state.readLockSet.get(tLock).keySet()){
if(state.readLockSet.get(tLock).get(x).contains(this.getLock())){
state.readLockSet.get(tLock).get(x).add(state.threadLocks.get(this.getThread())) ;
}
}
}
}
return false;
}
@Override
public boolean HandleSubRelease(GoldilocksState state, int verbosity) {
for(Variable x: state.writeLockSet.keySet()){
if(state.writeLockSet.get(x).contains(state.threadLocks.get(this.getThread()))){
state.writeLockSet.get(x).add(this.getLock()) ;
}
}
for(Thread t: state.threadLocks.keySet()){
Lock tLock = state.threadLocks.get(t);
if(state.readLockSet.containsKey(tLock)){
for(Variable x: state.readLockSet.get(tLock).keySet()){
if(state.readLockSet.get(tLock).get(x).contains(state.threadLocks.get(this.getThread()))){
state.readLockSet.get(tLock).get(x).add(this.getLock()) ;
}
}
}
}
return false;
}
@Override
public boolean HandleSubRead(GoldilocksState state, int verbosity) {
boolean raceDetected = false;
Thread t = this.getThread();
Variable x = this.getVariable();
Lock tLock = state.threadLocks.get(t);
if(state.writeLockSet.containsKey(x)){
if(!state.writeLockSet.get(x).contains(tLock)){
raceDetected = true;
}
}
if(raceDetected){
System.out.println("Goldilocks algorithm detected a race");
}
if(!state.readLockSet.containsKey(tLock)){
state.readLockSet.put(tLock, new HashMap<Variable, HashSet<Lock>> ());
}
state.readLockSet.get(tLock).put(x, new HashSet<Lock> ());
state.readLockSet.get(tLock).get(x).add(tLock);
return raceDetected;
}
@Override
public boolean HandleSubWrite(GoldilocksState state, int verbosity) {
boolean raceDetected = false;
Thread t = this.getThread();
Variable x = this.getVariable();
Lock tLock = state.threadLocks.get(t);
if(state.writeLockSet.containsKey(x)){
if(!state.writeLockSet.get(x).contains(tLock)){
raceDetected = true;
}
}
for(Thread u: state.threadLocks.keySet()){
Lock uLock = state.threadLocks.get(u);
if(state.readLockSet.containsKey(uLock)){
if(state.readLockSet.get(uLock).containsKey(x)){
if(!state.readLockSet.get(uLock).get(x).contains(tLock)){
raceDetected = true;
}
}
}
}
if(raceDetected){
System.out.println("Goldilocks discipline violated on variable " + this.getVariable().getName());
}
state.writeLockSet.put(x, new HashSet<Lock> ());
state.writeLockSet.get(x).add(tLock);
return raceDetected;
}
@Override
public boolean HandleSubFork(GoldilocksState state, int verbosity) {
Lock uLock = state.threadLocks.get(this.getTarget());
for(Variable x: state.writeLockSet.keySet()){
if(state.writeLockSet.get(x).contains(state.threadLocks.get(this.getThread()))){
state.writeLockSet.get(x).add(uLock) ;
}
}
for(Thread t: state.threadLocks.keySet()){
Lock tLock = state.threadLocks.get(t);
if(state.readLockSet.containsKey(tLock)){
for(Variable x: state.readLockSet.get(tLock).keySet()){
if(state.readLockSet.get(tLock).get(x).contains(state.threadLocks.get(this.getThread()))){
state.readLockSet.get(tLock).get(x).add(uLock) ;
}
}
}
}
return false;
}
@Override
public boolean HandleSubJoin(GoldilocksState state, int verbosity) {
Lock uLock = state.threadLocks.get(this.getTarget());
for(Variable x: state.writeLockSet.keySet()){
if(state.writeLockSet.get(x).contains(uLock)){
state.writeLockSet.get(x).add(state.threadLocks.get(this.getThread())) ;
}
}
for(Thread t: state.threadLocks.keySet()){
Lock tLock = state.threadLocks.get(t);
if(state.readLockSet.containsKey(tLock)){
for(Variable x: state.readLockSet.get(tLock).keySet()){
if(state.readLockSet.get(tLock).get(x).contains(uLock)){
state.readLockSet.get(tLock).get(x).add(state.threadLocks.get(this.getThread())) ;
}
}
}
}
return false;
}
@Override
public void printRaceInfoTransactionType(GoldilocksState state, int verbosity) {
// TODO Auto-generated method stub
}
@Override
public boolean HandleSubBegin(GoldilocksState state, int verbosity) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean HandleSubEnd(GoldilocksState state, int verbosity) {
// TODO Auto-generated method stub
return false;
}
}