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 Main implements Runnable {
18     static final int numberOfThreads = 2;
19     static boolean sExitFlag = false;
20     static boolean sEntered = false;
21     int threadIndex;
22 
deoptimizeAll()23     private static native void deoptimizeAll();
assertIsInterpreted()24     private static native void assertIsInterpreted();
assertIsManaged()25     private static native void assertIsManaged();
ensureJitCompiled(Class<?> cls, String methodName)26     private static native void ensureJitCompiled(Class<?> cls, String methodName);
27 
Main(int index)28     Main(int index) {
29         threadIndex = index;
30     }
31 
main(String[] args)32     public static void main(String[] args) throws Exception {
33         System.loadLibrary(args[0]);
34 
35         final Thread[] threads = new Thread[numberOfThreads];
36         for (int t = 0; t < threads.length; t++) {
37             threads[t] = new Thread(new Main(t));
38             threads[t].start();
39         }
40         for (Thread t : threads) {
41             t.join();
42         }
43         System.out.println("Finishing");
44     }
45 
$noinline$bar()46     private static int $noinline$bar() {
47         // Should be entered via interpreter bridge.
48         assertIsInterpreted();
49         synchronized (Main.class) {
50             sEntered = true;
51             Main.class.notify();
52             while (!sExitFlag) {
53                 try {
54                     Main.class.wait();
55                 } catch (InterruptedException e) {
56                     throw new Error("Unexpected exception.");
57                 }
58             }
59         }
60         assertIsInterpreted();
61         return 0x1234;
62     }
63 
$noinline$foo()64     public void $noinline$foo() {
65         assertIsManaged();
66         if ($noinline$bar() != 0x1234) {
67             System.out.println("Bad return value");
68         }
69         assertIsInterpreted();
70     }
71 
run()72     public void run() {
73         if (threadIndex == 0) {
74             synchronized (Main.class) {
75                 while (!sEntered) {
76                     try {
77                         Main.class.wait();
78                     } catch (InterruptedException e) {
79                         throw new Error("Unexpected exception.");
80                     }
81                 }
82             }
83             deoptimizeAll();
84             synchronized (Main.class) {
85                 sExitFlag = true;
86                 Main.class.notify();
87             }
88         } else {
89             ensureJitCompiled(Main.class, "$noinline$foo");
90             $noinline$foo();
91         }
92     }
93 }
94