8175917: [JVMCI] Avoid long JNI handle chains
authorchaeubl
Mon, 27 Feb 2017 17:36:36 +0100
changeset 44092 bc842cc2356b
parent 44091 dcca5d534437
child 44093 e22e0d071bf9
8175917: [JVMCI] Avoid long JNI handle chains Reviewed-by: never, kvn
hotspot/src/share/vm/jvmci/jvmciCompilerToVM.cpp
hotspot/src/share/vm/jvmci/jvmciCompilerToVM.hpp
--- a/hotspot/src/share/vm/jvmci/jvmciCompilerToVM.cpp	Wed Mar 01 14:51:12 2017 +0300
+++ b/hotspot/src/share/vm/jvmci/jvmciCompilerToVM.cpp	Mon Feb 27 17:36:36 2017 +0100
@@ -56,6 +56,31 @@
 #include "utilities/resourceHash.hpp"
 
 
+void JNIHandleMark::push_jni_handle_block() {
+  JavaThread* thread = JavaThread::current();
+  if (thread != NULL) {
+    // Allocate a new block for JNI handles.
+    // Inlined code from jni_PushLocalFrame()
+    JNIHandleBlock* java_handles = ((JavaThread*)thread)->active_handles();
+    JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
+    assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
+    compile_handles->set_pop_frame_link(java_handles);
+    thread->set_active_handles(compile_handles);
+  }
+}
+
+void JNIHandleMark::pop_jni_handle_block() {
+  JavaThread* thread = JavaThread::current();
+  if (thread != NULL) {
+    // Release our JNI handle block
+    JNIHandleBlock* compile_handles = thread->active_handles();
+    JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
+    thread->set_active_handles(java_handles);
+    compile_handles->set_pop_frame_link(NULL);
+    JNIHandleBlock::release_block(compile_handles, thread); // may block
+  }
+}
+
 // Entry to native method implementation that transitions current thread to '_thread_in_vm'.
 #define C2V_VMENTRY(result_type, name, signature) \
   JNIEXPORT result_type JNICALL c2v_ ## name signature { \
@@ -89,6 +114,7 @@
   return NULL;
 }
 
+
 int CompilerToVM::Data::Klass_vtable_start_offset;
 int CompilerToVM::Data::Klass_vtable_length_offset;
 
@@ -985,6 +1011,8 @@
 C2V_VMENTRY(jint, installCode, (JNIEnv *jniEnv, jobject, jobject target, jobject compiled_code, jobject installed_code, jobject speculation_log))
   ResourceMark rm;
   HandleMark hm;
+  JNIHandleMark jni_hm;
+
   Handle target_handle = JNIHandles::resolve(target);
   Handle compiled_code_handle = JNIHandles::resolve(compiled_code);
   CodeBlob* cb = NULL;
--- a/hotspot/src/share/vm/jvmci/jvmciCompilerToVM.hpp	Wed Mar 01 14:51:12 2017 +0300
+++ b/hotspot/src/share/vm/jvmci/jvmciCompilerToVM.hpp	Mon Feb 27 17:36:36 2017 +0100
@@ -206,4 +206,14 @@
   inline void do_void()                     { }
 };
 
+class JNIHandleMark : public StackObj {
+  public:
+    JNIHandleMark() { push_jni_handle_block(); }
+    ~JNIHandleMark() { pop_jni_handle_block(); }
+
+  private:
+    static void push_jni_handle_block();
+    static void pop_jni_handle_block();
+};
+
 #endif // SHARE_VM_JVMCI_JVMCI_COMPILER_TO_VM_HPP