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