Skip to content

Commit ec91413

Browse files
committed
review v4
1 parent b18c83a commit ec91413

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/hotspot/share/runtime/lightweightSynchronizer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,9 @@ ObjectMonitor* LightweightSynchronizer::inflate_and_enter(oop object, BasicLock*
10291029
}
10301030

10311031
if (monitor->try_enter(locking_thread)) {
1032+
if (monitor->has_successor(current)) {
1033+
monitor->clear_successor();
1034+
}
10321035
return monitor;
10331036
}
10341037

@@ -1144,6 +1147,7 @@ ObjectMonitor* LightweightSynchronizer::inflate_and_enter(oop object, BasicLock*
11441147

11451148
if (current == locking_thread) {
11461149
monitor->enter_with_contention_mark(locking_thread, contention_mark);
1150+
assert(!monitor->has_successor(current), "what?");
11471151
} else {
11481152
monitor->enter_for_with_contention_mark(locking_thread, contention_mark);
11491153
}

src/hotspot/share/runtime/objectMonitor.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/hotspot/share/runtime/objectMonitor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ class ObjectMonitor : public CHeapObj<mtObjectMonitor> {
456456
TryLockResult try_lock(JavaThread* current);
457457

458458
bool try_spin(JavaThread* current);
459+
bool final_try_spin(JavaThread* current);
459460
bool short_fixed_spin(JavaThread* current, int spin_count, bool adapt);
460461
void exit_epilog(JavaThread* current, ObjectWaiter* Wakee);
461462

0 commit comments

Comments
 (0)