1 /*
   2  * Copyright (c) 2017 Red Hat, Inc. and/or its affiliates.
   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:+UseShenandoahGC -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  31  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyStrictOopOperations
  32  *                    -XX:ShenandoahGCHeuristics=passive -XX:+ShenandoahDegeneratedGC
  33  *                    VerifyJCStressTest
  34  *
  35  * @run main/othervm  -XX:+UseShenandoahGC -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  36  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyStrictOopOperations
  37  *                    -XX:ShenandoahGCHeuristics=passive -XX:-ShenandoahDegeneratedGC
  38  *                    VerifyJCStressTest
  39  *
  40  * @run main/othervm  -XX:+UseShenandoahGC -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  41  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyStrictOopOperations -XX:+ShenandoahVerifyOptoBarriers
  42  *                    -XX:ShenandoahGCHeuristics=adaptive
  43  *                    VerifyJCStressTest
  44  *
  45  * @run main/othervm  -XX:+UseShenandoahGC -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  46  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyStrictOopOperations -XX:+ShenandoahVerifyOptoBarriers
  47  *                    -XX:ShenandoahGCHeuristics=static
  48  *                    VerifyJCStressTest
  49  */
  50 
  51 import java.util.*;
  52 import java.util.concurrent.*;
  53 import java.util.concurrent.locks.*;
  54 
  55 public class VerifyJCStressTest {
  56 
  57     public static void main(String[] args) throws Exception {
  58         ExecutorService service = Executors.newFixedThreadPool(
  59           2,
  60           r -> {
  61             Thread t = new Thread(r);
  62             t.setDaemon(true);
  63             return t;
  64           }
  65         );
  66 
  67         for (int c = 0; c < 10000; c++) {
  68           final Test[] tests = new Test[10000];
  69           for (int t = 0; t < tests.length; t++) {
  70             tests[t] = new Test();
  71           }
  72 
  73           Future<?> f1 = service.submit(() -> {
  74             IntResult2 r = new IntResult2();
  75             for (Test test : tests) {
  76               test.RL_Us(r);
  77             }
  78           });
  79           Future<?> f2 = service.submit(() -> {
  80             for (Test test : tests) {
  81               test.WLI_Us();
  82             }
  83           });
  84 
  85           f1.get();
  86           f2.get();
  87         }
  88     }
  89 
  90     public static class IntResult2 {
  91       int r1, r2;
  92     }
  93 
  94     public static class Test {
  95       final StampedLock lock = new StampedLock();
  96 
  97       int x, y;
  98 
  99       public void RL_Us(IntResult2 r) {
 100           StampedLock lock = this.lock;
 101           long stamp = lock.readLock();
 102           r.r1 = x;
 103           r.r2 = y;
 104           lock.unlock(stamp);
 105       }
 106 
 107       public void WLI_Us() {
 108           try {
 109               StampedLock lock = this.lock;
 110               long stamp = lock.writeLockInterruptibly();
 111               x = 1;
 112               y = 2;
 113               lock.unlock(stamp);
 114           } catch (InterruptedException e) {
 115               throw new RuntimeException(e);
 116           }
 117       }
 118     }
 119 
 120 }
 121