Path: blob/master/test/hotspot/jtreg/gc/cslocker/TestCSLocker.java
40942 views
/*1* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc.cslocker;2425import static gc.testlibrary.Allocation.blackHole;2627/*28* @test TestCSLocker29* @bug 618620030* @library /31* @summary This short test check RFE 6186200 changes. One thread locked32* @summary completely in JNI CS, while other is trying to allocate memory33* @summary provoking GC. OOM means FAIL, deadlock means PASS.34* @run main/native/othervm -Xmx256m gc.cslocker.TestCSLocker35*/3637public class TestCSLocker extends Thread38{39static int timeout = 5000;40public static void main(String args[]) throws Exception {41long startTime = System.currentTimeMillis();4243// start garbage producer thread44GarbageProducer garbageProducer = new GarbageProducer(1000000, 10);45garbageProducer.start();4647// start CS locker thread48CSLocker csLocker = new CSLocker();49csLocker.start();5051// check timeout to success deadlocking52while(System.currentTimeMillis() < startTime + timeout) {53System.out.println("sleeping...");54sleep(1000);55}5657csLocker.unlock();58garbageProducer.interrupt();59}60}6162class GarbageProducer extends Thread63{64private int size;65private int sleepTime;6667GarbageProducer(int size, int sleepTime) {68this.size = size;69this.sleepTime = sleepTime;70}7172public void run() {73boolean isRunning = true;7475while (isRunning) {76try {77blackHole(new int[size]);78sleep(sleepTime);79} catch (InterruptedException e) {80isRunning = false;81}82}83}84}8586class CSLocker extends Thread87{88static { System.loadLibrary("TestCSLocker"); }8990public void run() {91int[] a = new int[10];92a[0] = 1;93if (!lock(a)) {94throw new RuntimeException("failed to acquire CSLock");95}96}9798native boolean lock(int[] array);99native void unlock();100}101102103