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