@@ -461,6 +461,9 @@ bool ObjectMonitor::spin_enter(JavaThread* current) {
461461
462462 // Check for recursion.
463463 if (try_enter (current)) {
464+ if (has_successor (current)) {
465+ clear_successor ();
466+ }
464467 return true ;
465468 }
466469
@@ -475,6 +478,9 @@ bool ObjectMonitor::spin_enter(JavaThread* current) {
475478 // Note that if we acquire the monitor from an initial spin
476479 // we forgo posting JVMTI events and firing DTRACE probes.
477480 if (try_spin (current)) {
481+ if (has_successor (current)) {
482+ clear_successor ();
483+ }
478484 assert (has_owner (current), " must be current: owner=" INT64_FORMAT, owner_raw ());
479485 assert (_recursions == 0 , " must be 0: recursions=%zd" , _recursions);
480486 assert_mark_word_consistency ();
@@ -492,7 +498,6 @@ bool ObjectMonitor::enter(JavaThread* current) {
492498 }
493499
494500 assert (!has_owner (current), " invariant" );
495- assert (!has_successor (current), " invariant" );
496501 assert (!SafepointSynchronize::is_at_safepoint (), " invariant" );
497502 assert (current->thread_state () != _thread_blocked, " invariant" );
498503
@@ -551,6 +556,12 @@ void ObjectMonitor::enter_with_contention_mark(JavaThread* current, ObjectMonito
551556 notify_contended_enter (current);
552557 result = Continuation::try_preempt (current, ce->cont_oop (current));
553558 if (result == freeze_ok) {
559+ if (has_successor (current)) {
560+ clear_successor ();
561+ // Invariant: after setting succ=null a contending thread
562+ // must recheck-retry _owner before parking. (Done in vthread_monitor_enter)
563+ OrderAccess::fence ();
564+ }
554565 bool acquired = vthread_monitor_enter (current);
555566 if (acquired) {
556567 // We actually acquired the monitor while trying to add the vthread to the
@@ -927,7 +938,9 @@ void ObjectMonitor::enter_internal(JavaThread* current) {
927938
928939 // Try the lock - TATAS
929940 if (try_lock (current) == TryLockResult::Success) {
930- assert (!has_successor (current), " invariant" );
941+ if (has_successor (current)) {
942+ clear_successor ();
943+ }
931944 assert (has_owner (current), " invariant" );
932945 return ;
933946 }
@@ -941,7 +954,7 @@ void ObjectMonitor::enter_internal(JavaThread* current) {
941954 // to the owner. This has subtle but beneficial affinity
942955 // effects.
943956
944- if (try_spin (current)) {
957+ if (final_try_spin (current)) {
945958 assert (has_owner (current), " invariant" );
946959 assert (!has_successor (current), " invariant" );
947960 return ;
@@ -1106,7 +1119,7 @@ void ObjectMonitor::reenter_internal(JavaThread* current, ObjectWaiter* currentN
11061119
11071120 // If that fails, spin again. Note that spin count may be zero so the above TryLock
11081121 // is necessary.
1109- if (try_spin (current)) {
1122+ if (final_try_spin (current)) {
11101123 break ;
11111124 }
11121125
@@ -2393,12 +2406,22 @@ bool ObjectMonitor::try_spin(JavaThread* current) {
23932406 _SpinDuration = adjust_down (_SpinDuration);
23942407 }
23952408
2409+ return false ;
2410+ }
2411+
2412+ bool ObjectMonitor::final_try_spin (JavaThread* current) {
2413+ if (try_spin (current)) {
2414+ if (has_successor (current)) {
2415+ // We are locked, clear the successor.
2416+ clear_successor ();
2417+ }
2418+ return true ;
2419+ }
2420+
23962421 if (has_successor (current)) {
23972422 clear_successor ();
23982423 // Invariant: after setting succ=null a contending thread
2399- // must recheck-retry _owner before parking. This usually happens
2400- // in the normal usage of try_spin(), but it's safest
2401- // to make try_spin() as foolproof as possible.
2424+ // must recheck-retry _owner before parking.
24022425 OrderAccess::fence ();
24032426 if (try_lock (current) == TryLockResult::Success) {
24042427 return true ;
0 commit comments