1 /* 2 * Copyright (c) 2007, 2012, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 */ 25 package org.graalvm.compiler.jtt.threads; 26 27 import org.graalvm.compiler.core.common.CancellationBailoutException; 28 import org.graalvm.compiler.debug.DebugContext; 29 import org.graalvm.compiler.jtt.JTTTest; 30 import org.graalvm.compiler.nodes.Cancellable; 31 import org.junit.Rule; 32 import org.junit.Test; 33 import org.junit.rules.DisableOnDebug; 34 import org.junit.rules.TestRule; 35 import org.junit.rules.Timeout; 36 37 import jdk.vm.ci.meta.ResolvedJavaMethod; 38 39 public class Object_wait03 extends JTTTest { 40 41 /** 42 * Timeout for compilation. 43 */ 44 static final long COMPILATION_TIMEOUT_MS = 15_000; 45 46 /** 47 * Total timeout for compilation and execution of compiled code. 48 */ 49 static final long TIMEOUT_MS = COMPILATION_TIMEOUT_MS * 2; 50 51 @Rule public TestRule timeout = new DisableOnDebug(Timeout.millis(TIMEOUT_MS)); 52 53 private static class TestClass implements Runnable { 54 @Override 55 public void run() { 56 try { 57 Thread.sleep(sleep); 58 } catch (InterruptedException ex) { 59 60 } 61 synchronized (object) { 62 done = true; 63 object.notifyAll(); 64 } 65 } 66 } 67 68 static volatile boolean done; 69 static final Object object = new Object(); 70 static int sleep; 71 72 public static boolean test(int i) throws InterruptedException { 73 done = false; 74 sleep = i * 200; 75 synchronized (object) { 76 new Thread(new TestClass()).start(); 77 dowait(); 78 } 79 return done; 80 } 81 82 private static void dowait() throws InterruptedException { 83 synchronized (object) { 84 while (!done) { 85 object.wait(200); 86 } 87 } 88 } 89 90 static class CompilationTimeout extends Thread implements Cancellable { 91 boolean timedOut; 92 final long durationMS; 93 94 CompilationTimeout(long durationMS) { 95 super("CompilationTimeout-" + durationMS + "ms"); 96 this.durationMS = durationMS; 97 setDaemon(true); 98 start(); 99 } 100 101 @Override 102 public void run() { 103 try { 104 Thread.sleep(durationMS); 105 } catch (InterruptedException e) { 106 } 107 timedOut = true; 108 } 109 110 @Override 111 public boolean isCancelled() { 112 return timedOut; 113 } 114 } 115 116 @Override 117 protected Cancellable getCancellable(ResolvedJavaMethod method) { 118 return new CompilationTimeout(COMPILATION_TIMEOUT_MS); 119 } 120 121 private void run(int i) throws Throwable { 122 initializeForTimeout(); 123 try { 124 runTest("test", i); 125 } catch (CancellationBailoutException e) { 126 String message = String.format("Compilation cancelled after " + COMPILATION_TIMEOUT_MS + " ms"); 127 // For diagnosing expectedly long compilations (GR-3853) 128 DebugContext debug = getDebugContext(); 129 debug.forceDump(lastCompiledGraph, message); 130 throw new AssertionError(message, e); 131 } 132 } 133 134 @Test 135 public void run0() throws Throwable { 136 run(0); 137 } 138 139 @Test 140 public void run1() throws Throwable { 141 run(1); 142 } 143 144 @Test 145 public void run2() throws Throwable { 146 run(2); 147 } 148 }