1 /*
  2  * Copyright (c) 2003, 2024, 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 import jdk.test.lib.jvmti.DebugeeClass;
 25 
 26 /*
 27  * @test
 28  *
 29  * @summary converted from VM Testbase nsk/jvmti/MonitorWaited/monitorwaited001.
 30  * VM Testbase keywords: [quick, jpda, jvmti, noras]
 31  * VM Testbase readme:
 32  * DESCRIPTION
 33  *     The test exercises JVMTI event callback function
 34  *         MonitorWaited(jni, thread, object, timed_out).
 35  *     The test checks if the thread, object, and timed_out parameters of
 36  *     the function contain expected values for callback when a thread finishes
 37  *     waiting on an object.
 38  * COMMENTS
 39  *     The test updated to match new JVMTI spec 0.2.90:
 40  *     - change signature of agentProc function
 41  *       and save JNIEnv pointer now passed as argument.
 42  *
 43  * @requires vm.continuations
 44  * @library /test/lib
 45  * @compile monitorwaited01.java
 46  * @run main/othervm/native -agentlib:monitorwaited01 monitorwaited01 platform
 47  * @run main/othervm/native -agentlib:monitorwaited01 monitorwaited01 virtual
 48  */
 49 
 50 
 51 
 52 public class monitorwaited01 extends DebugeeClass {
 53 
 54     static {
 55         loadLibrary("monitorwaited01");
 56     }
 57 
 58     public static void main(String args[]) {
 59         boolean isVirtual = "virtual".equals(args[0]);
 60         int result = new monitorwaited01().runIt(isVirtual);
 61         if (result != 0) {
 62             throw new RuntimeException("Unexpected status: " + result);
 63         }
 64     }
 65 
 66     static long timeout =  60000; // milliseconds
 67 
 68 
 69     // run debuggee
 70     public int runIt(boolean isVirtual) {
 71         int status = DebugeeClass.TEST_PASSED;
 72         System.out.println("Timeout = " + timeout + " msc.");
 73 
 74         monitorwaited01Task task = new monitorwaited01Task();
 75         Thread.Builder builder;
 76         if (isVirtual) {
 77             builder = Thread.ofVirtual();
 78         } else {
 79             builder = Thread.ofPlatform();
 80         }
 81         Thread thread = builder.name("Debuggee Thread").unstarted(task);
 82         setExpected(task.waitingMonitor, thread);
 83 
 84         // run thread
 85         try {
 86             // start thread
 87             synchronized (task.startingMonitor) {
 88                 thread.start();
 89                 task.startingMonitor.wait(timeout);
 90             }
 91         } catch (InterruptedException e) {
 92             throw new Failure(e);
 93         }
 94 
 95         Thread.yield();
 96         System.out.println("Thread started");
 97 
 98         synchronized (task.waitingMonitor) {
 99             task.waitingMonitor.notify();
100         }
101 
102         // wait for thread finish
103         try {
104             thread.join(timeout);
105         } catch (InterruptedException e) {
106             throw new Failure(e);
107         }
108 
109         System.out.println("Sync: thread finished");
110         status = checkStatus(status);
111 
112         return status;
113     }
114 
115     private native void setExpected(Object monitor, Object thread);
116 }
117 
118 /* =================================================================== */
119 
120 class monitorwaited01Task implements Runnable {
121     public Object startingMonitor = new Object();
122     public Object waitingMonitor = new Object();
123 
124     public void run() {
125         synchronized (waitingMonitor) {
126 
127             monitorwaited01.checkStatus(DebugeeClass.TEST_PASSED);
128 
129             // notify about starting
130             synchronized (startingMonitor) {
131                 startingMonitor.notify();
132             }
133 
134             // wait until main thread notify
135             try {
136                 waitingMonitor.wait(monitorwaited01.timeout);
137             } catch (InterruptedException e) {
138                 throw new RuntimeException(e);
139             }
140         }
141     }
142 }