Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
162 lines (141 loc) · 4.02 KB
/
Copy pathIndex.ets
File metadata and controls
162 lines (141 loc) · 4.02 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
import {
SynStart,
SynEnd,
wait,
notify,
SharedBoolean,
SharedNumber,
Syc,
Runnable,
Thread,
} from './ThreadBridge';
export function sharedWash(runnable: Runnable) {
let archetype: Runnable;
if (runnable.className === 'Customer') {
archetype = new Customer(
(runnable as Customer).barberShop,
(runnable as Customer).customerId
);
} else if (runnable.className === 'Barber') {
archetype = new Barber((runnable as Barber).barberShop);
} else {
archetype = new Thread();
}
addFunc(runnable, archetype);
runnable.run();
}
class BarberShop {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'BarberShop';
private readonly chairs: number;
private sleeping = new SharedBoolean(true);
private cutting = new SharedBoolean(false);
private waiting = new SharedNumber(0);
constructor(chairs: number) {
this.chairs = chairs;
}
async customerArrived(customerId: number): Promise<void> {
SynStart(this.syn);
while (this.waiting.getValue() === this.chairs || this.cutting.getValue()) {
console.log(`No free chairs or barber is busy, customer ${customerId} leaves.`);
await wait(this.syn);
}
this.waiting.setValue(this.waiting.getValue() + 1);
console.log(`Customer ${customerId} sits in the waiting room.`);
if (this.sleeping.getValue()) {
console.log('Barber wakes up.');
this.sleeping.setValue(false);
}
this.waiting.setValue(this.waiting.getValue() - 1);
console.log(`Customer ${customerId} is having a haircut.`);
SynEnd(this.syn);
}
startCutting(): void {
SynStart(this.syn);
this.cutting.setValue(true);
console.log('Barber starts cutting hair.');
SynEnd(this.syn);
}
finishCutting(): void {
SynStart(this.syn);
this.cutting.setValue(false);
console.log('Barber finishes cutting hair.');
notify(this.syn);
SynEnd(this.syn);
}
async sleep(): Promise<void> {
SynStart(this.syn);
console.log('Barber goes to sleep.');
this.sleeping.setValue(true);
while (this.waiting.getValue() === 0) {
await wait(this.syn);
}
console.log('Barber wakes up.');
this.sleeping.setValue(false);
SynEnd(this.syn);
}
}
class Customer implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'Customer';
private readonly barberShop: BarberShop;
private readonly customerId = new SharedNumber();
constructor(barberShop: BarberShop, customerId: number) {
this.barberShop = barberShop;
this.customerId.setValue(customerId);
}
run(): void {
try {
this.barberShop.customerArrived(this.customerId.getValue());
this.barberShop.startCutting();
Thread.sleep(2000);
this.barberShop.finishCutting();
} catch (e) {
if (e instanceof InterruptedException) {
console.error(e);
}
}
}
}
class Barber implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'Barber';
private barberShop: BarberShop;
constructor(barberShop: BarberShop) {
this.barberShop = barberShop;
}
run(): void {
try {
while (true) {
this.barberShop.sleep();
this.barberShop.startCutting();
Thread.sleep(2000);
this.barberShop.finishCutting();
}
} catch (e) {
if (e instanceof InterruptedException) {
console.error(e);
}
}
}
}
class SleepingBarberProblem {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'SleepingBarberProblem';
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();
}
}
}
if (isMainThread()) {
// You can put the entry of your code here to test.
}