8195972: Refactor oops in JNI to use the Access API
authorkbarrett
Fri, 30 Mar 2018 21:57:50 -0400
changeset 49656 be608cad0b2a
parent 49655 d6893a76c554
child 49657 45071514f87a
8195972: Refactor oops in JNI to use the Access API Summary: Use Access API in JNIHandles Reviewed-by: coleenp, eosterlund
src/hotspot/share/runtime/jniHandles.cpp
src/hotspot/share/runtime/jniHandles.hpp
src/hotspot/share/runtime/jniHandles.inline.hpp
--- a/src/hotspot/share/runtime/jniHandles.cpp	Fri Mar 30 18:46:14 2018 +0000
+++ b/src/hotspot/share/runtime/jniHandles.cpp	Fri Mar 30 21:57:50 2018 -0400
@@ -26,6 +26,7 @@
 #include "gc/shared/oopStorage.inline.hpp"
 #include "logging/log.hpp"
 #include "memory/iterator.hpp"
+#include "oops/access.inline.hpp"
 #include "oops/oop.inline.hpp"
 #include "runtime/handles.inline.hpp"
 #include "runtime/jniHandles.inline.hpp"
@@ -34,9 +35,6 @@
 #include "trace/traceMacros.hpp"
 #include "utilities/align.hpp"
 #include "utilities/debug.hpp"
-#if INCLUDE_ALL_GCS
-#include "gc/g1/g1BarrierSet.hpp"
-#endif
 
 OopStorage* JNIHandles::_global_handles = NULL;
 OopStorage* JNIHandles::_weak_global_handles = NULL;
