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 id=default 26 * @summary Do not suspend virtual threads in a critical section. 27 * @bug 8311218 28 * @requires vm.continuations 29 * @library /testlibrary 30 * @run main/othervm SuspendWithInterruptLock 31 */ 32 33 /** 34 * @test id=xint 35 * @summary Do not suspend virtual threads in a critical section. 36 * @bug 8311218 37 * @requires vm.continuations 38 * @library /testlibrary 39 * @run main/othervm -Xint SuspendWithInterruptLock 40 */ 41 42 import jvmti.JVMTIUtils; 43 44 public class SuspendWithInterruptLock { 45 static volatile boolean done; 46 47 public static void main(String[] args) throws Exception { 48 Thread yielder = Thread.ofVirtual().name("yielder").start(() -> yielder()); 49 Thread stateReader = Thread.ofVirtual().name("stateReader").start(() -> stateReader(yielder)); 50 Thread suspender = new Thread(() -> suspender(stateReader)); 51 suspender.start(); 52 53 yielder.join(); 54 stateReader.join(); 55 suspender.join(); 56 } 57 58 static private void yielder() { 59 int iterations = 100_000; 60 while (iterations-- > 0) { 61 Thread.yield(); 62 } 63 done = true; 64 } 65 66 static private void stateReader(Thread target) { 67 while (!done) { 68 target.getState(); 69 } 70 } 71 72 static private void suspender(Thread target) { 73 while (!done) { 74 suspendThread(target); 75 sleep(1); 76 resumeThread(target); 77 // Allow progress 78 sleep(5); 79 } 80 } 81 82 static void suspendThread(Thread t) { 83 try { 84 JVMTIUtils.suspendThread(t); 85 } catch (JVMTIUtils.JvmtiException e) { 86 if (e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_ALIVE) { 87 throw e; 88 } 89 } 90 } 91 92 static void resumeThread(Thread t) { 93 try { 94 JVMTIUtils.resumeThread(t); 95 } catch (JVMTIUtils.JvmtiException e) { 96 if (e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_ALIVE) { 97 throw e; 98 } 99 } 100 } 101 102 static private void sleep(long millis) { 103 try { 104 Thread.sleep(millis); 105 } catch (InterruptedException e) {} 106 } 107 } 108