Skip to content

Commit d01df7a

Browse files
authored
Merge pull request #22020 from hangshao0/main
Do not re-detach a monitor that is already detached
2 parents 42b37ca + 638668c commit d01df7a

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

runtime/codert_vm/jswalk.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,13 +1881,15 @@ walkLiveMonitorSlotsForYield(J9StackWalkState *walkState, J9JITStackAtlas *gcSta
18811881
j9object_t obj = *objAddress;
18821882

18831883
if ((NULL != obj) && (targetSyncObject != obj)) {
1884-
J9ObjectMonitor *mon = vmFuncs->detachMonitorInfo(currentThread, obj);
1884+
BOOLEAN alreadyDetached = FALSE;
1885+
J9ObjectMonitor *mon = vmFuncs->detachMonitorInfo(currentThread, obj, &alreadyDetached);
18851886
if (NULL == mon) {
18861887
return J9_STACKWALK_RC_NO_MEMORY;
1888+
} else if (!alreadyDetached) {
1889+
mon->next = objMonitorHead;
1890+
objMonitorHead = mon;
1891+
monitorCount++;
18871892
}
1888-
mon->next = objMonitorHead;
1889-
objMonitorHead = mon;
1890-
monitorCount++;
18911893
}
18921894
}
18931895
}

runtime/oti/j9nonbuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5407,7 +5407,7 @@ typedef struct J9InternalVMFunctions {
54075407
struct J9ObjectMonitor * (*monitorTablePeek)(struct J9JavaVM *vm, j9object_t object);
54085408
jobject (*takeVirtualThreadListToUnblock)(struct J9VMThread *currentThread);
54095409
UDATA (*preparePinnedVirtualThreadForUnmount)(struct J9VMThread *currentThread, j9object_t syncObj, BOOLEAN isObjectWait);
5410-
J9ObjectMonitor * (*detachMonitorInfo)(struct J9VMThread *currentThread, j9object_t lockObject);
5410+
J9ObjectMonitor * (*detachMonitorInfo)(struct J9VMThread *currentThread, j9object_t lockObject, BOOLEAN *alreadyDetached);
54115411
#endif /* JAVA_SPEC_VERSION >= 24 */
54125412
jobjectArray (*getSystemPropertyList)(JNIEnv *env);
54135413
} J9InternalVMFunctions;

runtime/oti/vm_api.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4803,11 +4803,12 @@ takeVirtualThreadListToUnblock(J9VMThread *currentThread);
48034803
*
48044804
* @param currentThread the current thread
48054805
* @param lockObject the object with monitor to detach
4806+
* @param alreadyDetached whether object monitor has already been detached
48064807
*
48074808
* @return the inflated J9ObjectMonitor pointer
48084809
*/
48094810
J9ObjectMonitor *
4810-
detachMonitorInfo(J9VMThread *currentThread, j9object_t lockObject);
4811+
detachMonitorInfo(J9VMThread *currentThread, j9object_t lockObject, BOOLEAN *alreadyDetached);
48114812
#endif /* JAVA_SPEC_VERSION >= 24 */
48124813
/* ---------------- hookableAsync.c ---------------- */
48134814

runtime/vm/ContinuationHelpers.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ exitVThreadTransitionCritical(J9VMThread *currentThread, jobject thread)
758758

759759
#if JAVA_SPEC_VERSION >= 24
760760
J9ObjectMonitor *
761-
detachMonitorInfo(J9VMThread *currentThread, j9object_t lockObject)
761+
detachMonitorInfo(J9VMThread *currentThread, j9object_t lockObject, BOOLEAN *alreadyDetached)
762762
{
763763
J9ObjectMonitor *objectMonitor = NULL;
764764
j9objectmonitor_t lock = 0;
@@ -783,11 +783,19 @@ detachMonitorInfo(J9VMThread *currentThread, j9object_t lockObject)
783783
objectMonitor = J9_INFLLOCK_OBJECT_MONITOR(lock);
784784
}
785785

786-
J9ThreadAbstractMonitor *monitor = (J9ThreadAbstractMonitor *)objectMonitor->monitor;
787-
Trc_VM_detachMonitorInfo_Detach(currentThread, currentThread->currentContinuation, objectMonitor, monitor, monitor->owner, monitor->count, currentThread->osThread);
788-
monitor->owner = (J9Thread *)J9_OBJECT_MONITOR_OWNER_DETACHED;
789786
Assert_VM_notNull(currentThread->currentContinuation);
790-
objectMonitor->ownerContinuation = currentThread->currentContinuation;
787+
J9ThreadAbstractMonitor *monitor = (J9ThreadAbstractMonitor *)objectMonitor->monitor;
788+
*alreadyDetached = IS_J9_OBJECT_MONITOR_OWNER_DETACHED(monitor->owner);
789+
if (*alreadyDetached) {
790+
/* If the monitor is detached, its ownerContinuation should be set to currentContinuation already. */
791+
Assert_VM_true(objectMonitor->ownerContinuation == currentThread->currentContinuation);
792+
} else if (NULL == objectMonitor->ownerContinuation) {
793+
objectMonitor->ownerContinuation = currentThread->currentContinuation;
794+
Trc_VM_detachMonitorInfo_Detach(currentThread, currentThread->currentContinuation, objectMonitor, monitor, monitor->owner, monitor->count, currentThread->osThread);
795+
monitor->owner = (J9Thread *)J9_OBJECT_MONITOR_OWNER_DETACHED;
796+
} else {
797+
Assert_VM_unreachable();
798+
}
791799

