1 /*
  2  * Copyright (c) 2003, 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 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  * @library /test/lib
 44  * @compile monitorwaited01.java
 45  * @run main/othervm/native -agentlib:monitorwaited01 monitorwaited01 platform
 46  * @run main/othervm/native -agentlib:monitorwaited01 monitorwaited01 virtual
 47  */
 48 
 49 
 50 
 51 public class monitorwaited01 extends DebugeeClass {
 52 
 53     static {
 54         loadLibrary("monitorwaited01");
 55     }
 56 
 57     public static void main(String args[]) {
 58         boolean isVirtual = "virtual".equals(args[0]);
 59         int result = new monitorwaited01().runIt(isVirtual);
 60         if (result != 0) {
 61             throw new RuntimeException("Unexpected status: " + result);
 62         }
 63     }
 64 
 65     static long timeout =  60000; // milliseconds
 66 
 67 
 68     // run debuggee
 69     public int runIt(boolean isVirtual) {
 70         int status = DebugeeClass.TEST_PASSED;
 71         System.out.println("Timeout = " + timeout + " msc.");
 72 
 73         monitorwaited01Task task = new monitorwaited01Task();
 74         Thread.Builder builder;
 75         if (isVirtual) {
 76             builder = Thread.ofVirtual();
 77         } else {
 78             builder = Thread.ofPlatform();
 79         }
 80         Thread thread = builder.name("Debuggee Thread").unstarted(task);
 81         setExpected(task.waitingMonitor, thread);
 82 
 83         // run thread
 84         try {
 85             // start thread
 86             synchronized (task.startingMonitor) {
 87                 thread.start();
 88                 task.startingMonitor.wait(timeout);
 89             }
 90         } catch (InterruptedException e) {
 91             throw new Failure(e);
 92         }
 93 
 94         Thread.yield();
 95         System.out.println("Thread started");
 96 
 97         synchronized (task.waitingMonitor) {
 98             task.waitingMonitor.notify();
 99         }
100 
101         // wait for thread finish
102         try {
103             thread.join(timeout);
104         } catch (InterruptedException e) {
105             throw new Failure(e);
106         }
107 
108         System.out.println("Sync: thread finished");
109         status = checkStatus(status);
110 
111         return status;
112     }
113 
114     private native void setExpected(Object monitor, Object thread);
115 }
116 
117 /* =================================================================== */
118 
119 class monitorwaited01Task implements Runnable {
120     public Object startingMonitor = new Object();
121     public Object waitingMonitor = new Object();
122 
123     public void run() {
124         synchronized (waitingMonitor) {
125 
126             monitorwaited01.checkStatus(DebugeeClass.TEST_PASSED);
127 
128             // notify about starting
129             synchronized (startingMonitor) {
130                 startingMonitor.notify();
131             }
132 
133             // wait until main thread notify
134             try {
135                 waitingMonitor.wait(monitorwaited01.timeout);
136             } catch (InterruptedException e) {
137                 throw new RuntimeException(e);
138             }
139         }
140     }
141 }