• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { ArkTSUtils, ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
17import { BusinessError } from '@kit.BasicServicesKit';
18
19const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
20
21function sleep(ms: number) {
22  return new Promise<number>(resolve => setTimeout(resolve, ms));
23}
24
25workerPort.onmessage = async (e: MessageEvents) => {
26  const CASE: string = 'ActsConditionTestConditionVariableThreadWorkerTerminate001';
27  console.info(`${CASE} implementCVThreadWorker start`);
28  let result = false;
29  let conditionVariable: ArkTSUtils.locks.ConditionVariable = e.data;
30  const threadWait = async () => {
31    console.log(`${CASE} Thread Wait: Waiting...`);
32    try {
33      await conditionVariable.wait().then(() => {
34        console.log(`${CASE} Thread Wait: Then continue...`);
35        result = true;
36      });
37    } catch (err) {
38      console.log(`${CASE} implementCVThreadWorker wait err: ` + err);
39    }
40    console.log(`${CASE} Thread Wait: Notified and continuing...`);
41  };
42  const threadNotify = async () => {
43    await sleep(1000);
44    console.log(`${CASE} Thread Notify: Notifying one thread...`);
45    try {
46      conditionVariable.notifyAll();
47    } catch (err) {
48      console.log(`${CASE} implementCVThreadWorker notify err: ` + err);
49    }
50  };
51  await Promise.all([
52    threadWait(),
53    threadNotify()
54  ]);
55  await sleep(2000);
56  workerPort.postMessage(result);
57  console.info(`${CASE} implementCVThreadWorker end`);
58}
59
60workerPort.onmessageerror = (e: MessageEvents) => {
61}
62
63workerPort.onerror = (e: ErrorEvent) => {
64}