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