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 package nsk.jvmti.unit.functions;
25 
26 import java.io.PrintStream;
27 
28 public class rawmonitor {
29 
30     final static int JCK_STATUS_BASE = 95;
31     final static int THREADS_LIMIT = 1000;
32     final static String NAME_PREFIX = "rawmonitor-";
33     static int fail_id = 0;
34 
35     static {
36         try {
37             System.loadLibrary("rawmonitor");
38         } catch (UnsatisfiedLinkError ule) {
39             System.err.println("Could not load rawmonitor library");
40             System.err.println("java.library.path:"
41                 + System.getProperty("java.library.path"));
42             throw ule;
43         }
44     }
45 
GetResult()46     native static int GetResult();
CreateRawMonitor(int i)47     native static void CreateRawMonitor(int i);
RawMonitorEnter(int i)48     native static void RawMonitorEnter(int i);
RawMonitorExit(int i)49     native static void RawMonitorExit(int i);
RawMonitorWait(int i)50     native static void RawMonitorWait(int i);
51 
52 
53     static volatile int thrCount = 0;
54 
main(String args[])55     public static void main(String args[]) {
56         args = nsk.share.jvmti.JVMTITest.commonInit(args);
57 
58         // produce JCK-like exit status.
59         System.exit(run(args, System.out) + JCK_STATUS_BASE);
60     }
61 
run(String args[], PrintStream out)62     public static int run(String args[], PrintStream out) {
63         CreateRawMonitor(1);
64 
65         TestThread t = new TestThread(NAME_PREFIX + thrCount);
66         t.start();
67 
68         // Just to test raw monitor during onload.
69         // Created and entered in onload phase. Release it now.
70         RawMonitorExit(0);
71 
72         try {
73             t.join();
74         } catch (InterruptedException e) {
75             throw new Error("Unexpected: " + e);
76         }
77         return GetResult() + fail_id;
78     }
79 
80     static class TestThread extends Thread {
81         static int counter=0;
TestThread(String name)82         public TestThread(String name) {
83             super(name);
84         }
run()85         public void run() {
86             thrCount++;
87             if (thrCount < THREADS_LIMIT) {
88                 TestThread t = new TestThread(NAME_PREFIX + thrCount);
89                 t.start();
90 
91                 for (int i=0; i < 100; i++) {
92                     RawMonitorEnter(0);
93                     int tst = counter;
94                //   System.out.print(".");
95                     counter++;
96                     if (counter != tst+1) {
97                         System.out.println("Monitor Enter is not working");
98                         fail_id = 4;
99                     }
100 
101               //    System.out.println(counter);
102                     RawMonitorExit(0);
103                 }
104 
105                 try {
106                     t.join();
107                 } catch (InterruptedException e) {
108                     throw new Error("Unexpected: " + e);
109                 }
110             }
111         }
112     }
113 }
114