1 /*
   2  * Copyright (c) 2015, 2016 SAP SE. All rights reserved.
   3  * Copyright (c) 2018 Red Hat, Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* @test
  26  * @bug 8207838
  27  * @summary Regression test for passing float args to a synchronized jni function.
  28  *
  29  *
  30  * @run main/othervm/native compiler.floatingpoint.TestFloatSyncJNIArgs
  31  */
  32 
  33 package compiler.floatingpoint;
  34 
  35 public class TestFloatSyncJNIArgs {
  36     static {
  37         try {
  38             System.loadLibrary("TestFloatSyncJNIArgs");
  39         } catch (UnsatisfiedLinkError e) {
  40             System.out.println("could not load native lib: " + e);
  41         }
  42     }
  43 
  44     private static final int numberOfThreads = 8;
  45 
  46     static volatile Error testFailed = null;
  47 
  48     public synchronized static native float combine15floats(
  49         float f1, float f2, float f3, float f4,
  50         float f5, float f6, float f7, float f8,
  51         float f9, float f10, float f11, float f12,
  52         float f13, float f14, float f15);
  53 
  54     public synchronized static native double combine15doubles(
  55         double d1, double d2, double d3, double d4,
  56         double d5, double d6, double d7, double d8,
  57         double d9, double d10, double d11, double d12,
  58         double d13, double d14, double d15);
  59 
  60     static void test() throws Exception {
  61         Thread[] threads = new Thread[numberOfThreads];
  62 
  63         for (int i = 0; i < numberOfThreads; i++) {
  64             threads[i] = new Thread(() -> {
  65                 for (int j = 0; j < 10000; j++) {
  66                     float f = combine15floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
  67                                               9, 10, 11, 12, 13, 14, 15);
  68                     if (f != 81720.0f) {
  69                         testFailed = new Error("jni function didn't combine 15 float args properly: " + f);
  70                         throw testFailed;
  71                     }
  72                 }
  73             });
  74         }
  75         for (int i = 0; i < numberOfThreads; i++) {
  76             threads[i].start();
  77         }
  78         for (int i = 0; i < numberOfThreads; i++) {
  79             threads[i].join();
  80         }
  81         if (testFailed != null) {
  82             throw testFailed;
  83         }
  84 
  85         for (int i = 0; i < numberOfThreads; i++) {
  86             threads[i] = new Thread(() -> {
  87                 for (int j = 0; j < 10000; j++) {
  88                     double d = combine15doubles(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
  89                                                 9, 10, 11, 12, 13, 14, 15);
  90                     if (d != 81720.0) {
  91                         testFailed = new Error("jni function didn't combine 15 double args properly: " + d);
  92                         throw testFailed;
  93                     }
  94                 }
  95             });
  96         }
  97         for (int i = 0; i < numberOfThreads; i++) {
  98             threads[i].start();
  99         }
 100         for (int i = 0; i < numberOfThreads; i++) {
 101             threads[i].join();
 102         }
 103         if (testFailed != null) {
 104             throw testFailed;
 105         }
 106     }
 107 
 108     public static void main(String[] args) throws Exception {
 109         for (int i = 0; i < 200; ++i) {
 110             test();
 111         }
 112     }
 113 }