-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAerodromeState.java
More file actions
287 lines (235 loc) · 9.52 KB
/
AerodromeState.java
File metadata and controls
287 lines (235 loc) · 9.52 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
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
285
286
287
package engine.atomicity.conflictserializability.aerodrome;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import engine.atomicity.State;
import event.Lock;
import event.Thread;
import event.Variable;
import util.vectorclock.VectorClockOpt;
public class AerodromeState extends State {
// Internal data
public HashMap<Thread, Integer> threadToIndex;
private HashMap<Lock, Integer> lockToIndex;
private HashMap<Variable, Integer> variableToIndex;
private int numThreads;
private int numLocks;
private int numVariables;
public ArrayList<VectorClockOpt> clockThread; // mathcal{C}
public ArrayList<VectorClockOpt> clockThreadBegin; // mathcal{C^BEGIN}
public HashMap<Lock, VectorClockOpt> clockLock; // mathcal{L}
public HashMap<Variable, VectorClockOpt> clockWriteVariable; // mathcal{W}
public HashMap<Variable, VectorClockOpt> clockReadVariable; // mathcal{R}
public HashMap<Variable, VectorClockOpt> clockReadVariableCheck; // mathcal{chR}
public HashMap<Lock, Thread> lastThreadToRelease; // lastRelThr
public HashMap<Variable, Thread> lastThreadToWrite; // lastWThr
private HashMap<Thread, Integer> threadToNestingDepth;
//Optimization data structures
public HashMap<Thread, HashSet<Variable>> updateSetThread_write; // UpdateSet^w
public HashMap<Thread, HashSet<Variable>> updateSetThread_read; // UpdateSet^r
// Other data-structures to track if parent transaction is alive
public HashMap<Thread, HashSet<Thread>> threadsForkedInActiveTransaction;
public HashSet<Thread> parentTransactionIsAlive;
//parameter flags
public int verbosity;
public AerodromeState(HashSet<Thread> tSet, int verbosity) {
this.verbosity = verbosity;
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;
}
private void initialize1DArrayOfVectorClocksWithBottom(ArrayList<VectorClockOpt> arr, int len) {
for (int i = 0; i < len; i++) {
arr.add(new VectorClockOpt(this.numThreads));
}
}
public void initData(HashSet<Thread> tSet) {
this.clockThread = new ArrayList<VectorClockOpt>();
initialize1DArrayOfVectorClocksWithBottom(this.clockThread, this.numThreads);
for(int tIndex = 0; tIndex < this.numThreads; tIndex ++){
this.clockThread.get(tIndex).setClockIndex(tIndex, 1);
}
this.clockThreadBegin = new ArrayList<VectorClockOpt>();
initialize1DArrayOfVectorClocksWithBottom(this.clockThreadBegin, this.numThreads);
this.clockLock = new HashMap<Lock, VectorClockOpt> ();
this.clockWriteVariable = new HashMap<Variable, VectorClockOpt> ();
this.clockReadVariable = new HashMap<Variable, VectorClockOpt> ();
this.clockReadVariableCheck = new HashMap<Variable, VectorClockOpt> ();
this.lastThreadToRelease = new HashMap<Lock, Thread> ();
this.lastThreadToWrite = new HashMap<Variable, Thread> ();
this.threadToNestingDepth = new HashMap<Thread, Integer> ();
for(Thread t: tSet){
this.threadToNestingDepth.put(t, 0);
}
this.updateSetThread_read = new HashMap<Thread, HashSet<Variable>> ();
this.updateSetThread_write = new HashMap<Thread, HashSet<Variable>> ();
for(Thread t: tSet){
this.updateSetThread_read.put(t, new HashSet<Variable> ());
this.updateSetThread_write.put(t, new HashSet<Variable> ());
}
this.parentTransactionIsAlive = new HashSet<Thread> ();
this.threadsForkedInActiveTransaction = new HashMap<Thread, HashSet<Thread>> ();
for(Thread t: tSet) {
this.threadsForkedInActiveTransaction.put(t, new HashSet<Thread> ());
}
}
// Access methods
private VectorClockOpt getVectorClockFrom1DArray(ArrayList<VectorClockOpt> arr, int index) {
return arr.get(index);
}
public void incClockThread(Thread t) {
int tIndex = threadToIndex.get(t);
int origVal = this.clockThread.get(tIndex).getClockIndex(tIndex);
this.clockThread.get(tIndex).setClockIndex(tIndex, (Integer)(origVal + 1));
}
public VectorClockOpt getVectorClock(ArrayList<VectorClockOpt> arr, Thread t) {
int tIndex = threadToIndex.get(t);
return getVectorClockFrom1DArray(arr, tIndex);
}
public VectorClockOpt getVectorClock(HashMap<Lock, VectorClockOpt> arr, Lock l) {
return arr.get(l);
}
public VectorClockOpt getVectorClock(HashMap<Variable, VectorClockOpt> arr, Variable v) {
return arr.get(v);
}
public int checkAndAddLock(Lock l){
if(!lockToIndex.containsKey(l)){
lockToIndex.put(l, this.numLocks);
this.numLocks ++;
this.clockLock.put(l, new VectorClockOpt(this.numThreads));
}
return lockToIndex.get(l);
}
public int checkAndAddVariable(Variable v){
if(!variableToIndex.containsKey(v)){
variableToIndex.put(v, this.numVariables);
this.numVariables ++;
this.clockReadVariable.put(v, new VectorClockOpt(this.numThreads));
this.clockReadVariableCheck.put(v, new VectorClockOpt(this.numThreads));
this.clockWriteVariable.put(v, new VectorClockOpt(this.numThreads));
}
return variableToIndex.get(v);
}
public boolean transactionIsActive(Thread t) {
return threadToNestingDepth.get(t) > 0;
}
public void incrementNestingDepth(Thread t) {
int cur_depth = threadToNestingDepth.get(t);
threadToNestingDepth.put(t, cur_depth + 1);
}
public void decrementNestingDepth(Thread t) {
int cur_depth = threadToNestingDepth.get(t);
threadToNestingDepth.put(t, cur_depth - 1);
}
public boolean checkAndGetClock(VectorClockOpt checkClock, VectorClockOpt fromClock, Thread target) {
int tIndex = this.threadToIndex.get(target);
VectorClockOpt C_target_begin = getVectorClock(clockThreadBegin, target);
if(C_target_begin.isLessThanOrEqual(checkClock, tIndex) && transactionIsActive(target)) {
return true;
}
VectorClockOpt C_target = getVectorClock(clockThread, target);
C_target.updateWithMax(C_target, fromClock);
return false;
}
public boolean handshakeAtEndEvent_Optimized(Thread t) {
boolean violationDetected = false;
int tIndex = this.threadToIndex.get(t);
VectorClockOpt C_t_begin = this.clockThreadBegin.get(tIndex);
VectorClockOpt C_t = this.clockThread.get(tIndex);
for(Thread u: this.threadToIndex.keySet()) {
if(!u.equals(t)) {
VectorClockOpt C_u = getVectorClock(clockThread, u);
int uIndex = this.threadToIndex.get(u);
VectorClockOpt C_u_begin = this.clockThreadBegin.get(uIndex);
if(C_u_begin.isLessThanOrEqual(C_t, uIndex)) {
this.updateSetThread_read.get(u).addAll(this.updateSetThread_read.get(t));
this.updateSetThread_write.get(u).addAll(this.updateSetThread_write.get(t));
}
if(C_t_begin.isLessThanOrEqual(C_u, tIndex)) {
violationDetected |= checkAndGetClock(C_t, C_t, u);
if(violationDetected) break;
}
}
}
if(violationDetected) return violationDetected;
for(Lock l: this.lockToIndex.keySet()) {
VectorClockOpt L_l = getVectorClock(clockLock, l);
if(C_t_begin.isLessThanOrEqual(L_l, tIndex)) {
L_l.updateWithMax(L_l, C_t);
}
}
for(Variable v: this.updateSetThread_write.get(t)) {
VectorClockOpt W_v = getVectorClock(clockWriteVariable, v);
W_v.updateWithMax(W_v, C_t);
}
this.updateSetThread_write.get(t).clear();
for(Variable v: this.updateSetThread_read.get(t)) {
VectorClockOpt R_v = getVectorClock(clockReadVariable, v);
R_v.updateWithMax(R_v, C_t);
VectorClockOpt chR_v = getVectorClock(clockReadVariableCheck, v);
updateCheckClock(chR_v, C_t, t);
}
this.updateSetThread_read.get(t).clear();
return violationDetected;
}
public void updateSetAtWrite(Thread t, Variable v) {
int tIndex = this.threadToIndex.get(t);
VectorClockOpt C_t = this.clockThread.get(tIndex);
for(Thread u: this.threadToIndex.keySet()) {
if(transactionIsActive(u)) {
int uIndex = this.threadToIndex.get(u);
VectorClockOpt C_u_begin = this.clockThreadBegin.get(uIndex);
if(C_u_begin.isLessThanOrEqual(C_t, uIndex)) {
this.updateSetThread_write.get(u).add(v);
}
}
}
}
public void updateSetAtRead(Thread t, Variable v) {
int tIndex = this.threadToIndex.get(t);
VectorClockOpt C_t = this.clockThread.get(tIndex);
for(Thread u: this.threadToIndex.keySet()) {
if(transactionIsActive(u)) {
int uIndex = this.threadToIndex.get(u);
VectorClockOpt C_u_begin = this.clockThreadBegin.get(uIndex);
if(C_u_begin.isLessThanOrEqual(C_t, uIndex)) {
this.updateSetThread_read.get(u).add(v);
}
}
}
}
public void updateCheckClock(VectorClockOpt vc1, VectorClockOpt vc2, Thread t) {
int tIndex = threadToIndex.get(t);
vc1.updateMax2WithoutLocal(vc2, tIndex);
}
// This assumes that local clocks are not increment for events inside a transaction
public boolean hasIncomingEdge(Thread t) {
if(parentTransactionIsAlive.contains(t)) return true;
int tIndex = this.threadToIndex.get(t);
VectorClockOpt C_t_begin = this.clockThreadBegin.get(tIndex);
VectorClockOpt C_t = this.clockThread.get(tIndex);
return C_t_begin.isLessThan(C_t);
}
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));
}
}