1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.test.lib.jfr;
  27 
  28 import jdk.test.lib.Asserts;
  29 
  30 public class RecurseThread extends Thread {
  31 
  32     public int totalDepth;
  33     public long dummy = 0; // Just to make sure the optimizer does not remove the test code.
  34     private volatile boolean timeToQuit = false;
  35     private volatile boolean isInRunLoop = false;
  36 
  37     public RecurseThread(int totalDepth) {
  38         this.totalDepth = totalDepth;
  39     }
  40 
  41     @Override
  42     public void run() {
  43         // totalDepth includes functions run() and recurse() and runloop().
  44         // Remove 3 from totalDepth when recursing.
  45         final int minDepth = 3;
  46         Asserts.assertGreaterThanOrEqual(totalDepth, minDepth, "totalDepth too small");
  47         int recurseDepth = totalDepth - minDepth;
  48 
  49         // We want the last function before runloop() to be recurseA().
  50         boolean startWithRecurseA = (totalDepth % 2) != 0;
  51         dummy = startWithRecurseA ? recurseA(recurseDepth) : recurseB(recurseDepth);
  52     }
  53 
  54     public void quit() {
  55         timeToQuit = true;
  56     }
  57 
  58     public boolean isInRunLoop() {
  59         return isInRunLoop;
  60     }
  61 
  62     private long recurseA(int depth) {
  63         if (depth == 0) {
  64             return recurseEnd();
  65         } else {
  66             return recurseB(depth - 1);
  67         }
  68     }
  69 
  70     private long recurseB(int depth) {
  71         if (depth == 0) {
  72             return recurseEnd();
  73         } else {
  74             return recurseA(depth - 1);
  75         }
  76     }
  77 
  78     // Test expects this function to be at the top of the stack.
  79     // We should not call other functions from here.
  80     private long recurseEnd() {
  81         isInRunLoop = true;
  82         long[] dummyTable = new long[] { 0, 2, 4, 8, 16 };
  83         long dummyTotal = 0;
  84         while (!timeToQuit) {
  85             dummyTotal = 0;
  86             for (int i = 0; i < 5; ++i) {
  87                 dummyTotal += dummyTable[i];
  88             }
  89         }
  90         return dummyTotal;
  91     }
  92 
  93 }