• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package art;
18 
19 import java.util.Arrays;
20 import java.lang.reflect.Executable;
21 import java.lang.reflect.Method;
22 import java.util.concurrent.Semaphore;
23 
24 public class Test1944 {
25   // Just calculate fib forever.
fib(Semaphore started)26   public static void fib(Semaphore started) {
27     started.release();
28     long a = 1;
29     long b = 1;
30     while (true) {
31       long c = a + b;
32       a = b;
33       b = c;
34     }
35   }
36 
37   // Don't bother actually doing anything.
notifySingleStep(Thread thr, Executable e, long loc)38   public static void notifySingleStep(Thread thr, Executable e, long loc) { }
39 
exitNow()40   public static native void exitNow();
41 
42   private static int num_threads = 10;
43 
run()44   public static void run() throws Exception {
45     final Semaphore started = new Semaphore(-(num_threads - 1));
46 
47     Trace.enableSingleStepTracing(Test1944.class,
48         Test1944.class.getDeclaredMethod(
49             "notifySingleStep", Thread.class, Executable.class, Long.TYPE),
50         null);
51 
52     Thread[] threads = new Thread[num_threads];
53     for (int i = 0; i < num_threads; i++) {
54       threads[i] = new Thread(() -> { fib(started); });
55       // Make half daemons.
56       threads[i].setDaemon(i % 2 == 0);
57       threads[i].start();
58     }
59     // Wait for all threads to start.
60     started.acquire();
61     System.out.println("All threads started");
62     // sleep a little
63     Thread.sleep(10);
64     // Die.
65     System.out.println("Exiting suddenly");
66     exitNow();
67     System.out.println("FAILED: Should not reach here!");
68   }
69 }
70