From 5f7432f7b1ca52941b717fd35a929c91fbdd276d Mon Sep 17 00:00:00 2001 From: Serguei Spitsyn Date: Tue, 26 Mar 2024 02:21:47 +0000 Subject: [PATCH] 8328758: GetCurrentContendedMonitor function should use JvmtiHandshake Reviewed-by: lmesnik, pchilanomate --- src/hotspot/share/prims/jvmtiEnv.cpp | 42 ++++-------------------- src/hotspot/share/prims/jvmtiEnvBase.cpp | 10 ++++++ src/hotspot/share/prims/jvmtiEnvBase.hpp | 17 +++++----- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/src/hotspot/share/prims/jvmtiEnv.cpp b/src/hotspot/share/prims/jvmtiEnv.cpp index c6ddc501d1a..43f372b3a28 100644 --- a/src/hotspot/share/prims/jvmtiEnv.cpp +++ b/src/hotspot/share/prims/jvmtiEnv.cpp @@ -1463,44 +1463,14 @@ JvmtiEnv::GetOwnedMonitorStackDepthInfo(jthread thread, jint* monitor_info_count // monitor_ptr - pre-checked for null jvmtiError JvmtiEnv::GetCurrentContendedMonitor(jthread thread, jobject* monitor_ptr) { - JavaThread* calling_thread = JavaThread::current(); - HandleMark hm(calling_thread); + JavaThread* current = JavaThread::current(); - JvmtiVTMSTransitionDisabler disabler(thread); - ThreadsListHandle tlh(calling_thread); + *monitor_ptr = nullptr; - JavaThread* java_thread = nullptr; - oop thread_oop = nullptr; - jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, &java_thread, &thread_oop); - if (err != JVMTI_ERROR_NONE) { - return err; - } - - if (java_lang_VirtualThread::is_instance(thread_oop)) { - // There is no monitor info to collect if target virtual thread is unmounted. - if (java_thread != nullptr) { - GetCurrentContendedMonitorClosure op(calling_thread, this, monitor_ptr, /* is_virtual */ true); - Handshake::execute(&op, java_thread); - err = op.result(); - } else { - *monitor_ptr = nullptr; - if (!JvmtiEnvBase::is_vthread_alive(thread_oop)) { - err = JVMTI_ERROR_THREAD_NOT_ALIVE; - } - } - return err; - } - if (java_thread == calling_thread) { - // It is only safe to make a direct call on the current thread. - // All other usage needs to use a direct handshake for safety. - err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr, /* is_virtual */ false); - } else { - // get contended monitor information with handshake - GetCurrentContendedMonitorClosure op(calling_thread, this, monitor_ptr, /* is_virtual */ false); - Handshake::execute(&op, java_thread); - err = op.result(); - } - return err; + // get contended monitor information with handshake + GetCurrentContendedMonitorClosure op(this, current, monitor_ptr); + JvmtiHandshake::execute(&op, thread); + return op.result(); } /* end GetCurrentContendedMonitor */ diff --git a/src/hotspot/share/prims/jvmtiEnvBase.cpp b/src/hotspot/share/prims/jvmtiEnvBase.cpp index 44b1acf8043..e06b118d2f3 100644 --- a/src/hotspot/share/prims/jvmtiEnvBase.cpp +++ b/src/hotspot/share/prims/jvmtiEnvBase.cpp @@ -2497,6 +2497,16 @@ GetCurrentContendedMonitorClosure::do_thread(Thread *target) { } } +void +GetCurrentContendedMonitorClosure::do_vthread(Handle target_h) { + if (_target_jt == nullptr) { + _result = JVMTI_ERROR_NONE; // target virtual thread is unmounted + return; + } + // mounted virtual thread case + do_thread(_target_jt); +} + void GetStackTraceClosure::do_thread(Thread *target) { Thread* current = Thread::current(); diff --git a/src/hotspot/share/prims/jvmtiEnvBase.hpp b/src/hotspot/share/prims/jvmtiEnvBase.hpp index fedd447490a..25e7d5c371b 100644 --- a/src/hotspot/share/prims/jvmtiEnvBase.hpp +++ b/src/hotspot/share/prims/jvmtiEnvBase.hpp @@ -593,21 +593,22 @@ public: }; // HandshakeClosure to get current contended monitor. It is used for both platform and virtual threads. -class GetCurrentContendedMonitorClosure : public JvmtiHandshakeClosure { +class GetCurrentContendedMonitorClosure : public JvmtiUnitedHandshakeClosure { private: - JavaThread *_calling_thread; JvmtiEnv *_env; + JavaThread *_calling_thread; jobject *_owned_monitor_ptr; - bool _is_virtual; public: - GetCurrentContendedMonitorClosure(JavaThread* calling_thread, JvmtiEnv *env, jobject *mon_ptr, bool is_virtual) - : JvmtiHandshakeClosure("GetCurrentContendedMonitor"), - _calling_thread(calling_thread), + GetCurrentContendedMonitorClosure(JvmtiEnv *env, + JavaThread* calling_thread, + jobject *owned_monitor_ptr) + : JvmtiUnitedHandshakeClosure("GetCurrentContendedMonitor"), _env(env), - _owned_monitor_ptr(mon_ptr), - _is_virtual(is_virtual) {} + _calling_thread(calling_thread), + _owned_monitor_ptr(owned_monitor_ptr) {} void do_thread(Thread *target); + void do_vthread(Handle target_h); }; // HandshakeClosure to get stack trace.