< prev index next >

src/hotspot/share/runtime/objectMonitor.cpp

Print this page

  26 #include "classfile/vmSymbols.hpp"
  27 #include "gc/shared/oopStorage.hpp"
  28 #include "gc/shared/oopStorageSet.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "jfr/support/jfrThreadId.hpp"
  31 #include "logging/log.hpp"
  32 #include "logging/logStream.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "oops/markWord.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/oopHandle.inline.hpp"
  38 #include "oops/weakHandle.inline.hpp"
  39 #include "prims/jvmtiDeferredUpdates.hpp"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "runtime/atomic.hpp"
  42 #include "runtime/globals.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/interfaceSupport.inline.hpp"
  45 #include "runtime/javaThread.inline.hpp"

  46 #include "runtime/mutexLocker.hpp"
  47 #include "runtime/objectMonitor.hpp"
  48 #include "runtime/objectMonitor.inline.hpp"
  49 #include "runtime/orderAccess.hpp"
  50 #include "runtime/osThread.hpp"
  51 #include "runtime/perfData.hpp"
  52 #include "runtime/safefetch.hpp"
  53 #include "runtime/safepointMechanism.inline.hpp"
  54 #include "runtime/sharedRuntime.hpp"

  55 #include "services/threadService.hpp"

  56 #include "utilities/dtrace.hpp"
  57 #include "utilities/globalDefinitions.hpp"
  58 #include "utilities/macros.hpp"
  59 #include "utilities/preserveException.hpp"
  60 #if INCLUDE_JFR
  61 #include "jfr/support/jfrFlush.hpp"
  62 #endif
  63 
  64 #ifdef DTRACE_ENABLED
  65 
  66 // Only bother with this argument setup if dtrace is available
  67 // TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
  68 
  69 
  70 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
  71   char* bytes = nullptr;                                                   \
  72   int len = 0;                                                             \
  73   jlong jtid = SharedRuntime::get_java_tid(thread);                        \
  74   Symbol* klassname = obj->klass()->name();                                \
  75   if (klassname != nullptr) {                                              \

 229   if (self->is_Java_thread()) {
 230     // Mostly called from JavaThreads so sanity check the thread state.
 231     JavaThread* jt = JavaThread::cast(self);
 232     switch (jt->thread_state()) {
 233     case _thread_in_vm:    // the usual case
 234     case _thread_in_Java:  // during deopt
 235       break;
 236     default:
 237       fatal("called from an unsafe thread state");
 238     }
 239     assert(jt->is_active_Java_thread(), "must be active JavaThread");
 240   } else {
 241     // However, ThreadService::get_current_contended_monitor()
 242     // can call here via the VMThread so sanity check it.
 243     assert(self->is_VM_thread(), "must be");
 244   }
 245 #endif // ASSERT
 246 }
 247 
 248 ObjectMonitor::ObjectMonitor(oop object) :
 249   _header(markWord::zero()),
 250   _object(_oop_storage, object),
 251   _owner(nullptr),
 252   _previous_owner_tid(0),
 253   _next_om(nullptr),
 254   _recursions(0),
 255   _EntryList(nullptr),
 256   _cxq(nullptr),
 257   _succ(nullptr),
 258   _Responsible(nullptr),
 259   _SpinDuration(ObjectMonitor::Knob_SpinLimit),
 260   _contentions(0),
 261   _WaitSet(nullptr),
 262   _waiters(0),
 263   _WaitSetLock(0)
 264 { }
 265 
 266 ObjectMonitor::~ObjectMonitor() {
 267   _object.release(_oop_storage);
 268 }
 269 
 270 oop ObjectMonitor::object() const {
 271   check_object_context();
 272   return _object.resolve();
 273 }
 274 
 275 oop ObjectMonitor::object_peek() const {
 276   return _object.peek();
 277 }
 278 
 279 void ObjectMonitor::ExitOnSuspend::operator()(JavaThread* current) {
 280   if (current->is_suspended()) {
 281     _om->_recursions = 0;
 282     _om->_succ = nullptr;
 283     // Don't need a full fence after clearing successor here because of the call to exit().
 284     _om->exit(current, false /* not_suspended */);
 285     _om_exited = true;
 286 
 287     current->set_current_pending_monitor(_om);
 288   }
 289 }
 290 
 291 void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
 292   if (current->is_suspended()) {
 293     if (_om->_succ == current) {
 294       _om->_succ = nullptr;
 295       OrderAccess::fence(); // always do a full fence when successor is cleared
 296     }
 297   }
 298 }
 299 






 300 // -----------------------------------------------------------------------------
 301 // Enter support
 302 
 303 bool ObjectMonitor::enter_for(JavaThread* locking_thread) {
















 304   // Used by ObjectSynchronizer::enter_for to enter for another thread.
 305   // The monitor is private to or already owned by locking_thread which must be suspended.
 306   // So this code may only contend with deflation.
 307   assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");


 308 
 309   // Block out deflation as soon as possible.
 310   add_to_contentions(1);
 311 
 312   bool success = false;
 313   if (!is_being_async_deflated()) {
 314     void* prev_owner = try_set_owner_from(nullptr, locking_thread);
 315 
 316     if (prev_owner == nullptr) {
 317       assert(_recursions == 0, "invariant");
 318       success = true;
 319     } else if (prev_owner == locking_thread) {
 320       _recursions++;
 321       success = true;
 322     } else if (prev_owner == DEFLATER_MARKER) {
 323       // Racing with deflation.
 324       prev_owner = try_set_owner_from(DEFLATER_MARKER, locking_thread);
 325       if (prev_owner == DEFLATER_MARKER) {
 326         // Cancelled deflation. Increment contentions as part of the deflation protocol.
 327         add_to_contentions(1);
 328         success = true;
 329       } else if (prev_owner == nullptr) {
 330         // At this point we cannot race with deflation as we have both incremented
 331         // contentions, seen contention > 0 and seen a DEFLATER_MARKER.
 332         // success will only be false if this races with something other than
 333         // deflation.
 334         prev_owner = try_set_owner_from(nullptr, locking_thread);
 335         success = prev_owner == nullptr;
 336       }
 337     } else if (LockingMode == LM_LEGACY && locking_thread->is_lock_owned((address)prev_owner)) {
 338       assert(_recursions == 0, "must be");
 339       _recursions = 1;
 340       set_owner_from_BasicLock(prev_owner, locking_thread);
 341       success = true;







 342     }
 343     assert(success, "Failed to enter_for: locking_thread=" INTPTR_FORMAT
 344            ", this=" INTPTR_FORMAT "{owner=" INTPTR_FORMAT "}, observed owner: " INTPTR_FORMAT,
 345            p2i(locking_thread), p2i(this), p2i(owner_raw()), p2i(prev_owner));
 346   } else {
 347     // Async deflation is in progress and our contentions increment
 348     // above lost the race to async deflation. Undo the work and
 349     // force the caller to retry.
 350     const oop l_object = object();
 351     if (l_object != nullptr) {
 352       // Attempt to restore the header/dmw to the object's header so that
 353       // we only retry once if the deflater thread happens to be slow.
 354       install_displaced_markword_in_object(l_object);
 355     }
 356   }






 357 
 358   add_to_contentions(-1);

 359 
 360   assert(!success || owner_raw() == locking_thread, "must be");



 361 
 362   return success;


 363 }
 364 
 365 bool ObjectMonitor::enter(JavaThread* current) {
 366   assert(current == JavaThread::current(), "must be");
 367   // The following code is ordered to check the most common cases first
 368   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 369 
 370   void* cur = try_set_owner_from(nullptr, current);
 371   if (cur == nullptr) {
 372     assert(_recursions == 0, "invariant");
 373     return true;
 374   }
 375 
 376   if (cur == current) {
 377     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 378     _recursions++;
 379     return true;
 380   }
 381 
 382   if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {

 383     assert(_recursions == 0, "internal state error");
 384     _recursions = 1;
 385     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 386     return true;
 387   }
 388 
















 389   // We've encountered genuine contention.
 390 
 391   // Try one round of spinning *before* enqueueing current
 392   // and before going through the awkward and expensive state
 393   // transitions.  The following spin is strictly optional ...
 394   // Note that if we acquire the monitor from an initial spin
 395   // we forgo posting JVMTI events and firing DTRACE probes.
 396   if (TrySpin(current)) {
 397     assert(owner_raw() == current, "must be current: owner=" INTPTR_FORMAT, p2i(owner_raw()));
 398     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 399     assert(object()->mark() == markWord::encode(this),
 400            "object mark must match encoded this: mark=" INTPTR_FORMAT
 401            ", encoded this=" INTPTR_FORMAT, object()->mark().value(),
 402            markWord::encode(this).value());







 403     return true;
 404   }
 405 
 406   assert(owner_raw() != current, "invariant");
 407   assert(_succ != current, "invariant");
 408   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 409   assert(current->thread_state() != _thread_blocked, "invariant");
 410 
 411   // Keep track of contention for JVM/TI and M&M queries.
 412   add_to_contentions(1);
 413   if (is_being_async_deflated()) {
 414     // Async deflation is in progress and our contentions increment
 415     // above lost the race to async deflation. Undo the work and
 416     // force the caller to retry.
 417     const oop l_object = object();
 418     if (l_object != nullptr) {
 419       // Attempt to restore the header/dmw to the object's header so that
 420       // we only retry once if the deflater thread happens to be slow.
 421       install_displaced_markword_in_object(l_object);
 422     }
 423     add_to_contentions(-1);
 424     return false;
 425   }
 426 











 427   JFR_ONLY(JfrConditionalFlush<EventJavaMonitorEnter> flush(current);)
 428   EventJavaMonitorEnter event;
 429   if (event.is_started()) {
 430     event.set_monitorClass(object()->klass());
 431     // Set an address that is 'unique enough', such that events close in
 432     // time and with the same address are likely (but not guaranteed) to
 433     // belong to the same object.
 434     event.set_address((uintptr_t)this);
 435   }
 436 
 437   { // Change java thread status to indicate blocked on monitor enter.
 438     JavaThreadBlockedOnMonitorEnterState jtbmes(current, this);
 439 
 440     assert(current->current_pending_monitor() == nullptr, "invariant");
 441     current->set_current_pending_monitor(this);
 442 
 443     DTRACE_MONITOR_PROBE(contended__enter, this, object(), current);
 444     if (JvmtiExport::should_post_monitor_contended_enter()) {
 445       JvmtiExport::post_monitor_contended_enter(current, this);
 446 

 463         current->set_current_pending_monitor(nullptr);
 464         // We can go to a safepoint at the end of this block. If we
 465         // do a thread dump during that safepoint, then this thread will show
 466         // as having "-locked" the monitor, but the OS and java.lang.Thread
 467         // states will still report that the thread is blocked trying to
 468         // acquire it.
 469         // If there is a suspend request, ExitOnSuspend will exit the OM
 470         // and set the OM as pending.
 471       }
 472       if (!eos.exited()) {
 473         // ExitOnSuspend did not exit the OM
 474         assert(owner_raw() == current, "invariant");
 475         break;
 476       }
 477     }
 478 
 479     // We've just gotten past the enter-check-for-suspend dance and we now own
 480     // the monitor free and clear.
 481   }
 482 
 483   add_to_contentions(-1);
 484   assert(contentions() >= 0, "must not be negative: contentions=%d", contentions());
 485 
 486   // Must either set _recursions = 0 or ASSERT _recursions == 0.
 487   assert(_recursions == 0, "invariant");
 488   assert(owner_raw() == current, "invariant");
 489   assert(_succ != current, "invariant");
 490   assert(object()->mark() == markWord::encode(this), "invariant");
 491 
 492   // The thread -- now the owner -- is back in vm mode.
 493   // Report the glorious news via TI,DTrace and jvmstat.
 494   // The probe effect is non-trivial.  All the reportage occurs
 495   // while we hold the monitor, increasing the length of the critical
 496   // section.  Amdahl's parallel speedup law comes vividly into play.
 497   //
 498   // Another option might be to aggregate the events (thread local or
 499   // per-monitor aggregation) and defer reporting until a more opportune
 500   // time -- such as next time some thread encounters contention but has
 501   // yet to acquire the lock.  While spinning that thread could
 502   // spinning we could increment JVMStat counters, etc.
 503 
 504   DTRACE_MONITOR_PROBE(contended__entered, this, object(), current);
 505   if (JvmtiExport::should_post_monitor_contended_entered()) {
 506     JvmtiExport::post_monitor_contended_entered(current, this);
 507 
 508     // The current thread already owns the monitor and is not going to
 509     // call park() for the remainder of the monitor enter protocol. So
 510     // it doesn't matter if the JVMTI_EVENT_MONITOR_CONTENDED_ENTERED
 511     // event handler consumed an unpark() issued by the thread that
 512     // just exited the monitor.
 513   }
 514   if (event.should_commit()) {
 515     event.set_previousOwner(_previous_owner_tid);
 516     event.commit();
 517   }
 518   OM_PERFDATA_OP(ContendedLockAttempts, inc());
 519   return true;
 520 }
 521 
 522 // Caveat: TryLock() is not necessarily serializing if it returns failure.
 523 // Callers must compensate as needed.
 524 
 525 ObjectMonitor::TryLockResult ObjectMonitor::TryLock(JavaThread* current) {
 526   void* own = owner_raw();
 527   if (own != nullptr) return TryLockResult::HasOwner;
 528   if (try_set_owner_from(nullptr, current) == nullptr) {
 529     assert(_recursions == 0, "invariant");
 530     return TryLockResult::Success;
 531   }
 532   // The lock had been free momentarily, but we lost the race to the lock.
 533   // Interference -- the CAS failed.
 534   // We can either return -1 or retry.
 535   // Retry doesn't make as much sense because the lock was just acquired.
 536   return TryLockResult::Interference;
 537 }
 538 
 539 // Deflate the specified ObjectMonitor if not in-use. Returns true if it
 540 // was deflated and false otherwise.
 541 //
 542 // The async deflation protocol sets owner to DEFLATER_MARKER and
 543 // makes contentions negative as signals to contending threads that
 544 // an async deflation is in progress. There are a number of checks
 545 // as part of the protocol to make sure that the calling thread has
 546 // not lost the race to a contending thread.
 547 //
 548 // The ObjectMonitor has been successfully async deflated when:
 549 //   (contentions < 0)
 550 // Contending threads that see that condition know to retry their operation.
 551 //
 552 bool ObjectMonitor::deflate_monitor() {
 553   if (is_busy()) {
 554     // Easy checks are first - the ObjectMonitor is busy so no deflation.
 555     return false;
 556   }
 557 
 558   const oop obj = object_peek();
 559 
 560   if (obj == nullptr) {
 561     // If the object died, we can recycle the monitor without racing with
 562     // Java threads. The GC already broke the association with the object.
 563     set_owner_from(nullptr, DEFLATER_MARKER);
 564     assert(contentions() >= 0, "must be non-negative: contentions=%d", contentions());
 565     _contentions = INT_MIN; // minimum negative int
 566   } else {
 567     // Attempt async deflation protocol.
 568 
 569     // Set a null owner to DEFLATER_MARKER to force any contending thread
 570     // through the slow path. This is just the first part of the async
 571     // deflation dance.
 572     if (try_set_owner_from(nullptr, DEFLATER_MARKER) != nullptr) {

 603 
 604   // Sanity checks for the races:
 605   guarantee(owner_is_DEFLATER_MARKER(), "must be deflater marker");
 606   guarantee(contentions() < 0, "must be negative: contentions=%d",
 607             contentions());
 608   guarantee(_waiters == 0, "must be 0: waiters=%d", _waiters);
 609   guarantee(_cxq == nullptr, "must be no contending threads: cxq="
 610             INTPTR_FORMAT, p2i(_cxq));
 611   guarantee(_EntryList == nullptr,
 612             "must be no entering threads: EntryList=" INTPTR_FORMAT,
 613             p2i(_EntryList));
 614 
 615   if (obj != nullptr) {
 616     if (log_is_enabled(Trace, monitorinflation)) {
 617       ResourceMark rm;
 618       log_trace(monitorinflation)("deflate_monitor: object=" INTPTR_FORMAT
 619                                   ", mark=" INTPTR_FORMAT ", type='%s'",
 620                                   p2i(obj), obj->mark().value(),
 621                                   obj->klass()->external_name());
 622     }

 623 
 624     // Install the old mark word if nobody else has already done it.
 625     install_displaced_markword_in_object(obj);





 626   }
 627 
 628   // We leave owner == DEFLATER_MARKER and contentions < 0
 629   // to force any racing threads to retry.
 630   return true;  // Success, ObjectMonitor has been deflated.
 631 }
 632 
 633 // Install the displaced mark word (dmw) of a deflating ObjectMonitor
 634 // into the header of the object associated with the monitor. This
 635 // idempotent method is called by a thread that is deflating a
 636 // monitor and by other threads that have detected a race with the
 637 // deflation process.
 638 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {

 639   // This function must only be called when (owner == DEFLATER_MARKER
 640   // && contentions <= 0), but we can't guarantee that here because
 641   // those values could change when the ObjectMonitor gets moved from
 642   // the global free list to a per-thread free list.
 643 
 644   guarantee(obj != nullptr, "must be non-null");
 645 
 646   // Separate loads in is_being_async_deflated(), which is almost always
 647   // called before this function, from the load of dmw/header below.
 648 
 649   // _contentions and dmw/header may get written by different threads.
 650   // Make sure to observe them in the same order when having several observers.
 651   OrderAccess::loadload_for_IRIW();
 652 
 653   const oop l_object = object_peek();
 654   if (l_object == nullptr) {
 655     // ObjectMonitor's object ref has already been cleared by async
 656     // deflation or GC so we're done here.
 657     return;
 658   }

 955   // protected by the lock will be visible before we release the lock, and
 956   // therefore before some other thread (CPU) has a chance to acquire the lock.
 957   // See also: http://gee.cs.oswego.edu/dl/jmm/cookbook.html.
 958   //
 959   // Critically, any prior STs to _succ or EntryList must be visible before
 960   // the ST of null into _owner in the *subsequent* (following) corresponding
 961   // monitorexit.  Recall too, that in 1-0 mode monitorexit does not necessarily
 962   // execute a serializing instruction.
 963 
 964   return;
 965 }
 966 
 967 // ReenterI() is a specialized inline form of the latter half of the
 968 // contended slow-path from EnterI().  We use ReenterI() only for
 969 // monitor reentry in wait().
 970 //
 971 // In the future we should reconcile EnterI() and ReenterI().
 972 
 973 void ObjectMonitor::ReenterI(JavaThread* current, ObjectWaiter* currentNode) {
 974   assert(current != nullptr, "invariant");

 975   assert(currentNode != nullptr, "invariant");
 976   assert(currentNode->_thread == current, "invariant");
 977   assert(_waiters > 0, "invariant");
 978   assert(object()->mark() == markWord::encode(this), "invariant");
 979 
 980   assert(current->thread_state() != _thread_blocked, "invariant");
 981 
 982   for (;;) {
 983     ObjectWaiter::TStates v = currentNode->TState;
 984     guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
 985     assert(owner_raw() != current, "invariant");
 986 
 987     // This thread has been notified so try to reacquire the lock.
 988     if (TryLock(current) == TryLockResult::Success) {
 989       break;
 990     }
 991 
 992     // If that fails, spin again.  Note that spin count may be zero so the above TryLock
 993     // is necessary.
 994     if (TrySpin(current)) {
 995         break;
 996     }
 997 
 998     {
 999       OSThreadContendState osts(current->osthread());
1000 

1025     OrderAccess::fence();
1026 
1027     // Keep a tally of the # of futile wakeups.
1028     // Note that the counter is not protected by a lock or updated by atomics.
1029     // That is by design - we trade "lossy" counters which are exposed to
1030     // races during updates for a lower probe effect.
1031     // This PerfData object can be used in parallel with a safepoint.
1032     // See the work around in PerfDataManager::destroy().
1033     OM_PERFDATA_OP(FutileWakeups, inc());
1034   }
1035 
1036   // current has acquired the lock -- Unlink current from the cxq or EntryList .
1037   // Normally we'll find current on the EntryList.
1038   // Unlinking from the EntryList is constant-time and atomic-free.
1039   // From the perspective of the lock owner (this thread), the
1040   // EntryList is stable and cxq is prepend-only.
1041   // The head of cxq is volatile but the interior is stable.
1042   // In addition, current.TState is stable.
1043 
1044   assert(owner_raw() == current, "invariant");
1045   assert(object()->mark() == markWord::encode(this), "invariant");
1046   UnlinkAfterAcquire(current, currentNode);
1047   if (_succ == current) _succ = nullptr;
1048   assert(_succ != current, "invariant");
1049   currentNode->TState = ObjectWaiter::TS_RUN;
1050   OrderAccess::fence();      // see comments at the end of EnterI()
1051 }
1052 
1053 // By convention we unlink a contending thread from EntryList|cxq immediately
1054 // after the thread acquires the lock in ::enter().  Equally, we could defer
1055 // unlinking the thread until ::exit()-time.
1056 
1057 void ObjectMonitor::UnlinkAfterAcquire(JavaThread* current, ObjectWaiter* currentNode) {
1058   assert(owner_raw() == current, "invariant");
1059   assert(currentNode->_thread == current, "invariant");
1060 
1061   if (currentNode->TState == ObjectWaiter::TS_ENTER) {
1062     // Normal case: remove current from the DLL EntryList .
1063     // This is a constant-time operation.
1064     ObjectWaiter* nxt = currentNode->_next;
1065     ObjectWaiter* prv = currentNode->_prev;

1651     // Lifecycle - the node representing current must not appear on any queues.
1652     // Node is about to go out-of-scope, but even if it were immortal we wouldn't
1653     // want residual elements associated with this thread left on any lists.
1654     guarantee(node.TState == ObjectWaiter::TS_RUN, "invariant");
1655     assert(owner_raw() == current, "invariant");
1656     assert(_succ != current, "invariant");
1657   } // OSThreadWaitState()
1658 
1659   current->set_current_waiting_monitor(nullptr);
1660 
1661   guarantee(_recursions == 0, "invariant");
1662   int relock_count = JvmtiDeferredUpdates::get_and_reset_relock_count_after_wait(current);
1663   _recursions =   save          // restore the old recursion count
1664                 + relock_count; //  increased by the deferred relock count
1665   current->inc_held_monitor_count(relock_count); // Deopt never entered these counts.
1666   _waiters--;             // decrement the number of waiters
1667 
1668   // Verify a few postconditions
1669   assert(owner_raw() == current, "invariant");
1670   assert(_succ != current, "invariant");
1671   assert(object()->mark() == markWord::encode(this), "invariant");
1672 
1673   // check if the notification happened
1674   if (!WasNotified) {
1675     // no, it could be timeout or Thread.interrupt() or both
1676     // check for interrupt event, otherwise it is timeout
1677     if (interruptible && current->is_interrupted(true) && !HAS_PENDING_EXCEPTION) {
1678       THROW(vmSymbols::java_lang_InterruptedException());
1679     }
1680   }
1681 
1682   // NOTE: Spurious wake up will be consider as timeout.
1683   // Monitor notify has precedence over thread interrupt.
1684 }
1685 
1686 
1687 // Consider:
1688 // If the lock is cool (cxq == null && succ == null) and we're on an MP system
1689 // then instead of transferring a thread from the WaitSet to the EntryList
1690 // we might just dequeue a thread from the WaitSet and directly unpark() it.
1691 

2168   }
2169 
2170   _oop_storage = OopStorageSet::create_weak("ObjectSynchronizer Weak", mtSynchronizer);
2171 
2172   DEBUG_ONLY(InitDone = true;)
2173 }
2174 
2175 void ObjectMonitor::print_on(outputStream* st) const {
2176   // The minimal things to print for markWord printing, more can be added for debugging and logging.
2177   st->print("{contentions=0x%08x,waiters=0x%08x"
2178             ",recursions=" INTX_FORMAT ",owner=" INTPTR_FORMAT "}",
2179             contentions(), waiters(), recursions(),
2180             p2i(owner()));
2181 }
2182 void ObjectMonitor::print() const { print_on(tty); }
2183 
2184 #ifdef ASSERT
2185 // Print the ObjectMonitor like a debugger would:
2186 //
2187 // (ObjectMonitor) 0x00007fdfb6012e40 = {
2188 //   _header = 0x0000000000000001
2189 //   _object = 0x000000070ff45fd0
2190 //   _pad_buf0 = {
2191 //     [0] = '\0'
2192 //     ...
2193 //     [43] = '\0'
2194 //   }
2195 //   _owner = 0x0000000000000000
2196 //   _previous_owner_tid = 0
2197 //   _pad_buf1 = {
2198 //     [0] = '\0'
2199 //     ...
2200 //     [47] = '\0'
2201 //   }
2202 //   _next_om = 0x0000000000000000
2203 //   _recursions = 0
2204 //   _EntryList = 0x0000000000000000
2205 //   _cxq = 0x0000000000000000
2206 //   _succ = 0x0000000000000000
2207 //   _Responsible = 0x0000000000000000
2208 //   _SpinDuration = 5000
2209 //   _contentions = 0
2210 //   _WaitSet = 0x0000700009756248
2211 //   _waiters = 1
2212 //   _WaitSetLock = 0
2213 // }
2214 //
2215 void ObjectMonitor::print_debug_style_on(outputStream* st) const {
2216   st->print_cr("(ObjectMonitor*) " INTPTR_FORMAT " = {", p2i(this));
2217   st->print_cr("  _header = " INTPTR_FORMAT, header().value());
2218   st->print_cr("  _object = " INTPTR_FORMAT, p2i(object_peek()));
2219   st->print_cr("  _pad_buf0 = {");
2220   st->print_cr("    [0] = '\\0'");
2221   st->print_cr("    ...");
2222   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2223   st->print_cr("  }");
2224   st->print_cr("  _owner = " INTPTR_FORMAT, p2i(owner_raw()));
2225   st->print_cr("  _previous_owner_tid = " UINT64_FORMAT, _previous_owner_tid);
2226   st->print_cr("  _pad_buf1 = {");
2227   st->print_cr("    [0] = '\\0'");
2228   st->print_cr("    ...");
2229   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf1) - 1);
2230   st->print_cr("  }");
2231   st->print_cr("  _next_om = " INTPTR_FORMAT, p2i(next_om()));
2232   st->print_cr("  _recursions = " INTX_FORMAT, _recursions);
2233   st->print_cr("  _EntryList = " INTPTR_FORMAT, p2i(_EntryList));
2234   st->print_cr("  _cxq = " INTPTR_FORMAT, p2i(_cxq));
2235   st->print_cr("  _succ = " INTPTR_FORMAT, p2i(_succ));
2236   st->print_cr("  _Responsible = " INTPTR_FORMAT, p2i(_Responsible));
2237   st->print_cr("  _SpinDuration = %d", _SpinDuration);

  26 #include "classfile/vmSymbols.hpp"
  27 #include "gc/shared/oopStorage.hpp"
  28 #include "gc/shared/oopStorageSet.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "jfr/support/jfrThreadId.hpp"
  31 #include "logging/log.hpp"
  32 #include "logging/logStream.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "oops/markWord.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/oopHandle.inline.hpp"
  38 #include "oops/weakHandle.inline.hpp"
  39 #include "prims/jvmtiDeferredUpdates.hpp"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "runtime/atomic.hpp"
  42 #include "runtime/globals.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/interfaceSupport.inline.hpp"
  45 #include "runtime/javaThread.inline.hpp"
  46 #include "runtime/lightweightSynchronizer.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/objectMonitor.hpp"
  49 #include "runtime/objectMonitor.inline.hpp"
  50 #include "runtime/orderAccess.hpp"
  51 #include "runtime/osThread.hpp"
  52 #include "runtime/perfData.hpp"
  53 #include "runtime/safefetch.hpp"
  54 #include "runtime/safepointMechanism.inline.hpp"
  55 #include "runtime/sharedRuntime.hpp"
  56 #include "runtime/synchronizer.hpp"
  57 #include "services/threadService.hpp"
  58 #include "utilities/debug.hpp"
  59 #include "utilities/dtrace.hpp"
  60 #include "utilities/globalDefinitions.hpp"
  61 #include "utilities/macros.hpp"
  62 #include "utilities/preserveException.hpp"
  63 #if INCLUDE_JFR
  64 #include "jfr/support/jfrFlush.hpp"
  65 #endif
  66 
  67 #ifdef DTRACE_ENABLED
  68 
  69 // Only bother with this argument setup if dtrace is available
  70 // TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
  71 
  72 
  73 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
  74   char* bytes = nullptr;                                                   \
  75   int len = 0;                                                             \
  76   jlong jtid = SharedRuntime::get_java_tid(thread);                        \
  77   Symbol* klassname = obj->klass()->name();                                \
  78   if (klassname != nullptr) {                                              \

 232   if (self->is_Java_thread()) {
 233     // Mostly called from JavaThreads so sanity check the thread state.
 234     JavaThread* jt = JavaThread::cast(self);
 235     switch (jt->thread_state()) {
 236     case _thread_in_vm:    // the usual case
 237     case _thread_in_Java:  // during deopt
 238       break;
 239     default:
 240       fatal("called from an unsafe thread state");
 241     }
 242     assert(jt->is_active_Java_thread(), "must be active JavaThread");
 243   } else {
 244     // However, ThreadService::get_current_contended_monitor()
 245     // can call here via the VMThread so sanity check it.
 246     assert(self->is_VM_thread(), "must be");
 247   }
 248 #endif // ASSERT
 249 }
 250 
 251 ObjectMonitor::ObjectMonitor(oop object) :
 252   _metadata(0),
 253   _object(_oop_storage, object),
 254   _owner(nullptr),
 255   _previous_owner_tid(0),
 256   _next_om(nullptr),
 257   _recursions(0),
 258   _EntryList(nullptr),
 259   _cxq(nullptr),
 260   _succ(nullptr),
 261   _Responsible(nullptr),
 262   _SpinDuration(ObjectMonitor::Knob_SpinLimit),
 263   _contentions(0),
 264   _WaitSet(nullptr),
 265   _waiters(0),
 266   _WaitSetLock(0)
 267 { }
 268 
 269 ObjectMonitor::~ObjectMonitor() {
 270   _object.release(_oop_storage);
 271 }
 272 
 273 oop ObjectMonitor::object() const {
 274   check_object_context();
 275   return _object.resolve();
 276 }
 277 




 278 void ObjectMonitor::ExitOnSuspend::operator()(JavaThread* current) {
 279   if (current->is_suspended()) {
 280     _om->_recursions = 0;
 281     _om->_succ = nullptr;
 282     // Don't need a full fence after clearing successor here because of the call to exit().
 283     _om->exit(current, false /* not_suspended */);
 284     _om_exited = true;
 285 
 286     current->set_current_pending_monitor(_om);
 287   }
 288 }
 289 
 290 void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
 291   if (current->is_suspended()) {
 292     if (_om->_succ == current) {
 293       _om->_succ = nullptr;
 294       OrderAccess::fence(); // always do a full fence when successor is cleared
 295     }
 296   }
 297 }
 298 
 299 #define assert_mark_word_consistency()                                         \
 300   assert(UseObjectMonitorTable || object()->mark() == markWord::encode(this),  \
 301          "object mark must match encoded this: mark=" INTPTR_FORMAT            \
 302          ", encoded this=" INTPTR_FORMAT, object()->mark().value(),            \
 303          markWord::encode(this).value());
 304 
 305 // -----------------------------------------------------------------------------
 306 // Enter support
 307 
 308 bool ObjectMonitor::enter_is_async_deflating() {
 309   if (is_being_async_deflated()) {
 310     if (!UseObjectMonitorTable) {
 311       const oop l_object = object();
 312       if (l_object != nullptr) {
 313         // Attempt to restore the header/dmw to the object's header so that
 314         // we only retry once if the deflater thread happens to be slow.
 315         install_displaced_markword_in_object(l_object);
 316       }
 317     }
 318     return true;
 319   }
 320 
 321   return false;
 322 }
 323 
 324 void ObjectMonitor::enter_for_with_contention_mark(JavaThread* locking_thread, ObjectMonitorContentionMark& contention_mark) {
 325   // Used by ObjectSynchronizer::enter_for to enter for another thread.
 326   // The monitor is private to or already owned by locking_thread which must be suspended.
 327   // So this code may only contend with deflation.
 328   assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
 329   assert(contention_mark._monitor == this, "must be");
 330   assert(!is_being_async_deflated(), "must be");
 331 
 332 
 333   void* prev_owner = try_set_owner_from(nullptr, locking_thread);
 334 
 335   bool success = false;


 336 
 337   if (prev_owner == nullptr) {
 338     assert(_recursions == 0, "invariant");
 339     success = true;
 340   } else if (prev_owner == locking_thread) {
 341     _recursions++;
 342     success = true;
 343   } else if (prev_owner == DEFLATER_MARKER) {
 344     // Racing with deflation.
 345     prev_owner = try_set_owner_from(DEFLATER_MARKER, locking_thread);
 346     if (prev_owner == DEFLATER_MARKER) {
 347       // Cancelled deflation. Increment contentions as part of the deflation protocol.
 348       add_to_contentions(1);













 349       success = true;
 350     } else if (prev_owner == nullptr) {
 351       // At this point we cannot race with deflation as we have both incremented
 352       // contentions, seen contention > 0 and seen a DEFLATER_MARKER.
 353       // success will only be false if this races with something other than
 354       // deflation.
 355       prev_owner = try_set_owner_from(nullptr, locking_thread);
 356       success = prev_owner == nullptr;
 357     }
 358   } else if (LockingMode == LM_LEGACY && locking_thread->is_lock_owned((address)prev_owner)) {
 359     assert(_recursions == 0, "must be");
 360     _recursions = 1;
 361     set_owner_from_BasicLock(prev_owner, locking_thread);
 362     success = true;








 363   }
 364   assert(success, "Failed to enter_for: locking_thread=" INTPTR_FORMAT
 365           ", this=" INTPTR_FORMAT "{owner=" INTPTR_FORMAT "}, observed owner: " INTPTR_FORMAT,
 366           p2i(locking_thread), p2i(this), p2i(owner_raw()), p2i(prev_owner));
 367 }
 368 
 369 bool ObjectMonitor::enter_for(JavaThread* locking_thread) {
 370 
 371   // Block out deflation as soon as possible.
 372   ObjectMonitorContentionMark contention_mark(this);
 373 
 374   // Check for deflation.
 375   if (enter_is_async_deflating()) {
 376     return false;
 377   }
 378 
 379   enter_for_with_contention_mark(locking_thread, contention_mark);
 380   assert(owner_raw() == locking_thread, "must be");
 381   return true;
 382 }
 383 
 384 bool ObjectMonitor::try_enter(JavaThread* current) {
 385   // TryLock avoids the CAS
 386   TryLockResult r = TryLock(current);
 387   if (r == TryLockResult::Success) {



 388     assert(_recursions == 0, "invariant");
 389     return true;
 390   }
 391 
 392   if (r == TryLockResult::HasOwner && owner() == current) {

 393     _recursions++;
 394     return true;
 395   }
 396 
 397   void* cur = owner_raw();
 398   if (LockingMode == LM_LEGACY && current->is_lock_owned((address)cur)) {
 399     assert(_recursions == 0, "internal state error");
 400     _recursions = 1;
 401     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 402     return true;
 403   }
 404 
 405   return false;
 406 }
 407 
 408 bool ObjectMonitor::spin_enter(JavaThread* current) {
 409   assert(current == JavaThread::current(), "must be");
 410 
 411   // Check for recursion.
 412   if (try_enter(current)) {
 413     return true;
 414   }
 415 
 416   // Check for deflation.
 417   if (enter_is_async_deflating()) {
 418     return false;
 419   }
 420 
 421   // We've encountered genuine contention.
 422 
 423   // Do one round of spinning.


 424   // Note that if we acquire the monitor from an initial spin
 425   // we forgo posting JVMTI events and firing DTRACE probes.
 426   if (TrySpin(current)) {
 427     assert(owner_raw() == current, "must be current: owner=" INTPTR_FORMAT, p2i(owner_raw()));
 428     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 429     assert_mark_word_consistency();
 430     return true;
 431   }
 432 
 433   return false;
 434 }
 435 
 436 bool ObjectMonitor::enter(JavaThread* current) {
 437   assert(current == JavaThread::current(), "must be");
 438 
 439   if (spin_enter(current)) {
 440     return true;
 441   }
 442 
 443   assert(owner_raw() != current, "invariant");
 444   assert(_succ != current, "invariant");
 445   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 446   assert(current->thread_state() != _thread_blocked, "invariant");
 447 
 448   // Keep is_being_async_deflated stable across the rest of enter
 449   ObjectMonitorContentionMark contention_mark(this);
 450 
 451   // Check for deflation.
 452   if (enter_is_async_deflating()) {








 453     return false;
 454   }
 455 
 456   // At this point this ObjectMonitor cannot be deflated, finish contended enter
 457   enter_with_contention_mark(current, contention_mark);
 458   return true;
 459 }
 460 
 461 void ObjectMonitor::enter_with_contention_mark(JavaThread *current, ObjectMonitorContentionMark &cm) {
 462   assert(current == JavaThread::current(), "must be");
 463   assert(owner_raw() != current, "must be");
 464   assert(cm._monitor == this, "must be");
 465   assert(!is_being_async_deflated(), "must be");
 466 
 467   JFR_ONLY(JfrConditionalFlush<EventJavaMonitorEnter> flush(current);)
 468   EventJavaMonitorEnter event;
 469   if (event.is_started()) {
 470     event.set_monitorClass(object()->klass());
 471     // Set an address that is 'unique enough', such that events close in
 472     // time and with the same address are likely (but not guaranteed) to
 473     // belong to the same object.
 474     event.set_address((uintptr_t)this);
 475   }
 476 
 477   { // Change java thread status to indicate blocked on monitor enter.
 478     JavaThreadBlockedOnMonitorEnterState jtbmes(current, this);
 479 
 480     assert(current->current_pending_monitor() == nullptr, "invariant");
 481     current->set_current_pending_monitor(this);
 482 
 483     DTRACE_MONITOR_PROBE(contended__enter, this, object(), current);
 484     if (JvmtiExport::should_post_monitor_contended_enter()) {
 485       JvmtiExport::post_monitor_contended_enter(current, this);
 486 

 503         current->set_current_pending_monitor(nullptr);
 504         // We can go to a safepoint at the end of this block. If we
 505         // do a thread dump during that safepoint, then this thread will show
 506         // as having "-locked" the monitor, but the OS and java.lang.Thread
 507         // states will still report that the thread is blocked trying to
 508         // acquire it.
 509         // If there is a suspend request, ExitOnSuspend will exit the OM
 510         // and set the OM as pending.
 511       }
 512       if (!eos.exited()) {
 513         // ExitOnSuspend did not exit the OM
 514         assert(owner_raw() == current, "invariant");
 515         break;
 516       }
 517     }
 518 
 519     // We've just gotten past the enter-check-for-suspend dance and we now own
 520     // the monitor free and clear.
 521   }
 522 

 523   assert(contentions() >= 0, "must not be negative: contentions=%d", contentions());
 524 
 525   // Must either set _recursions = 0 or ASSERT _recursions == 0.
 526   assert(_recursions == 0, "invariant");
 527   assert(owner_raw() == current, "invariant");
 528   assert(_succ != current, "invariant");
 529   assert_mark_word_consistency();
 530 
 531   // The thread -- now the owner -- is back in vm mode.
 532   // Report the glorious news via TI,DTrace and jvmstat.
 533   // The probe effect is non-trivial.  All the reportage occurs
 534   // while we hold the monitor, increasing the length of the critical
 535   // section.  Amdahl's parallel speedup law comes vividly into play.
 536   //
 537   // Another option might be to aggregate the events (thread local or
 538   // per-monitor aggregation) and defer reporting until a more opportune
 539   // time -- such as next time some thread encounters contention but has
 540   // yet to acquire the lock.  While spinning that thread could
 541   // spinning we could increment JVMStat counters, etc.
 542 
 543   DTRACE_MONITOR_PROBE(contended__entered, this, object(), current);
 544   if (JvmtiExport::should_post_monitor_contended_entered()) {
 545     JvmtiExport::post_monitor_contended_entered(current, this);
 546 
 547     // The current thread already owns the monitor and is not going to
 548     // call park() for the remainder of the monitor enter protocol. So
 549     // it doesn't matter if the JVMTI_EVENT_MONITOR_CONTENDED_ENTERED
 550     // event handler consumed an unpark() issued by the thread that
 551     // just exited the monitor.
 552   }
 553   if (event.should_commit()) {
 554     event.set_previousOwner(_previous_owner_tid);
 555     event.commit();
 556   }
 557   OM_PERFDATA_OP(ContendedLockAttempts, inc());

 558 }
 559 
 560 // Caveat: TryLock() is not necessarily serializing if it returns failure.
 561 // Callers must compensate as needed.
 562 
 563 ObjectMonitor::TryLockResult ObjectMonitor::TryLock(JavaThread* current) {
 564   void* own = owner_raw();
 565   if (own != nullptr) return TryLockResult::HasOwner;
 566   if (try_set_owner_from(nullptr, current) == nullptr) {
 567     assert(_recursions == 0, "invariant");
 568     return TryLockResult::Success;
 569   }
 570   // The lock had been free momentarily, but we lost the race to the lock.
 571   // Interference -- the CAS failed.
 572   // We can either return -1 or retry.
 573   // Retry doesn't make as much sense because the lock was just acquired.
 574   return TryLockResult::Interference;
 575 }
 576 
 577 // Deflate the specified ObjectMonitor if not in-use. Returns true if it
 578 // was deflated and false otherwise.
 579 //
 580 // The async deflation protocol sets owner to DEFLATER_MARKER and
 581 // makes contentions negative as signals to contending threads that
 582 // an async deflation is in progress. There are a number of checks
 583 // as part of the protocol to make sure that the calling thread has
 584 // not lost the race to a contending thread.
 585 //
 586 // The ObjectMonitor has been successfully async deflated when:
 587 //   (contentions < 0)
 588 // Contending threads that see that condition know to retry their operation.
 589 //
 590 bool ObjectMonitor::deflate_monitor(Thread* current) {
 591   if (is_busy()) {
 592     // Easy checks are first - the ObjectMonitor is busy so no deflation.
 593     return false;
 594   }
 595 
 596   const oop obj = object_peek();
 597 
 598   if (obj == nullptr) {
 599     // If the object died, we can recycle the monitor without racing with
 600     // Java threads. The GC already broke the association with the object.
 601     set_owner_from(nullptr, DEFLATER_MARKER);
 602     assert(contentions() >= 0, "must be non-negative: contentions=%d", contentions());
 603     _contentions = INT_MIN; // minimum negative int
 604   } else {
 605     // Attempt async deflation protocol.
 606 
 607     // Set a null owner to DEFLATER_MARKER to force any contending thread
 608     // through the slow path. This is just the first part of the async
 609     // deflation dance.
 610     if (try_set_owner_from(nullptr, DEFLATER_MARKER) != nullptr) {

 641 
 642   // Sanity checks for the races:
 643   guarantee(owner_is_DEFLATER_MARKER(), "must be deflater marker");
 644   guarantee(contentions() < 0, "must be negative: contentions=%d",
 645             contentions());
 646   guarantee(_waiters == 0, "must be 0: waiters=%d", _waiters);
 647   guarantee(_cxq == nullptr, "must be no contending threads: cxq="
 648             INTPTR_FORMAT, p2i(_cxq));
 649   guarantee(_EntryList == nullptr,
 650             "must be no entering threads: EntryList=" INTPTR_FORMAT,
 651             p2i(_EntryList));
 652 
 653   if (obj != nullptr) {
 654     if (log_is_enabled(Trace, monitorinflation)) {
 655       ResourceMark rm;
 656       log_trace(monitorinflation)("deflate_monitor: object=" INTPTR_FORMAT
 657                                   ", mark=" INTPTR_FORMAT ", type='%s'",
 658                                   p2i(obj), obj->mark().value(),
 659                                   obj->klass()->external_name());
 660     }
 661   }
 662 
 663   if (UseObjectMonitorTable) {
 664     LightweightSynchronizer::deflate_monitor(current, obj, this);
 665   } else {
 666     if (obj != nullptr) {
 667       // Install the old mark word if nobody else has already done it.
 668       install_displaced_markword_in_object(obj);
 669     }
 670   }
 671 
 672   // We leave owner == DEFLATER_MARKER and contentions < 0
 673   // to force any racing threads to retry.
 674   return true;  // Success, ObjectMonitor has been deflated.
 675 }
 676 
 677 // Install the displaced mark word (dmw) of a deflating ObjectMonitor
 678 // into the header of the object associated with the monitor. This
 679 // idempotent method is called by a thread that is deflating a
 680 // monitor and by other threads that have detected a race with the
 681 // deflation process.
 682 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {
 683   assert(!UseObjectMonitorTable, "Lightweight has no dmw");
 684   // This function must only be called when (owner == DEFLATER_MARKER
 685   // && contentions <= 0), but we can't guarantee that here because
 686   // those values could change when the ObjectMonitor gets moved from
 687   // the global free list to a per-thread free list.
 688 
 689   guarantee(obj != nullptr, "must be non-null");
 690 
 691   // Separate loads in is_being_async_deflated(), which is almost always
 692   // called before this function, from the load of dmw/header below.
 693 
 694   // _contentions and dmw/header may get written by different threads.
 695   // Make sure to observe them in the same order when having several observers.
 696   OrderAccess::loadload_for_IRIW();
 697 
 698   const oop l_object = object_peek();
 699   if (l_object == nullptr) {
 700     // ObjectMonitor's object ref has already been cleared by async
 701     // deflation or GC so we're done here.
 702     return;
 703   }

1000   // protected by the lock will be visible before we release the lock, and
1001   // therefore before some other thread (CPU) has a chance to acquire the lock.
1002   // See also: http://gee.cs.oswego.edu/dl/jmm/cookbook.html.
1003   //
1004   // Critically, any prior STs to _succ or EntryList must be visible before
1005   // the ST of null into _owner in the *subsequent* (following) corresponding
1006   // monitorexit.  Recall too, that in 1-0 mode monitorexit does not necessarily
1007   // execute a serializing instruction.
1008 
1009   return;
1010 }
1011 
1012 // ReenterI() is a specialized inline form of the latter half of the
1013 // contended slow-path from EnterI().  We use ReenterI() only for
1014 // monitor reentry in wait().
1015 //
1016 // In the future we should reconcile EnterI() and ReenterI().
1017 
1018 void ObjectMonitor::ReenterI(JavaThread* current, ObjectWaiter* currentNode) {
1019   assert(current != nullptr, "invariant");
1020   assert(current->thread_state() != _thread_blocked, "invariant");
1021   assert(currentNode != nullptr, "invariant");
1022   assert(currentNode->_thread == current, "invariant");
1023   assert(_waiters > 0, "invariant");
1024   assert_mark_word_consistency();


1025 
1026   for (;;) {
1027     ObjectWaiter::TStates v = currentNode->TState;
1028     guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
1029     assert(owner_raw() != current, "invariant");
1030 
1031     // This thread has been notified so try to reacquire the lock.
1032     if (TryLock(current) == TryLockResult::Success) {
1033       break;
1034     }
1035 
1036     // If that fails, spin again.  Note that spin count may be zero so the above TryLock
1037     // is necessary.
1038     if (TrySpin(current)) {
1039         break;
1040     }
1041 
1042     {
1043       OSThreadContendState osts(current->osthread());
1044 

1069     OrderAccess::fence();
1070 
1071     // Keep a tally of the # of futile wakeups.
1072     // Note that the counter is not protected by a lock or updated by atomics.
1073     // That is by design - we trade "lossy" counters which are exposed to
1074     // races during updates for a lower probe effect.
1075     // This PerfData object can be used in parallel with a safepoint.
1076     // See the work around in PerfDataManager::destroy().
1077     OM_PERFDATA_OP(FutileWakeups, inc());
1078   }
1079 
1080   // current has acquired the lock -- Unlink current from the cxq or EntryList .
1081   // Normally we'll find current on the EntryList.
1082   // Unlinking from the EntryList is constant-time and atomic-free.
1083   // From the perspective of the lock owner (this thread), the
1084   // EntryList is stable and cxq is prepend-only.
1085   // The head of cxq is volatile but the interior is stable.
1086   // In addition, current.TState is stable.
1087 
1088   assert(owner_raw() == current, "invariant");
1089   assert_mark_word_consistency();
1090   UnlinkAfterAcquire(current, currentNode);
1091   if (_succ == current) _succ = nullptr;
1092   assert(_succ != current, "invariant");
1093   currentNode->TState = ObjectWaiter::TS_RUN;
1094   OrderAccess::fence();      // see comments at the end of EnterI()
1095 }
1096 
1097 // By convention we unlink a contending thread from EntryList|cxq immediately
1098 // after the thread acquires the lock in ::enter().  Equally, we could defer
1099 // unlinking the thread until ::exit()-time.
1100 
1101 void ObjectMonitor::UnlinkAfterAcquire(JavaThread* current, ObjectWaiter* currentNode) {
1102   assert(owner_raw() == current, "invariant");
1103   assert(currentNode->_thread == current, "invariant");
1104 
1105   if (currentNode->TState == ObjectWaiter::TS_ENTER) {
1106     // Normal case: remove current from the DLL EntryList .
1107     // This is a constant-time operation.
1108     ObjectWaiter* nxt = currentNode->_next;
1109     ObjectWaiter* prv = currentNode->_prev;

1695     // Lifecycle - the node representing current must not appear on any queues.
1696     // Node is about to go out-of-scope, but even if it were immortal we wouldn't
1697     // want residual elements associated with this thread left on any lists.
1698     guarantee(node.TState == ObjectWaiter::TS_RUN, "invariant");
1699     assert(owner_raw() == current, "invariant");
1700     assert(_succ != current, "invariant");
1701   } // OSThreadWaitState()
1702 
1703   current->set_current_waiting_monitor(nullptr);
1704 
1705   guarantee(_recursions == 0, "invariant");
1706   int relock_count = JvmtiDeferredUpdates::get_and_reset_relock_count_after_wait(current);
1707   _recursions =   save          // restore the old recursion count
1708                 + relock_count; //  increased by the deferred relock count
1709   current->inc_held_monitor_count(relock_count); // Deopt never entered these counts.
1710   _waiters--;             // decrement the number of waiters
1711 
1712   // Verify a few postconditions
1713   assert(owner_raw() == current, "invariant");
1714   assert(_succ != current, "invariant");
1715   assert_mark_word_consistency();
1716 
1717   // check if the notification happened
1718   if (!WasNotified) {
1719     // no, it could be timeout or Thread.interrupt() or both
1720     // check for interrupt event, otherwise it is timeout
1721     if (interruptible && current->is_interrupted(true) && !HAS_PENDING_EXCEPTION) {
1722       THROW(vmSymbols::java_lang_InterruptedException());
1723     }
1724   }
1725 
1726   // NOTE: Spurious wake up will be consider as timeout.
1727   // Monitor notify has precedence over thread interrupt.
1728 }
1729 
1730 
1731 // Consider:
1732 // If the lock is cool (cxq == null && succ == null) and we're on an MP system
1733 // then instead of transferring a thread from the WaitSet to the EntryList
1734 // we might just dequeue a thread from the WaitSet and directly unpark() it.
1735 

2212   }
2213 
2214   _oop_storage = OopStorageSet::create_weak("ObjectSynchronizer Weak", mtSynchronizer);
2215 
2216   DEBUG_ONLY(InitDone = true;)
2217 }
2218 
2219 void ObjectMonitor::print_on(outputStream* st) const {
2220   // The minimal things to print for markWord printing, more can be added for debugging and logging.
2221   st->print("{contentions=0x%08x,waiters=0x%08x"
2222             ",recursions=" INTX_FORMAT ",owner=" INTPTR_FORMAT "}",
2223             contentions(), waiters(), recursions(),
2224             p2i(owner()));
2225 }
2226 void ObjectMonitor::print() const { print_on(tty); }
2227 
2228 #ifdef ASSERT
2229 // Print the ObjectMonitor like a debugger would:
2230 //
2231 // (ObjectMonitor) 0x00007fdfb6012e40 = {
2232 //   _metadata = 0x0000000000000001
2233 //   _object = 0x000000070ff45fd0
2234 //   _pad_buf0 = {
2235 //     [0] = '\0'
2236 //     ...
2237 //     [43] = '\0'
2238 //   }
2239 //   _owner = 0x0000000000000000
2240 //   _previous_owner_tid = 0
2241 //   _pad_buf1 = {
2242 //     [0] = '\0'
2243 //     ...
2244 //     [47] = '\0'
2245 //   }
2246 //   _next_om = 0x0000000000000000
2247 //   _recursions = 0
2248 //   _EntryList = 0x0000000000000000
2249 //   _cxq = 0x0000000000000000
2250 //   _succ = 0x0000000000000000
2251 //   _Responsible = 0x0000000000000000
2252 //   _SpinDuration = 5000
2253 //   _contentions = 0
2254 //   _WaitSet = 0x0000700009756248
2255 //   _waiters = 1
2256 //   _WaitSetLock = 0
2257 // }
2258 //
2259 void ObjectMonitor::print_debug_style_on(outputStream* st) const {
2260   st->print_cr("(ObjectMonitor*) " INTPTR_FORMAT " = {", p2i(this));
2261   st->print_cr("  _metadata = " INTPTR_FORMAT, _metadata);
2262   st->print_cr("  _object = " INTPTR_FORMAT, p2i(object_peek()));
2263   st->print_cr("  _pad_buf0 = {");
2264   st->print_cr("    [0] = '\\0'");
2265   st->print_cr("    ...");
2266   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2267   st->print_cr("  }");
2268   st->print_cr("  _owner = " INTPTR_FORMAT, p2i(owner_raw()));
2269   st->print_cr("  _previous_owner_tid = " UINT64_FORMAT, _previous_owner_tid);
2270   st->print_cr("  _pad_buf1 = {");
2271   st->print_cr("    [0] = '\\0'");
2272   st->print_cr("    ...");
2273   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf1) - 1);
2274   st->print_cr("  }");
2275   st->print_cr("  _next_om = " INTPTR_FORMAT, p2i(next_om()));
2276   st->print_cr("  _recursions = " INTX_FORMAT, _recursions);
2277   st->print_cr("  _EntryList = " INTPTR_FORMAT, p2i(_EntryList));
2278   st->print_cr("  _cxq = " INTPTR_FORMAT, p2i(_cxq));
2279   st->print_cr("  _succ = " INTPTR_FORMAT, p2i(_succ));
2280   st->print_cr("  _Responsible = " INTPTR_FORMAT, p2i(_Responsible));
2281   st->print_cr("  _SpinDuration = %d", _SpinDuration);
< prev index next >