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 unblocking a virtual thread waiting to enter a monitor 27 * @key randomness 28 * @modules java.base/java.lang:+open 29 * @library /test/lib 30 * @run junit MonitorUnblocking 31 */ 32 33 import java.util.concurrent.CountDownLatch; 34 import java.util.concurrent.ThreadLocalRandom; 35 36 import jdk.test.lib.thread.VThreadRunner; 37 import org.junit.jupiter.api.RepeatedTest; 38 import static org.junit.jupiter.api.Assertions.*; 39 40 class MonitorUnblocking { 41 42 /** 43 * Test unblocking a virtual thread waiting to enter a monitor held by a platform thread. 44 */ 45 @RepeatedTest(50) 46 void testUnblocking() throws Exception { 47 var lock = new Object(); 48 var started = new CountDownLatch(1); 49 var vthread = Thread.ofVirtual().unstarted(() -> { 50 started.countDown(); 51 synchronized (lock) { 52 // do nothing 53 } 54 }); 55 try { 56 synchronized (lock) { 57 vthread.start(); 58 started.await(); 59 60 // random delay before exiting monitor 61 switch (ThreadLocalRandom.current().nextInt(4)) { 62 case 0 -> { /* no delay */} 63 case 1 -> Thread.onSpinWait(); 64 case 2 -> Thread.yield(); 65 case 3 -> await(vthread, Thread.State.BLOCKED); 66 default -> fail(); 67 } 68 } 69 } finally { 70 vthread.join(); 71 } 72 } 73 74 /** 75 * Test unblocking a virtual thread waiting to enter a monitor held by another 76 * virtual thread. 77 */ 78 @RepeatedTest(50) 79 void testUnblocking2() throws Exception { 80 // need at least two carrier threads 81 int previousParallelism = VThreadRunner.ensureParallelism(2); 82 try { 83 VThreadRunner.run(this::testUnblocking); 84 } finally { 85 VThreadRunner.setParallelism(previousParallelism); 86 } 87 } 88 89 private void await(Thread thread, Thread.State expectedState) { 90 Thread.State state = thread.getState(); 91 while (state != expectedState) { 92 assertTrue(state != Thread.State.TERMINATED, "Thread has terminated"); 93 Thread.yield(); 94 state = thread.getState(); 95 } 96 } 97 }