< prev index next >

test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorStackDepthInfo/GetOwnedMonitorStackDepthInfoTest.java

Print this page

 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 /**
 26  * @test
 27  * @bug 8153629
 28  * @summary Need to cover JVMTI's GetOwnedMonitorStackDepthInfo function
 29  * @requires vm.jvmti
 30  * @compile GetOwnedMonitorStackDepthInfoTest.java
 31  * @run main/othervm/native -agentlib:GetOwnedMonitorStackDepthInfoTest GetOwnedMonitorStackDepthInfoTest
 32  */
 33 
 34 
 35 public class GetOwnedMonitorStackDepthInfoTest {
 36 
 37     static {
 38         try {
 39             System.loadLibrary("GetOwnedMonitorStackDepthInfoTest");
 40         } catch (UnsatisfiedLinkError ule) {
 41             System.err.println("Could not load GetOwnedMonitorStackDepthInfoTest library");
 42             System.err.println("java.library.path: "
 43                     + System.getProperty("java.library.path"));
 44             throw ule;
 45         }
 46     }
 47 
 48     private static native int verifyOwnedMonitors();
 49 
 50     private static volatile int results = -1;
 51 
 52 
 53     public static void main(String[] args) throws Exception {
 54 
 55         new GetOwnedMonitorStackDepthInfoTest().runTest();
 56 
 57     }
 58 
 59     public void runTest() throws Exception {

 60         final Object lock1 = new Lock1();
 61         Thread t1 = new Thread(() -> {
 62             synchronized (lock1) {
 63                 System.out.println("Thread in sync section 1: "
 64                         + Thread.currentThread().getName());
 65                 test1();
 66             }
 67         });
 68 
 69         t1.start();
 70         t1.join();
 71 
 72         if (results != 0) {
 73             throw new RuntimeException("FAILED status returned from the agent");
 74         }
 75 
 76     }
 77 
 78     private synchronized void test1() {
 79         test2();
 80     }
 81 
 82     private void test2() {
 83         Object lock2 = new Lock2();
 84         synchronized (lock2) {
 85             System.out.println("Thread in sync section 2: "
 86                     + Thread.currentThread().getName());
 87             results = verifyOwnedMonitors();
 88         }
 89 
 90     }
 91 
 92     private static class Lock1 {
 93 
 94     }
 95 
 96     private static class Lock2 {
 97 
 98     }
 99 }
100 

 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 /**
 26  * @test
 27  * @bug 8153629
 28  * @summary Need to cover JVMTI's GetOwnedMonitorStackDepthInfo function
 29  * @requires vm.jvmti
 30  * @compile --enable-preview -source ${jdk.version} GetOwnedMonitorStackDepthInfoTest.java
 31  * @run main/othervm/native --enable-preview -agentlib:GetOwnedMonitorStackDepthInfoTest GetOwnedMonitorStackDepthInfoTest
 32  */
 33 
 34 
 35 public class GetOwnedMonitorStackDepthInfoTest {
 36 
 37     static {
 38         try {
 39             System.loadLibrary("GetOwnedMonitorStackDepthInfoTest");
 40         } catch (UnsatisfiedLinkError ule) {
 41             System.err.println("Could not load GetOwnedMonitorStackDepthInfoTest library");
 42             System.err.println("java.library.path: "
 43                     + System.getProperty("java.library.path"));
 44             throw ule;
 45         }
 46     }
 47 
 48     private static native int verifyOwnedMonitors();
 49 
 50     private static volatile int results = -1;
 51 
 52 
 53     public static void main(String[] args) throws Exception {
 54         new GetOwnedMonitorStackDepthInfoTest().runTest(true);
 55         new GetOwnedMonitorStackDepthInfoTest().runTest(false);

 56     }
 57 
 58     public void runTest(boolean isVirtual) throws Exception {
 59         var threadFactory = isVirtual ? Thread.ofVirtual().factory() : Thread.ofPlatform().factory();
 60         final Object lock1 = new Lock1();
 61         Thread t1 = threadFactory.newThread(() -> {
 62             synchronized (lock1) {
 63                 System.out.println("Thread in sync section 1: "
 64                         + Thread.currentThread().getName());
 65                 test1();
 66             }
 67         });
 68 
 69         t1.start();
 70         t1.join();
 71 
 72         if (results != 0) {
 73             throw new RuntimeException("FAILED status returned from the agent");
 74         }
 75 
 76     }
 77 
 78     private synchronized void test1() {
 79         test2();
 80     }
 81 
 82     private void test2() {
 83         Object lock2 = new Lock2();
 84         synchronized (lock2) {
 85             System.out.println("Thread in sync section 2: "
 86                     + Thread.currentThread().getName());
 87             results = verifyOwnedMonitors();
 88         }
 89 
 90     }
 91 
 92     private static class Lock1 {
 93 
 94     }
 95 
 96     private static class Lock2 {
 97 
 98     }
 99 }

< prev index next >