< prev index next >

src/hotspot/share/runtime/objectMonitor.cpp

Print this page
rev 58110 : v2.09a with 8235795, 8235931 and 8236035 extracted; rebased to jdk-14+28; merge with 8236035.patch.cr1; merge with 8235795.patch.cr1; merge with 8236035.patch.cr2; merge with 8235795.patch.cr2; merge with 8235795.patch.cr3.
rev 58111 : See CR9-to-CR10-changes; merge with jdk-15+11.


 254     return;
 255   }
 256 
 257   if (cur == Self) {
 258     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 259     _recursions++;
 260     return;
 261   }
 262 
 263   if (Self->is_lock_owned((address)cur)) {
 264     assert(_recursions == 0, "internal state error");
 265     _recursions = 1;
 266     set_owner_from_BasicLock(cur, Self);  // Convert from BasicLock* to Thread*.
 267     return;
 268   }
 269 
 270   if (AsyncDeflateIdleMonitors &&
 271       try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 272     // The deflation protocol finished the first part (setting owner),
 273     // but it failed the second part (making ref_count negative) and
 274     // bailed. Or the ObjectMonitor was async deflated and reused.
 275     // Acquired the monitor.
 276     assert(_recursions == 0, "invariant");
 277     return;
 278   }
 279 
 280   // We've encountered genuine contention.
 281   assert(Self->_Stalled == 0, "invariant");
 282   Self->_Stalled = intptr_t(this);
 283 
 284   // Try one round of spinning *before* enqueueing Self
 285   // and before going through the awkward and expensive state
 286   // transitions.  The following spin is strictly optional ...
 287   // Note that if we acquire the monitor from an initial spin
 288   // we forgo posting JVMTI events and firing DTRACE probes.
 289   if (TrySpin(Self) > 0) {
 290     assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
 291     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 292     assert(((oop)object())->mark() == markWord::encode(this),
 293            "object mark must match encoded this: mark=" INTPTR_FORMAT
 294            ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
 295            markWord::encode(this).value());


 432 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {
 433   // This function must only be called when (owner == DEFLATER_MARKER
 434   // && ref_count <= 0), but we can't guarantee that here because
 435   // those values could change when the ObjectMonitor gets moved from
 436   // the global free list to a per-thread free list.
 437 
 438   guarantee(obj != NULL, "must be non-NULL");
 439   if (object() != obj) {
 440     // ObjectMonitor's object ref no longer refers to the target object
 441     // so the object's header has already been restored.
 442     return;
 443   }
 444 
 445   markWord dmw = header();
 446   if (dmw.value() == 0) {
 447     // ObjectMonitor's header/dmw has been cleared so the ObjectMonitor
 448     // has been deflated and taken off the global free list.
 449     return;
 450   }
 451 
 452   // A non-NULL dmw has to be either neutral (not locked and not marked)
 453   // or is already participating in this restoration protocol.
 454   assert(dmw.is_neutral() || (dmw.is_marked() && dmw.hash() == 0),
 455          "failed precondition: dmw=" INTPTR_FORMAT, dmw.value());
 456 
 457   markWord marked_dmw = markWord::zero();
 458   if (!dmw.is_marked() && dmw.hash() == 0) {
 459     // This dmw has not yet started the restoration protocol so we
 460     // mark a copy of the dmw to begin the protocol.
 461     // Note: A dmw with a hashcode does not take this code path.
 462     marked_dmw = dmw.set_marked();
 463 
 464     // All of the callers to this function can be racing with each
 465     // other trying to update the _header field.
 466     dmw = (markWord) Atomic::cmpxchg(&_header, dmw, marked_dmw);
 467     if (dmw.value() == 0) {
 468       // ObjectMonitor's header/dmw has been cleared so the object's
 469       // header has already been restored.
 470       return;
 471     }
 472     // The _header field is now marked. The winner's 'dmw' variable
 473     // contains the original, unmarked header/dmw value and any
 474     // losers have a marked header/dmw value that will be cleaned
 475     // up below.
 476   }
 477 
 478   if (dmw.is_marked()) {
 479     // Clear the mark from the header/dmw copy in preparation for
 480     // possible restoration from this thread.
 481     assert(dmw.hash() == 0, "hashcode must be 0: dmw=" INTPTR_FORMAT,
 482            dmw.value());
 483     dmw = dmw.set_unmarked();
 484   }
 485   assert(dmw.is_neutral(), "must be neutral: dmw=" INTPTR_FORMAT, dmw.value());
 486 
 487   // Install displaced mark word if the object's header still points
 488   // to this ObjectMonitor. All racing callers to this function will
 489   // reach this point, but only one can win.
 490   obj->cas_set_mark(dmw, markWord::encode(this));








 491 
 492   // Note: It does not matter which thread restored the header/dmw
 493   // into the object's header. The thread deflating the monitor just
 494   // wanted the object's header restored and it is. The threads that
 495   // detected a race with the deflation process also wanted the
 496   // object's header restored before they retry their operation and
 497   // because it is restored they will only retry once.
 498 }
 499 
 500 // Convert the fields used by is_busy() to a string that can be
 501 // used for diagnostic output.
 502 const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
 503   ss->print("is_busy: contentions=%d, waiters=%d, ", _contentions, _waiters);
 504   if (!AsyncDeflateIdleMonitors) {
 505     ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
 506   } else if (_owner != DEFLATER_MARKER) {
 507     ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
 508   } else {
 509     // We report NULL instead of DEFLATER_MARKER here because is_busy()
 510     // ignores DEFLATER_MARKER values.


 520 void ObjectMonitor::EnterI(TRAPS) {
 521   jint l_ref_count = ref_count();
 522   ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count());
 523 
 524   Thread * const Self = THREAD;
 525   assert(Self->is_Java_thread(), "invariant");
 526   assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
 527 
 528   // Try the lock - TATAS
 529   if (TryLock (Self) > 0) {
 530     assert(_succ != Self, "invariant");
 531     assert(_owner == Self, "invariant");
 532     assert(_Responsible != Self, "invariant");
 533     return;
 534   }
 535 
 536   if (AsyncDeflateIdleMonitors &&
 537       try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 538     // The deflation protocol finished the first part (setting owner),
 539     // but it failed the second part (making ref_count negative) and
 540     // bailed. Or the ObjectMonitor was async deflated and reused.
 541     // Acquired the monitor.
 542     assert(_succ != Self, "invariant");
 543     assert(_Responsible != Self, "invariant");
 544     return;
 545   }
 546 
 547   assert(InitDone, "Unexpectedly not initialized");
 548 
 549   // We try one round of spinning *before* enqueueing Self.
 550   //
 551   // If the _owner is ready but OFFPROC we could use a YieldTo()
 552   // operation to donate the remainder of this thread's quantum
 553   // to the owner.  This has subtle but beneficial affinity
 554   // effects.
 555 
 556   if (TrySpin(Self) > 0) {
 557     assert(_owner == Self, "invariant");
 558     assert(_succ != Self, "invariant");
 559     assert(_Responsible != Self, "invariant");
 560     return;
 561   }


 647     assert(_owner != Self, "invariant");
 648 
 649     // park self
 650     if (_Responsible == Self) {
 651       Self->_ParkEvent->park((jlong) recheckInterval);
 652       // Increase the recheckInterval, but clamp the value.
 653       recheckInterval *= 8;
 654       if (recheckInterval > MAX_RECHECK_INTERVAL) {
 655         recheckInterval = MAX_RECHECK_INTERVAL;
 656       }
 657     } else {
 658       Self->_ParkEvent->park();
 659     }
 660 
 661     if (TryLock(Self) > 0) break;
 662 
 663     if (AsyncDeflateIdleMonitors &&
 664         try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 665       // The deflation protocol finished the first part (setting owner),
 666       // but it failed the second part (making ref_count negative) and
 667       // bailed. Or the ObjectMonitor was async deflated and reused.
 668       // Acquired the monitor.
 669       break;
 670     }
 671 
 672     // The lock is still contested.
 673     // Keep a tally of the # of futile wakeups.
 674     // Note that the counter is not protected by a lock or updated by atomics.
 675     // That is by design - we trade "lossy" counters which are exposed to
 676     // races during updates for a lower probe effect.
 677 
 678     // This PerfData object can be used in parallel with a safepoint.
 679     // See the work around in PerfDataManager::destroy().
 680     OM_PERFDATA_OP(FutileWakeups, inc());
 681     ++nWakeups;
 682 
 683     // Assuming this is not a spurious wakeup we'll normally find _succ == Self.
 684     // We can defer clearing _succ until after the spin completes
 685     // TrySpin() must tolerate being called with _succ == Self.
 686     // Try yet another round of adaptive spinning.
 687     if (TrySpin(Self) > 0) break;
 688 


 780   assert(SelfNode != NULL, "invariant");
 781   assert(SelfNode->_thread == Self, "invariant");
 782   assert(_waiters > 0, "invariant");
 783   assert(((oop)(object()))->mark() == markWord::encode(this), "invariant");
 784   assert(((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
 785   JavaThread * jt = (JavaThread *) Self;
 786 
 787   int nWakeups = 0;
 788   for (;;) {
 789     ObjectWaiter::TStates v = SelfNode->TState;
 790     guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
 791     assert(_owner != Self, "invariant");
 792 
 793     if (TryLock(Self) > 0) break;
 794     if (TrySpin(Self) > 0) break;
 795 
 796     if (AsyncDeflateIdleMonitors &&
 797         try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 798       // The deflation protocol finished the first part (setting owner),
 799       // but it failed the second part (making ref_count negative) and
 800       // bailed. Or the ObjectMonitor was async deflated and reused.
 801       // Acquired the monitor.
 802       break;
 803     }
 804 
 805     // State transition wrappers around park() ...
 806     // ReenterI() wisely defers state transitions until
 807     // it's clear we must park the thread.
 808     {
 809       OSThreadContendState osts(Self->osthread());
 810       ThreadBlockInVM tbivm(jt);
 811 
 812       // cleared by handle_special_suspend_equivalent_condition()
 813       // or java_suspend_self()
 814       jt->set_suspend_equivalent();
 815       Self->_ParkEvent->park();
 816 
 817       // were we externally suspended while we were waiting?
 818       for (;;) {
 819         if (!ExitSuspendEquivalent(jt)) break;
 820         if (_succ == Self) { _succ = NULL; OrderAccess::fence(); }
 821         jt->java_suspend_self();


2108     // previous or concurrent async deflation is a race.
2109     if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) {
2110       // Async deflation is in progress and our ref_count increment
2111       // above lost the race to async deflation. Attempt to restore
2112       // the header/dmw to the object's header so that we only retry
2113       // once if the deflater thread happens to be slow.
2114       om_ptr->install_displaced_markword_in_object(object);
2115       om_ptr->dec_ref_count();
2116       return false;
2117     }
2118     if (om_ptr->ref_count() <= 0) {
2119       // Async deflation is in the process of bailing out, but has not
2120       // yet restored the ref_count field so we return false to force
2121       // a retry. We want a positive ref_count value for a true return.
2122       om_ptr->dec_ref_count();
2123       return false;
2124     }
2125     // The ObjectMonitor could have been deflated and reused for
2126     // another object before we bumped the ref_count so make sure
2127     // our object still refers to this ObjectMonitor.


2128     const markWord tmp = object->mark();
2129     if (!tmp.has_monitor() || tmp.monitor() != om_ptr) {
2130       // Async deflation and reuse won the race so we have to retry.
2131       // Skip object header restoration since that's already done.
2132       om_ptr->dec_ref_count();
2133       return false;
2134     }
2135   }
2136 
2137   ADIM_guarantee(_om_ptr == NULL, "sanity check: _om_ptr=" INTPTR_FORMAT,
2138                  p2i(_om_ptr));
2139   _om_ptr = om_ptr;
2140   return true;
2141 }
2142 
2143 // For internal use by ObjectSynchronizer::inflate().
2144 // This function is only used when we don't have to worry about async
2145 // deflation of the specified ObjectMonitor*.
2146 void ObjectMonitorHandle::set_om_ptr(ObjectMonitor* om_ptr) {
2147   if (_om_ptr == NULL) {
2148     ADIM_guarantee(om_ptr != NULL, "cannot clear an unset om_ptr");
2149     om_ptr->inc_ref_count();
2150     _om_ptr = om_ptr;
2151   } else {
2152     ADIM_guarantee(om_ptr == NULL, "can only clear a set om_ptr");
2153     _om_ptr->dec_ref_count();
2154     _om_ptr = NULL;
2155   }
2156 }
2157 
2158 // Save the specified ObjectMonitor* if it is safe, i.e., not being
2159 // async deflated.
2160 //
2161 // This function returns true if the ObjectMonitor* has been safely
2162 // saved. This function returns false if the specified ObjectMonitor*
2163 // is NULL or if we have lost a race with async deflation; the caller
2164 // can retry as appropriate.
2165 bool ObjectMonitorHandle::set_om_ptr_if_safe(ObjectMonitor* om_ptr) {
2166   if (om_ptr == NULL) {
2167     return false;  // Nothing to save if input is NULL
2168   }
2169 
2170   om_ptr->inc_ref_count();
2171 
2172   if (AsyncDeflateIdleMonitors) {
2173     if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) {
2174       // Async deflation is in progress and our ref_count increment
2175       // above lost the race to async deflation.
2176       om_ptr->dec_ref_count();
2177       return false;
2178     }
2179     if (om_ptr->ref_count() <= 0) {
2180       // Async deflation is in the process of bailing out, but has not
2181       // yet restored the ref_count field so we return false to force
2182       // a retry. We want a positive ref_count value for a true return.
2183       om_ptr->dec_ref_count();
2184       return false;
2185     }


2263   } else {
2264     st->print("unknown=%d", _allocation_state);
2265   }
2266   st->cr();
2267   st->print_cr("  _pad_buf0 = {");
2268   st->print_cr("    [0] = '\\0'");
2269   st->print_cr("    ...");
2270   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2271   st->print_cr("  }");
2272   st->print_cr("  _owner = " INTPTR_FORMAT, p2i(_owner));
2273   st->print_cr("  _previous_owner_tid = " JLONG_FORMAT, _previous_owner_tid);
2274   st->print_cr("  _pad_buf1 = {");
2275   st->print_cr("    [0] = '\\0'");
2276   st->print_cr("    ...");
2277   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf1) - 1);
2278   st->print_cr("  }");
2279   st->print_cr("  _ref_count = %d", ref_count());
2280   st->print_cr("  _pad_buf2 = {");
2281   st->print_cr("    [0] = '\\0'");
2282   st->print_cr("    ...");
2283   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf1) - 1);
2284   st->print_cr("  }");
2285   st->print_cr("  _next_om = " INTPTR_FORMAT, p2i(next_om()));
2286   st->print_cr("  _recursions = " INTX_FORMAT, _recursions);
2287   st->print_cr("  _EntryList = " INTPTR_FORMAT, p2i(_EntryList));
2288   st->print_cr("  _cxq = " INTPTR_FORMAT, p2i(_cxq));
2289   st->print_cr("  _succ = " INTPTR_FORMAT, p2i(_succ));
2290   st->print_cr("  _Responsible = " INTPTR_FORMAT, p2i(_Responsible));
2291   st->print_cr("  _Spinner = %d", _Spinner);
2292   st->print_cr("  _SpinDuration = %d", _SpinDuration);
2293   st->print_cr("  _contentions = %d", _contentions);
2294   st->print_cr("  _WaitSet = " INTPTR_FORMAT, p2i(_WaitSet));
2295   st->print_cr("  _waiters = %d", _waiters);
2296   st->print_cr("  _WaitSetLock = %d", _WaitSetLock);
2297   st->print_cr("}");
2298 }
2299 #endif


 254     return;
 255   }
 256 
 257   if (cur == Self) {
 258     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 259     _recursions++;
 260     return;
 261   }
 262 
 263   if (Self->is_lock_owned((address)cur)) {
 264     assert(_recursions == 0, "internal state error");
 265     _recursions = 1;
 266     set_owner_from_BasicLock(cur, Self);  // Convert from BasicLock* to Thread*.
 267     return;
 268   }
 269 
 270   if (AsyncDeflateIdleMonitors &&
 271       try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 272     // The deflation protocol finished the first part (setting owner),
 273     // but it failed the second part (making ref_count negative) and
 274     // bailed. Acquired the monitor.

 275     assert(_recursions == 0, "invariant");
 276     return;
 277   }
 278 
 279   // We've encountered genuine contention.
 280   assert(Self->_Stalled == 0, "invariant");
 281   Self->_Stalled = intptr_t(this);
 282 
 283   // Try one round of spinning *before* enqueueing Self
 284   // and before going through the awkward and expensive state
 285   // transitions.  The following spin is strictly optional ...
 286   // Note that if we acquire the monitor from an initial spin
 287   // we forgo posting JVMTI events and firing DTRACE probes.
 288   if (TrySpin(Self) > 0) {
 289     assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
 290     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 291     assert(((oop)object())->mark() == markWord::encode(this),
 292            "object mark must match encoded this: mark=" INTPTR_FORMAT
 293            ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
 294            markWord::encode(this).value());


 431 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {
 432   // This function must only be called when (owner == DEFLATER_MARKER
 433   // && ref_count <= 0), but we can't guarantee that here because
 434   // those values could change when the ObjectMonitor gets moved from
 435   // the global free list to a per-thread free list.
 436 
 437   guarantee(obj != NULL, "must be non-NULL");
 438   if (object() != obj) {
 439     // ObjectMonitor's object ref no longer refers to the target object
 440     // so the object's header has already been restored.
 441     return;
 442   }
 443 
 444   markWord dmw = header();
 445   if (dmw.value() == 0) {
 446     // ObjectMonitor's header/dmw has been cleared so the ObjectMonitor
 447     // has been deflated and taken off the global free list.
 448     return;
 449   }
 450 
 451   // A non-NULL dmw has to be neutral (not locked and not marked).
 452   ADIM_guarantee(dmw.is_neutral(), "must be neutral: dmw=" INTPTR_FORMAT, dmw.value());
































 453 
 454   // Install displaced mark word if the object's header still points
 455   // to this ObjectMonitor. All racing callers to this function will
 456   // reach this point, but only one can win.
 457   markWord res = obj->cas_set_mark(dmw, markWord::encode(this));
 458   if (res != markWord::encode(this)) {
 459     // This should be rare so log at the Info level when it happens.
 460     log_info(monitorinflation)("install_displaced_markword_in_object: "
 461                                "failed cas_set_mark: new_mark=" INTPTR_FORMAT
 462                                ", old_mark=" INTPTR_FORMAT ", res=" INTPTR_FORMAT,
 463                                dmw.value(), markWord::encode(this).value(),
 464                                res.value());
 465   }
 466 
 467   // Note: It does not matter which thread restored the header/dmw
 468   // into the object's header. The thread deflating the monitor just
 469   // wanted the object's header restored and it is. The threads that
 470   // detected a race with the deflation process also wanted the
 471   // object's header restored before they retry their operation and
 472   // because it is restored they will only retry once.
 473 }
 474 
 475 // Convert the fields used by is_busy() to a string that can be
 476 // used for diagnostic output.
 477 const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
 478   ss->print("is_busy: contentions=%d, waiters=%d, ", _contentions, _waiters);
 479   if (!AsyncDeflateIdleMonitors) {
 480     ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
 481   } else if (_owner != DEFLATER_MARKER) {
 482     ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
 483   } else {
 484     // We report NULL instead of DEFLATER_MARKER here because is_busy()
 485     // ignores DEFLATER_MARKER values.


 495 void ObjectMonitor::EnterI(TRAPS) {
 496   jint l_ref_count = ref_count();
 497   ADIM_guarantee(l_ref_count > 0, "must be positive: l_ref_count=%d, ref_count=%d", l_ref_count, ref_count());
 498 
 499   Thread * const Self = THREAD;
 500   assert(Self->is_Java_thread(), "invariant");
 501   assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
 502 
 503   // Try the lock - TATAS
 504   if (TryLock (Self) > 0) {
 505     assert(_succ != Self, "invariant");
 506     assert(_owner == Self, "invariant");
 507     assert(_Responsible != Self, "invariant");
 508     return;
 509   }
 510 
 511   if (AsyncDeflateIdleMonitors &&
 512       try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 513     // The deflation protocol finished the first part (setting owner),
 514     // but it failed the second part (making ref_count negative) and
 515     // bailed. Acquired the monitor.

 516     assert(_succ != Self, "invariant");
 517     assert(_Responsible != Self, "invariant");
 518     return;
 519   }
 520 
 521   assert(InitDone, "Unexpectedly not initialized");
 522 
 523   // We try one round of spinning *before* enqueueing Self.
 524   //
 525   // If the _owner is ready but OFFPROC we could use a YieldTo()
 526   // operation to donate the remainder of this thread's quantum
 527   // to the owner.  This has subtle but beneficial affinity
 528   // effects.
 529 
 530   if (TrySpin(Self) > 0) {
 531     assert(_owner == Self, "invariant");
 532     assert(_succ != Self, "invariant");
 533     assert(_Responsible != Self, "invariant");
 534     return;
 535   }


 621     assert(_owner != Self, "invariant");
 622 
 623     // park self
 624     if (_Responsible == Self) {
 625       Self->_ParkEvent->park((jlong) recheckInterval);
 626       // Increase the recheckInterval, but clamp the value.
 627       recheckInterval *= 8;
 628       if (recheckInterval > MAX_RECHECK_INTERVAL) {
 629         recheckInterval = MAX_RECHECK_INTERVAL;
 630       }
 631     } else {
 632       Self->_ParkEvent->park();
 633     }
 634 
 635     if (TryLock(Self) > 0) break;
 636 
 637     if (AsyncDeflateIdleMonitors &&
 638         try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 639       // The deflation protocol finished the first part (setting owner),
 640       // but it failed the second part (making ref_count negative) and
 641       // bailed. Acquired the monitor.

 642       break;
 643     }
 644 
 645     // The lock is still contested.
 646     // Keep a tally of the # of futile wakeups.
 647     // Note that the counter is not protected by a lock or updated by atomics.
 648     // That is by design - we trade "lossy" counters which are exposed to
 649     // races during updates for a lower probe effect.
 650 
 651     // This PerfData object can be used in parallel with a safepoint.
 652     // See the work around in PerfDataManager::destroy().
 653     OM_PERFDATA_OP(FutileWakeups, inc());
 654     ++nWakeups;
 655 
 656     // Assuming this is not a spurious wakeup we'll normally find _succ == Self.
 657     // We can defer clearing _succ until after the spin completes
 658     // TrySpin() must tolerate being called with _succ == Self.
 659     // Try yet another round of adaptive spinning.
 660     if (TrySpin(Self) > 0) break;
 661 


 753   assert(SelfNode != NULL, "invariant");
 754   assert(SelfNode->_thread == Self, "invariant");
 755   assert(_waiters > 0, "invariant");
 756   assert(((oop)(object()))->mark() == markWord::encode(this), "invariant");
 757   assert(((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
 758   JavaThread * jt = (JavaThread *) Self;
 759 
 760   int nWakeups = 0;
 761   for (;;) {
 762     ObjectWaiter::TStates v = SelfNode->TState;
 763     guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
 764     assert(_owner != Self, "invariant");
 765 
 766     if (TryLock(Self) > 0) break;
 767     if (TrySpin(Self) > 0) break;
 768 
 769     if (AsyncDeflateIdleMonitors &&
 770         try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
 771       // The deflation protocol finished the first part (setting owner),
 772       // but it failed the second part (making ref_count negative) and
 773       // bailed. Acquired the monitor.

 774       break;
 775     }
 776 
 777     // State transition wrappers around park() ...
 778     // ReenterI() wisely defers state transitions until
 779     // it's clear we must park the thread.
 780     {
 781       OSThreadContendState osts(Self->osthread());
 782       ThreadBlockInVM tbivm(jt);
 783 
 784       // cleared by handle_special_suspend_equivalent_condition()
 785       // or java_suspend_self()
 786       jt->set_suspend_equivalent();
 787       Self->_ParkEvent->park();
 788 
 789       // were we externally suspended while we were waiting?
 790       for (;;) {
 791         if (!ExitSuspendEquivalent(jt)) break;
 792         if (_succ == Self) { _succ = NULL; OrderAccess::fence(); }
 793         jt->java_suspend_self();


2080     // previous or concurrent async deflation is a race.
2081     if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) {
2082       // Async deflation is in progress and our ref_count increment
2083       // above lost the race to async deflation. Attempt to restore
2084       // the header/dmw to the object's header so that we only retry
2085       // once if the deflater thread happens to be slow.
2086       om_ptr->install_displaced_markword_in_object(object);
2087       om_ptr->dec_ref_count();
2088       return false;
2089     }
2090     if (om_ptr->ref_count() <= 0) {
2091       // Async deflation is in the process of bailing out, but has not
2092       // yet restored the ref_count field so we return false to force
2093       // a retry. We want a positive ref_count value for a true return.
2094       om_ptr->dec_ref_count();
2095       return false;
2096     }
2097     // The ObjectMonitor could have been deflated and reused for
2098     // another object before we bumped the ref_count so make sure
2099     // our object still refers to this ObjectMonitor.
2100     // Note: With handshakes after deflation is this race even
2101     // possible anymore?
2102     const markWord tmp = object->mark();
2103     if (!tmp.has_monitor() || tmp.monitor() != om_ptr) {
2104       // Async deflation and reuse won the race so we have to retry.
2105       // Skip object header restoration since that's already done.
2106       om_ptr->dec_ref_count();
2107       return false;
2108     }
2109   }
2110 
2111   ADIM_guarantee(_om_ptr == NULL, "sanity check: _om_ptr=" INTPTR_FORMAT,
2112                  p2i(_om_ptr));
2113   _om_ptr = om_ptr;
2114   return true;
2115 }
2116 
2117 // For internal use by ObjectSynchronizer::inflate().
2118 // This function is only used when we don't have to worry about async
2119 // deflation of the specified ObjectMonitor*.
2120 void ObjectMonitorHandle::set_om_ptr(ObjectMonitor* om_ptr) {
2121   if (_om_ptr == NULL) {
2122     ADIM_guarantee(om_ptr != NULL, "cannot clear an unset om_ptr");
2123     om_ptr->inc_ref_count();
2124     _om_ptr = om_ptr;
2125   } else {
2126     ADIM_guarantee(om_ptr == NULL, "can only clear a set om_ptr");
2127     _om_ptr->dec_ref_count();
2128     _om_ptr = NULL;
2129   }
2130 }
2131 
2132 // Save the specified ObjectMonitor* if it is safe, i.e., not being
2133 // async deflated.
2134 //
2135 // This function returns true if the ObjectMonitor* has been safely
2136 // saved. This function returns false if the specified ObjectMonitor*
2137 // is NULL or if we have lost a race with async deflation; the caller
2138 // can retry as appropriate.
2139 bool ObjectMonitorHandle::save_om_ptr_if_safe(ObjectMonitor* om_ptr) {
2140   if (om_ptr == NULL) {
2141     return false;  // Nothing to save if input is NULL
2142   }
2143 
2144   om_ptr->inc_ref_count();
2145 
2146   if (AsyncDeflateIdleMonitors) {
2147     if (om_ptr->owner_is_DEFLATER_MARKER() && om_ptr->ref_count() <= 0) {
2148       // Async deflation is in progress and our ref_count increment
2149       // above lost the race to async deflation.
2150       om_ptr->dec_ref_count();
2151       return false;
2152     }
2153     if (om_ptr->ref_count() <= 0) {
2154       // Async deflation is in the process of bailing out, but has not
2155       // yet restored the ref_count field so we return false to force
2156       // a retry. We want a positive ref_count value for a true return.
2157       om_ptr->dec_ref_count();
2158       return false;
2159     }


2237   } else {
2238     st->print("unknown=%d", _allocation_state);
2239   }
2240   st->cr();
2241   st->print_cr("  _pad_buf0 = {");
2242   st->print_cr("    [0] = '\\0'");
2243   st->print_cr("    ...");
2244   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2245   st->print_cr("  }");
2246   st->print_cr("  _owner = " INTPTR_FORMAT, p2i(_owner));
2247   st->print_cr("  _previous_owner_tid = " JLONG_FORMAT, _previous_owner_tid);
2248   st->print_cr("  _pad_buf1 = {");
2249   st->print_cr("    [0] = '\\0'");
2250   st->print_cr("    ...");
2251   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf1) - 1);
2252   st->print_cr("  }");
2253   st->print_cr("  _ref_count = %d", ref_count());
2254   st->print_cr("  _pad_buf2 = {");
2255   st->print_cr("    [0] = '\\0'");
2256   st->print_cr("    ...");
2257   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf2) - 1);
2258   st->print_cr("  }");
2259   st->print_cr("  _next_om = " INTPTR_FORMAT, p2i(next_om()));
2260   st->print_cr("  _recursions = " INTX_FORMAT, _recursions);
2261   st->print_cr("  _EntryList = " INTPTR_FORMAT, p2i(_EntryList));
2262   st->print_cr("  _cxq = " INTPTR_FORMAT, p2i(_cxq));
2263   st->print_cr("  _succ = " INTPTR_FORMAT, p2i(_succ));
2264   st->print_cr("  _Responsible = " INTPTR_FORMAT, p2i(_Responsible));
2265   st->print_cr("  _Spinner = %d", _Spinner);
2266   st->print_cr("  _SpinDuration = %d", _SpinDuration);
2267   st->print_cr("  _contentions = %d", _contentions);
2268   st->print_cr("  _WaitSet = " INTPTR_FORMAT, p2i(_WaitSet));
2269   st->print_cr("  _waiters = %d", _waiters);
2270   st->print_cr("  _WaitSetLock = %d", _WaitSetLock);
2271   st->print_cr("}");
2272 }
2273 #endif
< prev index next >