< prev index next >

hotspot/test/compiler/testlibrary/rtm/BusyLock.java

Print this page
rev 7654 : 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
Reviewed-by: dholmes
Contributed-by: paul.sandoz@oracle.com, filipp.zhinkin@gmail.com
   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


   1 /*
   2  * Copyright (c) 2014, 2015, 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 java.util.concurrent.BrokenBarrierException;
  28 import java.util.concurrent.CyclicBarrier;
  29 
  30 /**
  31  * Test case for busy lock scenario.
  32  * One thread enters the monitor and sleep for a while.
  33  * Another thread is blocked on the same monitor.
  34  */
  35 public class BusyLock implements CompilableTest, Runnable {
  36     private static final int DEFAULT_TIMEOUT = 1000;
  37     private final CyclicBarrier barrier;
  38 
  39     // Following field have to be static in order to avoid escape analysis.
  40     @SuppressWarnings("UnsuedDeclaration")
  41     private static int field = 0;

  42     protected final Object monitor;
  43     protected final int timeout;
  44 
  45     public BusyLock() {
  46         this(BusyLock.DEFAULT_TIMEOUT);
  47     }
  48 
  49     public BusyLock(int timeout) {
  50         this.timeout = timeout;
  51         this.monitor = new Object();
  52         this.barrier = new CyclicBarrier(2);
  53     }
  54 
  55     @Override
  56     public void run() {
  57         try {
  58             synchronized (monitor) {



  59                 barrier.await();
  60                 Thread.sleep(timeout);






  61             }
  62         } catch (InterruptedException | BrokenBarrierException e) {
  63             throw new RuntimeException("Synchronization error happened.", e);
  64         }
  65     }
  66 
  67     public void syncAndTest() {
  68         try {

  69             // wait until monitor is locked by a ::run method
  70             barrier.await();
  71         } catch (InterruptedException | BrokenBarrierException e) {
  72             throw new RuntimeException("Synchronization error happened.", e);
  73         }
  74         test();
  75     }
  76 
  77     public void test() {
  78         synchronized(monitor) {
  79             BusyLock.field++;
  80         }
  81     }
  82 
  83     @Override
  84     public String getMethodWithLockName() {
  85         return this.getClass().getName() + "::test";
  86     }
  87 
  88     @Override


< prev index next >