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