1 /*
  2  * Copyright (c) 2022, 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  * @test
 26  * @summary Test Thread.getStackTrace to examine the stack trace of a virtual
 27  *     thread and its carrier
 28  * @requires vm.continuations
 29  * @modules java.base/java.lang:+open
 30  * @run main GetStackTrace
 31  */
 32 
 33 import java.util.Objects;
 34 import java.util.concurrent.BlockingQueue;
 35 import java.util.concurrent.Executor;
 36 import java.util.concurrent.LinkedTransferQueue;
 37 import java.util.stream.Stream;
 38 
 39 public class GetStackTrace {
 40 
 41     private static final Object LOCK = new Object();
 42 
 43     public static void main(String[] args) throws Exception {
 44         try (var scheduler = new Scheduler()) {
 45             Thread vthread = scheduler.startVirtualThread(() -> {
 46                 synchronized (LOCK) {
 47                     try {
 48                         LOCK.wait();
 49                     } catch (InterruptedException e) { }
 50                 }
 51             });
 52 
 53             try {
 54                 // wait for virtual thread to wait
 55                 while (vthread.getState() != Thread.State.WAITING) {
 56                     Thread.sleep(10);
 57                 }
 58 
 59                 // bottom-most frame of virtual thread should be VirtualThread.run
 60                 System.out.println(vthread);
 61                 StackTraceElement[] vthreadStack = vthread.getStackTrace();
 62                 Stream.of(vthreadStack).forEach(System.out::println);
 63                 assertEquals("run", vthreadStack[vthreadStack.length - 1].getMethodName());
 64 
 65                 System.out.println();
 66 
 67                 // top-most frame of carrier thread should be Continuation.run
 68                 // bottom-most frame of carrier thread should be Thread.run
 69                 var carrier = scheduler.thread();
 70                 System.out.println(carrier);
 71                 StackTraceElement[] carrierStack = carrier.getStackTrace();
 72                 Stream.of(carrierStack).forEach(System.out::println);
 73                 assertEquals("run", carrierStack[0].getMethodName());
 74                 assertEquals("run", carrierStack[carrierStack.length - 1].getMethodName());
 75             } finally {
 76                 vthread.interrupt();
 77             }
 78         }
 79     }
 80 
 81     /**
 82      * A scheduler with one thread.
 83      */
 84     private static class Scheduler implements AutoCloseable, Executor {
 85         private final BlockingQueue<Runnable> tasks = new LinkedTransferQueue<>();
 86         private final Thread thread;
 87         private volatile boolean done;
 88 
 89         Scheduler() {
 90             this.thread = Thread.ofPlatform().start(() -> {
 91                 try {
 92                     while (!done) {
 93                         Runnable task = tasks.take();
 94                         task.run();
 95                     }
 96                 } catch (InterruptedException e) { }
 97             });
 98         }
 99 
100         Thread thread() {
101             return thread;
102         }
103 
104         @Override
105         public void close() throws InterruptedException {
106             done = true;
107             thread.interrupt();
108             thread.join();
109         }
110 
111         @Override
112         public void execute(Runnable task) {
113             tasks.add(task);
114         }
115 
116         Thread startVirtualThread(Runnable task) {
117             return ThreadBuilders.virtualThreadBuilder(this).start(task);
118         }
119     }
120 
121     private static void assertTrue(boolean e) {
122         if (!e) throw new RuntimeException();
123     }
124 
125     private static void assertEquals(Object x, Object y) {
126         if (!Objects.equals(x, y))
127             throw new RuntimeException();
128     }
129 }