-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIndex.ets
More file actions
133 lines (115 loc) · 2.81 KB
/
Copy pathIndex.ets
File metadata and controls
133 lines (115 loc) · 2.81 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
import {
SynStart,
SynEnd,
SharedBoolean,
Syc,
addFunc,
Runnable,
Thread,
isMainThread,
} from './ThreadBridge';
export function sharedWash(runnable: Runnable) {
let archetype: Runnable;
if (runnable.className === "Driver") {
archetype = new Driver(new Semaphores());
} else if (runnable.className === "Conductor") {
archetype = new Conductor(new Semaphores());
} else {
archetype = new Thread();
}
addFunc(runnable, archetype);
runnable.run();
}
class Bus {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "Bus";
public static main(args: string[]): void {
const semaphores = new Semaphores();
const driver = new Thread(new Driver(semaphores));
const conductor = new Thread(new Conductor(semaphores));
driver.start();
conductor.start();
}
}
class Semaphores {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "Semaphores";
public s1 = new SharedBoolean(false);
public s2 = new SharedBoolean(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 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 Driver implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "Driver";
public semaphores: Semaphores;
constructor(semaphores: Semaphores) {
this.semaphores = semaphores;
}
run(): void {
while (true) {
while (!this.semaphores.ps1()) {}
console.log("Bus starts.");
console.log("Driver is driving.");
console.log("Bus stops.");
this.semaphores.vs2();
}
}
}
class Conductor implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = "Conductor";
public semaphores: Semaphores;
constructor(semaphores: Semaphores) {
this.semaphores = semaphores;
}
public run(): void {
while (true) {
console.log("Conductor closes the door.");
this.semaphores.vs1();
console.log("Conductor sells tickets.");
while (!this.semaphores.ps2()) {
// Busy wait
}
console.log("Conductor opens the door.");
console.log("Customers get on/off the bus.");
}
}
}
if (isMainThread()) {
Bus.main([]);
}