1 /*
  2  * Copyright (c) 2003, 2018, 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  *
 27  * @summary converted from VM Testbase nsk/jvmti/GetCurrentContendedMonitor/contmon002.
 28  * VM Testbase keywords: [quick, jpda, jvmti, noras]
 29  * VM Testbase readme:
 30  * DESCRIPTION
 31  *     The test checks if JVMTI function GetCurrentContendedMonitor returns
 32  *     NULL when there is no current contended monitor.
 33  *     Failing criteria for the test are:
 34  *       - value returned by GetCurrentContendedMonitor is not NULL;
 35  *       - failures of used JVMTI functions.
 36  * COMMENTS
 37  *     Fixed according to the bug 4509016.
 38  *     Ported from JVMDI.
 39  *     Fixed according to 4925857 bug:
 40  *       - rearranged synchronization of tested thread
 41  *       - enhanced descripton
 42  *
 43  * @library /test/lib
 44  * @compile --enable-preview -source ${jdk.version} contmon02.java
 45  * @run main/othervm/native --enable-preview -agentlib:contmon02 contmon02
 46  */
 47 
 48 public class contmon02 {
 49 
 50     native static void checkMonitor(int point, Thread thr);
 51 
 52     static {
 53         System.loadLibrary("contmon02");
 54     }
 55 
 56     public static boolean startingBarrier = true;
 57 
 58     public static void doSleep() {
 59         try {
 60             Thread.sleep(10);
 61         } catch (Exception e) {
 62             throw new Error("Unexpected " + e);
 63         }
 64     }
 65 
 66     public static void main(String argv[]) {
 67         test(true);
 68         test(false);
 69     }
 70 
 71     public static void test(boolean isVirtual) {
 72         checkMonitor(1, Thread.currentThread());
 73 
 74         contmon02Task task = new contmon02Task();
 75         Thread thread = isVirtual ? Thread.ofVirtual().start(task) : Thread.ofPlatform().start(task);
 76 
 77         while (startingBarrier) {
 78             doSleep();
 79         }
 80         checkMonitor(2, thread);
 81         task.letItGo();
 82         try {
 83             thread.join();
 84         } catch (InterruptedException e) {
 85             throw new Error("Unexpected " + e);
 86         }
 87     }
 88 }
 89 
 90 class contmon02Task implements Runnable {
 91     private volatile boolean flag = true;
 92 
 93     private synchronized void meth() {
 94         contmon02.startingBarrier = false;
 95         int i = 0;
 96         int n = 1000;
 97         while (flag) {
 98             if (n <= 0) {
 99                 n = 1000;
100             }
101             if (i > n) {
102                 i = 0;
103                 n--;
104             }
105             i++;
106         }
107     }
108 
109     public void run() {
110         meth();
111     }
112 
113     public void letItGo() {
114         flag = false;
115     }
116 }