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 { launch } from "std/concurrency" 17 18class Racer { 19 static x: int = Racer.f(); 20 21 static f(): int { 22 Racer.heavyComputation(); 23 return 1; 24 } 25 26 static heavyComputation(): int { 27 let i = 0; 28 for (let j = 0; j < 10000; j++) { 29 i += j * j; 30 } 31 return i; 32 } 33} 34 35function reader(): Int { 36 if (Racer.x != 1) { 37 console.println("Data race in class initializer detected!"); 38 exit(1); 39 } 40 return 0; 41} 42 43function raceInTheCCtor(): void { 44 for (let i = 0; i < 20; i++) { 45 launch<Int, () => Int>(reader); 46 } 47} 48 49function main(): int { 50 raceInTheCCtor(); 51 return 0; 52} 53