-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathThreadBridge.ets
More file actions
166 lines (136 loc) · 3.8 KB
/
Copy pathThreadBridge.ets
File metadata and controls
166 lines (136 loc) · 3.8 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
import { process } from '@kit.ArkTS'
import { sharedWash } from "./Index"
import taskpool from "@ohos.taskpool";
export interface Runnable{
className: string;
run(): void;
}
export class Thread implements Runnable{
private runnableShared: Runnable;
public className: string = "Thread";
static getThreadOwn(myClass: Runnable): Runnable{
return myClass;
}
public async start(){
// if (isMainThread()) {
await taskpool.execute(runThread, this.runnableShared);
// }
}
public run() {}
constructor(runnableShared: Runnable | null = null) {
if(runnableShared !== null){
this.runnableShared = runnableShared;
}
else{
this.runnableShared = Thread.getThreadOwn(this);
}
}
}
@Concurrent
function runThread(runnable: Runnable) {
sharedWash(runnable);
}
export function isMainThread(): boolean {
return process.pid == process.tid;
}
export class SharedBoolean{
private sharedArray: Uint8Array = new Uint8Array(new SharedArrayBuffer(1));
constructor(b ?: boolean) {
this.sharedArray[0] = 0;
if(b !== undefined){
if(b === true){
this.sharedArray[0] = 1;
}
}
}
public setValue(b: boolean) {
if(b === true){
this.sharedArray[0] = 1;
}
else {
this.sharedArray[0] = 0;
}
}
public getValue(): boolean {
if (this.sharedArray[0] == 0) {
return false;
}
return true;
}
}
export class SharedString {
private sharedArray: Uint8Array = new Uint8Array(new SharedArrayBuffer(1024));
constructor(s ?: string) {
if (s !== undefined){
this.setValue(s);
}
}
public setValue(s: string) {
let length: number = s.length + 3;
let buffer: SharedArrayBuffer = new SharedArrayBuffer(length);
let sharedArray: Uint8Array = new Uint8Array(buffer);
for(let i=0;i<s.length;i++){
sharedArray[i] = s.charCodeAt(i);
}
sharedArray[s.length] = 0;
this.sharedArray = sharedArray;
}
public getValue() {
let receivedString = "";
for(let i = 0; i < this.sharedArray.length && this.sharedArray[i] !== 0; i++){
receivedString += String.fromCharCode(this.sharedArray[i]);
}
return receivedString;
}
}
export class SharedNumber {
private sharedArray: Float64Array = new Float64Array(new SharedArrayBuffer(8));
constructor(n ?: number) {
this.sharedArray[0] = 0;
if(n !== undefined){
this.sharedArray[0] = n;
}
}
public setValue(n: number) {
this.sharedArray[0] = n;
}
public getValue(): number {
return this.sharedArray[0];
}
}
export class Syc {
public synArray: Int32Array = new Int32Array(new SharedArrayBuffer(32));
public wnSynArray: Int32Array = new Int32Array(new SharedArrayBuffer(32));
constructor() {
this.synArray[0] = 1;
this.wnSynArray[0] = 0;
}
}
export function SynStart(syc: Syc){
let synArray: Int32Array = syc.synArray;
while(Atomics.exchange(synArray, 0, 0) !== 1);
}
export function SynEnd(syc: Syc){
let synArray: Int32Array = syc.synArray;
Atomics.exchange(synArray, 0, 1);
}
export function wait(syc: Syc){
let wnSynArray: Int32Array = syc.wnSynArray;
SynEnd(syc);
wnSynArray[0] = 0;
while(Atomics.exchange(wnSynArray, 0, 0) !== 1);
}
export function notify(syc: Syc){
let wnSynArray: Int32Array = syc.wnSynArray;
Atomics.exchange(wnSynArray, 0, 0)
}
export function addFunc(runnableShared: object, archetype: object){
if (runnableShared === archetype) {
return ;
}
const members = Object.getOwnPropertyNames(runnableShared);
for(let i :number = 0;i<members.length;i++){
addFunc(runnableShared[members[i]], archetype[members[i]]);
}
runnableShared['__proto__'] = archetype['__proto__'];
}