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  * @bug 8284161 8286788
27  * @summary Test that a carrier thread waits on a virtual thread
28  * @requires vm.continuations
29  * @modules java.base/java.lang:+open
30  * @run junit CarrierThreadWaits
31  */
32 
33 /**
34  * @test
35  * @requires vm.continuations & vm.debug
36  * @modules java.base/java.lang:+open
37  * @run junit/othervm -XX:LockingMode=0 CarrierThreadWaits
38  */
39 
40 import java.lang.management.LockInfo;
41 import java.lang.management.ManagementFactory;
42 import java.lang.management.ThreadInfo;
43 import java.util.concurrent.CountDownLatch;
44 import java.util.concurrent.Executor;
45 import java.util.concurrent.ForkJoinPool;
46 import java.util.concurrent.atomic.AtomicBoolean;
47 import java.util.concurrent.atomic.AtomicReference;
48 
49 import org.junit.jupiter.api.Test;
50 import static org.junit.jupiter.api.Assertions.*;
51 
52 class CarrierThreadWaits {
53 
54     @Test
55     void testCarrierThreadWaiting() throws Exception {
56         try (ForkJoinPool pool = new ForkJoinPool(1)) {
57             var carrierRef = new AtomicReference<Thread>();
58             Executor scheduler = task -> {
59                 pool.submit(() -> {
60                     carrierRef.set(Thread.currentThread());
61                     task.run();
62                 });
63             };
64 
65             // start a virtual thread that spins and remains mounted until "done"
66             var latch = new CountDownLatch(1);
67             var done = new AtomicBoolean();
68             Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
69             Thread vthread = builder.start(() -> {
70                 latch.countDown();
71                 while (!done.get()) {
72                     Thread.onSpinWait();
73                 }
74             });
75 
76             // wait for virtual thread to execute
77             latch.await();
78 
79             try {
80                 long carrierId = carrierRef.get().threadId();
81                 long vthreadId = vthread.threadId();
82 
83                 // carrier thread should be on WAITING on virtual thread
84                 ThreadInfo ti = ManagementFactory.getThreadMXBean().getThreadInfo(carrierId);
85                 assertTrue(ti.getThreadState() == Thread.State.WAITING);
86                 assertEquals(vthread.getClass().getName(), ti.getLockInfo().getClassName());
87                 assertTrue(ti.getLockInfo().getIdentityHashCode() == System.identityHashCode(vthread));
88                 assertTrue(ti.getLockOwnerId() == vthreadId);
89 
90             } finally {
91                 done.set(true);
92             }
93         }
94     }
95 
96 }