Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
135 lines (115 loc) · 4.35 KB
/
Copy pathIndex.ets
File metadata and controls
135 lines (115 loc) · 4.35 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
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 BarberShop {
public synArray: any = getSyc();
public sharedType: string = "object";
private readonly chairs: any = getClass('number');
private sleeping: any = getClass('boolean', true);
private cutting: any = getClass('boolean', false);
private waiting: any = getClass('number', 0);
constructor(chairs: number) {
setValues(this.chairs, getValues(chairs));
setValues(this.sleeping, true);
setValues(this.cutting, false);
setValues(this.waiting, 0);
}
async customerArrived(customerId: number) {
SynStart(getValues(this['synArray']));
while (getValues(this.waiting) === getValues(this.chairs) || getValues(this.cutting)) {
console.log("No free chairs or barber is busy, customer " + customerId + " leaves.");
await wait(getValues(this['synArray']));
}
setValues(this.waiting, getValues(this.waiting) + 1);
console.log("Customer " + customerId + " sits in the waiting room.");
if (getValues(this.sleeping)) {
console.log("Barber wakes up.");
setValues(this.sleeping, false);
}
setValues(this.waiting, getValues(this.waiting) - 1);
console.log("Customer " + customerId + " is having a haircut.");
SynEnd(getValues(this['synArray']));
}
startCutting() {
SynStart(getValues(this['synArray']));
setValues(this.cutting, true);
console.log("Barber starts cutting hair.");
SynEnd(getValues(this['synArray']));
}
finishCutting() {
SynStart(getValues(this['synArray']));
setValues(this.cutting, false);
console.log("Barber finishes cutting hair.");
notify(getValues(this['synArray']));
SynEnd(getValues(this['synArray']));
}
async sleep() {
SynStart(getValues(this['synArray']));
console.log("Barber goes to sleep.");
setValues(this.sleeping, true);
while (getValues(this.waiting) === 0) {
await wait(getValues(this['synArray']));
}
console.log("Barber wakes up.");
setValues(this.sleeping, false);
SynEnd(getValues(this['synArray']));
}
}
class Customer implements Runnable {
private readonly barberShop: any = getClass('BarberShop', new BarberShop(3));
private readonly customerId: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
constructor(barberShop: BarberShop, customerId: number) {
setValues(this.barberShop, barberShop);
setValues(this.customerId, customerId);
}
run(): void {
try {
getValues(this.barberShop.customerArrived(getValues(this.customerId)));
getValues(this.barberShop.startCutting());
getValues(Thread.sleep(2000));
getValues(this.barberShop.finishCutting());
} catch (e) {
if (e instanceof InterruptedException) {
getValues(e.printStackTrace());
}
}
}
}
class Barber implements Runnable {
private barberShop: any = getClass('BarberShop', new BarberShop());
public synArray: any = getSyc();
public sharedType: string = "object";
constructor(barberShop: BarberShop) {
setValues(this.barberShop, barberShop);
}
run() {
try {
while (true) {
getValues(this.barberShop.sleep());
getValues(this.barberShop.startCutting());
setTimeout(() => {
getValues(this.barberShop.finishCutting());
}, 2000);
}
} catch (e) {
console.error(e);
}
}
}
class SleepingBarberProblem {
public synArray: any = getSyc();
public sharedType: string = "object";
public static main(): void {
const barberShop = new BarberShop(3);
const barberThread = new Thread(new Barber(barberShop), 'Barber');
barberThread.start();
for (let i = 1; i <= 10; i++) {
const customerThread = new Thread(new Customer(barberShop, i), 'Customer ' + i);
customerThread.start();
}
}
}