1/* 2 * Copyright (c) 2023-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 {CoroutineExtras, AtomicFlag} from "std/debug/concurrency" 17import {StdDebug} from "std/debug" 18import { launch } from "std/concurrency" 19import { Job } from "std/core" 20 21type L = StdDebug.Logger 22 23let continue_execution: AtomicFlag = new AtomicFlag(false); 24 25async function async_coro(): Promise<Int> { 26 return CoroutineExtras.getWorkerId(); 27} 28 29function check_async_id(): int { 30 let id_current: int = CoroutineExtras.getWorkerId(); 31 let id_coro: int = (await async_coro()) as int; 32 if (id_current != id_coro) { 33 return 1; 34 } else { 35 return 0; 36 } 37} 38 39function checker(id_main: int): Int { 40 let id_current: int = CoroutineExtras.getWorkerId(); 41 while (continue_execution.get() != true) { 42 // spin 43 } 44 return (id_main != id_current) ? 0 : 1; 45} 46 47function check_sync_id_nonmain(number_of_coros: int): int { 48 let id_current: int = CoroutineExtras.getWorkerId(); 49 let promises: (Job<Int> | undefined)[] = new (Job<Int> | undefined)[number_of_coros]; 50 51 for (let i = 0; i < number_of_coros; ++i) { 52 promises[i] = launch<Int, (i: int) => Int>(checker, id_current); 53 } 54 continue_execution.set(true); 55 let result: int = 0; 56 for (let i = 0; i < number_of_coros; ++i) { 57 result += (promises[i]!).Await(); 58 } 59 return result == 0 ? result : 1; 60} 61 62function check_default_policy(): int { 63 L.log("Testing the DEFAULT affinity type"); 64 65 if (check_async_id() != 0) { 66 L.logError("async function's worker ID is different from launcher function's worker ID"); 67 return 1; 68 } 69 return 0; 70} 71 72function check_nonmain_policy(): int { 73 L.log("Testing the NON_MAIN affinity type"); 74 CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); 75 76 if (check_async_id() != 0) { 77 L.logError("async function's worker ID is different from launcher function's worker ID"); 78 return 1; 79 } 80 if (check_sync_id_nonmain(15) != 0) { 81 L.logError("regular coroutine's worker ID is the same as MAIN worker ID"); 82 return 1; 83 } 84 return 0; 85} 86 87export function main(): int { 88 // Call L's enableLogging method to enable logs. 89 90 // Test 1. Check that the async function will execute on the same worker 91 if (check_default_policy() != 0) { 92 L.logError("test for the DEFAULT affinity type failed"); 93 return 1; 94 } 95 96 /** 97 * Test 2. Check that POLICY_NON_MAIN does not affect async functions, 98 * while regular coroutines will execute on non-MAIN workers. 99 */ 100 if (check_nonmain_policy() != 0) { 101 L.logError("test for the NON_MAIN affinity type failed"); 102 return 1; 103 } 104 105 /** 106 * Test 3. Check that EXCLUSIVE launch flag works 107 */ 108 // NOTE(konstanting): will be added later (see #14697) 109 110 return 0; 111} 112 113