test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/CurrentContendedMonitor/curcontmonitor001a.java
changeset 50317 cf71bff5f533
equal deleted inserted replaced
50316:60ebcc705421 50317:cf71bff5f533
       
     1 /*
       
     2  * Copyright (c) 2001, 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.jdwp.ThreadReference.CurrentContendedMonitor;
       
    25 
       
    26 import nsk.share.*;
       
    27 import nsk.share.jpda.*;
       
    28 import nsk.share.jdwp.*;
       
    29 
       
    30 import java.io.*;
       
    31 
       
    32 /**
       
    33  * This class represents debuggee part in the test.
       
    34  */
       
    35 public class curcontmonitor001a {
       
    36 
       
    37     // name for the tested thread
       
    38     public static final String THREAD_NAME = "TestedThreadName";
       
    39     public static final String THREAD_FIELD_NAME = "thread";
       
    40     public static final String MONITOR_FIELD_NAME = "monitor";
       
    41 
       
    42     // frames count for tested thread in recursive method invokation
       
    43     public static final int FRAMES_COUNT = 10;
       
    44 
       
    45     // notification object to notify debuggee that thread is started
       
    46     private static Object threadStarting = new Object();
       
    47     // object which thread will wait for before being interruted
       
    48     private static Object threadWaiting = new Object();
       
    49 
       
    50     // scaffold objects
       
    51     private static volatile ArgumentHandler argumentHandler = null;
       
    52     private static volatile Log log = null;
       
    53 
       
    54     public static void main(String args[]) {
       
    55         curcontmonitor001a _curcontmonitor001a = new curcontmonitor001a();
       
    56         System.exit(curcontmonitor001.JCK_STATUS_BASE + _curcontmonitor001a.runIt(args, System.err));
       
    57     }
       
    58 
       
    59     public int runIt(String args[], PrintStream out) {
       
    60         //make log for debugee messages
       
    61         argumentHandler = new ArgumentHandler(args);
       
    62         log = new Log(out, argumentHandler);
       
    63         long timeout = argumentHandler.getWaitTime() * 60 * 1000; // milliseconds
       
    64 
       
    65         // make communication pipe to debugger
       
    66         log.display("Creating pipe");
       
    67         IOPipe pipe = argumentHandler.createDebugeeIOPipe(log);
       
    68 
       
    69         // load tested class and create tested thread
       
    70         log.display("Creating object of tested class");
       
    71         TestedClass.thread = new TestedClass(THREAD_NAME);
       
    72 
       
    73         // start the thread and wait for notification from it
       
    74         synchronized (threadStarting) {
       
    75             TestedClass.thread.start();
       
    76             try {
       
    77                 threadStarting.wait();
       
    78             } catch (InterruptedException e) {
       
    79                 log.complain("Interruption while waiting for thread started:\n\t" + e);
       
    80                 pipe.println(curcontmonitor001.ERROR);
       
    81                 log.display("Debugee FAILED");
       
    82                 return curcontmonitor001.FAILED;
       
    83             }
       
    84 
       
    85             // ensure that tested thread is waiting for monitor object
       
    86             synchronized (TestedClass.thread.monitor) {
       
    87                 // send debugger signal READY
       
    88                 log.display("Sending signal to debugger: " + curcontmonitor001.READY);
       
    89                 pipe.println(curcontmonitor001.READY);
       
    90             }
       
    91         }
       
    92 
       
    93         // wait for signal QUIT from debugeer
       
    94         log.display("Waiting for signal from debugger: " + curcontmonitor001.QUIT);
       
    95         String signal = pipe.readln();
       
    96         log.display("Received signal from debugger: " + signal);
       
    97 
       
    98         // interrupt waiting thread
       
    99         log.display("Interrupting tested thread being waited");
       
   100         TestedClass.thread.interrupt();
       
   101 
       
   102         // check received signal
       
   103         if (signal == null || !signal.equals(curcontmonitor001.QUIT)) {
       
   104             log.complain("Unexpected communication signal from debugee: " + signal
       
   105                         + " (expected: " + curcontmonitor001.QUIT + ")");
       
   106             log.display("Debugee FAILED");
       
   107             return curcontmonitor001.FAILED;
       
   108         }
       
   109 
       
   110         // exit debugee
       
   111         log.display("Debugee PASSED");
       
   112         return curcontmonitor001.PASSED;
       
   113     }
       
   114 
       
   115     // tested thread class
       
   116     public static class TestedClass extends Thread {
       
   117 
       
   118         // field with the tested Thread value
       
   119         public static volatile TestedClass thread = null;
       
   120         // field with monitor object which thread will infinitively wait for
       
   121         public static volatile Object monitor = new Object();
       
   122 
       
   123         public TestedClass(String name) {
       
   124             super(name);
       
   125         }
       
   126 
       
   127         // start the thread and recursive invoke makeFrames()
       
   128         public void run() {
       
   129             log.display("Tested thread started");
       
   130 
       
   131             synchronized (monitor) {
       
   132 
       
   133                 // notify debuggee that thread started
       
   134                 synchronized (threadStarting) {
       
   135                     threadStarting.notifyAll();
       
   136                 }
       
   137 
       
   138                 // wait infinitely for monitor object
       
   139                 try {
       
   140                     monitor.wait();
       
   141                 } catch (InterruptedException e) {
       
   142                     log.display("Tested thread interrupted");
       
   143                 }
       
   144             }
       
   145 
       
   146             log.display("Tested thread finished");
       
   147         }
       
   148 
       
   149     }
       
   150 
       
   151 }