Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
155 lines (138 loc) · 4.74 KB
/
Copy pathIndex.ets
File metadata and controls
155 lines (138 loc) · 4.74 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
import { setValues, getSyc, getClass, getValues, SynStart, SynEnd, addFunc, Runnable, Thread, wait, notify } from './ThreadBridge';
export function sharedWash(threadId: number) {
let archetype = Thread.runnableList[threadId];
archetype.run();
}
class ReadWrite {
public synArray: any = getSyc();
public sharedType: string = "object";
public static main(args: string[]): void {
const semaphores = new Semaphores();
const thread0 = new Thread(new Reader(getValues(semaphores), 0));
const thread1 = new Thread(new Reader(getValues(semaphores), 1));
const thread2 = new Thread(new Thread(new Reader(getValues(semaphores), 2)));
const thread3 = new Thread(new Writer(getValues(semaphores), 3));
const thread4 = new Thread(new Writer(getValues(semaphores), 4));
getValues(thread0.start());
getValues(thread1.start());
getValues(thread2.start());
getValues(thread3.start());
getValues(thread4.start());
}
}
class Semaphores {
public count: any = getClass('number', 0);
public rw: any = getClass('boolean', true);
public w: any = getClass('boolean', true);
public mutex: any = getClass('boolean', true);
public synArray: any = getSyc();
public sharedType: string = "object";
constructor() {
setValues(this.count, 0);
setValues(this.rw, true);
setValues(this.w, true);
setValues(this.mutex, true);
}
public Pw(): boolean {
SynStart(this['synArray']);
if (getValues(this.w)) {
setValues(this.w, false);
SynEnd(this['synArray']);
return true;
} else {
SynEnd(this['synArray']);
return false;
}
}
public Pmutex(): boolean {
SynStart(this['synArray']);
if (getValues(this.mutex)) {
setValues(this.mutex, false);
SynEnd(this['synArray']);
return true;
} else {
SynEnd(this['synArray']);
return false;
}
}
public Prw(): boolean {
SynStart(this['synArray']);
if (getValues(this.rw)) {
setValues(this.rw, false);
SynEnd(this['synArray']);
return true;
} else {
SynEnd(this['synArray']);
return false;
}
}
public Vmutex(): void {
SynStart(this['synArray']);
setValues(this.mutex, true);
SynEnd(this['synArray']);
}
public Vw(): void {
SynStart(this['synArray']);
setValues(this.w, true);
SynEnd(this['synArray']);
}
public Vrw(): void {
SynStart(this['synArray']);
setValues(this.rw, true);
SynEnd(this['synArray']);
}
}
class Reader implements Runnable {
public semaphores: any = getClass('Semaphores', new Semaphores());
public rank: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
constructor(semaphores: Semaphores, rank: number) {
setValues(this.semaphores, semaphores);
setValues(this.rank, rank);
}
public run(): void {
while (true) {
while (!getValues(this.semaphores.Pw())) {
}
while (!getValues(this.semaphores.Pmutex())) {
}
if (getValues(this.semaphores.count) === 0) {
while (!getValues(this.semaphores.Prw())) {
}
}
setValues(this.semaphores.count, getValues(this.semaphores.count) + 1);
getValues(this.semaphores.Vmutex());
getValues(this.semaphores.Vw());
console.log("Thread " + getValues(this.rank) + " is reading.");
while (!getValues(this.semaphores.Pmutex())) {
}
setValues(this.semaphores.count, getValues(this.semaphores.count) - 1);
if (getValues(this.semaphores.count) === 0) {
getValues(this.semaphores.Vrw());
}
getValues(this.semaphores.Vmutex());
}
}
}
class Writer implements Runnable {
public semaphores: any = getClass('Semaphores', new Semaphores());
public rank: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
constructor(semaphores: Semaphores, rank: number) {
setValues(this.semaphores, semaphores);
setValues(this.rank, rank);
}
public run(): void {
while (true) {
while (!getValues(this.semaphores.Pw())) {
}
while (!getValues(this.semaphores.Prw())) {
}
console.log("Thread " + getValues(this.rank) + " is writing.");
getValues(this.semaphores.Vrw());
getValues(this.semaphores.Vw());
}
}
}