Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

8269240: java/foreign/stackwalk/TestAsyncStackWalk.java test failed with concurrent GC #149

Closed
wants to merge 11 commits into from
18 changes: 9 additions & 9 deletions src/hotspot/share/prims/universalUpcallHandler.cpp
Expand Up @@ -52,22 +52,22 @@ void ProgrammableUpcallHandler::upcall_helper(JavaThread* thread, jobject rec, a
}

JavaThread* ProgrammableUpcallHandler::maybe_attach_and_get_thread(bool* should_detach) {
Thread* thread = Thread::current_or_null();
JavaThread* thread = JavaThread::current_or_null();
if (thread == nullptr) {
JavaVM_ *vm = (JavaVM *)(&main_vm);
JNIEnv* p_env = nullptr; // unused
jint result = vm->functions->AttachCurrentThread(vm, (void**) &p_env, nullptr);
guarantee(result == JNI_OK, "Could not attach thread for upcall. JNI error code: %d", result);
*should_detach = true;
thread = Thread::current();
assert(!thread->as_Java_thread()->has_last_Java_frame(), "newly-attached thread not expected to have last Java frame");
thread = JavaThread::current();
assert(!thread->has_last_Java_frame(), "newly-attached thread not expected to have last Java frame");
} else {
*should_detach = false;
}
return thread->as_Java_thread();
return thread;
}

void ProgrammableUpcallHandler::detach_thread(Thread* thread) {
void ProgrammableUpcallHandler::detach_current_thread() {
JavaVM_ *vm = (JavaVM *)(&main_vm);
vm->functions->DetachCurrentThread(vm);
}
Expand Down Expand Up @@ -135,7 +135,7 @@ void ProgrammableUpcallHandler::on_exit(OptimizedEntryBlob::FrameData* context)

debug_only(thread->dec_java_call_counter());

// Old thread-local info. has been restored. We are not back in native code.
// Old thread-local info. has been restored. We are now back in native code.
ThreadStateTransition::transition_from_java(thread, _thread_in_native);

thread->frame_anchor()->copy(&context->jfa);
Expand All @@ -147,7 +147,7 @@ void ProgrammableUpcallHandler::on_exit(OptimizedEntryBlob::FrameData* context)
assert(!thread->has_pending_exception(), "Upcall can not throw an exception");

if (context->should_detach) {
detach_thread(thread);
detach_current_thread();
}
}

Expand All @@ -157,11 +157,11 @@ void ProgrammableUpcallHandler::attach_thread_and_do_upcall(jobject rec, address

{
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));
upcall_helper(thread->as_Java_thread(), rec, buff);
upcall_helper(thread, rec, buff);
}

if (should_detach) {
detach_thread(thread);
detach_current_thread();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/prims/universalUpcallHandler.hpp
Expand Up @@ -49,7 +49,7 @@ class ProgrammableUpcallHandler {

static void handle_uncaught_exception(oop exception);
static JavaThread* maybe_attach_and_get_thread(bool* should_detach);
static void detach_thread(Thread* thread);
static void detach_current_thread();

static JavaThread* on_entry(OptimizedEntryBlob::FrameData* context);
static void on_exit(OptimizedEntryBlob::FrameData* context);
Expand Down
7 changes: 7 additions & 0 deletions src/hotspot/share/runtime/thread.hpp
Expand Up @@ -1423,6 +1423,8 @@ class JavaThread: public Thread {
public:
// Returns the running thread as a JavaThread
static inline JavaThread* current();
// Returns the current thread as a JavaThread, or NULL if not attached
static inline JavaThread* current_or_null();
Comment on lines +1426 to +1427
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't intended to suggest the introduction of this method, but I see now how my comment led to it. It is fine.


// Returns the active Java thread. Do not use this if you know you are calling
// from a JavaThread, as it's slower than JavaThread::current. If called from
Expand Down Expand Up @@ -1593,6 +1595,11 @@ inline JavaThread* JavaThread::current() {
return Thread::current()->as_Java_thread();
}

inline JavaThread* JavaThread::current_or_null() {
Thread* current = Thread::current_or_null();
return current != nullptr ? current->as_Java_thread() : nullptr;
}

inline JavaThread* Thread::as_Java_thread() {
assert(is_Java_thread(), "incorrect cast to JavaThread");
return static_cast<JavaThread*>(this);
Expand Down