blob: 722bee8056321461858a0ff11c224fda5e423950 [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
Andreas Gampeceafe352016-12-12 18:49:33 -0800112 public static void print(String[][] stack) {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700113 System.out.println("---------");
Andreas Gampeceafe352016-12-12 18:49:33 -0800114 for (String[] stackElement : stack) {
115 for (String part : stackElement) {
116 System.out.print(' ');
117 System.out.print(part);
118 }
119 System.out.println();
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700120 }
121 }
122
123 public static void print(Thread t, int start, int max) {
124 print(getStackTrace(t, start, max));
125 }
126
127 // Wrap generated stack traces into a class to separate them nicely.
128 public static class Recurse {
129
130 public static int foo(int x, int start, int max, ControlData data) {
131 bar(x, start, max, data);
132 return 0;
133 }
134
135 private static long bar(int x, int start, int max, ControlData data) {
136 baz(x, start, max, data);
137 return 0;
138 }
139
140 private static Object baz(int x, int start, int max, ControlData data) {
141 if (x == 0) {
142 printOrWait(start, max, data);
143 } else {
144 foo(x - 1, start, max, data);
145 }
146 return null;
147 }
148
149 private static void printOrWait(int start, int max, ControlData data) {
150 if (data == null) {
151 print(Thread.currentThread(), start, max);
152 } else {
153 if (data.waitFor != null) {
154 synchronized (data.waitFor) {
155 data.reached.countDown();
156 try {
157 data.waitFor.wait(); // Use wait() as it doesn't have a "hidden" Java call-graph.
158 } catch (Throwable t) {
159 throw new RuntimeException(t);
160 }
161 }
162 } else {
163 data.reached.countDown();
164 while (!data.stop) {
165 // Busy-loop.
166 }
167 }
168 }
169 }
170 }
171
172 public static class ControlData {
173 CountDownLatch reached = new CountDownLatch(1);
174 Object waitFor = null;
175 volatile boolean stop = false;
176 }
177
Andreas Gampeceafe352016-12-12 18:49:33 -0800178 public static native String[][] getStackTrace(Thread thread, int start, int max);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700179}