1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /*
  25  * @test VerifyJCStressTest
  26  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
  27  * @key gc
  28  * @modules java.base/jdk.internal.misc
  29  *          java.management
  30  * @run main/othervm  -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  31  *                    -XX:+UseShenandoahGC -Xmx1g -Xms1g
  32  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals
  33  *                    -XX:ShenandoahGCHeuristics=passive -XX:+ShenandoahDegeneratedGC
  34  *                    VerifyJCStressTest
  35  *
  36  * @run main/othervm  -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  37  *                    -XX:+UseShenandoahGC -Xmx1g -Xms1g
  38  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals
  39  *                    -XX:ShenandoahGCHeuristics=passive -XX:-ShenandoahDegeneratedGC
  40  *                    VerifyJCStressTest
  41  *
  42  * @run main/othervm  -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  43  *                    -XX:+UseShenandoahGC -Xmx1g -Xms1g
  44  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals -XX:+ShenandoahVerifyOptoBarriers
  45  *                    -XX:ShenandoahGCHeuristics=adaptive
  46  *                    VerifyJCStressTest
  47  *
  48  * @run main/othervm  -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  49  *                    -XX:+UseShenandoahGC -Xmx1g -Xms1g
  50  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals -XX:+ShenandoahVerifyOptoBarriers
  51  *                    -XX:ShenandoahGCHeuristics=static
  52  *                    VerifyJCStressTest
  53  *
  54  * @run main/othervm  -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  55  *                    -XX:+UseShenandoahGC -Xmx1g -Xms1g
  56  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals -XX:+ShenandoahVerifyOptoBarriers
  57  *                    -XX:ShenandoahGCHeuristics=traversal
  58  *                    VerifyJCStressTest
  59  */
  60 
  61 import java.util.*;
  62 import java.util.concurrent.*;
  63 import java.util.concurrent.locks.*;
  64 
  65 public class VerifyJCStressTest {
  66 
  67     public static void main(String[] args) throws Exception {
  68         ExecutorService service = Executors.newFixedThreadPool(
  69           2,
  70           r -> {
  71             Thread t = new Thread(r);
  72             t.setDaemon(true);
  73             return t;
  74           }
  75         );
  76 
  77         for (int c = 0; c < 10000; c++) {
  78           final Test[] tests = new Test[10000];
  79           for (int t = 0; t < tests.length; t++) {
  80             tests[t] = new Test();
  81           }
  82 
  83           Future<?> f1 = service.submit(() -> {
  84             IntResult2 r = new IntResult2();
  85             for (Test test : tests) {
  86               test.RL_Us(r);
  87             }
  88           });
  89           Future<?> f2 = service.submit(() -> {
  90             for (Test test : tests) {
  91               test.WLI_Us();
  92             }
  93           });
  94 
  95           f1.get();
  96           f2.get();
  97         }
  98     }
  99 
 100     public static class IntResult2 {
 101       int r1, r2;
 102     }
 103 
 104     public static class Test {
 105       final StampedLock lock = new StampedLock();
 106 
 107       int x, y;
 108 
 109       public void RL_Us(IntResult2 r) {
 110           StampedLock lock = this.lock;
 111           long stamp = lock.readLock();
 112           r.r1 = x;
 113           r.r2 = y;
 114           lock.unlock(stamp);
 115       }
 116 
 117       public void WLI_Us() {
 118           try {
 119               StampedLock lock = this.lock;
 120               long stamp = lock.writeLockInterruptibly();
 121               x = 1;
 122               y = 2;
 123               lock.unlock(stamp);
 124           } catch (InterruptedException e) {
 125               throw new RuntimeException(e);
 126           }
 127       }
 128     }
 129 
 130 }