792800
return objectMonitor;
793801
}
@@ -796,9 +804,15 @@ void
796804
updateMonitorInfo(J9VMThread *currentThread, J9ObjectMonitor *objectMonitor)
797805
{
798806
J9ThreadAbstractMonitor *monitor = (J9ThreadAbstractMonitor *)objectMonitor->monitor;
799-
Trc_VM_updateMonitorInfo_Attach(currentThread, currentThread->currentContinuation, objectMonitor, monitor, monitor->owner, monitor->count, currentThread->osThread);
800-
monitor->owner = currentThread->osThread;
801-
objectMonitor->ownerContinuation = NULL;
807+
if (IS_J9_OBJECT_MONITOR_OWNER_DETACHED(monitor->owner)) {
808+
Assert_VM_true(objectMonitor->ownerContinuation == currentThread->currentContinuation);
809+
Trc_VM_updateMonitorInfo_Attach(currentThread, currentThread->currentContinuation, objectMonitor, monitor, monitor->owner, monitor->count, currentThread->osThread);
810+
monitor->owner = currentThread->osThread;
811+
objectMonitor->ownerContinuation = NULL;
812+
} else {
813+
Assert_VM_true(monitor->owner == currentThread->osThread);
814+
Assert_VM_true(NULL == objectMonitor->ownerContinuation);
815+
}
802816
}
803817

804818
UDATA
@@ -823,13 +837,15 @@ walkFrameMonitorEnterRecords(J9VMThread *currentThread, J9StackWalkState *walkSt
823837
) {
824838
j9object_t obj = monitorEnterRecords->object;
825839
if (obj != targetSyncObject) {
826-
J9ObjectMonitor *mon = detachMonitorInfo(currentThread, obj);
840+
BOOLEAN alreadyDetached = FALSE;
841+
J9ObjectMonitor *mon = detachMonitorInfo(currentThread, obj, &alreadyDetached);
827842
if (NULL == mon) {
828843
return J9_STACKWALK_RC_NO_MEMORY;
844+
} else if (!alreadyDetached) {
845+
mon->next = objMonitorHead;
846+
objMonitorHead = mon;
847+
monitorCount++;
829848
}
830-
mon->next = objMonitorHead;
831-
objMonitorHead = mon;
832-
monitorCount++;
833849
}
834850
monitorEnterRecords = monitorEnterRecords->next;
835851
}
@@ -854,13 +870,15 @@ walkFrameMonitorEnterRecords(J9VMThread *currentThread, J9StackWalkState *walkSt
854870
}
855871

856872
if (syncObject != targetSyncObject) {
857-
J9ObjectMonitor *mon = detachMonitorInfo(currentThread, syncObject);
873+
BOOLEAN alreadyDetached = FALSE;
874+
J9ObjectMonitor *mon = detachMonitorInfo(currentThread, syncObject, &alreadyDetached);
858875
if (NULL == mon) {
859876
return J9_STACKWALK_RC_NO_MEMORY;
877+
} else if (!alreadyDetached) {
878+
mon->next = objMonitorHead;
879+
objMonitorHead = mon;
880+
monitorCount++;
860881
}
861-
mon->next = objMonitorHead;
862-
objMonitorHead = mon;
863-
monitorCount++;
864882
}
865883
}
866884

@@ -1073,14 +1091,16 @@ preparePinnedVirtualThreadForUnmount(J9VMThread *currentThread, j9object_t syncO
10731091
j9object_t object = monitorRecords->object;
10741092

10751093
if (syncObj != object) {
1076-
J9ObjectMonitor *objectMonitor = detachMonitorInfo(currentThread, object);
1094+
BOOLEAN alreadyDetached = FALSE;
1095+
J9ObjectMonitor *objectMonitor = detachMonitorInfo(currentThread, object, &alreadyDetached);
10771096
if (NULL == objectMonitor) {
10781097
result = J9_OBJECT_MONITOR_OOM;
10791098
goto done;
1099+
} else if (!alreadyDetached) {
1100+
objectMonitor->next = enteredMonitorsList;
1101+
enteredMonitorsList = objectMonitor;
1102+
monitorCount++;
10801103
}
1081-
objectMonitor->next = enteredMonitorsList;
1082-
enteredMonitorsList = objectMonitor;
1083-
monitorCount++;
10841104
}
10851105
monitorRecords = monitorRecords->next;
10861106
}

0 commit comments

Comments
 (0)