Skip to content

Commit 3b2585c

Browse files
committed
8276658: Clean up JNI local handles code
Reviewed-by: dholmes, pchilanomate
1 parent aeba653 commit 3b2585c

25 files changed

+123
-350
lines changed

src/hotspot/share/ci/ciInstanceKlass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ciInstanceKlass::ciInstanceKlass(Klass* k) :
8787
(void)CURRENT_ENV->get_object(holder);
8888
}
8989

90-
Thread *thread = Thread::current();
90+
JavaThread *thread = JavaThread::current();
9191
if (ciObjectFactory::is_initialized()) {
9292
_loader = JNIHandles::make_local(thread, ik->class_loader());
9393
_protection_domain = JNIHandles::make_local(thread,

src/hotspot/share/compiler/compileBroker.cpp

+1-36
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,7 @@ void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
22002200
}
22012201

22022202
// Allocate a new set of JNI handles.
2203-
push_jni_handle_block();
2203+
JNIHandleMark jhm(thread);
22042204
Method* target_handle = task->method();
22052205
int compilable = ciEnv::MethodCompilable;
22062206
const char* failure_reason = NULL;
@@ -2319,9 +2319,6 @@ void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
23192319
post_compilation_event(event, task);
23202320
}
23212321
}
2322-
// Remove the JNI handle block after the ciEnv destructor has run in
2323-
// the previous block.
2324-
pop_jni_handle_block();
23252322

