test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC04/tc04t001.java
changeset 55482 4d5eabe8d341
parent 55464 a3e3f3caf284
child 58679 9c3209ff7550
equal deleted inserted replaced
55481:e17c9a93b505 55482:4d5eabe8d341
    84                 THREADS_LIMIT*tc04t001Thread.INCREMENT_LIMIT) {
    84                 THREADS_LIMIT*tc04t001Thread.INCREMENT_LIMIT) {
    85             log.complain("Wrong value: " + tc04t001Thread.value +
    85             log.complain("Wrong value: " + tc04t001Thread.value +
    86                 ", expected: " + THREADS_LIMIT*tc04t001Thread.INCREMENT_LIMIT);
    86                 ", expected: " + THREADS_LIMIT*tc04t001Thread.INCREMENT_LIMIT);
    87             status = Consts.TEST_FAILED;
    87             status = Consts.TEST_FAILED;
    88         }
    88         }
    89 
       
    90 /* DEBUG -- to check if the threads taking turns in right order
       
    91         boolean race = false;
       
    92         for (int i = 1; i < 2*tc04t001Thread.INCREMENT_LIMIT; i++) {
       
    93              if (tc04t001Thread.passOrder[i] == tc04t001Thread.passOrder[i-1]) {
       
    94                 race = true;
       
    95                 System.out.println("Race condition in the test:");
       
    96                 System.out.println("passOrder[" + (i-1) + "]:"
       
    97                     + tc04t001Thread.passOrder[i-1]);
       
    98                 System.out.println("passOrder[" + (i) + "]:"
       
    99                     + tc04t001Thread.passOrder[i]);
       
   100              }
       
   101         }
       
   102         if (race)
       
   103             System.out.println("There was a race condition in the test.");
       
   104 */
       
   105 
       
   106         return status;
    89         return status;
   107     }
    90     }
   108 }
    91 }
   109 
    92 
   110 /* =================================================================== */
    93 /* =================================================================== */
   115     final static int DELAY = 1000;
    98     final static int DELAY = 1000;
   116 
    99 
   117     static volatile int value = 0;
   100     static volatile int value = 0;
   118 
   101 
   119     static Flicker flicker = new Flicker();
   102     static Flicker flicker = new Flicker();
   120 /* DEBUG -- to check if the threads taking turns in right order
       
   121     static volatile int iter = 0;
       
   122     static volatile int passOrder[] =
       
   123         new int[INCREMENT_LIMIT*tc04t001.THREADS_LIMIT];
       
   124 */
       
   125 
   103 
   126     private int id;
   104     private int id;
   127     private static volatile int lastEnterEventsCount;
   105     private static volatile int lastEnterEventsCount;
   128     private static native   int enterEventsCount();
   106     private static native   int enterEventsCount();
   129 
   107 
   133     }
   111     }
   134 
   112 
   135     public synchronized void run() {
   113     public synchronized void run() {
   136         for (int i = 0; i < INCREMENT_LIMIT; i++) {
   114         for (int i = 0; i < INCREMENT_LIMIT; i++) {
   137             flicker.waitFor(id);
   115             flicker.waitFor(id);
   138             lastEnterEventsCount = enterEventsCount();
       
   139             increment(id);
   116             increment(id);
   140             try {
   117             try {
   141                 wait(1);
   118                 wait(1);
   142             } catch (InterruptedException e) {}
   119             } catch (InterruptedException e) {}
   143         }
   120         }
   144         tc04t001.threadsDoneSignal.countDown();
   121         tc04t001.threadsDoneSignal.countDown();
   145     }
   122     }
   146 
   123 
   147     static synchronized void increment(int i) {
   124     static synchronized void increment(int i) {
   148 /* DEBUG -- to check if the threads taking turns in right order
       
   149         passOrder[iter++] = i;
       
   150 */
       
   151         flicker.unlock(i);
   125         flicker.unlock(i);
   152         int temp = value;
   126         int temp = value;
       
   127         boolean done = false;
   153 
   128 
   154         // Wait in a loop for a MonitorContendedEnter event.
   129         // Wait in a loop for a MonitorContendedEnter event.
   155         // Timeout is: 20ms * DELAY.
   130         // Timeout is: 20ms * DELAY.
   156         for (int j = 0; j < DELAY; j++) {
   131         for (int j = 0; j < DELAY; j++) {
   157             try {
   132             try {
   158                 sleep(20);
   133                 sleep(20);
   159             } catch (InterruptedException e) {}
   134             } catch (InterruptedException e) {}
   160 
   135 
       
   136             done = (tc04t001.threadsDoneSignal.getCount() == 1);
       
   137             if (done) {
       
   138                 break; // This thread is the only remaining thread, no more contention
       
   139             }
   161             if (enterEventsCount() > lastEnterEventsCount) {
   140             if (enterEventsCount() > lastEnterEventsCount) {
       
   141                 System.out.println("Thread-" + i + ": increment event: " + enterEventsCount());
   162                 break; // Got an expected MonitorContendedEnter event
   142                 break; // Got an expected MonitorContendedEnter event
   163             }
   143             }
   164         }
   144         }
   165         System.out.println("Thread-" + i + ": increment event: " + enterEventsCount());
       
   166 
   145 
   167         if (enterEventsCount() == lastEnterEventsCount) {
   146         if (!done && enterEventsCount() == lastEnterEventsCount) {
   168             String msg = "Timeout in waiting for a MonitorContendedEnter event";
   147             String msg = "Timeout in waiting for a MonitorContendedEnter event";
   169             throw new RuntimeException(msg);
   148             throw new RuntimeException(msg);
   170         }
   149         }
   171         value = temp + 1;
   150         value = temp + 1;
       
   151         lastEnterEventsCount = enterEventsCount();
   172     }
   152     }
   173 }
   153 }
   174 
   154 
   175 class Flicker {
   155 class Flicker {
   176 
   156