Skip to content

Commit

Permalink
Re-fetch objects after VM access is released and reacquired
Browse files Browse the repository at this point in the history
Stale objects can manifest many issues. In this case, stale objects
cause complex and intermittent synchronization issues.

Related: eclipse-openj9#17865
Related: eclipse-openj9#17869
Related: eclipse-openj9#18370

Signed-off-by: Babneet Singh <sbabneet@ca.ibm.com>
  • Loading branch information
babsingh committed Nov 9, 2023
1 parent 2dc399e commit a1cdacf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
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

0 comments on commit a1cdacf

Please sign in to comment.