23262323
if (failure_reason != NULL) {
23272324
task->set_failure_reason(failure_reason, failure_reason_on_C_heap);
@@ -2482,38 +2479,6 @@ void CompileBroker::update_compile_perf_data(CompilerThread* thread, const metho
24822479
counters->set_compile_type((jlong) last_compile_type);
24832480
}
24842481

2485-
// ------------------------------------------------------------------
2486-
// CompileBroker::push_jni_handle_block
2487-
//
2488-
// Push on a new block of JNI handles.
2489-
void CompileBroker::push_jni_handle_block() {
2490-
JavaThread* thread = JavaThread::current();
2491-
2492-
// Allocate a new block for JNI handles.
2493-
// Inlined code from jni_PushLocalFrame()
2494-
JNIHandleBlock* java_handles = thread->active_handles();
2495-
JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
2496-
assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
2497-
compile_handles->set_pop_frame_link(java_handles); // make sure java handles get gc'd.
2498-
thread->set_active_handles(compile_handles);
2499-
}
2500-
2501-
2502-
// ------------------------------------------------------------------
2503-
// CompileBroker::pop_jni_handle_block
2504-
//
2505-
// Pop off the current block of JNI handles.
2506-
void CompileBroker::pop_jni_handle_block() {
2507-
JavaThread* thread = JavaThread::current();
2508-
2509-
// Release our JNI handle block
2510-
JNIHandleBlock* compile_handles = thread->active_handles();
2511-
JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
2512-
thread->set_active_handles(java_handles);
2513-
compile_handles->set_pop_frame_link(NULL);
2514-
JNIHandleBlock::release_block(compile_handles, thread); // may block
2515-
}
2516-
25172482
// ------------------------------------------------------------------
25182483
// CompileBroker::collect_statistics
25192484
//

src/hotspot/share/compiler/compileBroker.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ class CompileBroker: AllStatic {
259259
int compilable, const char* failure_reason);
260260
static void update_compile_perf_data(CompilerThread *thread, const methodHandle& method, bool is_osr);
261261

262-
static void push_jni_handle_block();
263-
static void pop_jni_handle_block();
264262
static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
265263

266264
static void compile_method_base(const methodHandle& method,

src/hotspot/share/gc/shared/concurrentGCThread.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,9 +42,6 @@ void ConcurrentGCThread::create_and_start(ThreadPriority prio) {
4242
}
4343

4444
void ConcurrentGCThread::run() {
45-
// Setup handle area
46-
set_active_handles(JNIHandleBlock::allocate_block());
47-
4845
// Wait for initialization to complete
4946
wait_init_completed();
5047

src/hotspot/share/jfr/dcmd/jfrDcmds.cpp

+2-49
Original file line numberDiff line numberDiff line change
@@ -55,53 +55,6 @@ bool register_jfr_dcmds() {
5555
return true;
5656
}
5757

58-
// JNIHandle management
59-
60-
// ------------------------------------------------------------------
61-
// push_jni_handle_block
62-
//
63-
// Push on a new block of JNI handles.
64-
static void push_jni_handle_block(JavaThread* const thread) {
65-
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(thread));
66-
67-
// Allocate a new block for JNI handles.
68-
// Inlined code from jni_PushLocalFrame()
69-
JNIHandleBlock* prev_handles = thread->active_handles();
70-
JNIHandleBlock* entry_handles = JNIHandleBlock::allocate_block(thread);
71-
assert(entry_handles != NULL && prev_handles != NULL, "should not be NULL");
72-
entry_handles->set_pop_frame_link(prev_handles); // make sure prev handles get gc'd.
73-
thread->set_active_handles(entry_handles);
74-
}
75-
76-
// ------------------------------------------------------------------
77-
// pop_jni_handle_block
78-
//
79-
// Pop off the current block of JNI handles.
80-
static void pop_jni_handle_block(JavaThread* const thread) {
81-
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(thread));
82-
83-
// Release our JNI handle block
84-
JNIHandleBlock* entry_handles = thread->active_handles();
85-
JNIHandleBlock* prev_handles = entry_handles->pop_frame_link();
86-
// restore
87-
thread->set_active_handles(prev_handles);
88-
entry_handles->set_pop_frame_link(NULL);
89-
JNIHandleBlock::release_block(entry_handles, thread); // may block
90-
}
91-
92-
class JNIHandleBlockManager : public StackObj {
93-
private:
94-
JavaThread* const _thread;
95-
public:
96-
JNIHandleBlockManager(JavaThread* thread) : _thread(thread) {
97-
push_jni_handle_block(_thread);
98-
}
99-
100-
~JNIHandleBlockManager() {
101-
pop_jni_handle_block(_thread);
102-
}
103-
};
104-
10558
static bool is_module_available(outputStream* output, TRAPS) {
10659
return JfrJavaSupport::is_jdk_jfr_module_available(output, THREAD);
10760
}
@@ -223,7 +176,7 @@ void JfrDCmd::invoke(JfrJavaArguments& method, TRAPS) const {
223176
constructor_args.set_klass(javaClass(), CHECK);
224177

225178
HandleMark hm(THREAD);
226-
JNIHandleBlockManager jni_handle_management(THREAD);
179+
JNIHandleMark jni_handle_management(THREAD);
227180

228181
const oop dcmd = construct_dcmd_instance(&constructor_args, CHECK);
229182

@@ -494,7 +447,7 @@ void JfrConfigureFlightRecorderDCmd::execute(DCmdSource source, TRAPS) {
494447
}
495448

496449
HandleMark hm(THREAD);
497-
JNIHandleBlockManager jni_handle_management(THREAD);
450+
JNIHandleMark jni_handle_management(THREAD);
498451

499452
JavaValue result(T_OBJECT);
500453
JfrJavaArguments constructor_args(&result);

src/hotspot/share/jfr/jni/jfrJavaSupport.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void check_new_unstarted_java_thread(JavaThread* t) {
7171
*/
7272
jobject JfrJavaSupport::local_jni_handle(const oop obj, JavaThread* t) {
7373
DEBUG_ONLY(check_java_thread_in_vm(t));
74-
return t->active_handles()->allocate_handle(obj);
74+
return t->active_handles()->allocate_handle(t, obj);
7575
}
7676

7777
jobject JfrJavaSupport::local_jni_handle(const jobject handle, JavaThread* t) {

src/hotspot/share/jvmci/jvmciCodeInstaller.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ GrowableArray<ScopeValue*>* CodeInstaller::record_virtual_objects(JVMCIObject de
899899
}
900900
Klass* klass = jvmci_env()->asKlass(type);
901901
oop javaMirror = klass->java_mirror();
902-
ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(Thread::current(), javaMirror));
902+
ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(javaMirror));
903903
ObjectValue* sv = is_auto_box ? new AutoBoxObjectValue(id, klass_sv) : new ObjectValue(id, klass_sv);
904904
if (id < 0 || id >= objects->length()) {
905905
JVMCI_ERROR_NULL("virtual object id %d out of bounds", id);

src/hotspot/share/jvmci/jvmciCompilerToVM.cpp

-23
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,6 @@ static void requireInHotSpot(const char* caller, JVMCI_TRAPS) {
8686
}
8787
}
8888

89-
void JNIHandleMark::push_jni_handle_block(JavaThread* thread) {
90-
if (thread != NULL) {
91-
// Allocate a new block for JNI handles.
92-
// Inlined code from jni_PushLocalFrame()
93-
JNIHandleBlock* java_handles = thread->active_handles();
94-
JNIHandleBlock* compile_handles = JNIHandleBlock::allocate_block(thread);
95-
assert(compile_handles != NULL && java_handles != NULL, "should not be NULL");
96-
compile_handles->set_pop_frame_link(java_handles);
97-
thread->set_active_handles(compile_handles);
98-
}
99-
}
100-
101-
void JNIHandleMark::pop_jni_handle_block(JavaThread* thread) {
102-
if (thread != NULL) {
103-
// Release our JNI handle block
104-
JNIHandleBlock* compile_handles = thread->active_handles();
105-
JNIHandleBlock* java_handles = compile_handles->pop_frame_link();
106-
thread->set_active_handles(java_handles);
107-
compile_handles->set_pop_frame_link(NULL);
108-
JNIHandleBlock::release_block(compile_handles, thread); // may block
109-
}
110-
}
111-
11289
class JVMCITraceMark : public StackObj {
11390
const char* _msg;
11491
public:

src/hotspot/share/jvmci/jvmciCompilerToVM.hpp

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -173,15 +173,4 @@ class JavaArgumentUnboxer : public SignatureIterator {
173173
}
174174
};
175175

176-
class JNIHandleMark : public StackObj {
177-
JavaThread* _thread;
178-
public:
179-
JNIHandleMark(JavaThread* thread) : _thread(thread) { push_jni_handle_block(thread); }
180-
~JNIHandleMark() { pop_jni_handle_block(_thread); }
181-
182-
private:
183-
static void push_jni_handle_block(JavaThread* thread);
184-
static void pop_jni_handle_block(JavaThread* thread);
185-
};
186-
187176
#endif // SHARE_JVMCI_JVMCICOMPILERTOVM_HPP

src/hotspot/share/prims/jni.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,8 @@ JNI_ENTRY(jint, jni_PushLocalFrame(JNIEnv *env, jint capacity))
639639
HOTSPOT_JNI_PUSHLOCALFRAME_RETURN((uint32_t)JNI_ERR);
640640
return JNI_ERR;
641641
}
642-
JNIHandleBlock* old_handles = thread->active_handles();
643-
JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
644-
assert(new_handles != NULL, "should not be NULL");
645-
new_handles->set_pop_frame_link(old_handles);
646-
thread->set_active_handles(new_handles);
642+
643+
thread->push_jni_handle_block();
647644
jint ret = JNI_OK;
648645
HOTSPOT_JNI_PUSHLOCALFRAME_RETURN(ret);
649646
return ret;

src/hotspot/share/prims/jvmtiEnvBase.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ JvmtiModuleClosure::get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobje
15101510
return JVMTI_ERROR_OUT_OF_MEMORY;
15111511
}
15121512
for (jint idx = 0; idx < len; idx++) {
1513-
array[idx] = JNIHandles::make_local(Thread::current(), _tbl->at(idx).resolve());
1513+
array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve());
15141514
}
15151515
_tbl = NULL;
15161516
*modules_ptr = array;

