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