@@ -101,7 +99,8 @@
     oop* ptr = _global_handles->allocate();
     // Return NULL on allocation failure.
     if (ptr != NULL) {
-      *ptr = obj();
+      assert(*ptr == NULL, "invariant");
+      RootAccess<IN_CONCURRENT_ROOT>::oop_store(ptr, obj());
       res = reinterpret_cast<jobject>(ptr);
     } else {
       report_handle_allocation_failure(alloc_failmode, "global");
@@ -124,7 +123,8 @@
     oop* ptr = _weak_global_handles->allocate();
     // Return NULL on allocation failure.
     if (ptr != NULL) {
-      *ptr = obj();
+      assert(*ptr == NULL, "invariant");
+      RootAccess<ON_PHANTOM_OOP_REF>::oop_store(ptr, obj());
       char* tptr = reinterpret_cast<char*>(ptr) + weak_tag_value;
       res = reinterpret_cast<jobject>(tptr);
     } else {
@@ -151,26 +151,23 @@
 oop JNIHandles::resolve_jweak(jweak handle) {
   assert(handle != NULL, "precondition");
   assert(is_jweak(handle), "precondition");
-  oop result = jweak_ref(handle);
-#if INCLUDE_ALL_GCS
-  if (result != NULL && UseG1GC) {
-    G1BarrierSet::enqueue(result);
-  }
-#endif // INCLUDE_ALL_GCS
-  return result;
+  return RootAccess<ON_PHANTOM_OOP_REF>::oop_load(jweak_ptr(handle));
 }
 
 bool JNIHandles::is_global_weak_cleared(jweak handle) {
   assert(handle != NULL, "precondition");
   assert(is_jweak(handle), "not a weak handle");
-  return jweak_ref(handle) == NULL;
+  oop* oop_ptr = jweak_ptr(handle);
+  oop value = RootAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(oop_ptr);
+  return value == NULL;
 }
 
 void JNIHandles::destroy_global(jobject handle) {
   if (handle != NULL) {
     assert(!is_jweak(handle), "wrong method for detroying jweak");
-    jobject_ref(handle) = NULL;
-    _global_handles->release(&jobject_ref(handle));
+    oop* oop_ptr = jobject_ptr(handle);
+    RootAccess<IN_CONCURRENT_ROOT>::oop_store(oop_ptr, (oop)NULL);
+    _global_handles->release(oop_ptr);
   }
 }
 
@@ -178,8 +175,9 @@
 void JNIHandles::destroy_weak_global(jobject handle) {
   if (handle != NULL) {
     assert(is_jweak(handle), "JNI handle not jweak");
-    jweak_ref(handle) = NULL;
-    _weak_global_handles->release(&jweak_ref(handle));
+    oop* oop_ptr = jweak_ptr(handle);
+    RootAccess<ON_PHANTOM_OOP_REF>::oop_store(oop_ptr, (oop)NULL);
+    _weak_global_handles->release(oop_ptr);
   }
 }
 
@@ -218,11 +216,11 @@
   assert(handle != NULL, "precondition");
   jobjectRefType result = JNIInvalidRefType;
   if (is_jweak(handle)) {
-    if (is_storage_handle(_weak_global_handles, &jweak_ref(handle))) {
+    if (is_storage_handle(_weak_global_handles, jweak_ptr(handle))) {
       result = JNIWeakGlobalRefType;
     }
   } else {
-    switch (_global_handles->allocation_status(&jobject_ref(handle))) {
+    switch (_global_handles->allocation_status(jobject_ptr(handle))) {
     case OopStorage::ALLOCATED_ENTRY:
       result = JNIGlobalRefType;
       break;
@@ -279,13 +277,13 @@
 
 bool JNIHandles::is_global_handle(jobject handle) {
   assert(handle != NULL, "precondition");
-  return !is_jweak(handle) && is_storage_handle(_global_handles, &jobject_ref(handle));
+  return !is_jweak(handle) && is_storage_handle(_global_handles, jobject_ptr(handle));
 }
 
 
 bool JNIHandles::is_weak_global_handle(jobject handle) {
   assert(handle != NULL, "precondition");
-  return is_jweak(handle) && is_storage_handle(_weak_global_handles, &jweak_ref(handle));
+  return is_jweak(handle) && is_storage_handle(_weak_global_handles, jweak_ptr(handle));
 }
 
 size_t JNIHandles::global_handle_memory_usage() {
@@ -351,6 +349,8 @@
   // Zap block values
   _top = 0;
   for (int index = 0; index < block_size_in_oops; index++) {
+    // NOT using Access here; just bare clobbering to NULL, since the
+    // block no longer contains valid oops.
     _handles[index] = NULL;
   }
 }
@@ -506,7 +506,7 @@
   // Try last block
   if (_last->_top < block_size_in_oops) {
     oop* handle = &(_last->_handles)[_last->_top++];
-    *handle = obj;
+    RootAccess<AS_DEST_NOT_INITIALIZED>::oop_store(handle, obj);
     return (jobject) handle;
   }
 
@@ -514,7 +514,7 @@
   if (_free_list != NULL) {
     oop* handle = _free_list;
     _free_list = (oop*) *_free_list;
-    *handle = obj;
+    RootAccess<AS_DEST_NOT_INITIALIZED>::oop_store(handle, obj);
     return (jobject) handle;
   }
   // Check if unused block follow last
--- a/src/hotspot/share/runtime/jniHandles.hpp	Fri Mar 30 18:46:14 2018 +0000
+++ b/src/hotspot/share/runtime/jniHandles.hpp	Fri Mar 30 21:57:50 2018 -0400
@@ -28,10 +28,8 @@
 #include "memory/allocation.hpp"
 #include "runtime/handles.hpp"
 
-class JNIHandleBlock;
 class OopStorage;
 
-
 // Interface for creating and resolving local/global JNI handles
 
 class JNIHandles : AllStatic {
@@ -41,8 +39,8 @@
   static OopStorage* _weak_global_handles;
 
   inline static bool is_jweak(jobject handle);
-  inline static oop& jobject_ref(jobject handle); // NOT jweak!
-  inline static oop& jweak_ref(jobject handle);
+  inline static oop* jobject_ptr(jobject handle); // NOT jweak!
+  inline static oop* jweak_ptr(jobject handle);
 
   template<bool external_guard> inline static oop resolve_impl(jobject handle);
   static oop resolve_jweak(jweak handle);
--- a/src/hotspot/share/runtime/jniHandles.inline.hpp	Fri Mar 30 18:46:14 2018 +0000
+++ b/src/hotspot/share/runtime/jniHandles.inline.hpp	Fri Mar 30 21:57:50 2018 -0400
@@ -25,6 +25,7 @@
 #ifndef SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
 #define SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
 
+#include "oops/access.inline.hpp"
 #include "oops/oop.hpp"
 #include "runtime/jniHandles.hpp"
 #include "utilities/debug.hpp"
@@ -36,15 +37,15 @@
   return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;
 }
 
-inline oop& JNIHandles::jobject_ref(jobject handle) {
+inline oop* JNIHandles::jobject_ptr(jobject handle) {
   assert(!is_jweak(handle), "precondition");
-  return *reinterpret_cast<oop*>(handle);
+  return reinterpret_cast<oop*>(handle);
 }
 
-inline oop& JNIHandles::jweak_ref(jobject handle) {
+inline oop* JNIHandles::jweak_ptr(jobject handle) {
   assert(is_jweak(handle), "precondition");
   char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;
-  return *reinterpret_cast<oop*>(ptr);
+  return reinterpret_cast<oop*>(ptr);
 }
 
 // external_guard is true if called from resolve_external_guard.
@@ -56,7 +57,7 @@
   if (is_jweak(handle)) {       // Unlikely
     result = resolve_jweak(handle);
   } else {
-    result = jobject_ref(handle);
+    result = RootAccess<IN_CONCURRENT_ROOT>::oop_load(jobject_ptr(handle));
     // Construction of jobjects canonicalize a null value into a null
     // jobject, so for non-jweak the pointee should never be null.
     assert(external_guard || result != NULL, "Invalid JNI handle");
@@ -82,7 +83,7 @@
 inline void JNIHandles::destroy_local(jobject handle) {
   if (handle != NULL) {
     assert(!is_jweak(handle), "Invalid JNI local handle");
-    jobject_ref(handle) = NULL;
+    RootAccess<>::oop_store(jobject_ptr(handle), (oop)NULL);
   }
 }