< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/share/gc/lock/jni/StringCriticalLocker.cpp

Print this page
rev 52100 : 8212083: Handle remaining gc/lock native code and fix two strings
Summary:
Reviewed-by:


   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 #include <jni.h>
  24 #include <stdio.h>
  25 #include <time.h>

  26 #include "jni_tools.h"
  27 
  28 extern "C" {
  29 
  30 static jfieldID objFieldId = NULL;
  31 
  32 /*
  33  * Class:     nsk_share_gc_lock_jni_StringCriticalLocker
  34  * Method:    criticalNative
  35  * Signature: ([Z)Z
  36  */
  37 JNIEXPORT jchar JNICALL Java_nsk_share_gc_lock_jni_StringCriticalLocker_criticalNative
  38 (JNIEnv *env, jobject o, jlong enterTime, jlong sleepTime) {


  39         jsize size, i;
  40         jstring str;
  41         const jchar *pa;
  42         jchar hash = 0;
  43         time_t start_time, current_time;
  44 
  45         if (objFieldId == NULL) {
  46                 jclass klass = env->GetObjectClass(o);
  47                 if (klass == NULL) {
  48                         printf("Error: GetObjectClass returned NULL\n");
  49                         return JNI_FALSE;
  50                 }
  51                 objFieldId = env->GetFieldID(klass, "obj", "Ljava/lang/Object;");
  52                 if (objFieldId == NULL) {
  53                         printf("Error: GetFieldID returned NULL\n");
  54                         return JNI_FALSE;
  55                 }
  56         }
  57         str = (jstring) env->GetObjectField(o, objFieldId);
  58         if (str == NULL) {
  59                 printf("Error: GetObjectField returned NULL\n");
  60                 return JNI_FALSE;
  61         }
  62         env->SetObjectField(o, objFieldId, NULL);

  63         size = env->GetStringLength(str);
  64         start_time = time(NULL);
  65         enterTime /= 1000;
  66         current_time = 0;
  67         while (current_time - start_time < enterTime) {
  68                 pa = env->GetStringCritical(str, NULL);
  69                 if (pa != NULL) {
  70                         for (i = 0; i < size; ++i)
  71                                 hash ^= pa[i];
  72                 } else {
  73                         hash = JNI_FALSE;
  74                 }
  75                 mssleep((long) sleepTime);
  76                 env->ReleaseStringCritical(str, pa);
  77                 mssleep((long) sleepTime);
  78                 current_time = time(NULL);
  79         }
  80         env->SetObjectField(o, objFieldId, str);
  81         return hash;
  82 }


   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 #include <jni.h>
  24 #include <stdio.h>
  25 #include <time.h>
  26 #include "ExceptionCheckingJniEnv.hpp"
  27 #include "jni_tools.h"
  28 
  29 extern "C" {
  30 
  31 static jfieldID objFieldId = NULL;
  32 
  33 /*
  34  * Class:     nsk_share_gc_lock_jni_StringCriticalLocker
  35  * Method:    criticalNative
  36  * Signature: ([Z)Z
  37  */
  38 JNIEXPORT jchar JNICALL Java_nsk_share_gc_lock_jni_StringCriticalLocker_criticalNative
  39 (JNIEnv *jni_env, jobject o, jlong enterTime, jlong sleepTime) {
  40         ExceptionCheckingJniEnvPtr env(jni_env);
  41 
  42         jsize size, i;
  43         jstring str;
  44         const jchar *pa;
  45         jchar hash = 0;
  46         time_t start_time, current_time;
  47 
  48         if (objFieldId == NULL) {
  49                 jclass klass = env->GetObjectClass(o);




  50                 objFieldId = env->GetFieldID(klass, "obj", "Ljava/lang/Object;");




  51         }
  52         str = (jstring) env->GetObjectField(o, objFieldId);




  53         env->SetObjectField(o, objFieldId, NULL);
  54 
  55         size = env->GetStringLength(str);
  56         start_time = time(NULL);
  57         enterTime /= 1000;
  58         current_time = 0;
  59         while (current_time - start_time < enterTime) {
  60                 pa = env->GetStringCritical(str, NULL);
  61                 if (pa != NULL) {
  62                         for (i = 0; i < size; ++i)
  63                                 hash ^= pa[i];
  64                 } else {
  65                         hash = JNI_FALSE;
  66                 }
  67                 mssleep((long) sleepTime);
  68                 env->ReleaseStringCritical(str, pa);
  69                 mssleep((long) sleepTime);
  70                 current_time = time(NULL);
  71         }
  72         env->SetObjectField(o, objFieldId, str);
  73         return hash;
  74 }
< prev index next >