1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 public class SimpleLoop implements Runnable {
18     static final int numberOfThreads = 2;
19     volatile static boolean sExitFlag = false;
20     volatile static boolean sEntered = false;
21     int threadIndex;
22 
SimpleLoop(int index)23     SimpleLoop(int index) {
24         threadIndex = index;
25     }
26 
main()27     public static void main() throws Exception {
28         final Thread[] threads = new Thread[numberOfThreads];
29         for (int t = 0; t < threads.length; t++) {
30             threads[t] = new Thread(new SimpleLoop(t));
31             threads[t].start();
32         }
33         for (Thread t : threads) {
34             t.join();
35         }
36 
37         System.out.println("Simple loop finishing");
38     }
39 
$noinline$busyLoop()40     public void $noinline$busyLoop() {
41         Main.assertIsManaged();
42         sEntered = true;
43         for (;;) {
44             if (sExitFlag) {
45                 break;
46             }
47         }
48         Main.assertIsInterpreted();
49     }
50 
run()51     public void run() {
52         if (threadIndex == 0) {
53             while (!sEntered) {
54               Thread.yield();
55             }
56             Main.deoptimizeAll();
57             sExitFlag = true;
58         } else {
59             $noinline$busyLoop();
60         }
61     }
62 }
63