1 /*
 2  * Copyright (c) 2023, 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
27  * @bug 8317262
28  * @library /testlibrary /test/lib
29  * @build jdk.test.whitebox.WhiteBox
30  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
31  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+HandshakeALot -XX:GuaranteedSafepointInterval=1 TestStackWalk
32  */
33 
34 import jdk.test.lib.Asserts;
35 import jdk.test.whitebox.WhiteBox;
36 import java.util.concurrent.CountDownLatch;
37 
38 public class TestStackWalk {
39     static Thread worker1;
40     static Thread worker2;
41     static volatile boolean done;
42     static volatile int counter = 0;
43     static Object lock = new Object();
44 
45     public static void main(String... args) throws Exception {
46         worker1 = new Thread(() -> syncedWorker());
47         worker1.start();
48         worker2 = new Thread(() -> syncedWorker());
49         worker2.start();
50         Thread worker3 = new Thread(() -> stackWalker());
51         worker3.start();
52 
53         worker1.join();
54         worker2.join();
55         worker3.join();
56     }
57 
58     public static void syncedWorker() {
59         synchronized (lock) {
60             while (!done) {
61                 counter++;
62             }
63         }
64     }
65 
66     public static void stackWalker() {
67         // Suspend workers so the one looping waiting for "done"
68         // doesn't execute the handshake below, increasing the
69         // chances the VMThread will do it.
70         suspendWorkers();
71 
72         WhiteBox wb = WhiteBox.getWhiteBox();
73         long end = System.currentTimeMillis() + 20000;
74         while (end > System.currentTimeMillis()) {
75             wb.handshakeWalkStack(worker1, false /* all_threads */);
76             wb.handshakeWalkStack(worker2, false /* all_threads */);
77         }
78 
79         resumeWorkers();
80         done = true;
81     }
82 
83     static void suspendWorkers() {
84         worker1.suspend();
85         worker2.suspend();
86     }
87 
88     static void resumeWorkers() {
89         worker1.resume();
90         worker2.resume();
91     }
92 }