• 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
16let mainId = CoroutineExtras.getWorkerId();
17
18// Using one worker in runtime options allows to use 63 free worker id's
19const NUM_EWORKERS: int = 63;
20
21function CreateWorkers(): Array<EAWorker> {
22    let workers = new Array<EAWorker>();
23    for (let i = 0; i < NUM_EWORKERS; i++) {
24        workers.push(new EAWorker());
25    }
26    return workers;
27}
28
29function CheckLimitReached() {
30    let catched = false;
31    try {
32        let w = new EAWorker();
33        w.join();
34    } catch(e) {
35        catched = true;
36    }
37
38    return catched;
39}
40
41function LoadWorkers(workers: Array<EAWorker>): Array<Job<int>> {
42    let jobs = new Array<Job<int>>();
43
44    for (let i = 0; i < NUM_EWORKERS; i++) {
45        let job = workers[i].run<int>(():int => {
46            let runId = CoroutineExtras.getWorkerId();
47            assertNE(runId, mainId);
48            return runId;
49        });
50        jobs.push(job);
51    }
52
53    return jobs;
54}
55
56function AwaitJobs(jobs: Array<Job<int>>): Array<int> {
57    let ids = Array<int>();
58
59    for (let i = 0; i < NUM_EWORKERS; i++) {
60        let id = jobs[i].Await();
61        ids.push(id);
62    }
63
64    return ids;
65}
66
67function JoinWorkers(workers: Array<EAWorker>) {
68    workers.forEach((eaWorker: EAWorker) => {
69        eaWorker.join();
70    });
71}
72
73function CheckUnique(arr: Array<int>) {
74    let s = new Set<int>(arr);
75    assertEQ(s.size, arr.length);
76}
77
78function main() {
79    let workers = CreateWorkers();
80
81    if (!CheckLimitReached()) {
82        JoinWorkers(workers);
83        assertTrue(false, 'EAWorkers limit should be reached before');
84    }
85
86    let jobs = LoadWorkers(workers);
87    let ids = AwaitJobs(jobs);
88    CheckUnique(ids);
89    JoinWorkers(workers);
90}
91