hotspot/test/compiler/testlibrary/rtm/BusyLock.java
author fzhinkin
Tue, 30 Dec 2014 11:09:42 +0300
changeset 28394 6d382dc493e5
parent 24005 6841a4be0faa
child 28621 37cc414b6491
permissions -rw-r--r--
8050486: compiler/rtm/ tests fail due to monitor deflation at safepoint synchronization Reviewed-by: kvn, iignatyev
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24005
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     1
/*
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     4
 *
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     8
 *
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    14
 *
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    18
 *
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    21
 * questions.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    22
 *
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    23
 */
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    24
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    25
package rtm;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    26
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    27
import com.oracle.java.testlibrary.Utils;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    28
import sun.misc.Unsafe;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    29
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    30
import java.util.concurrent.BrokenBarrierException;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    31
import java.util.concurrent.CyclicBarrier;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    32
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    33
/**
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    34
 * Test case for busy lock scenario.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    35
 * One thread enters the monitor and sleep for a while.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    36
 * Another thread is blocked on the same monitor.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    37
 */
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    38
public class BusyLock implements CompilableTest, Runnable {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    39
    private static final int DEFAULT_TIMEOUT = 1000;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    40
    private final CyclicBarrier barrier;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    41
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    42
    // Following field have to be static in order to avoid escape analysis.
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    43
    @SuppressWarnings("UnsuedDeclaration")
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    44
    private static int field = 0;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    45
    private static final Unsafe UNSAFE = Utils.getUnsafe();
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    46
    protected final Object monitor;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    47
    protected final int timeout;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    48
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    49
    public BusyLock() {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    50
        this(BusyLock.DEFAULT_TIMEOUT);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    51
    }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    52
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    53
    public BusyLock(int timeout) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    54
        this.timeout = timeout;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    55
        this.monitor = new Object();
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    56
        this.barrier = new CyclicBarrier(2);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    57
    }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    58
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    59
    @Override
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    60
    public void run() {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    61
        try {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    62
            // wait until forceAbort leave monitor
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    63
            barrier.await();
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    64
            if (UNSAFE.tryMonitorEnter(monitor)) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    65
                try {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    66
                    barrier.await();
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    67
                    Thread.sleep(timeout);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    68
                } finally {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    69
                    UNSAFE.monitorExit(monitor);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    70
                }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    71
            } else {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    72
                throw new RuntimeException("Monitor should be entered by " +
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    73
                                           "::run() first.");
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    74
            }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    75
        } catch (InterruptedException | BrokenBarrierException e) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    76
            throw new RuntimeException("Synchronization error happened.", e);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    77
        }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    78
    }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    79
28394
6d382dc493e5 8050486: compiler/rtm/ tests fail due to monitor deflation at safepoint synchronization
fzhinkin
parents: 24005
diff changeset
    80
    public void syncAndTest() {
24005
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    81
        try {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    82
            barrier.await();
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    83
            // wait until monitor is locked by a ::run method
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    84
            barrier.await();
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    85
        } catch (InterruptedException | BrokenBarrierException e) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    86
            throw new RuntimeException("Synchronization error happened.", e);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    87
        }
28394
6d382dc493e5 8050486: compiler/rtm/ tests fail due to monitor deflation at safepoint synchronization
fzhinkin
parents: 24005
diff changeset
    88
        test();
6d382dc493e5 8050486: compiler/rtm/ tests fail due to monitor deflation at safepoint synchronization
fzhinkin
parents: 24005
diff changeset
    89
    }
6d382dc493e5 8050486: compiler/rtm/ tests fail due to monitor deflation at safepoint synchronization
fzhinkin
parents: 24005
diff changeset
    90
6d382dc493e5 8050486: compiler/rtm/ tests fail due to monitor deflation at safepoint synchronization
fzhinkin
parents: 24005
diff changeset
    91
    public void test() {
24005
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    92
        synchronized(monitor) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    93
            BusyLock.field++;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    94
        }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    95
    }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    96
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    97
    @Override
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    98
    public String getMethodWithLockName() {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
    99
        return this.getClass().getName() + "::test";
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   100
    }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   101
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   102
    @Override
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   103
    public String[] getMethodsToCompileNames() {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   104
        return new String[] { getMethodWithLockName() };
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   105
    }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   106
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   107
    /**
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   108
     * Usage:
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   109
     * BusyLock [ <inflate monitor> [ <timeout> ] ]
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   110
     *
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   111
     * Default values are:
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   112
     * <ul>
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   113
     *     <li>inflate monitor = {@code true}</li>
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   114
     *     <li>timeout = {@code BusyLock.DEFAULT_TIMEOUT}</li>
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   115
     * </ul>
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   116
     */
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   117
    public static void main(String args[]) throws Exception {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   118
        int timeoutValue = BusyLock.DEFAULT_TIMEOUT;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   119
        boolean inflateMonitor = true;
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   120
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   121
        if (args.length > 0 ) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   122
            inflateMonitor = Boolean.valueOf(args[0]);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   123
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   124
            if (args.length > 1) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   125
                timeoutValue = Integer.valueOf(args[1]);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   126
            }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   127
        }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   128
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   129
        BusyLock busyLock = new BusyLock(timeoutValue);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   130
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   131
        if (inflateMonitor) {
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   132
            AbortProvoker.inflateMonitor(busyLock.monitor);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   133
        }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   134
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   135
        Thread t = new Thread(busyLock);
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   136
        t.start();
28394
6d382dc493e5 8050486: compiler/rtm/ tests fail due to monitor deflation at safepoint synchronization
fzhinkin
parents: 24005
diff changeset
   137
        busyLock.syncAndTest();
24005
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   138
        t.join();
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   139
    }
6841a4be0faa 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
   140
}