-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryVector.java
More file actions
executable file
·56 lines (50 loc) · 1.71 KB
/
EntryVector.java
File metadata and controls
executable file
·56 lines (50 loc) · 1.71 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
import java.lang.Math;
public class EntryVector {
private Entry[] e_vector;
private int[] ncall; /* counts the number of pending request for each entry */
private int called; /* counts the number of entries in witch there's at least one pending request */
public EntryVector(int dim){
e_vector = new Entry[dim];
called = 0;
ncall = new int[dim];
for (int i = 0; i < dim; i++){
e_vector[i] = new Entry(50);
ncall[i] = 0;
}
}
public void entryCall(int e) throws InterruptedException{
synchronized (this){
if (ncall[e] == 0){
called++;
notify();
}
ncall[e]++;
}
e_vector[e].entryCall();
}
public void entryRet(int e) throws InterruptedException{
synchronized (this){
ncall[e]--;
if (ncall[e] == 0)
called--;
}
e_vector[e].entryRet();
}
public void accept(int e) throws InterruptedException{
e_vector[e].accept();
}
public int acceptAny() throws InterruptedException{
synchronized (this){
while (called == 0) /* the are no request */
wait();
}
int[] requestedEntry = new int[called];
int j = 0; /* first available position in requestedEntry */
for (int i = 0; i < ncall.length; i++) /* scan the array ncall */
if (ncall[i] != 0) /* if the current entry has some pending request */
requestedEntry[j++] = i; /* store the index of the entry in the current available position of requestedEntry */
int chosen = (int)(called*Math.random()); /* choose a random elements among those in requestedEntry */
e_vector[requestedEntry[chosen]].accept(); /* call accept in the entry whose index it's contained in the chosen element */
return requestedEntry[chosen];
}
}