Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-fetch objects after VM access is released and reacquired #18420

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions runtime/jvmti/jvmtiHelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ getVMThread(J9VMThread *currentThread, jthread thread, J9VMThread **vmThreadPtr,
#if JAVA_SPEC_VERSION >= 19
isVirtualThread = IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObject);
if (isVirtualThread) {
jint vthreadState = 0;
j9object_t carrierThread = NULL;
vm->internalVMFunctions->acquireVThreadInspector(currentThread, thread, TRUE);

jint vthreadState = J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObject);
j9object_t carrierThread = (j9object_t)J9VMJAVALANGVIRTUALTHREAD_CARRIERTHREAD(currentThread, threadObject);
/* Re-fetch threadObject since acquireVThreadInspector can release and reacquire VM access. */
threadObject = J9_JNI_UNWRAP_REFERENCE(thread);
vthreadState = J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObject);
carrierThread = (j9object_t)J9VMJAVALANGVIRTUALTHREAD_CARRIERTHREAD(currentThread, threadObject);
if (NULL != carrierThread) {
targetThread = J9VMJAVALANGTHREAD_THREADREF(currentThread, carrierThread);
}
Expand Down Expand Up @@ -1659,7 +1662,7 @@ setEventNotificationMode(J9JVMTIEnv * j9env, J9VMThread * currentThread, jint mo
if (event_thread == NULL) {
eventMap = &(j9env->globalEventEnable);
} else {
j9object_t threadObject = J9_JNI_UNWRAP_REFERENCE(event_thread);
j9object_t threadObject = NULL;
J9VMThread *vmThreadForTLS = NULL;
rc = getVMThread(
currentThread, event_thread, &targetThread, JVMTI_ERROR_NONE,
Expand All @@ -1668,6 +1671,10 @@ setEventNotificationMode(J9JVMTIEnv * j9env, J9VMThread * currentThread, jint mo
goto done;
}
vmThreadForTLS = targetThread;
/* Fetch threadObject after getVMThread because getVMThread can release and
* reacquire VM access.
*/
threadObject = J9_JNI_UNWRAP_REFERENCE(event_thread);
#if JAVA_SPEC_VERSION >= 19
rc = allocateTLS(vm, threadObject);
if (JVMTI_ERROR_NONE != rc) {
Expand Down
7 changes: 4 additions & 3 deletions runtime/vm/ContinuationHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,18 @@ acquireVThreadInspector(J9VMThread *currentThread, jobject thread, BOOLEAN spin)
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
MM_ObjectAccessBarrierAPI objectAccessBarrier = MM_ObjectAccessBarrierAPI(currentThread);
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
I_64 vthreadInspectorCount;
j9object_t threadObj = NULL;
I_64 vthreadInspectorCount = 0;
retry:
/* Consistently re-fetch threadObj for all the cases below. */
threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
vthreadInspectorCount = J9OBJECT_I64_LOAD(currentThread, threadObj, vm->virtualThreadInspectorCountOffset);
if (vthreadInspectorCount < 0) {
/* Thread is in transition, wait. */
vmFuncs->internalExitVMToJNI(currentThread);
VM_AtomicSupport::yieldCPU();
/* After wait, the thread may suspend here. */
vmFuncs->internalEnterVMFromJNI(currentThread);
threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
if (spin) {
goto retry;
} else {
Expand Down