changeset 50164:3569b528972e

8200429: Adjust object pinning interface on CollectedHeap Reviewed-by: dholmes, rkennke
author pliden
date Wed, 04 Apr 2018 11:59:47 +0200
parents 8263950638ed
children 1a1a69219637
files src/hotspot/share/gc/shared/collectedHeap.cpp src/hotspot/share/gc/shared/collectedHeap.hpp src/hotspot/share/prims/jni.cpp
diffstat 3 files changed, 38 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/gc/shared/collectedHeap.cpp	Wed Apr 04 11:25:26 2018 +0200
+++ b/src/hotspot/share/gc/shared/collectedHeap.cpp	Wed Apr 04 11:59:47 2018 +0200
@@ -621,12 +621,15 @@
 
 #endif  // #ifndef PRODUCT
 
-oop CollectedHeap::pin_object(JavaThread* thread, oop o) {
-  Handle handle(thread, o);
-  GCLocker::lock_critical(thread);
-  return handle();
+bool CollectedHeap::supports_object_pinning() const {
+  return false;
 }
 
-void CollectedHeap::unpin_object(JavaThread* thread, oop o) {
-  GCLocker::unlock_critical(thread);
+oop CollectedHeap::pin_object(JavaThread* thread, oop obj) {
+  ShouldNotReachHere();
+  return NULL;
 }
+
+void CollectedHeap::unpin_object(JavaThread* thread, oop obj) {
+  ShouldNotReachHere();
+}
--- a/src/hotspot/share/gc/shared/collectedHeap.hpp	Wed Apr 04 11:25:26 2018 +0200
+++ b/src/hotspot/share/gc/shared/collectedHeap.hpp	Wed Apr 04 11:59:47 2018 +0200
@@ -589,14 +589,12 @@
   // perform cleanup tasks serially in the VMThread.
   virtual WorkGang* get_safepoint_workers() { return NULL; }
 
-  // Support for object pinning. This is used by JNI's Get*Critical() and
-  // Release*Critical() family of functions. A GC may either use the GCLocker
-  // protocol to ensure no critical arrays are in-use when entering
-  // a GC pause, or it can implement pinning, which must guarantee that
-  // the object does not move while pinned.
-  virtual oop pin_object(JavaThread* thread, oop o);
-
-  virtual void unpin_object(JavaThread* thread, oop o);
+  // Support for object pinning. This is used by JNI Get*Critical()
+  // and Release*Critical() family of functions. If supported, the GC
+  // must guarantee that pinned objects never move.
+  virtual bool supports_object_pinning() const;
+  virtual oop pin_object(JavaThread* thread, oop obj);
+  virtual void unpin_object(JavaThread* thread, oop obj);
 
   // Non product verification and debugging.
 #ifndef PRODUCT
--- a/src/hotspot/share/prims/jni.cpp	Wed Apr 04 11:25:26 2018 +0200
+++ b/src/hotspot/share/prims/jni.cpp	Wed Apr 04 11:59:47 2018 +0200
@@ -36,6 +36,7 @@
 #include "classfile/symbolTable.hpp"
 #include "classfile/systemDictionary.hpp"
 #include "classfile/vmSymbols.hpp"
+#include "gc/shared/gcLocker.inline.hpp"
 #include "interpreter/linkResolver.hpp"
 #include "memory/allocation.hpp"
 #include "memory/allocation.inline.hpp"
@@ -3145,6 +3146,24 @@
   }
 JNI_END
 
+static oop lock_gc_or_pin_object(JavaThread* thread, jobject obj) {
+  if (Universe::heap()->supports_object_pinning()) {
+    const oop o = JNIHandles::resolve_non_null(obj);
+    return Universe::heap()->pin_object(thread, o);
+  } else {
+    GCLocker::lock_critical(thread);
+    return JNIHandles::resolve_non_null(obj);
+  }
+}
+
+static void unlock_gc_or_unpin_object(JavaThread* thread, jobject obj) {
+  if (Universe::heap()->supports_object_pinning()) {
+    const oop o = JNIHandles::resolve_non_null(obj);
+    return Universe::heap()->unpin_object(thread, o);
+  } else {
+    GCLocker::unlock_critical(thread);
+  }
+}
 
 JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy))
   JNIWrapper("GetPrimitiveArrayCritical");
@@ -3152,8 +3171,7 @@
   if (isCopy != NULL) {
     *isCopy = JNI_FALSE;
   }
-  oop a = JNIHandles::resolve_non_null(array);
-  a = Universe::heap()->pin_object(thread, a);
+  oop a = lock_gc_or_pin_object(thread, array);
   assert(a->is_array(), "just checking");
   BasicType type;
   if (a->is_objArray()) {
@@ -3170,8 +3188,7 @@
 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
   JNIWrapper("ReleasePrimitiveArrayCritical");
   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(env, array, carray, mode);
-  oop a = JNIHandles::resolve_non_null(array);
-  Universe::heap()->unpin_object(thread, a);
+  unlock_gc_or_unpin_object(thread, array);
 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN();
 JNI_END
 
@@ -3179,8 +3196,7 @@
 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
   JNIWrapper("GetStringCritical");
   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(env, string, (uintptr_t *) isCopy);
-  oop s = JNIHandles::resolve_non_null(string);
-  s = Universe::heap()->pin_object(thread, s);
+  oop s = lock_gc_or_pin_object(thread, string);
   typeArrayOop s_value = java_lang_String::value(s);
   bool is_latin1 = java_lang_String::is_latin1(s);
   if (isCopy != NULL) {
@@ -3217,7 +3233,7 @@
     // This assumes that ReleaseStringCritical bookends GetStringCritical.
     FREE_C_HEAP_ARRAY(jchar, chars);
   }
-  Universe::heap()->unpin_object(thread, s);
+  unlock_gc_or_unpin_object(thread, str);
 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN();
 JNI_END