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 * @test 26 * @summary Test parking when pinned and emitting the JFR VirtualThreadPinnedEvent throws 27 * @modules java.base/jdk.internal.event 28 * @compile/module=java.base jdk/internal/event/VirtualThreadPinnedEvent.java 29 * @run junit VirtualThreadPinnedEventThrows 30 */ 31 32 import java.lang.ref.Reference; 33 import java.util.concurrent.atomic.AtomicBoolean; 34 import java.util.concurrent.locks.LockSupport; 35 import jdk.internal.event.VirtualThreadPinnedEvent; 36 37 import org.junit.jupiter.api.Test; 38 import static org.junit.jupiter.api.Assertions.*; 39 40 class VirtualThreadPinnedEventThrows { 41 42 /** 43 * Test parking when pinned and creating the VirtualThreadPinnedEvent fails with OOME. 44 */ 45 @Test 46 void testVirtualThreadPinnedEventCreateThrows() throws Exception { 47 VirtualThreadPinnedEvent.setCreateThrows(true); 48 try { 49 testParkWhenPinned(); 50 } finally { 51 VirtualThreadPinnedEvent.setCreateThrows(false); 52 } 53 } 54 55 /** 56 * Test parking when pinned and VirtualThreadPinnedEvent.begin fails with OOME. 57 */ 58 @Test 59 void testVirtualThreadPinnedEventBeginThrows() throws Exception { 60 VirtualThreadPinnedEvent.setBeginThrows(true); 61 try { 62 testParkWhenPinned(); 63 } finally { 64 VirtualThreadPinnedEvent.setBeginThrows(false); 65 } 66 } 67 68 /** 69 * Test parking when pinned and VirtualThreadPinnedEvent.commit fails with OOME. 70 */ 71 @Test 72 void testVirtualThreadPinnedEventCommitThrows() throws Exception { 73 VirtualThreadPinnedEvent.setCommitThrows(true); 74 try { 75 testParkWhenPinned(); 76 } finally { 77 VirtualThreadPinnedEvent.setCommitThrows(false); 78 } 79 } 80 81 /** 82 * Test parking a virtual thread when pinned. 83 */ 84 private void testParkWhenPinned() throws Exception { 85 Object lock = new Object(); 86 try { 87 var completed = new AtomicBoolean(); 88 Thread thread = Thread.startVirtualThread(() -> { 89 synchronized (lock) { 90 LockSupport.park(); 91 completed.set(true); 92 } 93 }); 94 95 // wait for thread to park 96 Thread.State state; 97 while ((state = thread.getState()) != Thread.State.WAITING) { 98 assertTrue(state != Thread.State.TERMINATED); 99 Thread.sleep(10); 100 } 101 102 // unpark and check that thread completed without exception 103 LockSupport.unpark(thread); 104 thread.join(); 105 assertTrue(completed.get()); 106 } finally { 107 Reference.reachabilityFence(lock); 108 } 109 } 110 }