src/hotspot/share/prims/jvmtiExport.cpp

+2-35
Original file line numberDiff line numberDiff line change
@@ -141,52 +141,25 @@ class JvmtiEventMark : public StackObj {
141141
JavaThread *_thread;
142142
JNIEnv* _jni_env;
143143
JvmtiThreadState::ExceptionState _saved_exception_state;
144-
#if 0
145-
JNIHandleBlock* _hblock;
146-
#endif
147144

148145
public:
149146
JvmtiEventMark(JavaThread *thread) : _thread(thread),
150147
_jni_env(thread->jni_environment()),
151148
_saved_exception_state(JvmtiThreadState::ES_CLEARED) {
152-
#if 0
153-
_hblock = thread->active_handles();
154-
_hblock->clear_thoroughly(); // so we can be safe
155-
#else
156-
// we want to use the code above - but that needs the JNIHandle changes - later...
157-
// for now, steal JNI push local frame code
158149
JvmtiThreadState *state = thread->jvmti_thread_state();
159150
// we are before an event.
160151
// Save current jvmti thread exception state.
161152
if (state != NULL) {
162153
_saved_exception_state = state->get_exception_state();
163154
}
164155

165-
JNIHandleBlock* old_handles = thread->active_handles();
166-
JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
167-
assert(new_handles != NULL, "should not be NULL");
168-
new_handles->set_pop_frame_link(old_handles);
169-
thread->set_active_handles(new_handles);
170-
#endif
156+
thread->push_jni_handle_block();
171157
assert(thread == JavaThread::current(), "thread must be current!");
172158
thread->frame_anchor()->make_walkable(thread);
173159
};
174160

175161
~JvmtiEventMark() {
176-
#if 0
177-
_hblock->clear(); // for consistency with future correct behavior
178-
#else
179-
// we want to use the code above - but that needs the JNIHandle changes - later...
180-
// for now, steal JNI pop local frame code
181-
JNIHandleBlock* old_handles = _thread->active_handles();
182-
JNIHandleBlock* new_handles = old_handles->pop_frame_link();
183-
assert(new_handles != NULL, "should not be NULL");
184-
_thread->set_active_handles(new_handles);
185-
// Note that we set the pop_frame_link to NULL explicitly, otherwise
186-
// the release_block call will release the blocks.
187-
old_handles->set_pop_frame_link(NULL);
188-
JNIHandleBlock::release_block(old_handles, _thread); // may block
189-
#endif
162+
_thread->pop_jni_handle_block();
190163

191164
JvmtiThreadState* state = _thread->jvmti_thread_state();
192165
// we are continuing after an event.
@@ -196,13 +169,7 @@ class JvmtiEventMark : public StackObj {
196169
}
197170
}
198171

199-
#if 0
200-
jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); }
201-
#else
202-
// we want to use the code above - but that needs the JNIHandle changes - later...
203-
// for now, use regular make_local
204172
jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); }
205-
#endif
206173

207174
jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); }
208175

0 commit comments

Comments
 (0)