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