1+ // import { threadStart } from './Washer';
12import { sharedWash } from './Index';
23import taskpool from '@ohos.taskpool';
34
@@ -20,6 +21,9 @@ export function getValues(code: any){
2021 if(code == undefined){
2122 return ;
2223 }
24+ if(code['isSyn']){
25+ return code;
26+ }
2327 if(code['sharedType'] == undefined){
2428 return code;
2529 }
@@ -59,7 +63,7 @@ function stringChanger(sharedArray: any, s: string){
5963}
6064
6165export function setValues(obj: any, tmp: any){
62- if(tmp['sharedType']!=undefined){
66+ if(tmp['sharedType']!=undefined||obj['sharedType'] == undefined ){
6367 const members = Object.getOwnPropertyNames(obj);
6468 for(let i :number = 0;i<members.length;i++){
6569 obj[members[i]] = tmp[members[i]];
@@ -166,15 +170,6 @@ export function getClass(type: string, inClass ?: any){
166170 }
167171}
168172
169- export function getSyc(){
170- let syn = new SharedArrayBuffer(200);
171- let synArray = new Int8Array(syn);
172- for(let i=0;i<180;i++){
173- synArray[i] = 1;
174- }
175- return synArray;
176- }
177-
178173export interface Runnable{
179174 run(): void;
180175}
@@ -190,7 +185,9 @@ export class Thread{
190185 }
191186
192187 public start(){
193- taskpool.execute(threadStart, this.threadId);
188+ taskpool.execute(threadStart, this.threadId).then((value: number) => { // 100: test number
189+ console.log("taskpool start: " + value);
190+ });;
194191 }
195192
196193 constructor(runnableShared: any = null) {
@@ -206,6 +203,12 @@ export class Thread{
206203 }
207204}
208205
206+ @Concurrent
207+ function threadStart(threadId: number): number{
208+ sharedWash(threadId);
209+ return threadId;
210+ }
211+
209212export function addFunc(runnableShared: any, archetype: any){
210213 if(runnableShared['sharedType'] === 'object'){
211214 const members = Object.getOwnPropertyNames(runnableShared);
@@ -225,21 +228,34 @@ export function addFunc(runnableShared: any, archetype: any){
225228 return;
226229}
227230
231+ export function getSyc(){
232+ let syn = new SharedArrayBuffer(32);
233+ let synArray = new Int32Array(syn);
234+ let wnSyn = new SharedArrayBuffer(32);
235+ let wnSynArray = new Int32Array(wnSyn);
236+ synArray[0] = 1;
237+ wnSynArray[0] = 0;
238+ return {syn: synArray, wnSyn: wnSynArray, isSyn: true};
239+ }
228240
229- // @ts-ignore
230- @Concurrent
231- function threadStart(threadId: number){
232- sharedWash(threadId);
241+ export function SynStart(syc: any){
242+ let synArray: Int32Array = syc['syn'];
243+ while(Atomics.exchange(synArray, 0, 0) !== 1);
233244}
234245
246+ export function SynEnd(syc: any){
247+ let synArray: Int32Array = syc['syn'];
248+ Atomics.exchange(synArray, 0, 1);
249+ }
235250
236- export function SynStart(i : number, synArray: Int8Array ){
237- let lockState:number=0 ;
238- do {
239- lockState = Atomics.exchange(synArray, i, 0) ;
240- } while (lockState !== 1);
251+ export function wait(syc: any ){
252+ let wnSynArray: Int32Array = syc['wnSyn'] ;
253+ SynEnd(syc);
254+ wnSynArray[0] = 0 ;
255+ while(Atomics.exchange(wnSynArray, 0, 0) !== 1);
241256}
242257
243- export function SynEnd(i : number, synArray: Int8Array){
244- Atomics.exchange(synArray, i, 1);
258+ export function notify(syc: any){
259+ let wnSynArray: Int32Array = syc['wnSyn'];
260+ Atomics.exchange(wnSynArray, 0, 0)
245261}
0 commit comments