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