1 /*
2  * Copyright (C) 2016 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 final int totalOperations = 40000;
20     static boolean sFlag = false;
21     static volatile boolean done = false;
22     int threadIndex;
23 
deoptimizeAll()24     public static native void deoptimizeAll();
undeoptimizeAll()25     public static native void undeoptimizeAll();
26 
Main(int index)27     Main(int index) {
28         threadIndex = index;
29     }
30 
main(String[] args)31     public static void main(String[] args) throws Exception {
32         System.loadLibrary(args[0]);
33 
34         final Thread[] threads = new Thread[numberOfThreads];
35         for (int t = 0; t < threads.length; t++) {
36             threads[t] = new Thread(new Main(t));
37             threads[t].start();
38         }
39         for (Thread t : threads) {
40             t.join();
41         }
42         System.out.println("Finishing");
43     }
44 
$noinline$run0()45     public String $noinline$run0() {
46         // Prevent inlining.
47         if (sFlag) {
48             throw new Error();
49         }
50         char[] arr = {'a', 'b', 'c'};
51         String str = new String(arr, 0, arr.length);
52         if (!str.equals("abc")) {
53             System.out.println("Failure 1! " + str);
54             System.exit(0);
55         }
56         return str;
57     }
58 
run()59     public void run() {
60         if (threadIndex == 0) {
61             // This thread keeps doing deoptimization of all threads.
62             // Hopefully that will trigger one deoptimization when returning from
63             // StringFactory.newEmptyString() in one of the other threads.
64             for (int i = 0; i < totalOperations; ++i) {
65                 if (i % 50 == 0) {
66                     deoptimizeAll();
67                 }
68                 if (i % 50 == 25) {
69                     undeoptimizeAll();
70                 }
71             }
72             done = true;
73         } else {
74             // This thread keeps doing new String() from a char array.
75             while (!done) {
76                 String str = $noinline$run0();
77                 if (!str.equals("abc")) {
78                     System.out.println("Failure 2! " + str);
79                     System.exit(0);
80                 }
81             }
82         }
83     }
84 }
85