blob: df4501d3383c987ac39e2aa3e5e8a67f1fd8db11 [file] [log] [blame]
Andreas Gampeb5eb94a2016-10-27 19:23:09 -07001/*
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
17import java.util.Arrays;
18import java.util.concurrent.CountDownLatch;
19
20public class Main {
21 public static void main(String[] args) throws Exception {
22 System.loadLibrary(args[1]);
23
24 doTest();
25 doTestOtherThreadWait();
26 doTestOtherThreadBusyLoop();
27 }
28
29 public static void doTest() throws Exception {
30 System.out.println("###################");
31 System.out.println("### Same thread ###");
32 System.out.println("###################");
33 System.out.println("From top");
34 Recurse.foo(4, 0, 25, null);
35 Recurse.foo(4, 1, 25, null);
36 Recurse.foo(4, 0, 5, null);
37 Recurse.foo(4, 2, 5, null);
38
39 System.out.println("From bottom");
40 Recurse.foo(4, -1, 25, null);
41 Recurse.foo(4, -5, 5, null);
42 Recurse.foo(4, -7, 5, null);
43 }
44
45 public static void doTestOtherThreadWait() throws Exception {
46 System.out.println();
47 System.out.println("################################");
48 System.out.println("### Other thread (suspended) ###");
49 System.out.println("################################");
50 final ControlData data = new ControlData();
51 data.waitFor = new Object();
52 Thread t = new Thread() {
53 public void run() {
54 Recurse.foo(4, 0, 0, data);
55 }
56 };
57 t.start();
58 data.reached.await();
59 Thread.yield();
60 Thread.sleep(500); // A little bit of time...
61
62 System.out.println("From top");
63 print(t, 0, 25);
64 print(t, 1, 25);
65 print(t, 0, 5);
66 print(t, 2, 5);
67
68 System.out.println("From bottom");
69 print(t, -1, 25);
70 print(t, -5, 5);
71 print(t, -7, 5);
72
73 // Let the thread make progress and die.
74 synchronized(data.waitFor) {
75 data.waitFor.notifyAll();
76 }
77 t.join();
78 }
79
80 public static void doTestOtherThreadBusyLoop() throws Exception {
81 System.out.println();
82 System.out.println("###########################");
83 System.out.println("### Other thread (live) ###");
84 System.out.println("###########################");
85 final ControlData data = new ControlData();
86 Thread t = new Thread() {
87 public void run() {
88 Recurse.foo(4, 0, 0, data);
89 }
90 };
91 t.start();
92 data.reached.await();
93 Thread.yield();
94 Thread.sleep(500); // A little bit of time...
95
96 System.out.println("From top");
97 print(t, 0, 25);
98 print(t, 1, 25);
99 print(t, 0, 5);
100 print(t, 2, 5);
101
102 System.out.println("From bottom");
103 print(t, -1, 25);
104 print(t, -5, 5);
105 print(t, -7, 5);
106
107 // Let the thread stop looping and die.
108 data.stop = true;
109 t.join();
110 }
111
112 public static void print(String[] stack) {
113 System.out.println("---------");
114 for (int i = 0; i < stack.length; i += 2) {
115 System.out.print(' ');
116 System.out.print(stack[i]);
117 System.out.print(' ');
118 System.out.println(stack[i + 1]);
119 }
120 }
121
122 public static void print(Thread t, int start, int max) {
123 print(getStackTrace(t, start, max));
124 }
125
126 // Wrap generated stack traces into a class to separate them nicely.
127 public static class Recurse {
128
129 public static int foo(int x, int start, int max, ControlData data) {
130 bar(x, start, max, data);
131 return 0;
132 }
133
134 private static long bar(int x, int start, int max, ControlData data) {
135 baz(x, start, max, data);
136 return 0;
137 }
138
139 private static Object baz(int x, int start, int max, ControlData data) {
140 if (x == 0) {
141 printOrWait(start, max, data);
142 } else {
143 foo(x - 1, start, max, data);
144 }
145 return null;
146 }
147
148 private static void printOrWait(int start, int max, ControlData data) {
149 if (data == null) {
150 print(Thread.currentThread(), start, max);
151 } else {
152 if (data.waitFor != null) {
153 synchronized (data.waitFor) {
154 data.reached.countDown();
155 try {
156 data.waitFor.wait(); // Use wait() as it doesn't have a "hidden" Java call-graph.
157 } catch (Throwable t) {
158 throw new RuntimeException(t);
159 }
160 }
161 } else {
162 data.reached.countDown();
163 while (!data.stop) {
164 // Busy-loop.
165 }
166 }
167 }
168 }
169 }
170
171 public static class ControlData {
172 CountDownLatch reached = new CountDownLatch(1);
173 Object waitFor = null;
174 volatile boolean stop = false;
175 }
176
177 public static native String[] getStackTrace(Thread thread, int start, int max);
178}