Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
157 lines (138 loc) · 4.17 KB
/
Copy pathIndex.ets
File metadata and controls
157 lines (138 loc) · 4.17 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
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 ReadCompute {
public synArray: any = getSyc();
public sharedType: string = "object";
public static main(args: string[]): void {
const semaphores = new Semaphores();
const p = new Thread(new P(getValues(semaphores)));
const p1 = new Thread(new P1(getValues(semaphores)));
const p2 = new Thread(new P2(getValues(semaphores)));
getValues(p.start());
getValues(p1.start());
getValues(p2.start());
}
}
class Semaphores {
public synArray : any = getSyc();
public sharedType: string = "object";
public mutex: any = getClass('boolean', true);
public full: any = getClass('number', 0);
public empty: any = getClass('number', 50);
public pMutex(): boolean {
SynStart(this['synArray']);
if (getValues(this.mutex)) {
setValues(this.mutex, false);
SynEnd(this['synArray']);
return true;
} else {
SynEnd(this['synArray']);
return false;
}
}
public pFull(): boolean {
SynStart(this['synArray']);
if (getValues(this.full) > 0) {
setValues(this.full, getValues(this.full) - 1);
SynEnd(this['synArray']);
return true;
} else {
SynEnd(this['synArray']);
return false;
}
}
public pEmpty(): boolean {
SynStart(this['synArray']);
if (getValues(this.empty) > 0) {
setValues(this.empty, getValues(this.empty) - 1);
SynEnd(this['synArray']);
return true;
} else {
SynEnd(this['synArray']);
return false;
}
}
public vMutex(): void {
SynStart(this['synArray']);
setValues(this.mutex, true);
SynEnd(this['synArray']);
}
public vFull(): void {
SynStart(this['synArray']);
setValues(this.full, getValues(this.full) + 1);
SynEnd(this['synArray']);
}
public vEmpty(): void {
SynStart(this['synArray']);
setValues(this.empty, getValues(this.empty) + 1);
SynEnd(this['synArray']);
}
}
class P implements Runnable {
public semaphores: any = getClass('Semaphores', new Semaphores());
public synArray: any = getSyc();
public sharedType: string = "object";
public constructor(semaphores: Semaphores) {
setValues(this.semaphores, semaphores);
}
public run(): void {
while (true) {
while (!getValues(this.semaphores.pEmpty())) {
}
while (!getValues(this.semaphores.pMutex())) {
}
console.log("P reads data from device into the buffer.");
getValues(this.semaphores.vMutex());
getValues(this.semaphores.vFull());
}
}
}
class P1 implements Runnable {
public synArray: any = getSyc();
public sharedType: string = "object";
public semaphores: any = getClass('Semaphores');
constructor(semaphores: Semaphores) {
setValues(this.semaphores, semaphores);
}
public run(): void {
while (true) {
while (!getValues(this.semaphores.pFull())) {
}
console.log("P1 gets one data from the buffer.");
while (!getValues(this.semaphores.pFull())) {
}
console.log("P1 gets one data from the buffer.");
console.log("P1 computes the datas.");
getValues(this.semaphores.vEmpty());
console.log("P1 prints the result.");
getValues(this.semaphores.vEmpty());
console.log("P1 prints the result.");
}
}
}
class P2 implements Runnable {
public semaphores: any = getClass('Semaphores', null);
public synArray: any = getSyc();
public sharedType: string = "object";
constructor(semaphores: Semaphores) {
setValues(this.semaphores, semaphores);
}
public run(): void {
while (true) {
while (!getValues(this.semaphores.pFull())) {
}
console.log("P2 gets one data from the buffer.");
while (!getValues(this.semaphores.pFull())) {
}
console.log("P2 gets one data from the buffer.");
console.log("P2 computes the datas.");
getValues(this.semaphores.vEmpty());
console.log("P2 prints the result.");
getValues(this.semaphores.vEmpty());
console.log("P2 prints the result.");
}
}
}