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 TestVerifyJCStress
  26  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
  27  * @key gc
  28  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  *
  32  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  33  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  34  *      -XX:+ShenandoahDegeneratedGC -XX:+ShenandoahVerify
  35  *      TestVerifyJCStress
  36  *
  37  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  38  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  39  *      -XX:-ShenandoahDegeneratedGC -XX:+ShenandoahVerify
  40  *      TestVerifyJCStress
  41  */
  42 
  43 /*
  44  * @test TestVerifyJCStress
  45  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
  46  * @key gc
  47  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  48  * @modules java.base/jdk.internal.misc
  49  *          java.management
  50  *
  51  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  52  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
  53  *      -XX:+ShenandoahVerify -XX:+IgnoreUnrecognizedVMOptions -XX:+ShenandoahVerifyOptoBarriers
  54  *      TestVerifyJCStress
  55  *
  56  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  57  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  58  *      -XX:+ShenandoahVerify -XX:+IgnoreUnrecognizedVMOptions -XX:+ShenandoahVerifyOptoBarriers
  59  *      TestVerifyJCStress
  60  */
  61 
  62 /*
  63  * @test TestVerifyJCStress
  64  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
  65  * @key gc
  66  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  67  * @modules java.base/jdk.internal.misc
  68  *          java.management
  69  *
  70  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
  71  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
  72  *      -XX:+ShenandoahVerify -XX:+IgnoreUnrecognizedVMOptions -XX:+ShenandoahVerifyOptoBarriers
  73  *      TestVerifyJCStress
  74  */
  75 
  76 import java.util.*;
  77 import java.util.concurrent.*;
  78 import java.util.concurrent.locks.*;
  79 
  80 public class TestVerifyJCStress {
  81 
  82     public static void main(String[] args) throws Exception {
  83         ExecutorService service = Executors.newFixedThreadPool(
  84                 2,
  85                 r -> {
  86                     Thread t = new Thread(r);
  87                     t.setDaemon(true);
  88                     return t;
  89                 }
  90         );
  91 
  92         for (int c = 0; c < 10000; c++) {
  93             final Test[] tests = new Test[10000];
  94             for (int t = 0; t < tests.length; t++) {
  95                 tests[t] = new Test();
  96             }
  97 
  98             Future<?> f1 = service.submit(() -> {
  99                 IntResult2 r = new IntResult2();
 100                 for (Test test : tests) {
 101                     test.RL_Us(r);
 102                 }
 103             });
 104             Future<?> f2 = service.submit(() -> {
 105                 for (Test test : tests) {
 106                     test.WLI_Us();
 107                 }
 108             });
 109 
 110             f1.get();
 111             f2.get();
 112         }
 113     }
 114 
 115     public static class IntResult2 {
 116         int r1, r2;
 117     }
 118 
 119     public static class Test {
 120         final StampedLock lock = new StampedLock();
 121 
 122         int x, y;
 123 
 124         public void RL_Us(IntResult2 r) {
 125             StampedLock lock = this.lock;
 126             long stamp = lock.readLock();
 127             r.r1 = x;
 128             r.r2 = y;
 129             lock.unlock(stamp);
 130         }
 131 
 132         public void WLI_Us() {
 133             try {
 134                 StampedLock lock = this.lock;
 135                 long stamp = lock.writeLockInterruptibly();
 136                 x = 1;
 137                 y = 2;
 138                 lock.unlock(stamp);
 139             } catch (InterruptedException e) {
 140                 throw new RuntimeException(e);
 141             }
 142         }
 143     }
 144 
 145 }