-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIndex.ets
More file actions
169 lines (145 loc) · 4.11 KB
/
Copy pathIndex.ets
File metadata and controls
169 lines (145 loc) · 4.11 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
163
164
165
166
167
168
169
import {
SynStart,
SynEnd,
wait,
notify,
SharedBoolean,
SharedString,
SharedNumber,
Syc,
isMainThread,
addFunc,
Runnable,
Thread,
} from './ThreadBridge';
export function sharedWash(runnable: Runnable) {
let archetype: Runnable;
if (runnable.className === 'ReaderWorker') {
archetype = new ReaderWorker('Archetype Reader', new SharedData());
} else if (runnable.className === 'WriterWorker') {
archetype = new WriterWorker('Archetype Writer', new SharedData());
} else {
archetype = new Thread();
}
addFunc(runnable, archetype);
runnable.run();
}
class CustomReadWriteLock {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'CustomReadWriteLock';
private readers = new SharedNumber(0);
private writing = new SharedBoolean(false);
public lockRead(): void {
{
SynStart(this.syn);
while (this.writing.getValue()) {}
this.readers.setValue(this.readers.getValue() + 1);
SynEnd(this.syn);
}
}
public unlockRead(): void {
{
SynStart(this.syn);
this.readers.setValue(this.readers.getValue() - 1);
if (this.readers.getValue() === 0) {}
SynEnd(this.syn);
}
}
public lockWrite(): void {
{
SynStart(this.syn);
while (this.readers.getValue() > 0 || this.writing.getValue()) {}
this.writing.setValue(true);
SynEnd(this.syn);
}
}
public unlockWrite(): void {
{
SynStart(this.syn);
this.writing.setValue(false);
SynEnd(this.syn);
}
}
}
class SharedData {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'SharedData';
private lock: CustomReadWriteLock = new CustomReadWriteLock();
private data = new SharedNumber(0);
public readData(readerName: string): void {
this.lock.lockRead();
console.log(`${readerName} is reading data: ${this.data.getValue()}`);
for (let j = 0; j < 1000; j++) {}
this.lock.unlockRead();
}
public writeData(writerName: string, newData: number): void {
this.lock.lockWrite();
console.log(`${writerName} is writing data: ${newData}`);
this.data.setValue(newData);
for (let j = 0; j < 1000; j++) {}
this.lock.unlockWrite();
}
}
class ReaderWorker implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'ReaderWorker';
private readonly name = new SharedString();
private readonly sharedData: SharedData;
constructor(name: string, sharedData: SharedData) {
this.name.setValue(name);
this.sharedData = sharedData;
}
run(): void {
for (let i = 0; i < 5; i++) {
this.sharedData.readData(this.name.getValue());
try {
wait(new Syc()); // Replace Thread.sleep with wait
} catch (e) {
console.error(e);
}
}
}
}
class WriterWorker implements Runnable {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'WriterWorker';
private readonly name = new SharedString();
private readonly sharedData: SharedData;
constructor(name: string, sharedData: SharedData) {
this.name.setValue(name);
this.sharedData = sharedData;
}
run(): void {
for (let i = 0; i < 5; i++) {
this.sharedData.writeData(this.name.getValue(), i);
try {
wait(new Syc()); // Replace Thread.sleep with wait
} catch (e) {
console.error(e);
}
}
}
}
class ReadWriteSimulation {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'ReadWriteSimulation';
static main(args: string[]): void {
const sharedData = new SharedData();
const reader1 = new Thread(new ReaderWorker('Reader 1', sharedData));
const reader2 = new Thread(new ReaderWorker('Reader 2', sharedData));
const writer1 = new Thread(new WriterWorker('Writer 1', sharedData));
const writer2 = new Thread(new WriterWorker('Writer 2', sharedData));
reader1.start();
reader2.start();
writer1.start();
writer2.start();
}
}
if (isMainThread()) {
ReadWriteSimulation.main([]);
}