-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathWCPView.java
More file actions
424 lines (379 loc) · 15 KB
/
WCPView.java
File metadata and controls
424 lines (379 loc) · 15 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package engine.racedetectionengine.wcp;
import java.util.HashMap;
import java.util.HashSet;
///import java.util.ListIterator;
import event.Lock;
import event.Thread;
import util.ll.EfficientLinkedList;
import util.ll.EfficientNode;
import util.vectorclock.ClockPair;
import util.vectorclock.VectorClock;
public class WCPView {
//private HashMap<Lock, HashMap<Thread, EfficientStore>> view; // (LOCK, index of WRITER) -> Store
private HashMap<Lock, EfficientLinkedList<ClockPair>> view; // LOCK -> Store
private HashMap<Lock, HashMap<Thread, EfficientNode<ClockPair>>> stackBottomPointerForReader; //LOCK -> (READER -> pointer_in_stack)
private HashMap<Lock, HashMap<Thread, Integer>> stackBottomPointerIndexForReader; //LOCK -> (READER -> 0_based_index__of_reader's_view_from_bottom_of_the_actual_stack)
private HashMap<Lock, HashMap<Thread, Boolean>> stackEmptyForReader; //LOCK -> (READER -> Boolean)
private HashSet<Thread> threadSet;
private HashSet<Lock> lockSet;
public HashMap<String, HashMap<String, Long>> lockThreadLastInteraction;
//private VectorClock tempMin;
WCPView(HashSet<Thread> tSet) {
this.threadSet = new HashSet<Thread>(tSet);
this.lockSet = new HashSet<Lock> ();
this.view = new HashMap<Lock, EfficientLinkedList<ClockPair>>();
this.stackBottomPointerForReader = new HashMap<Lock, HashMap<Thread, EfficientNode<ClockPair>>>();
this.stackBottomPointerIndexForReader = new HashMap<Lock, HashMap<Thread, Integer>>();
this.stackEmptyForReader = new HashMap<Lock, HashMap<Thread, Boolean>>();
//this.tempMin = new VectorClock(this.threadSet.size()) ;
}
public void checkAndAddLock(Lock l){
if(!lockSet.contains(l)){
lockSet.add(l);
view.put(l, new EfficientLinkedList<ClockPair>());
stackBottomPointerForReader.put(l, new HashMap<Thread, EfficientNode<ClockPair>> ());
stackBottomPointerIndexForReader.put(l, new HashMap<Thread, Integer> ());
stackEmptyForReader.put(l, new HashMap<Thread, Boolean> ());
for(Thread tReader : this.threadSet){
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
stackBottomPointerForReader.get(l).put(tReader, null );
stackBottomPointerIndexForReader.get(l).put(tReader, -1 );
stackEmptyForReader.get(l).put(tReader, (Boolean)true);
}
}
}
//return lockToIndex.get(l);
}
/*
public void pushClockPair(Thread tWriter, Lock l, WCPClockPair clockPair) {
checkAndAddLock(l);
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tWriter.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tWriter.getName());
}
//System.out.println("Pushing to stack : (l, tWriter) = (" + l + ", " + tWriter + ")" );
this.view.get(l).pushTop(clockPair);
for(Thread tReader : this.threadSet){
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
if(this.stackEmptyForReader.get(l).get(tReader)){
this.stackEmptyForReader.get(l).put(tReader, (Boolean)false);
this.stackBottomPointerForReader.get(l).put(tReader, view.get(l).getheadNode());
this.stackBottomPointerIndexForReader.get(l).put(tReader, 0);
}
}
}
}
*/
public void pushClockPair(Lock l, ClockPair clockPair) {
checkAndAddLock(l);
//System.out.println("Pushing to stack : (l, tWriter) = (" + l + ", " + tWriter + ")" );
this.view.get(l).pushTop(clockPair);
for(Thread tReader : this.threadSet){
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
if(this.stackEmptyForReader.get(l).get(tReader)){
this.stackEmptyForReader.get(l).put(tReader, (Boolean)false);
this.stackBottomPointerForReader.get(l).put(tReader, view.get(l).getHeadNode());
this.stackBottomPointerIndexForReader.get(l).put(tReader, 0);
}
}
}
}
/*
private int getMinIndexOfReaders(Thread tWriter, Lock l){
//Precondition: Stack at (l, tWriter) is non-empty
//Returns -1 if all readers have empty stacks, else returns the index of the minimum bottom pointer of non-empty stacks
checkAndAddLock(l);
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tWriter.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tWriter.getName());
}
int minIndex = -1;
boolean atLeastOne = false;
for(Thread tReader : this.threadSet){
if(tWriter.getId() != tReader.getId()){
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
if( ! this.stackEmptyForReader.get(l).get(tReader)){
int bottomReaderIndex = this.stackBottomPointerIndexForReader.get(l).get(tReader);
if(! atLeastOne){
atLeastOne = true;
minIndex = bottomReaderIndex;
}
else{
if(minIndex > bottomReaderIndex){
minIndex = bottomReaderIndex;
}
}
}
}
}
}
return minIndex;
}
*/
private int getMinIndexOfReaders(Lock l){
//Precondition: Stack at (l, tWriter) is non-empty
//Returns -1 if all readers have empty stacks, else returns the index of the minimum bottom pointer of non-empty stacks
checkAndAddLock(l);
int minIndex = -1;
boolean atLeastOne = false;
for(Thread tReader : this.threadSet){
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
if( ! this.stackEmptyForReader.get(l).get(tReader)){
int bottomReaderIndex = this.stackBottomPointerIndexForReader.get(l).get(tReader);
if(! atLeastOne){
atLeastOne = true;
minIndex = bottomReaderIndex;
}
else{
if(minIndex > bottomReaderIndex){
minIndex = bottomReaderIndex;
}
}
}
}
}
return minIndex;
}
/*
private void removeViewPrefixOfLength (Thread tWriter, Lock l, int prefixLength){
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tWriter.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tWriter.getName());
}
if(this.view.get(l).getLength() < prefixLength){
throw new IllegalArgumentException("Invalid operation removeViewPrefixOfLength : Size of stack at (" + l.getName() + ") is " + this.view.get(l).getLength() + ", asked to remove : " + prefixLength );
}
this.view.get(l).removeBottomPrefixOfLength(prefixLength);
for(Thread tReader : this.threadSet){
if(tWriter.getId() != tReader.getId()){
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
if( ! this.stackEmptyForReader.get(l).get(tReader)){
int minPtr = this.stackBottomPointerIndexForReader.get(l).get(tReader);
minPtr = minPtr - prefixLength;
if(minPtr >= 0){
this.stackBottomPointerIndexForReader.get(l).put(tReader, minPtr);
}
else{
this.stackEmptyForReader.get(l).put(tReader, true);
this.stackBottomPointerForReader.get(l).put(tReader, null);
this.stackBottomPointerIndexForReader.get(l).put(tReader, -1);
}
}
}
}
}
}
*/
private void removeViewPrefixOfLength (Lock l, int prefixLength){
if(this.view.get(l).getLength() < prefixLength){
throw new IllegalArgumentException("Invalid operation removeViewPrefixOfLength : Size of stack at (" + l.getName() + ") is " + this.view.get(l).getLength() + ", asked to remove : " + prefixLength );
}
this.view.get(l).removeBottomPrefixOfLength(prefixLength);
for(Thread tReader : this.threadSet){
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
if( ! this.stackEmptyForReader.get(l).get(tReader)){
int minPtr = this.stackBottomPointerIndexForReader.get(l).get(tReader);
minPtr = minPtr - prefixLength;
if(minPtr >= 0){
this.stackBottomPointerIndexForReader.get(l).put(tReader, minPtr);
}
else{
this.stackEmptyForReader.get(l).put(tReader, true);
this.stackBottomPointerForReader.get(l).put(tReader, null);
this.stackBottomPointerIndexForReader.get(l).put(tReader, -1);
}
}
}
}
}
/*
private void updateStoreToMatchBottomWithMin(Thread tWriter, Lock l){
checkAndAddLock(l);
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tWriter.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tWriter.getName());
}
EfficientStore st = this.view.get(l);
int sz = st.getLength();
if(sz > 0){
//int minIndex = this.getMinIndexOfReaders(tWriter, l);
int minIndex = this.getMinIndexOfReaders(l);
if(minIndex >= 0){
//removeViewPrefixOfLength(tWriter, l, minIndex);
removeViewPrefixOfLength(l, minIndex);
}
else{
//removeViewPrefixOfLength(tWriter, l, st.getLength());
removeViewPrefixOfLength(l, st.getLength());
}
}
}
*/
private void updateStoreToMatchBottomWithMin(Lock l){
checkAndAddLock(l);
EfficientLinkedList<ClockPair> st = this.view.get(l);
int sz = st.getLength();
if(sz > 0){
int minIndex = this.getMinIndexOfReaders(l);
if(minIndex >= 0){
removeViewPrefixOfLength(l, minIndex);
}
else{
removeViewPrefixOfLength(l, st.getLength());
}
}
}
/*
// result is going to be overwritten with the release of the largest acq <= ct
public void getMaxLowerBound(Thread tWriter, Thread tReader, Lock l, VectorClock ct, VectorClock result) {
checkAndAddLock(l);
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tWriter.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tWriter.getName());
}
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tReader.getName());
}
result.setToZero();
WCPClockPair clockPair = null;
boolean pairFound = false;
EfficientNode lIter = null;
int lIterIndex = -1;
if(! this.stackEmptyForReader.get(l).get(tReader)){
//int index = this.view.get(l).get(tWriter).getIndexOfAcquire(readerStackBottomPointer.get(l).get(tWriter).get(tReader));
lIter = stackBottomPointerForReader.get(l).get(tReader);
lIterIndex = stackBottomPointerIndexForReader.get(l).get(tReader);
while(lIter != null){
clockPair = lIter.getData();
VectorClock acquireClock = clockPair.getAcquire();
if (acquireClock.isLessThanOrEqual(ct)) {
pairFound = true;
} else {
break;
}
lIter = lIter.getNext();
lIterIndex = lIterIndex + 1;
}
}
if(pairFound){
result.copyFrom(clockPair.getRelease());
if(lIter != null){
this.stackBottomPointerForReader.get(l).put(tReader, lIter);
this.stackBottomPointerIndexForReader.get(l).put(tReader, lIterIndex);
}
else{
this.stackEmptyForReader.get(l).put(tReader,true);
this.stackBottomPointerForReader.get(l).put(tReader, null);
this.stackBottomPointerIndexForReader.get(l).put(tReader, -1);
}
//this.updateStoreToMatchBottomWithMin(tWriter, l);
this.updateStoreToMatchBottomWithMin(l);
}
}
*/
// result is going to be overwritten with the release of the largest acq <= ct
public void getMaxLowerBound(Thread tReader, Lock l, VectorClock ct, VectorClock result) {
checkAndAddLock(l);
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tReader.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tReader.getName());
}
result.setToZero();
ClockPair clockPair = null;
boolean pairFound = false;
EfficientNode<ClockPair> lIter = null;
int lIterIndex = -1;
if(! this.stackEmptyForReader.get(l).get(tReader)){
lIter = stackBottomPointerForReader.get(l).get(tReader);
lIterIndex = stackBottomPointerIndexForReader.get(l).get(tReader);
int totalSize = this.view.get(l).getLength();
while(lIter != null && lIterIndex < totalSize - 1){
clockPair = lIter.getData();
VectorClock acquireClock = clockPair.getAcquire();
if (acquireClock.isLessThanOrEqual(ct)) {
pairFound = true;
} else {
break;
}
result.updateWithMax(result, clockPair.getRelease());
lIter = lIter.getNext();
lIterIndex = lIterIndex + 1;
}
}
if(pairFound){
//result.copyFrom(clockPair.getRelease());
if(lIter != null){
this.stackBottomPointerForReader.get(l).put(tReader, lIter);
this.stackBottomPointerIndexForReader.get(l).put(tReader, lIterIndex);
}
else{
this.stackEmptyForReader.get(l).put(tReader,true);
this.stackBottomPointerForReader.get(l).put(tReader, null);
this.stackBottomPointerIndexForReader.get(l).put(tReader, -1);
}
//this.updateStoreToMatchBottomWithMin(tWriter, l);
this.updateStoreToMatchBottomWithMin(l);
}
}
/*
public void updateTopRelease(Thread tWriter, Lock l, VectorClock ct, VectorClock ht) {
checkAndAddLock(l);
if(!this.lockThreadLastInteraction.get(l.getName()).containsKey(tWriter.getName())){
throw new IllegalArgumentException("Invalid operation : No critical section on lock " + l.getName() + " in thread " + tWriter.getName());
}
//System.out.println("(tWriter, l) = (" + tWriter + ", " + l + ")" );
//System.out.println(this.view);
WCPClockPair clockPair = this.view.get(l).top();
clockPair.getRelease().updateWithMax(ct, ht);
}
*/
public void updateTopRelease(Lock l, VectorClock ct, VectorClock ht) {
checkAndAddLock(l);
//System.out.println("(tWriter, l) = (" + tWriter + ", " + l + ")" );
//System.out.println(this.view);
ClockPair clockPair = this.view.get(l).top();
clockPair.getRelease().updateWithMax(ct, ht);
}
public String toString(){
String str = "";
for(Lock l: lockSet){
str += "[" + l.getName() + "]";
str += " : " + view.get(l).toString() + "\n";
}
str += "\n";
return str;
}
public int getSize(){
int sz = 0;
for(Lock l: lockSet){
sz += view.get(l).getLength();
}
return sz;
}
public void printSize(){
System.err.println("Stack size = " + Integer.toString(this.getSize()));
}
public void destroyLock(Lock l){
if(!lockSet.contains(l)){
throw new IllegalArgumentException("Cannot delete non-existent lock " + l.getName());
}
else{
lockSet.remove(l);
view.remove(l);
this.stackBottomPointerForReader.remove(l);
this.stackBottomPointerIndexForReader.remove(l);
this.stackEmptyForReader.remove(l);
}
}
public void destroyLockThreadStack(Lock l, Thread t){
if(!lockSet.contains(l)){
throw new IllegalArgumentException("Cannot delete stack for non-existent lock " + l.getName());
}
else if(!threadSet.contains(t)){
throw new IllegalArgumentException("Cannot delete stack for non-existent thread " + t.getName());
}
else{
if(this.lockThreadLastInteraction.get(l.getName()).containsKey(t.getName())){
this.stackBottomPointerForReader.get(l).remove(t);
this.stackEmptyForReader.get(l).remove(t);
this.stackBottomPointerIndexForReader.get(l).remove(t);
this.lockThreadLastInteraction.get(l.getName()).remove(t.getName());
updateStoreToMatchBottomWithMin(l);
}
}
}
}