@@ -758,7 +758,7 @@ exitVThreadTransitionCritical(J9VMThread *currentThread, jobject thread)
758
758
759
759
#if JAVA_SPEC_VERSION >= 24
760
760
J9ObjectMonitor *
761
- detachMonitorInfo (J9VMThread *currentThread, j9object_t lockObject)
761
+ detachMonitorInfo (J9VMThread *currentThread, j9object_t lockObject, BOOLEAN *alreadyDetached )
762
762
{
763
763
J9ObjectMonitor *objectMonitor = NULL ;
764
764
j9objectmonitor_t lock = 0 ;
@@ -783,11 +783,19 @@ detachMonitorInfo(J9VMThread *currentThread, j9object_t lockObject)
783
783
objectMonitor = J9_INFLLOCK_OBJECT_MONITOR (lock);
784
784
}
785
785
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;
789
786
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
+ }
791
799
792
800
return objectMonitor;
793
801
}
@@ -796,9 +804,15 @@ void
796
804
updateMonitorInfo (J9VMThread *currentThread, J9ObjectMonitor *objectMonitor)
797
805
{
798
806
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
+ }
802
816
}
803
817
804
818
UDATA
@@ -823,13 +837,15 @@ walkFrameMonitorEnterRecords(J9VMThread *currentThread, J9StackWalkState *walkSt
823
837
) {
824
838
j9object_t obj = monitorEnterRecords->object ;
825
839
if (obj != targetSyncObject) {
826
- J9ObjectMonitor *mon = detachMonitorInfo (currentThread, obj);
840
+ BOOLEAN alreadyDetached = FALSE ;
841
+ J9ObjectMonitor *mon = detachMonitorInfo (currentThread, obj, &alreadyDetached);
827
842
if (NULL == mon) {
828
843
return J9_STACKWALK_RC_NO_MEMORY;
844
+ } else if (!alreadyDetached) {
845
+ mon->next = objMonitorHead;
846
+ objMonitorHead = mon;
847
+ monitorCount++;
829
848
}
830
- mon->next = objMonitorHead;
831
- objMonitorHead = mon;
832
- monitorCount++;
833
849
}
834
850
monitorEnterRecords = monitorEnterRecords->next ;
835
851
}
@@ -854,13 +870,15 @@ walkFrameMonitorEnterRecords(J9VMThread *currentThread, J9StackWalkState *walkSt
854
870
}
855
871
856
872
if (syncObject != targetSyncObject) {
857
- J9ObjectMonitor *mon = detachMonitorInfo (currentThread, syncObject);
873
+ BOOLEAN alreadyDetached = FALSE ;
874
+ J9ObjectMonitor *mon = detachMonitorInfo (currentThread, syncObject, &alreadyDetached);
858
875
if (NULL == mon) {
859
876
return J9_STACKWALK_RC_NO_MEMORY;
877
+ } else if (!alreadyDetached) {
878
+ mon->next = objMonitorHead;
879
+ objMonitorHead = mon;
880
+ monitorCount++;
860
881
}
861
- mon->next = objMonitorHead;
862
- objMonitorHead = mon;
863
- monitorCount++;
864
882
}
865
883
}
866
884
@@ -1073,14 +1091,16 @@ preparePinnedVirtualThreadForUnmount(J9VMThread *currentThread, j9object_t syncO
1073
1091
j9object_t object = monitorRecords->object ;
1074
1092
1075
1093
if (syncObj != object) {
1076
- J9ObjectMonitor *objectMonitor = detachMonitorInfo (currentThread, object);
1094
+ BOOLEAN alreadyDetached = FALSE ;
1095
+ J9ObjectMonitor *objectMonitor = detachMonitorInfo (currentThread, object, &alreadyDetached);
1077
1096
if (NULL == objectMonitor) {
1078
1097
result = J9_OBJECT_MONITOR_OOM;
1079
1098
goto done;
1099
+ } else if (!alreadyDetached) {
1100
+ objectMonitor->next = enteredMonitorsList;
1101
+ enteredMonitorsList = objectMonitor;
1102
+ monitorCount++;
1080
1103
}
1081
- objectMonitor->next = enteredMonitorsList;
1082
- enteredMonitorsList = objectMonitor;
1083
- monitorCount++;
1084
1104
}
1085
1105
monitorRecords = monitorRecords->next ;
1086
1106
}
0 commit comments