• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 {AtomicFlag} from "std/debug/concurrency"
17
18class Event {
19    public Wait() {
20        while (this.flag.get() != true) {
21            Coroutine.Schedule();
22        }
23    }
24
25    public Fire() {
26        this.flag.set(true);
27    }
28
29    private flag = new AtomicFlag(false);
30};
31
32let event = new Event();
33
34function foo() : int {
35    event.Wait();
36    return 42;
37}
38
39function bar(p: Promise<int>) : int {
40    return await p;
41}
42
43function main() {
44    let p1 = launch foo();
45    for (let i = 0; i < 2; ++i) {
46        let p2 = launch bar(p1);
47    }
48    // NOTE(panferovi): for more determinism we need to make sure
49    // that bar coroutines are waiting for its awakening
50    event.Fire();
51    let result = await p1;
52    assert result == 42;
53}
54