1 /*
   2  * Copyright (c) 2004, 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.scenarios.contention.TC02;
  25 
  26 import java.io.PrintStream;
  27 
  28 import nsk.share.*;
  29 import nsk.share.jvmti.*;
  30 
  31 //    THIS TEST IS LINE NUMBER SENSITIVE
  32 
  33 public class tc02t001 extends DebugeeClass {
  34 
  35     // run test from command line
  36     public static void main(String argv[]) {
  37         argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
  38 
  39         // JCK-compatible exit
  40         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
  41     }
  42 
  43     // run test from JCK-compatible environment
  44     public static int run(String argv[], PrintStream out) {
  45         return new tc02t001().runIt(argv, out);
  46     }
  47 
  48     /* =================================================================== */
  49 
  50     // scaffold objects
  51     ArgumentHandler argHandler = null;
  52     Log log = null;
  53     int status = Consts.TEST_PASSED;
  54     static long timeout = 0;
  55 
  56     // tested thread
  57     tc02t001Thread thread = null;
  58 
  59     // run debuggee
  60     public int runIt(String argv[], PrintStream out) {
  61         argHandler = new ArgumentHandler(argv);
  62         log = new Log(out, argHandler);
  63         timeout = argHandler.getWaitTime() * 60 * 1000;
  64         log.display("Timeout = " + timeout + " msc.");
  65 
  66         thread = new tc02t001Thread("Debuggee Thread");
  67         synchronized (thread.M) {
  68             thread.start();
  69             thread.startingBarrier.waitFor();
  70             status = checkStatus(status);
  71 
  72             thread.waitingBarrier1.unlock();
  73             try {
  74                 Thread.sleep(1000);
  75                 thread.M.wait(timeout);
  76             } catch (InterruptedException e) {
  77                 throw new Failure(e);
  78             }
  79 
  80             thread.waitingBarrier2.unlock();
  81             try {
  82                 Thread.sleep(1000);
  83                 thread.M.wait(timeout);
  84             } catch (InterruptedException e) {
  85                 throw new Failure(e);
  86             }
  87 
  88             thread.waitingBarrier3.unlock();
  89             try {
  90                 Thread.sleep(1000);
  91                 thread.M.wait(timeout);
  92             } catch (InterruptedException e) {
  93                 throw new Failure(e);
  94             }
  95         }
  96 
  97         try {
  98             thread.join(timeout);
  99         } catch (InterruptedException e) {
 100             throw new Failure(e);
 101         }
 102 
 103         log.display("Debugee finished");
 104         status = checkStatus(status);
 105 
 106         return status;
 107     }
 108 }
 109 
 110 /* =================================================================== */
 111 
 112 class tc02t001Thread extends Thread {
 113     public Wicket startingBarrier = new Wicket();
 114     public Wicket waitingBarrier1 = new Wicket();
 115     public Wicket waitingBarrier2 = new Wicket();
 116     public Wicket waitingBarrier3 = new Wicket();
 117     public Object M = new Object();
 118 
 119     public tc02t001Thread(String name) {
 120         super(name);
 121     }
 122 
 123     public void run() {
 124         startingBarrier.unlock();
 125 
 126         waitingBarrier1.waitFor();
 127         synchronized (M) { // tc02t001.c::lines[0]
 128             M.notify();
 129         }
 130 
 131         waitingBarrier2.waitFor();
 132         synchronized (M) { // tc02t001.c::lines[1]
 133             M.notify();
 134         }
 135 
 136         waitingBarrier3.waitFor();
 137         synchronized (M) { // tc02t001.c::lines[2]
 138             M.notify();
 139         }
 140     }
 141 }