Skip to content

Commit 1ea7879

Browse files
authored
Merge pull request #18964 from babsingh/main6
Update JVMTI InterruptThread for virtual threads
2 parents 8ce1cab + b4a5edf commit 1ea7879

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

runtime/jvmti/jvmtiThread.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,24 @@ jvmtiInterruptThread(jvmtiEnv *env,
442442
J9JavaVM *vm = JAVAVM_FROM_ENV(env);
443443
J9VMThread *currentThread = NULL;
444444
jvmtiError rc = JVMTI_ERROR_NONE;
445+
#if JAVA_SPEC_VERSION >= 19
446+
BOOLEAN isVirtualThread = FALSE;
447+
J9Class *vThreadClass = NULL;
448+
jclass vThreadJClass = NULL;
449+
#endif /* JAVA_SPEC_VERSION >= 19 */
445450

446451
Trc_JVMTI_jvmtiInterruptThread_Entry(env);
447452

448453
rc = getCurrentVMThread(vm, &currentThread);
449454
if (JVMTI_ERROR_NONE == rc) {
450455
J9VMThread *targetThread = NULL;
456+
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
457+
#if JAVA_SPEC_VERSION >= 19
458+
JNIEnv *jniEnv = (JNIEnv *)currentThread;
459+
#endif /* JAVA_SPEC_VERSION >= 19 */
460+
461+
vmFuncs->internalEnterVMFromJNI(currentThread);
451462

452-
vm->internalVMFunctions->internalEnterVMFromJNI(currentThread);
453-
454463
ENSURE_PHASE_LIVE(env);
455464
ENSURE_CAPABILITY(env, can_signal_thread);
456465

@@ -459,7 +468,15 @@ jvmtiInterruptThread(jvmtiEnv *env,
459468
J9JVMTI_GETVMTHREAD_ERROR_ON_NULL_JTHREAD | J9JVMTI_GETVMTHREAD_ERROR_ON_DEAD_THREAD);
460469
if (JVMTI_ERROR_NONE == rc) {
461470
#if JAVA_SPEC_VERSION >= 19
462-
if (NULL != targetThread)
471+
j9object_t threadObject = J9_JNI_UNWRAP_REFERENCE(thread);
472+
isVirtualThread = IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObject);
473+
474+
if (isVirtualThread && (NULL == vm->vThreadInterrupt)) {
475+
vThreadClass = J9VMJAVALANGVIRTUALTHREAD_OR_NULL(vm);
476+
vThreadJClass = (jclass)vmFuncs->j9jni_createLocalRef(jniEnv, vThreadClass->classObject);
477+
}
478+
479+
if ((NULL != targetThread) && !isVirtualThread)
463480
#endif /* JAVA_SPEC_VERSION >= 19 */
464481
{
465482
omrthread_interrupt(targetThread->osThread);
@@ -472,8 +489,30 @@ jvmtiInterruptThread(jvmtiEnv *env,
472489
releaseVMThread(currentThread, targetThread, thread);
473490
}
474491
done:
475-
vm->internalVMFunctions->internalExitVMToJNI(currentThread);
492+
vmFuncs->internalExitVMToJNI(currentThread);
493+
494+
#if JAVA_SPEC_VERSION >= 19
495+
if ((JVMTI_ERROR_NONE == rc) && isVirtualThread) {
496+
if (NULL == vm->vThreadInterrupt) {
497+
jmethodID vThreadInterrupt = jniEnv->GetMethodID(vThreadJClass, "interrupt", "()V");
498+
499+
if (NULL == vThreadInterrupt) {
500+
rc = JVMTI_ERROR_INTERNAL;
501+
goto exit;
502+
}
503+
504+
vm->vThreadInterrupt = vThreadInterrupt;
505+
}
506+
507+
jniEnv->CallObjectMethod(thread, vm->vThreadInterrupt);
508+
509+
if (jniEnv->ExceptionOccurred()) {
510+
rc = JVMTI_ERROR_INTERNAL;
511+
}
512+
}
476513
}
514+
exit:
515+
#endif /* JAVA_SPEC_VERSION >= 19 */
477516

478517
TRACE_JVMTI_RETURN(jvmtiInterruptThread);
479518
}

runtime/oti/j9nonbuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5928,6 +5928,9 @@ typedef struct J9JavaVM {
59285928
jmethodID addOpens;
59295929
jmethodID addUses;
59305930
jmethodID addProvides;
5931+
#if JAVA_SPEC_VERSION >= 19
5932+
jmethodID vThreadInterrupt;
5933+
#endif /* JAVA_SPEC_VERSION >= 19 */
59315934
UDATA addModulesCount;
59325935
UDATA safePointState;
59335936
UDATA safePointResponseCount;

0 commit comments

Comments
 (0)