Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
131 lines (117 loc) · 3.29 KB
/
Copy pathIndex.ets
File metadata and controls
131 lines (117 loc) · 3.29 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
import {
SynStart,
SynEnd,
SharedBoolean,
SharedNumber,
Syc,
addFunc,
Runnable,
Thread,
isMainThread,
} from './ThreadBridge';
export function sharedWash(runnable: Runnable) {
let archetype: Runnable;
if (runnable.className === 'Consumer') {
archetype = new Consumer(new Semaphores(), 0);
} else if (runnable.className === 'Seller') {
archetype = new Seller(new Semaphores(), 0);
} else {
archetype = new Thread();
}
addFunc(runnable, archetype);
runnable.run();
}
class Bread {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'Bread';
}
class Semaphores {
public i = new SharedNumber(0);
public j = new SharedNumber(0);
public mutex_i = new SharedBoolean(true);
public mutex_j = new SharedBoolean(true);
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'Semaphores';
public P_mutex_i(): boolean {
SynStart(this.syn);
if (this.mutex_i.getValue()) {
this.mutex_i.setValue(false);
SynEnd(this.syn);
return true;
} else {
SynEnd(this.syn);
return false;
}
}
public P_mutex_j(): boolean {
SynStart(this.syn);
if (this.mutex_j.getValue()) {
this.mutex_j.setValue(false);
SynEnd(this.syn);
return true;
} else {
SynEnd(this.syn);
return false;
}
}
public V_mutex_i(): void {
SynStart(this.syn);
this.mutex_i.setValue(true);
SynEnd(this.syn);
}
public V_mutex_j(): void {
SynStart(this.syn);
this.mutex_j.setValue(true);
SynEnd(this.syn);
}
}
class Consumer implements Runnable {
public semaphores: Semaphores;
public rank = new SharedNumber();
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'Consumer';
constructor(semaphores: Semaphores, rank: number) {
this.semaphores = semaphores;
this.rank.setValue(rank);
}
public run(): void {
while (true) {
console.log(`Consumer ${this.rank.getValue()} enter the bakery.`);
while (!this.semaphores.P_mutex_i()) {}
console.log(`Consumer ${this.rank.getValue()} get the number ${this.semaphores.i.getValue()}.`);
this.semaphores.i.setValue(this.semaphores.i.getValue() + 1);
this.semaphores.V_mutex_i();
console.log(`Consumer ${this.rank.getValue()} is waiting for buying bread.`);
}
}
}
class Seller implements Runnable {
public semaphores: Semaphores;
public rank = new SharedNumber();
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'Seller';
constructor(semaphores: Semaphores, rank: number) {
this.semaphores = semaphores;
this.rank.setValue(rank);
}
run(): void {
while (true) {
while (!this.semaphores.P_mutex_j()) {}
if (this.semaphores.j.getValue() < this.semaphores.i.getValue()) {
console.log(`Seller ${this.rank.getValue()} calls number ${this.semaphores.j.getValue()}.`);
this.semaphores.j.setValue(this.semaphores.j.getValue() + 1);
this.semaphores.V_mutex_j();
} else {
this.semaphores.V_mutex_j();
console.log(`Seller ${this.rank.getValue()} is free.`);
}
}
}
}
if (isMainThread()) {
// You can put the entry of your code here to test.
}