1 /* 2 * Copyright (c) 2014, 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 rtm; 26 27 import com.oracle.java.testlibrary.Utils; 28 import sun.misc.Unsafe; 29 30 import java.util.concurrent.BrokenBarrierException; 31 import java.util.concurrent.CyclicBarrier; 32 33 /** 34 * Test case for busy lock scenario. 35 * One thread enters the monitor and sleep for a while. 36 * Another thread is blocked on the same monitor. 37 */ 38 public class BusyLock implements CompilableTest, Runnable { 39 private static final int DEFAULT_TIMEOUT = 1000; 40 private final CyclicBarrier barrier; 41 42 // Following field have to be static in order to avoid escape analysis. 43 @SuppressWarnings("UnsuedDeclaration") 44 private static int field = 0; 45 private static final Unsafe UNSAFE = Utils.getUnsafe(); 46 protected final Object monitor; 47 protected final int timeout; 48 49 public BusyLock() { 50 this(BusyLock.DEFAULT_TIMEOUT); 51 } 52 53 public BusyLock(int timeout) { 54 this.timeout = timeout; 55 this.monitor = new Object(); 56 this.barrier = new CyclicBarrier(2); 57 } 58 59 @Override 60 public void run() { 61 try { 62 // wait until forceAbort leave monitor 63 barrier.await(); 64 if (UNSAFE.tryMonitorEnter(monitor)) { 65 try { 66 barrier.await(); 67 Thread.sleep(timeout); 68 } finally { 69 UNSAFE.monitorExit(monitor); 70 } 71 } else { 72 throw new RuntimeException("Monitor should be entered by " + 73 "::run() first."); 74 } 75 } catch (InterruptedException | BrokenBarrierException e) { 76 throw new RuntimeException("Synchronization error happened.", e); 77 } 78 } 79 80 public void syncAndTest() { 81 try { 82 barrier.await(); 83 // wait until monitor is locked by a ::run method 84 barrier.await(); 85 } catch (InterruptedException | BrokenBarrierException e) { 86 throw new RuntimeException("Synchronization error happened.", e); 87 } 88 test(); 89 } 90 91 public void test() { 92 synchronized(monitor) { 93 BusyLock.field++; 94 } 95 } 96 97 @Override 98 public String getMethodWithLockName() { 99 return this.getClass().getName() + "::test"; 100 } 101 102 @Override 103 public String[] getMethodsToCompileNames() { 104 return new String[] { getMethodWithLockName() }; 105 } 106 107 /** 108 * Usage: 109 * BusyLock [ <inflate monitor> [ <timeout> ] ] 110 * 111 * Default values are: 112 * <ul> 113 * <li>inflate monitor = {@code true}</li> 114 * <li>timeout = {@code BusyLock.DEFAULT_TIMEOUT}</li> 115 * </ul> 116 */ 117 public static void main(String args[]) throws Exception { 118 int timeoutValue = BusyLock.DEFAULT_TIMEOUT; 119 boolean inflateMonitor = true; 120 121 if (args.length > 0 ) { 122 inflateMonitor = Boolean.valueOf(args[0]); 123 124 if (args.length > 1) { 125 timeoutValue = Integer.valueOf(args[1]); 126 } 127 } 128 129 BusyLock busyLock = new BusyLock(timeoutValue); 130 131 if (inflateMonitor) { 132 AbortProvoker.inflateMonitor(busyLock.monitor); 133 } 134 135 Thread t = new Thread(busyLock); 136 t.start(); 137 busyLock.syncAndTest(); 138 t.join(); 139 } 140 }