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