1.1 --- a/src/share/vm/runtime/jniHandles.cpp Thu Sep 05 18:40:52 2019 +0800 1.2 +++ b/src/share/vm/runtime/jniHandles.cpp Thu Sep 05 18:52:27 2019 +0800 1.3 @@ -24,11 +24,15 @@ 1.4 1.5 #include "precompiled.hpp" 1.6 #include "classfile/systemDictionary.hpp" 1.7 +#include "memory/iterator.hpp" 1.8 #include "oops/oop.inline.hpp" 1.9 #include "prims/jvmtiExport.hpp" 1.10 #include "runtime/jniHandles.hpp" 1.11 #include "runtime/mutexLocker.hpp" 1.12 #include "runtime/thread.inline.hpp" 1.13 +#if INCLUDE_ALL_GCS 1.14 +#include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" 1.15 +#endif 1.16 1.17 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 1.18 1.19 @@ -86,34 +90,52 @@ 1.20 return res; 1.21 } 1.22 1.23 - 1.24 jobject JNIHandles::make_weak_global(Handle obj) { 1.25 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 1.26 jobject res = NULL; 1.27 if (!obj.is_null()) { 1.28 // ignore null handles 1.29 - MutexLocker ml(JNIGlobalHandle_lock); 1.30 - assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); 1.31 - res = _weak_global_handles->allocate_handle(obj()); 1.32 + { 1.33 + MutexLocker ml(JNIGlobalHandle_lock); 1.34 + assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); 1.35 + res = _weak_global_handles->allocate_handle(obj()); 1.36 + } 1.37 + // Add weak tag. 1.38 + assert(is_ptr_aligned(res, weak_tag_alignment), "invariant"); 1.39 + char* tptr = reinterpret_cast<char*>(res) + weak_tag_value; 1.40 + res = reinterpret_cast<jobject>(tptr); 1.41 } else { 1.42 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 1.43 } 1.44 return res; 1.45 } 1.46 1.47 +template<bool external_guard> 1.48 +oop JNIHandles::resolve_jweak(jweak handle) { 1.49 + assert(is_jweak(handle), "precondition"); 1.50 + oop result = jweak_ref(handle); 1.51 + result = guard_value<external_guard>(result); 1.52 +#if INCLUDE_ALL_GCS 1.53 + if (result != NULL && UseG1GC) { 1.54 + G1SATBCardTableModRefBS::enqueue(result); 1.55 + } 1.56 +#endif // INCLUDE_ALL_GCS 1.57 + return result; 1.58 +} 1.59 + 1.60 +template oop JNIHandles::resolve_jweak<true>(jweak); 1.61 +template oop JNIHandles::resolve_jweak<false>(jweak); 1.62 1.63 void JNIHandles::destroy_global(jobject handle) { 1.64 if (handle != NULL) { 1.65 assert(is_global_handle(handle), "Invalid delete of global JNI handle"); 1.66 - *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it 1.67 + jobject_ref(handle) = deleted_handle(); 1.68 } 1.69 } 1.70 1.71 - 1.72 void JNIHandles::destroy_weak_global(jobject handle) { 1.73 if (handle != NULL) { 1.74 - assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle"); 1.75 - *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it 1.76 + jweak_ref(handle) = deleted_handle(); 1.77 } 1.78 } 1.79 1.80 @@ -129,6 +151,12 @@ 1.81 } 1.82 1.83 1.84 +void JNIHandles::weak_oops_do(OopClosure* f) { 1.85 + AlwaysTrueClosure always_true; 1.86 + weak_oops_do(&always_true, f); 1.87 +} 1.88 + 1.89 + 1.90 void JNIHandles::initialize() { 1.91 _global_handles = JNIHandleBlock::allocate_block(); 1.92 _weak_global_handles = JNIHandleBlock::allocate_block(); 1.93 @@ -186,11 +214,6 @@ 1.94 } 1.95 1.96 1.97 -class AlwaysAliveClosure: public BoolObjectClosure { 1.98 -public: 1.99 - bool do_object_b(oop obj) { return true; } 1.100 -}; 1.101 - 1.102 class CountHandleClosure: public OopClosure { 1.103 private: 1.104 int _count; 1.105 @@ -212,9 +235,8 @@ 1.106 "JNIHandles not initialized"); 1.107 1.108 CountHandleClosure global_handle_count; 1.109 - AlwaysAliveClosure always_alive; 1.110 oops_do(&global_handle_count); 1.111 - weak_oops_do(&always_alive, &global_handle_count); 1.112 + weak_oops_do(&global_handle_count); 1.113 1.114 st->print_cr("JNI global references: %d", global_handle_count.count()); 1.115 st->cr(); 1.116 @@ -231,10 +253,9 @@ 1.117 1.118 void JNIHandles::verify() { 1.119 VerifyHandleClosure verify_handle; 1.120 - AlwaysAliveClosure always_alive; 1.121 1.122 oops_do(&verify_handle); 1.123 - weak_oops_do(&always_alive, &verify_handle); 1.124 + weak_oops_do(&verify_handle); 1.125 } 1.126 1.127