--- old/src/hotspot/share/runtime/objectMonitor.cpp 2019-11-04 14:59:01.000000000 -0500 +++ new/src/hotspot/share/runtime/objectMonitor.cpp 2019-11-04 14:59:01.000000000 -0500 @@ -239,7 +239,8 @@ // Enter support void ObjectMonitor::enter(TRAPS) { - ADIM_guarantee(ref_count() > 0, "must be positive: ref_count=%d", ref_count()); + jint l_ref_count = ref_count(); + ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count()); // The following code is ordered to check the most common cases first // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors. @@ -257,10 +258,10 @@ return; } - if (Self->is_lock_owned ((address)cur)) { + if (Self->is_lock_owned((address)cur)) { assert(_recursions == 0, "internal state error"); _recursions = 1; - set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*. + simply_set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*. return; } @@ -300,7 +301,7 @@ JavaThread * jt = (JavaThread *) Self; assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); assert(jt->thread_state() != _thread_blocked, "invariant"); - assert(AsyncDeflateIdleMonitors || this->object() != NULL, "invariant"); + assert(this->object() != NULL, "invariant"); assert(_contentions >= 0, "must not be negative: contentions=%d", _contentions); // Prevent deflation. See ObjectSynchronizer::deflate_monitor(), @@ -506,6 +507,8 @@ } else if (_owner != DEFLATER_MARKER) { ss->print("owner=" INTPTR_FORMAT, p2i(_owner)); } else { + // We report NULL instead of DEFLATER_MARKER here because is_busy() + // ignores DEFLATER_MARKER values. ss->print("owner=" INTPTR_FORMAT, NULL); } ss->print(", cxq=" INTPTR_FORMAT ", EntryList=" INTPTR_FORMAT, p2i(_cxq), @@ -516,7 +519,8 @@ #define MAX_RECHECK_INTERVAL 1000 void ObjectMonitor::EnterI(TRAPS) { - ADIM_guarantee(ref_count() > 0, "must be positive: ref_count=%d", ref_count()); + jint l_ref_count = ref_count(); + ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count()); Thread * const Self = THREAD; assert(Self->is_Java_thread(), "invariant"); @@ -770,7 +774,8 @@ // In the future we should reconcile EnterI() and ReenterI(). void ObjectMonitor::ReenterI(Thread * Self, ObjectWaiter * SelfNode) { - ADIM_guarantee(ref_count() > 0, "must be positive: ref_count=%d", ref_count()); + jint l_ref_count = ref_count(); + ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count()); assert(Self != NULL, "invariant"); assert(SelfNode != NULL, "invariant"); @@ -990,7 +995,7 @@ void* cur = _owner; if (THREAD->is_lock_owned((address)cur)) { assert(_recursions == 0, "invariant"); - set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*. + simply_set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*. _recursions = 0; } else { // Apparent unbalanced locking ... @@ -1036,8 +1041,6 @@ // release semantics: prior loads and stores from within the critical section // must not float (reorder) past the following store that drops the lock. - // On SPARC that requires MEMBAR #loadstore|#storestore. - // But of course in TSO #loadstore|#storestore is not required. if (AsyncDeflateIdleMonitors) { set_owner_from(NULL, Self); } else { @@ -1253,7 +1256,7 @@ void* cur = _owner; if (THREAD->is_lock_owned((address)cur)) { assert(_recursions == 0, "internal state error"); - set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*. + simply_set_owner_from_BasicLock(Self, cur); // Convert from BasicLock* to Thread*. _recursions = 0; } } @@ -1303,7 +1306,7 @@ } void* cur = _owner; if (THREAD->is_lock_owned((address)cur)) { - set_owner_from_BasicLock(THREAD, cur); // Convert from BasicLock* to Thread*. + simply_set_owner_from_BasicLock(THREAD, cur); // Convert from BasicLock* to Thread*. _recursions = 0; return true; } @@ -2072,12 +2075,6 @@ DEBUG_ONLY(InitDone = true;) } -// For internal use by ObjectSynchronizer::monitors_iterate(). -ObjectMonitorHandle::ObjectMonitorHandle(ObjectMonitor * om_ptr) { - om_ptr->inc_ref_count(); - _om_ptr = om_ptr; -} - ObjectMonitorHandle::~ObjectMonitorHandle() { if (_om_ptr != NULL) { _om_ptr->dec_ref_count(); @@ -2099,7 +2096,7 @@ guarantee(mark.has_monitor(), "sanity check: mark=" INTPTR_FORMAT, mark.value()); - ObjectMonitor * om_ptr = mark.monitor(); + ObjectMonitor* om_ptr = mark.monitor(); om_ptr->inc_ref_count(); if (AsyncDeflateIdleMonitors) { @@ -2141,7 +2138,9 @@ } // For internal use by ObjectSynchronizer::inflate(). -void ObjectMonitorHandle::set_om_ptr(ObjectMonitor * om_ptr) { +// This function is only used when we don't have to worry about async +// deflation of the specified ObjectMonitor*. +void ObjectMonitorHandle::set_om_ptr(ObjectMonitor* om_ptr) { if (_om_ptr == NULL) { ADIM_guarantee(om_ptr != NULL, "cannot clear an unset om_ptr"); om_ptr->inc_ref_count(); @@ -2153,6 +2152,52 @@ } } +// Save the specified ObjectMonitor* if it is safe, i.e., not being +// async deflated. +// +// This function returns true if the ObjectMonitor* has been safely +// saved. This function returns false if the specified ObjectMonitor* +// is NULL or if we have lost a race with async deflation; the caller +// can retry as appropriate. +bool ObjectMonitorHandle::set_om_ptr_if_safe(ObjectMonitor* om_ptr) { + if (om_ptr == NULL) { + return false; // Nothing to save if input is NULL + } + + om_ptr->inc_ref_count(); + + if (AsyncDeflateIdleMonitors) { + if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) { + // Async deflation is in progress and our ref_count increment + // above lost the race to async deflation. + om_ptr->dec_ref_count(); + return false; + } + if (om_ptr->ref_count() <= 0) { + // Async deflation is in the process of bailing out, but has not + // yet restored the ref_count field so we return false to force + // a retry. We want a positive ref_count value for a true return. + om_ptr->dec_ref_count(); + return false; + } + // Unlike save_om_ptr(), we don't have context to determine if + // the ObjectMonitor has been deflated and reused for another + // object. + } + + ADIM_guarantee(_om_ptr == NULL, "sanity check: _om_ptr=" INTPTR_FORMAT, + p2i(_om_ptr)); + _om_ptr = om_ptr; + return true; +} + +// Unset the _om_ptr field and decrement the ref_count field. +void ObjectMonitorHandle::unset_om_ptr() { + ADIM_guarantee(_om_ptr != NULL, "_om_ptr must not be NULL"); + _om_ptr->dec_ref_count(); + _om_ptr = NULL; +} + void ObjectMonitor::print_on(outputStream* st) const { // The minimal things to print for markWord printing, more can be added for debugging and logging. st->print("{contentions=0x%08x,waiters=0x%08x" @@ -2201,7 +2246,7 @@ // } // void ObjectMonitor::print_debug_style_on(outputStream* st) const { - st->print_cr("(ObjectMonitor *) " INTPTR_FORMAT " = {", p2i(this)); + st->print_cr("(ObjectMonitor*) " INTPTR_FORMAT " = {", p2i(this)); st->print_cr(" _header = " INTPTR_FORMAT, header().value()); st->print_cr(" _object = " INTPTR_FORMAT, p2i(_object)); st->print(" _allocation_state = ");