• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 taskpool from '@ohos.taskpool';
17import { BusinessError } from '@ohos.base';
18import logger from "../util/Logger";
19
20@Component
21export struct AtomicsUsage {
22  @State result: string = "";
23  private taskNum: number = 2;
24  private scroller: Scroller = new Scroller();
25
26  build() {
27    Row() {
28      Column() {
29        Button($r('app.string.not_use_atomics'))
30          .width("80%")
31          .fontSize(30)
32          .fontWeight(FontWeight.Bold)
33          .margin({ top: 30 })
34          .onClick(async () => {
35            this.sharedArrayBufferUsage(false);
36          })
37        Button($r('app.string.use_atomics'))
38          .width("80%")
39          .fontSize(30)
40          .fontWeight(FontWeight.Bold)
41          .margin({ top: 30 })
42          .onClick(async () => {
43            this.sharedArrayBufferUsage(true);
44          })
45        Text($r('app.string.result'))
46          .width("80%")
47          .fontSize(30)
48          .margin({ top: 30 })
49          .fontWeight(FontWeight.Bold)
50          .fontColor(Color.Blue)
51        Scroll(this.scroller) {
52          Text(this.result)
53            .width("80%")
54            .fontSize(30)
55            .fontWeight(FontWeight.Bold)
56            .fontColor(Color.Blue)
57        }
58        .height("60%")
59        .margin({ top: 30 })
60      }
61      .width('100%')
62    }
63    .height('100%')
64  }
65
66  // 根据传入的值isAtomics判断是否使用原子操作
67  sharedArrayBufferUsage(isAtomics: boolean) {
68    // 创建长度为4的SharedArrayBuffer对象
69    let sab: SharedArrayBuffer = new SharedArrayBuffer(4);
70    // 由于SharedArrayBuffer是原始二进制数据缓冲区,无法直接使用,所以这里转换为Int32Array类型进行后续操作
71    let int32Array: Int32Array = new Int32Array(sab);
72    int32Array[0] = 0;
73    // 创建Task对象,并放入TaskGroup中执行
74    let taskGroup: taskpool.TaskGroup = new taskpool.TaskGroup();
75    for (let i = 0; i < this.taskNum; i++) {
76      if (isAtomics) {
77        taskGroup.addTask(new taskpool.Task(atomicsProcess, int32Array));
78      } else {
79        taskGroup.addTask(new taskpool.Task(normalProcess, int32Array));
80      }
81    }
82
83    taskpool.execute(taskGroup).then(() => {
84      // 将结果打印在Text上
85      this.result = this.result + "\n" + int32Array;
86      // 如果Scroll不在底部,则滑到到底部
87      if (!this.scroller.isAtEnd()) {
88        this.scroller.scrollEdge(Edge.Bottom);
89      }
90    }).catch((e: BusinessError) => {
91      logger.error(e.message);
92    })
93
94  }
95}
96// 非原子操作,进行10000次++
97@Concurrent
98function normalProcess(int32Array: Int32Array) {
99  for (let i = 0; i < 10000; i++) {
100    int32Array[0]++;
101  }
102}
103// 原子操作,进行10000次++
104@Concurrent
105function atomicsProcess(int32Array: Int32Array) {
106  for (let i = 0; i < 10000; i++) {
107    Atomics.add(int32Array, 0, 1);
108  }
109}