< 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) {                                              \

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






 313 // -----------------------------------------------------------------------------
 314 // Enter support
 315 
 316 bool ObjectMonitor::enter_for(JavaThread* locking_thread) {
















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


 321 
 322   // Block out deflation as soon as possible.
 323   add_to_contentions(1);
 324 
 325   bool success = false;
 326   if (!is_being_async_deflated()) {
 327     void* prev_owner = try_set_owner_from(nullptr, locking_thread);
 328 
 329     if (prev_owner == nullptr) {
 330       assert(_recursions == 0, "invariant");
 331       success = true;
 332     } else if (prev_owner == locking_thread) {
 333       _recursions++;
 334       success = true;
 335     } else if (prev_owner == DEFLATER_MARKER) {
 336       // Racing with deflation.
 337       prev_owner = try_set_owner_from(DEFLATER_MARKER, locking_thread);
 338       if (prev_owner == DEFLATER_MARKER) {
 339         // Cancelled deflation. Increment contentions as part of the deflation protocol.
 340         add_to_contentions(1);
 341         success = true;
 342       } else if (prev_owner == nullptr) {
 343         // At this point we cannot race with deflation as we have both incremented
 344         // contentions, seen contention > 0 and seen a DEFLATER_MARKER.
 345         // success will only be false if this races with something other than
 346         // deflation.
 347         prev_owner = try_set_owner_from(nullptr, locking_thread);
 348         success = prev_owner == nullptr;
 349       }
 350     } else if (LockingMode == LM_LEGACY && locking_thread->is_lock_owned((address)prev_owner)) {
 351       assert(_recursions == 0, "must be");
 352       _recursions = 1;
 353       set_owner_from_BasicLock(prev_owner, locking_thread);
 354       success = true;







 355     }
 356     assert(success, "Failed to enter_for: locking_thread=" INTPTR_FORMAT
 357            ", this=" INTPTR_FORMAT "{owner=" INTPTR_FORMAT "}, observed owner: " INTPTR_FORMAT,
 358            p2i(locking_thread), p2i(this), p2i(owner_raw()), p2i(prev_owner));
 359   } else {
 360     // Async deflation is in progress and our contentions increment
 361     // above lost the race to async deflation. Undo the work and
 362     // force the caller to retry.
 363     const oop l_object = object();
 364     if (l_object != nullptr) {
 365       // Attempt to restore the header/dmw to the object's header so that
 366       // we only retry once if the deflater thread happens to be slow.
 367       install_displaced_markword_in_object(l_object);
 368     }
 369   }




 370 
 371   add_to_contentions(-1);
 372 
 373   assert(!success || owner_raw() == locking_thread, "must be");

 374 
 375   return success;
 376 }


 377 
 378 bool ObjectMonitor::enter(JavaThread* current) {
 379   assert(current == JavaThread::current(), "must be");
 380   // The following code is ordered to check the most common cases first
 381   // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
 382 

 383   void* cur = try_set_owner_from(nullptr, current);
 384   if (cur == nullptr) {
 385     assert(_recursions == 0, "invariant");
 386     return true;
 387   }
 388 
 389   if (cur == current) {
 390     // TODO-FIXME: check for integer overflow!  BUGID 6557169.
 391     _recursions++;
 392     return true;
 393   }
 394 
 395   if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
 396     assert(_recursions == 0, "internal state error");
 397     _recursions = 1;
 398     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 399     return true;
 400   }
 401 
















 402   // We've encountered genuine contention.
 403 
 404   // Try one round of spinning *before* enqueueing current
 405   // and before going through the awkward and expensive state
 406   // transitions.  The following spin is strictly optional ...
 407   // Note that if we acquire the monitor from an initial spin
 408   // we forgo posting JVMTI events and firing DTRACE probes.
 409   if (TrySpin(current) > 0) {
 410     assert(owner_raw() == current, "must be current: owner=" INTPTR_FORMAT, p2i(owner_raw()));
 411     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 412     assert(object()->mark() == markWord::encode(this),
 413            "object mark must match encoded this: mark=" INTPTR_FORMAT
 414            ", encoded this=" INTPTR_FORMAT, object()->mark().value(),
 415            markWord::encode(this).value());







 416     return true;
 417   }
 418 
 419   assert(owner_raw() != current, "invariant");
 420   assert(_succ != current, "invariant");
 421   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 422   assert(current->thread_state() != _thread_blocked, "invariant");
 423 
 424   // Keep track of contention for JVM/TI and M&M queries.
 425   add_to_contentions(1);
 426   if (is_being_async_deflated()) {
 427     // Async deflation is in progress and our contentions increment
 428     // above lost the race to async deflation. Undo the work and
 429     // force the caller to retry.
 430     const oop l_object = object();
 431     if (l_object != nullptr) {
 432       // Attempt to restore the header/dmw to the object's header so that
 433       // we only retry once if the deflater thread happens to be slow.
 434       install_displaced_markword_in_object(l_object);
 435     }
 436     add_to_contentions(-1);
 437     return false;
 438   }
 439 











 440   JFR_ONLY(JfrConditionalFlush<EventJavaMonitorEnter> flush(current);)
 441   EventJavaMonitorEnter event;
 442   if (event.is_started()) {
 443     event.set_monitorClass(object()->klass());
 444     // Set an address that is 'unique enough', such that events close in
 445     // time and with the same address are likely (but not guaranteed) to
 446     // belong to the same object.
 447     event.set_address((uintptr_t)this);
 448   }
 449 
 450   { // Change java thread status to indicate blocked on monitor enter.
 451     JavaThreadBlockedOnMonitorEnterState jtbmes(current, this);
 452 
 453     assert(current->current_pending_monitor() == nullptr, "invariant");
 454     current->set_current_pending_monitor(this);
 455 
 456     DTRACE_MONITOR_PROBE(contended__enter, this, object(), current);
 457     if (JvmtiExport::should_post_monitor_contended_enter()) {
 458       JvmtiExport::post_monitor_contended_enter(current, this);
 459 

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

 616 
 617   // Sanity checks for the races:
 618   guarantee(owner_is_DEFLATER_MARKER(), "must be deflater marker");
 619   guarantee(contentions() < 0, "must be negative: contentions=%d",
 620             contentions());
 621   guarantee(_waiters == 0, "must be 0: waiters=%d", _waiters);
 622   guarantee(_cxq == nullptr, "must be no contending threads: cxq="
 623             INTPTR_FORMAT, p2i(_cxq));
 624   guarantee(_EntryList == nullptr,
 625             "must be no entering threads: EntryList=" INTPTR_FORMAT,
 626             p2i(_EntryList));
 627 
 628   if (obj != nullptr) {
 629     if (log_is_enabled(Trace, monitorinflation)) {
 630       ResourceMark rm;
 631       log_trace(monitorinflation)("deflate_monitor: object=" INTPTR_FORMAT
 632                                   ", mark=" INTPTR_FORMAT ", type='%s'",
 633                                   p2i(obj), obj->mark().value(),
 634                                   obj->klass()->external_name());
 635     }

 636 
 637     // Install the old mark word if nobody else has already done it.
 638     install_displaced_markword_in_object(obj);





 639   }
 640 
 641   // We leave owner == DEFLATER_MARKER and contentions < 0
 642   // to force any racing threads to retry.
 643   return true;  // Success, ObjectMonitor has been deflated.
 644 }
 645 
 646 // Install the displaced mark word (dmw) of a deflating ObjectMonitor
 647 // into the header of the object associated with the monitor. This
 648 // idempotent method is called by a thread that is deflating a
 649 // monitor and by other threads that have detected a race with the
 650 // deflation process.
 651 void ObjectMonitor::install_displaced_markword_in_object(const oop obj) {

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

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

 984   assert(currentNode != nullptr, "invariant");
 985   assert(currentNode->_thread == current, "invariant");
 986   assert(_waiters > 0, "invariant");
 987   assert(object()->mark() == markWord::encode(this), "invariant");
 988 
 989   assert(current->thread_state() != _thread_blocked, "invariant");
 990 
 991   int nWakeups = 0;
 992   for (;;) {
 993     ObjectWaiter::TStates v = currentNode->TState;
 994     guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
 995     assert(owner_raw() != current, "invariant");
 996 
 997     if (TryLock(current) > 0) break;
 998     if (TrySpin(current) > 0) break;
 999 
1000     {
1001       OSThreadContendState osts(current->osthread());
1002 
1003       assert(current->thread_state() == _thread_in_vm, "invariant");
1004 
1005       {
1006         ClearSuccOnSuspend csos(this);
1007         ThreadBlockInVMPreprocess<ClearSuccOnSuspend> tbivs(current, csos, true /* allow_suspend */);
1008         current->_ParkEvent->park();
1009       }

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

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

2142   }
2143 
2144   _oop_storage = OopStorageSet::create_weak("ObjectSynchronizer Weak", mtSynchronizer);
2145 
2146   DEBUG_ONLY(InitDone = true;)
2147 }
2148 
2149 void ObjectMonitor::print_on(outputStream* st) const {
2150   // The minimal things to print for markWord printing, more can be added for debugging and logging.
2151   st->print("{contentions=0x%08x,waiters=0x%08x"
2152             ",recursions=" INTX_FORMAT ",owner=" INTPTR_FORMAT "}",
2153             contentions(), waiters(), recursions(),
2154             p2i(owner()));
2155 }
2156 void ObjectMonitor::print() const { print_on(tty); }
2157 
2158 #ifdef ASSERT
2159 // Print the ObjectMonitor like a debugger would:
2160 //
2161 // (ObjectMonitor) 0x00007fdfb6012e40 = {
2162 //   _header = 0x0000000000000001
2163 //   _object = 0x000000070ff45fd0
2164 //   _pad_buf0 = {
2165 //     [0] = '\0'
2166 //     ...
2167 //     [43] = '\0'
2168 //   }
2169 //   _owner = 0x0000000000000000
2170 //   _previous_owner_tid = 0
2171 //   _pad_buf1 = {
2172 //     [0] = '\0'
2173 //     ...
2174 //     [47] = '\0'
2175 //   }
2176 //   _next_om = 0x0000000000000000
2177 //   _recursions = 0
2178 //   _EntryList = 0x0000000000000000
2179 //   _cxq = 0x0000000000000000
2180 //   _succ = 0x0000000000000000
2181 //   _Responsible = 0x0000000000000000
2182 //   _SpinDuration = 5000
2183 //   _contentions = 0
2184 //   _WaitSet = 0x0000700009756248
2185 //   _waiters = 1
2186 //   _WaitSetLock = 0
2187 // }
2188 //
2189 void ObjectMonitor::print_debug_style_on(outputStream* st) const {
2190   st->print_cr("(ObjectMonitor*) " INTPTR_FORMAT " = {", p2i(this));
2191   st->print_cr("  _header = " INTPTR_FORMAT, header().value());
2192   st->print_cr("  _object = " INTPTR_FORMAT, p2i(object_peek()));
2193   st->print_cr("  _pad_buf0 = {");
2194   st->print_cr("    [0] = '\\0'");
2195   st->print_cr("    ...");
2196   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2197   st->print_cr("  }");
2198   st->print_cr("  _owner = " INTPTR_FORMAT, p2i(owner_raw()));
2199   st->print_cr("  _previous_owner_tid = " UINT64_FORMAT, _previous_owner_tid);
2200   st->print_cr("  _pad_buf1 = {");
2201   st->print_cr("    [0] = '\\0'");
2202   st->print_cr("    ...");
2203   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf1) - 1);
2204   st->print_cr("  }");
2205   st->print_cr("  _next_om = " INTPTR_FORMAT, p2i(next_om()));
2206   st->print_cr("  _recursions = " INTX_FORMAT, _recursions);
2207   st->print_cr("  _EntryList = " INTPTR_FORMAT, p2i(_EntryList));
2208   st->print_cr("  _cxq = " INTPTR_FORMAT, p2i(_cxq));
2209   st->print_cr("  _succ = " INTPTR_FORMAT, p2i(_succ));
2210   st->print_cr("  _Responsible = " INTPTR_FORMAT, p2i(_Responsible));
2211   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) {                                              \

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




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


 349 
 350   if (prev_owner == nullptr) {
 351     assert(_recursions == 0, "invariant");
 352     success = true;
 353   } else if (prev_owner == locking_thread) {
 354     _recursions++;
 355     success = true;
 356   } else if (prev_owner == DEFLATER_MARKER) {
 357     // Racing with deflation.
 358     prev_owner = try_set_owner_from(DEFLATER_MARKER, locking_thread);
 359     if (prev_owner == DEFLATER_MARKER) {
 360       // Cancelled deflation. Increment contentions as part of the deflation protocol.
 361       add_to_contentions(1);













 362       success = true;
 363     } else if (prev_owner == nullptr) {
 364       // At this point we cannot race with deflation as we have both incremented
 365       // contentions, seen contention > 0 and seen a DEFLATER_MARKER.
 366       // success will only be false if this races with something other than
 367       // deflation.
 368       prev_owner = try_set_owner_from(nullptr, locking_thread);
 369       success = prev_owner == nullptr;
 370     }
 371   } else if (LockingMode == LM_LEGACY && locking_thread->is_lock_owned((address)prev_owner)) {
 372     assert(_recursions == 0, "must be");
 373     _recursions = 1;
 374     set_owner_from_BasicLock(prev_owner, locking_thread);
 375     success = true;








 376   }
 377   assert(success, "Failed to enter_for: locking_thread=" INTPTR_FORMAT
 378           ", this=" INTPTR_FORMAT "{owner=" INTPTR_FORMAT "}, observed owner: " INTPTR_FORMAT,
 379           p2i(locking_thread), p2i(this), p2i(owner_raw()), p2i(prev_owner));
 380 }
 381 
 382 bool ObjectMonitor::enter_for(JavaThread* locking_thread) {
 383 
 384   // Block out deflation as soon as possible.
 385   ObjectMonitorContentionMark contention_mark(this);
 386 
 387   // Check for deflation.
 388   if (enter_is_async_deflating()) {
 389     return false;
 390   }
 391 
 392   enter_for_with_contention_mark(locking_thread, contention_mark);
 393   assert(owner_raw() == locking_thread, "must be");
 394   return true;
 395 }
 396 
 397 bool ObjectMonitor::try_enter(JavaThread* current) {
 398   void* cur = try_set_owner_from(nullptr, current);
 399   if (cur == nullptr) {
 400     assert(_recursions == 0, "invariant");
 401     return true;
 402   }
 403 
 404   if (cur == current) {

 405     _recursions++;
 406     return true;
 407   }
 408 
 409   if (LockingMode != LM_LIGHTWEIGHT && current->is_lock_owned((address)cur)) {
 410     assert(_recursions == 0, "internal state error");
 411     _recursions = 1;
 412     set_owner_from_BasicLock(cur, current);  // Convert from BasicLock* to Thread*.
 413     return true;
 414   }
 415 
 416   return false;
 417 }
 418 
 419 bool ObjectMonitor::spin_enter(JavaThread* current) {
 420   assert(current == JavaThread::current(), "must be");
 421 
 422   // Check for recursion.
 423   if (try_enter(current)) {
 424     return true;
 425   }
 426 
 427   // Check for deflation.
 428   if (enter_is_async_deflating()) {
 429     return false;
 430   }
 431 
 432   // We've encountered genuine contention.
 433 
 434   // Do one round of spinning.


 435   // Note that if we acquire the monitor from an initial spin
 436   // we forgo posting JVMTI events and firing DTRACE probes.
 437   if (TrySpin(current) > 0) {
 438     assert(owner_raw() == current, "must be current: owner=" INTPTR_FORMAT, p2i(owner_raw()));
 439     assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
 440     assert_mark_word_concistency();
 441     return true;
 442   }
 443 
 444   return false;
 445 }
 446 
 447 bool ObjectMonitor::enter(JavaThread* current) {
 448   assert(current == JavaThread::current(), "must be");
 449 
 450   if (spin_enter(current)) {
 451     return true;
 452   }
 453 
 454   assert(owner_raw() != current, "invariant");
 455   assert(_succ != current, "invariant");
 456   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 457   assert(current->thread_state() != _thread_blocked, "invariant");
 458 
 459   // Keep is_being_async_deflated stable across the rest of enter
 460   ObjectMonitorContentionMark contention_mark(this);
 461 
 462   // Check for deflation.
 463   if (enter_is_async_deflating()) {








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

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

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

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

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

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


1032 
1033   int nWakeups = 0;
1034   for (;;) {
1035     ObjectWaiter::TStates v = currentNode->TState;
1036     guarantee(v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant");
1037     assert(owner_raw() != current, "invariant");
1038 
1039     if (TryLock(current) > 0) break;
1040     if (TrySpin(current) > 0) break;
1041 
1042     {
1043       OSThreadContendState osts(current->osthread());
1044 
1045       assert(current->thread_state() == _thread_in_vm, "invariant");
1046 
1047       {
1048         ClearSuccOnSuspend csos(this);
1049         ThreadBlockInVMPreprocess<ClearSuccOnSuspend> tbivs(current, csos, true /* allow_suspend */);
1050         current->_ParkEvent->park();
1051       }

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

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

2184   }
2185 
2186   _oop_storage = OopStorageSet::create_weak("ObjectSynchronizer Weak", mtSynchronizer);
2187 
2188   DEBUG_ONLY(InitDone = true;)
2189 }
2190 
2191 void ObjectMonitor::print_on(outputStream* st) const {
2192   // The minimal things to print for markWord printing, more can be added for debugging and logging.
2193   st->print("{contentions=0x%08x,waiters=0x%08x"
2194             ",recursions=" INTX_FORMAT ",owner=" INTPTR_FORMAT "}",
2195             contentions(), waiters(), recursions(),
2196             p2i(owner()));
2197 }
2198 void ObjectMonitor::print() const { print_on(tty); }
2199 
2200 #ifdef ASSERT
2201 // Print the ObjectMonitor like a debugger would:
2202 //
2203 // (ObjectMonitor) 0x00007fdfb6012e40 = {
2204 //   _metadata = 0x0000000000000001
2205 //   _object = 0x000000070ff45fd0
2206 //   _pad_buf0 = {
2207 //     [0] = '\0'
2208 //     ...
2209 //     [43] = '\0'
2210 //   }
2211 //   _owner = 0x0000000000000000
2212 //   _previous_owner_tid = 0
2213 //   _pad_buf1 = {
2214 //     [0] = '\0'
2215 //     ...
2216 //     [47] = '\0'
2217 //   }
2218 //   _next_om = 0x0000000000000000
2219 //   _recursions = 0
2220 //   _EntryList = 0x0000000000000000
2221 //   _cxq = 0x0000000000000000
2222 //   _succ = 0x0000000000000000
2223 //   _Responsible = 0x0000000000000000
2224 //   _SpinDuration = 5000
2225 //   _contentions = 0
2226 //   _WaitSet = 0x0000700009756248
2227 //   _waiters = 1
2228 //   _WaitSetLock = 0
2229 // }
2230 //
2231 void ObjectMonitor::print_debug_style_on(outputStream* st) const {
2232   st->print_cr("(ObjectMonitor*) " INTPTR_FORMAT " = {", p2i(this));
2233   st->print_cr("  _metadata = " INTPTR_FORMAT, _metadata);
2234   st->print_cr("  _object = " INTPTR_FORMAT, p2i(object_peek()));
2235   st->print_cr("  _pad_buf0 = {");
2236   st->print_cr("    [0] = '\\0'");
2237   st->print_cr("    ...");
2238   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
2239   st->print_cr("  }");
2240   st->print_cr("  _owner = " INTPTR_FORMAT, p2i(owner_raw()));
2241   st->print_cr("  _previous_owner_tid = " UINT64_FORMAT, _previous_owner_tid);
2242   st->print_cr("  _pad_buf1 = {");
2243   st->print_cr("    [0] = '\\0'");
2244   st->print_cr("    ...");
2245   st->print_cr("    [%d] = '\\0'", (int)sizeof(_pad_buf1) - 1);
2246   st->print_cr("  }");
2247   st->print_cr("  _next_om = " INTPTR_FORMAT, p2i(next_om()));
2248   st->print_cr("  _recursions = " INTX_FORMAT, _recursions);
2249   st->print_cr("  _EntryList = " INTPTR_FORMAT, p2i(_EntryList));
2250   st->print_cr("  _cxq = " INTPTR_FORMAT, p2i(_cxq));
2251   st->print_cr("  _succ = " INTPTR_FORMAT, p2i(_succ));
2252   st->print_cr("  _Responsible = " INTPTR_FORMAT, p2i(_Responsible));
2253   st->print_cr("  _SpinDuration = %d", _SpinDuration);
< prev index next >