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