1 /*
  2  * Copyright (c) 2007, 2019, 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 gc.cslocker;
 25 
 26 /*
 27  * @test TestCSLocker
 28  * @key gc
 29  * @bug 6186200
 30  * @summary This short test check RFE 6186200 changes. One thread locked
 31  * @summary completely in JNI CS, while other is trying to allocate memory
 32  * @summary provoking GC. OOM means FAIL, deadlock means PASS.
 33  * @run main/native/othervm -Xmx256m gc.cslocker.TestCSLocker
 34  */
 35 
 36 public class TestCSLocker extends Thread
 37 {
 38     static int timeout = 5000;
 39     public static void main(String args[]) throws Exception {
 40         long startTime = System.currentTimeMillis();
 41 
 42         // start garbage producer thread
 43         GarbageProducer garbageProducer = new GarbageProducer(1000000, 10);
 44         garbageProducer.start();
 45 
 46         // start CS locker thread
 47         CSLocker csLocker = new CSLocker();
 48         csLocker.start();
 49 
 50         // check timeout to success deadlocking
 51         while(System.currentTimeMillis() < startTime + timeout) {
 52             System.out.println("sleeping...");
 53             sleep(1000);
 54         }
 55 
 56         csLocker.unlock();
 57         garbageProducer.interrupt();
 58     }
 59 }
 60 
 61 class GarbageProducer extends Thread
 62 {
 63     private int size;
 64     private int sleepTime;
 65 
 66     GarbageProducer(int size, int sleepTime) {
 67         this.size = size;
 68         this.sleepTime = sleepTime;
 69     }
 70 
 71     public void run() {
 72         boolean isRunning = true;
 73 
 74         while (isRunning) {
 75             try {
 76                 int[] arr = null;
 77                 arr = new int[size];
 78                 sleep(sleepTime);
 79             } catch (InterruptedException e) {
 80                 isRunning = false;
 81             }
 82         }
 83     }
 84 }
 85 
 86 class CSLocker extends Thread
 87 {
 88     static { System.loadLibrary("TestCSLocker"); }
 89 
 90     public void run() {
 91         int[] a = new int[10];
 92         a[0] = 1;
 93         if (!lock(a)) {
 94             throw new RuntimeException("failed to acquire CSLock");
 95         }
 96     }
 97 
 98     native boolean lock(int[] array);
 99     native void unlock();
100 }