blob: 439fbaaf84130e45c5d495d0f15ec363d8b0f0cc [file] [log] [blame]
Andreas Gampe966de9e2017-01-12 20:51:02 -08001/*
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
17public class Recurse {
18 public static int foo(int x, int start, int max, ControlData data) {
19 bar(x, start, max, data);
20 return 0;
21 }
22
23 private static long bar(int x, int start, int max, ControlData data) {
24 baz(x, start, max, data);
25 return 0;
26 }
27
28 private static Object baz(int x, int start, int max, ControlData data) {
29 if (x == 0) {
30 printOrWait(start, max, data);
31 } else {
32 foo(x - 1, start, max, data);
33 }
34 return null;
35 }
36
37 private static void printOrWait(int start, int max, ControlData data) {
38 if (data == null) {
39 PrintThread.print(Thread.currentThread(), start, max);
40 } else {
41 if (data.waitFor != null) {
42 synchronized (data.waitFor) {
43 data.reached.countDown();
44 try {
45 data.waitFor.wait(); // Use wait() as it doesn't have a "hidden" Java call-graph.
46 } catch (Throwable t) {
47 throw new RuntimeException(t);
48 }
49 }
50 } else {
51 data.reached.countDown();
52 while (!data.stop) {
53 // Busy-loop.
54 }
55 }
56 }
57 }
58}