-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIndex.ets
More file actions
220 lines (196 loc) · 5.34 KB
/
Copy pathIndex.ets
File metadata and controls
220 lines (196 loc) · 5.34 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
import {
SynStart,
SynEnd,
wait,
notify,
SharedBoolean,
SharedString,
SharedNumber,
Syc,
isMainThread,
addFunc,
Runnable,
Thread,
} from "./ThreadBridge";
export function sharedWash(runnable: Runnable) {
let archetype: Runnable;
if (runnable.className === "VideoShow1") {
archetype = new VideoShow1((runnable as VideoShow1).semaphores);
} else if (runnable.className === "VideoShow2") {
archetype = new VideoShow2((runnable as VideoShow2).semaphores);
} else if (runnable.className === "VideoShow3") {
archetype = new VideoShow3((runnable as VideoShow3).semaphores);
} else {
archetype = new Thread();
}
addFunc(runnable, archetype);
runnable.run();
}
class VideoShow {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "VideoShow";
public static main(args: string[]): void {
const semaphores = new Semaphores();
const show1 = new Thread(new VideoShow1(semaphores));
const show2 = new Thread(new VideoShow2(semaphores));
const show3 = new Thread(new VideoShow3(semaphores));
show1.start();
show2.start();
show3.start();
}
}
class Semaphores {
public s = new SharedBoolean(true);
public s0 = new SharedBoolean(true);
public s1 = new SharedBoolean(true);
public s2 = new SharedBoolean(true);
public count0 = new SharedNumber(0);
public count1 = new SharedNumber(0);
public count2 = new SharedNumber(0);
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "Semaphores";
public ps(): boolean {
SynStart(this.syn);
if (this.s.getValue()) {
this.s.setValue(false);
SynEnd(this.syn);
return true;
} else {
SynEnd(this.syn);
return false;
}
}
public ps0(): boolean {
SynStart(this.syn);
if (this.s0.getValue()) {
this.s0.setValue(false);
SynEnd(this.syn);
return true;
} else {
SynEnd(this.syn);
return false;
}
}
public ps1(): boolean {
SynStart(this.syn);
if (this.s1.getValue()) {
this.s1.setValue(false);
SynEnd(this.syn);
return true;
} else {
SynEnd(this.syn);
return false;
}
}
public ps2(): boolean {
SynStart(this.syn);
if (this.s2.getValue()) {
this.s2.setValue(false);
SynEnd(this.syn);
return true;
} else {
SynEnd(this.syn);
return false;
}
}
public vs(): void {
SynStart(this.syn);
this.s.setValue(true);
SynEnd(this.syn);
}
public vs0(): void {
SynStart(this.syn);
this.s0.setValue(true);
SynEnd(this.syn);
}
public vs1(): void {
SynStart(this.syn);
this.s1.setValue(true);
SynEnd(this.syn);
}
public vs2(): void {
SynStart(this.syn);
this.s2.setValue(true);
SynEnd(this.syn);
}
}
class VideoShow1 implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "VideoShow1";
public semaphores: Semaphores;
constructor(semaphores: Semaphores) {
this.semaphores = semaphores;
}
run(): void {
while (!this.semaphores.ps0()) {}
this.semaphores.count0.setValue(this.semaphores.count0.getValue() + 1);
if (this.semaphores.count0.getValue() === 1) {
while (!this.semaphores.ps()) {}
}
this.semaphores.vs0();
console.log("Video show 1 begins.");
while (!this.semaphores.ps0()) {}
this.semaphores.count0.setValue(this.semaphores.count0.getValue() - 1);
if (this.semaphores.count0.getValue() === 0) {
console.log("Video show 1 ends.");
this.semaphores.vs();
}
this.semaphores.vs0();
}
}
class VideoShow2 implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "VideoShow2";
public semaphores: Semaphores;
constructor(semaphores: Semaphores) {
this.semaphores = semaphores;
}
public run(): void {
while (!this.semaphores.ps1()) {}
this.semaphores.count1.setValue(this.semaphores.count1.getValue() + 1);
if (this.semaphores.count1.getValue() === 1) {
while (!this.semaphores.ps()) {}
}
this.semaphores.vs1();
console.log("Video show 2 begins.");
while (!this.semaphores.ps1()) {}
this.semaphores.count1.setValue(this.semaphores.count1.getValue() - 1);
if (this.semaphores.count1.getValue() === 0) {
console.log("Video show 2 ends.");
this.semaphores.vs();
}
this.semaphores.vs1();
}
}
class VideoShow3 implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "VideoShow3";
public semaphores: Semaphores;
constructor(semaphores: Semaphores) {
this.semaphores = semaphores;
}
run(): void {
while (!this.semaphores.ps2()) {}
this.semaphores.count2.setValue(this.semaphores.count2.getValue() + 1);
if (this.semaphores.count2.getValue() === 1) {
while (!this.semaphores.ps()) {}
}
this.semaphores.vs2();
console.log("Video show 3 begins.");
while (!this.semaphores.ps2()) {}
this.semaphores.count2.setValue(this.semaphores.count2.getValue() - 1);
if (this.semaphores.count2.getValue() === 0) {
console.log("Video show 3 ends.");
this.semaphores.vs();
}
this.semaphores.vs2();
}
}
if (isMainThread()) {
// You can put the entry of your code here to test.
}