1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. 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 HandshakeWalkSuspendExitTest
  27  * @summary This test tries to stress the handshakes with new and exiting threads while suspending them.
  28  * @library /testlibrary /test/lib
  29  * @build HandshakeWalkSuspendExitTest
  30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI HandshakeWalkSuspendExitTest
  33  */
  34 
  35 import jdk.test.lib.Asserts;
  36 import sun.hotspot.WhiteBox;
  37 
  38 public class HandshakeWalkSuspendExitTest  implements Runnable {
  39 
  40     static final int _test_threads = 8;
  41     static final int _test_exit_threads = 128;
  42     static Thread[] _threads = new Thread[_test_threads];
  43     static volatile boolean exit_now = false;
  44     static java.util.concurrent.Semaphore _sem = new java.util.concurrent.Semaphore(0);
  45 
  46     @Override
  47     public void run() {
  48         WhiteBox wb = WhiteBox.getWhiteBox();
  49         while (!exit_now) {
  50             _sem.release();
  51             // We only suspend threads on even index and not ourself.
  52             // Otherwise we can accidentially suspend all threads.
  53             for (int i = 0; i < _threads.length; i += 2) {
  54                 wb.handshakeWalkStack(null /* ignored */, true /* stackwalk all threads */);
  55                 if (Thread.currentThread() != _threads[i]) {
  56                     _threads[i].suspend();
  57                     _threads[i].resume();
  58                 }
  59             }
  60             for (int i = 0; i < _threads.length; i += 2) {
  61                 wb.handshakeWalkStack(_threads[i] /* thread to stackwalk */, false /* stackwalk one thread */);
  62                 if (Thread.currentThread() != _threads[i]) {
  63                     _threads[i].suspend();
  64                     _threads[i].resume();
  65                 }
  66             }
  67         }
  68     }
  69 
  70     public static void main(String... args) throws Exception {
  71         HandshakeWalkSuspendExitTest test = new HandshakeWalkSuspendExitTest();
  72 
  73         for (int i = 0; i < _threads.length; i++) {
  74             _threads[i] = new Thread(test);
  75             _threads[i].start();
  76         }
  77         for (int i = 0; i < _test_threads; i++) {
  78             _sem.acquire();
  79         }
  80         Thread[] exit_threads = new Thread[_test_exit_threads];
  81         for (int i = 0; i < _test_exit_threads; i++) {
  82             exit_threads[i] = new Thread(new Runnable() { public void run() {} });
  83             exit_threads[i].start();
  84         }
  85         exit_now = true;
  86         for (int i = 0; i < _threads.length; i++) {
  87             _threads[i].join();
  88         }
  89         for (int i = 0; i < exit_threads.length; i++) {
  90             exit_threads[i].join();
  91         }
  92     }
  93 }