Skip to content

Commit f4b79e1

Browse files
authored
Merge pull request #21513 from fengxue-IS/051-jep491-GetCurrentContendedMonitor
(v0.51.0) Update GetCurrentContendedMonitor to support JEP491
2 parents e854ad7 + fac0a9c commit f4b79e1

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

runtime/jvmti/jvmtiThread.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,9 @@ jvmtiGetCurrentContendedMonitor(jvmtiEnv *env,
926926
rc = getCurrentVMThread(vm, &currentThread);
927927
if (JVMTI_ERROR_NONE == rc) {
928928
J9VMThread *targetThread = NULL;
929+
J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions;
929930

930-
vm->internalVMFunctions->internalEnterVMFromJNI(currentThread);
931+
vmFuncs->internalEnterVMFromJNI(currentThread);
931932

932933
ENSURE_PHASE_LIVE(env);
933934
ENSURE_CAPABILITY(env, can_get_current_contended_monitor);
@@ -944,35 +945,51 @@ jvmtiGetCurrentContendedMonitor(jvmtiEnv *env,
944945
#if JAVA_SPEC_VERSION >= 19
945946
j9object_t threadObject = (NULL == thread) ? currentThread->threadObject : J9_JNI_UNWRAP_REFERENCE(thread);
946947

947-
/* Unmounted VirtualThread and CarrierThread with VirtualThread mounted cannot be contended. */
948-
if (((NULL == targetThread) && IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObject))
949-
|| ((NULL != targetThread) && (threadObject == targetThread->carrierThreadObject) && (NULL != targetThread->currentContinuation))
950-
) {
948+
if ((NULL == targetThread) && IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObject)) {
949+
#if JAVA_SPEC_VERSION >= 24
950+
j9object_t continuationObject = J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObject);
951+
952+
if (NULL != continuationObject) {
953+
j9object_t syncObject = J9VMJDKINTERNALVMCONTINUATION_BLOCKER(currentThread, continuationObject);
954+
955+
/* Check if the Continuation.blocker field is set. */
956+
if (NULL != syncObject) {
957+
U_32 state = J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObject);
958+
if ((JVMTI_VTHREAD_STATE_BLOCKING == state) || (JVMTI_VTHREAD_STATE_BLOCKED == state)) {
959+
rv_monitor = (jobject)vmFuncs->j9jni_createLocalRef((JNIEnv *)currentThread, syncObject);
960+
}
961+
}
962+
}
963+
#endif /* JAVA_SPEC_VERSION >= 24 */
964+
/* Prior to JDK24, an unmounted VirtualThread cannot be contended. */
965+
goto release;
966+
} else if ((NULL != targetThread) && (threadObject == targetThread->carrierThreadObject) && (NULL != targetThread->currentContinuation)) {
967+
/* CarrierThread with VirtualThread mounted cannot be contended. */
951968
goto release;
952969
}
953970
#endif /* JAVA_SPEC_VERSION >= 19 */
954971

955972
/* CMVC 184481 - The targetThread should be suspended while we attempt to get its state. */
956-
vm->internalVMFunctions->haltThreadForInspection(currentThread, targetThread);
973+
vmFuncs->haltThreadForInspection(currentThread, targetThread);
957974

958975
vmstate = getVMThreadObjectStatesAll(targetThread, &lockObject, NULL, NULL);
959976

960977
if ((NULL != lockObject)
961978
&& (OMR_ARE_NO_BITS_SET(vmstate, J9VMTHREAD_STATE_PARKED | J9VMTHREAD_STATE_PARKED_TIMED))
962979
) {
963-
rv_monitor = (jobject)vm->internalVMFunctions->j9jni_createLocalRef((JNIEnv *)currentThread, lockObject);
980+
rv_monitor = (jobject)vmFuncs->j9jni_createLocalRef((JNIEnv *)currentThread, lockObject);
964981
} else {
965982
rv_monitor = NULL;
966983
}
967984

968-
vm->internalVMFunctions->resumeThreadForInspection(currentThread, targetThread);
985+
vmFuncs->resumeThreadForInspection(currentThread, targetThread);
969986
#if JAVA_SPEC_VERSION >= 19
970987
release:
971988
#endif /* JAVA_SPEC_VERSION >= 19 */
972989
releaseVMThread(currentThread, targetThread, thread);
973990
}
974991
done:
975-
vm->internalVMFunctions->internalExitVMToJNI(currentThread);
992+
vmFuncs->internalExitVMToJNI(currentThread);
976993
}
977994

978995
if (NULL != monitor_ptr) {

0 commit comments

Comments
 (0)