1 /*
   2  * Copyright (c) 1998, 2026, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "classfile/vmSymbols.hpp"
  26 #include "gc/shared/collectedHeap.hpp"
  27 #include "jfr/jfrEvents.hpp"
  28 #include "logging/log.hpp"
  29 #include "logging/logStream.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/padded.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "memory/universe.hpp"
  34 #include "oops/markWord.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/atomicAccess.hpp"
  37 #include "runtime/basicLock.inline.hpp"
  38 #include "runtime/frame.inline.hpp"
  39 #include "runtime/globals.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/handshake.hpp"
  42 #include "runtime/interfaceSupport.inline.hpp"
  43 #include "runtime/javaThread.hpp"
  44 #include "runtime/lockStack.inline.hpp"
  45 #include "runtime/mutexLocker.hpp"
  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/objectMonitorTable.hpp"
  48 #include "runtime/os.inline.hpp"
  49 #include "runtime/osThread.hpp"
  50 #include "runtime/safepointMechanism.inline.hpp"
  51 #include "runtime/safepointVerifiers.hpp"
  52 #include "runtime/sharedRuntime.hpp"
  53 #include "runtime/stubRoutines.hpp"
  54 #include "runtime/synchronizer.hpp"
  55 #include "runtime/threads.hpp"
  56 #include "runtime/timer.hpp"
  57 #include "runtime/timerTrace.hpp"
  58 #include "runtime/trimNativeHeap.hpp"
  59 #include "runtime/vframe.hpp"
  60 #include "runtime/vmThread.hpp"
  61 #include "utilities/align.hpp"
  62 #include "utilities/concurrentHashTable.inline.hpp"
  63 #include "utilities/concurrentHashTableTasks.inline.hpp"
  64 #include "utilities/dtrace.hpp"
  65 #include "utilities/events.hpp"
  66 #include "utilities/fastHash.hpp"
  67 #include "utilities/globalCounter.inline.hpp"
  68 #include "utilities/globalDefinitions.hpp"
  69 #include "utilities/linkedlist.hpp"
  70 #include "utilities/preserveException.hpp"
  71 
  72 class ObjectMonitorDeflationLogging;
  73 
  74 void MonitorList::add(ObjectMonitor* m) {
  75   ObjectMonitor* head;
  76   do {
  77     head = AtomicAccess::load(&_head);
  78     m->set_next_om(head);
  79   } while (AtomicAccess::cmpxchg(&_head, head, m) != head);
  80 
  81   size_t count = AtomicAccess::add(&_count, 1u, memory_order_relaxed);
  82   size_t old_max;
  83   do {
  84     old_max = AtomicAccess::load(&_max);
  85     if (count <= old_max) {
  86       break;
  87     }
  88   } while (AtomicAccess::cmpxchg(&_max, old_max, count, memory_order_relaxed) != old_max);
  89 }
  90 
  91 size_t MonitorList::count() const {
  92   return AtomicAccess::load(&_count);
  93 }
  94 
  95 size_t MonitorList::max() const {
  96   return AtomicAccess::load(&_max);
  97 }
  98 
  99 class ObjectMonitorDeflationSafepointer : public StackObj {
 100   JavaThread* const                    _current;
 101   ObjectMonitorDeflationLogging* const _log;
 102 
 103 public:
 104   ObjectMonitorDeflationSafepointer(JavaThread* current, ObjectMonitorDeflationLogging* log)
 105     : _current(current), _log(log) {}
 106 
 107   void block_for_safepoint(const char* op_name, const char* count_name, size_t counter);
 108 };
 109 
 110 // Walk the in-use list and unlink deflated ObjectMonitors.
 111 // Returns the number of unlinked ObjectMonitors.
 112 size_t MonitorList::unlink_deflated(size_t deflated_count,
 113                                     GrowableArray<ObjectMonitor*>* unlinked_list,
 114                                     ObjectMonitorDeflationSafepointer* safepointer) {
 115   size_t unlinked_count = 0;
 116   ObjectMonitor* prev = nullptr;
 117   ObjectMonitor* m = AtomicAccess::load_acquire(&_head);
 118 
 119   while (m != nullptr) {
 120     if (m->is_being_async_deflated()) {
 121       // Find next live ObjectMonitor. Batch up the unlinkable monitors, so we can
 122       // modify the list once per batch. The batch starts at "m".
 123       size_t unlinked_batch = 0;
 124       ObjectMonitor* next = m;
 125       // Look for at most MonitorUnlinkBatch monitors, or the number of
 126       // deflated and not unlinked monitors, whatever comes first.
 127       assert(deflated_count >= unlinked_count, "Sanity: underflow");
 128       size_t unlinked_batch_limit = MIN2<size_t>(deflated_count - unlinked_count, MonitorUnlinkBatch);
 129       do {
 130         ObjectMonitor* next_next = next->next_om();
 131         unlinked_batch++;
 132         unlinked_list->append(next);
 133         next = next_next;
 134         if (unlinked_batch >= unlinked_batch_limit) {
 135           // Reached the max batch, so bail out of the gathering loop.
 136           break;
 137         }
 138         if (prev == nullptr && AtomicAccess::load(&_head) != m) {
 139           // Current batch used to be at head, but it is not at head anymore.
 140           // Bail out and figure out where we currently are. This avoids long
 141           // walks searching for new prev during unlink under heavy list inserts.
 142           break;
 143         }
 144       } while (next != nullptr && next->is_being_async_deflated());
 145 
 146       // Unlink the found batch.
 147       if (prev == nullptr) {
 148         // The current batch is the first batch, so there is a chance that it starts at head.
 149         // Optimistically assume no inserts happened, and try to unlink the entire batch from the head.
 150         ObjectMonitor* prev_head = AtomicAccess::cmpxchg(&_head, m, next);
 151         if (prev_head != m) {
 152           // Something must have updated the head. Figure out the actual prev for this batch.
 153           for (ObjectMonitor* n = prev_head; n != m; n = n->next_om()) {
 154             prev = n;
 155           }
 156           assert(prev != nullptr, "Should have found the prev for the current batch");
 157           prev->set_next_om(next);
 158         }
 159       } else {
 160         // The current batch is preceded by another batch. This guarantees the current batch
 161         // does not start at head. Unlink the entire current batch without updating the head.
 162         assert(AtomicAccess::load(&_head) != m, "Sanity");
 163         prev->set_next_om(next);
 164       }
 165 
 166       unlinked_count += unlinked_batch;
 167       if (unlinked_count >= deflated_count) {
 168         // Reached the max so bail out of the searching loop.
 169         // There should be no more deflated monitors left.
 170         break;
 171       }
 172       m = next;
 173     } else {
 174       prev = m;
 175       m = m->next_om();
 176     }
 177 
 178     // Must check for a safepoint/handshake and honor it.
 179     safepointer->block_for_safepoint("unlinking", "unlinked_count", unlinked_count);
 180   }
 181 
 182 #ifdef ASSERT
 183   // Invariant: the code above should unlink all deflated monitors.
 184   // The code that runs after this unlinking does not expect deflated monitors.
 185   // Notably, attempting to deflate the already deflated monitor would break.
 186   {
 187     ObjectMonitor* m = AtomicAccess::load_acquire(&_head);
 188     while (m != nullptr) {
 189       assert(!m->is_being_async_deflated(), "All deflated monitors should be unlinked");
 190       m = m->next_om();
 191     }
 192   }
 193 #endif
 194 
 195   AtomicAccess::sub(&_count, unlinked_count);
 196   return unlinked_count;
 197 }
 198 
 199 MonitorList::Iterator MonitorList::iterator() const {
 200   return Iterator(AtomicAccess::load_acquire(&_head));
 201 }
 202 
 203 ObjectMonitor* MonitorList::Iterator::next() {
 204   ObjectMonitor* current = _current;
 205   _current = current->next_om();
 206   return current;
 207 }
 208 
 209 // The "core" versions of monitor enter and exit reside in this file.
 210 // The interpreter and compilers contain specialized transliterated
 211 // variants of the enter-exit fast-path operations.  See c2_MacroAssembler_x86.cpp
 212 // fast_lock(...) for instance.  If you make changes here, make sure to modify the
 213 // interpreter, and both C1 and C2 fast-path inline locking code emission.
 214 //
 215 // -----------------------------------------------------------------------------
 216 
 217 #ifdef DTRACE_ENABLED
 218 
 219 // Only bother with this argument setup if dtrace is available
 220 // TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
 221 
 222 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
 223   char* bytes = nullptr;                                                      \
 224   int len = 0;                                                             \
 225   jlong jtid = SharedRuntime::get_java_tid(thread);                        \
 226   Symbol* klassname = obj->klass()->name();                                \
 227   if (klassname != nullptr) {                                                 \
 228     bytes = (char*)klassname->bytes();                                     \
 229     len = klassname->utf8_length();                                        \
 230   }
 231 
 232 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis)            \
 233   {                                                                        \
 234     if (DTraceMonitorProbes) {                                             \
 235       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 236       HOTSPOT_MONITOR_WAIT(jtid,                                           \
 237                            (uintptr_t)(monitor), bytes, len, (millis));    \
 238     }                                                                      \
 239   }
 240 
 241 #define HOTSPOT_MONITOR_PROBE_notify HOTSPOT_MONITOR_NOTIFY
 242 #define HOTSPOT_MONITOR_PROBE_notifyAll HOTSPOT_MONITOR_NOTIFYALL
 243 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED
 244 
 245 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread)                  \
 246   {                                                                        \
 247     if (DTraceMonitorProbes) {                                             \
 248       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 249       HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */             \
 250                                     (uintptr_t)(monitor), bytes, len);     \
 251     }                                                                      \
 252   }
 253 
 254 #else //  ndef DTRACE_ENABLED
 255 
 256 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
 257 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
 258 
 259 #endif // ndef DTRACE_ENABLED
 260 
 261 // This exists only as a workaround of dtrace bug 6254741
 262 static int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, JavaThread* thr) {
 263   DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
 264   return 0;
 265 }
 266 
 267 static constexpr size_t inflation_lock_count() {
 268   return 256;
 269 }
 270 
 271 // Static storage for an array of PlatformMutex.
 272 alignas(PlatformMutex) static uint8_t _inflation_locks[inflation_lock_count()][sizeof(PlatformMutex)];
 273 
 274 static inline PlatformMutex* inflation_lock(size_t index) {
 275   return reinterpret_cast<PlatformMutex*>(_inflation_locks[index]);
 276 }
 277 
 278 void ObjectSynchronizer::initialize() {
 279   for (size_t i = 0; i < inflation_lock_count(); i++) {
 280     ::new(static_cast<void*>(inflation_lock(i))) PlatformMutex();
 281   }
 282   // Start the ceiling with the estimate for one thread.
 283   set_in_use_list_ceiling(AvgMonitorsPerThreadEstimate);
 284 
 285   // Start the timer for deflations, so it does not trigger immediately.
 286   _last_async_deflation_time_ns = os::javaTimeNanos();
 287 
 288   ObjectSynchronizer::create_om_table();
 289 }
 290 
 291 MonitorList ObjectSynchronizer::_in_use_list;
 292 // monitors_used_above_threshold() policy is as follows:
 293 //
 294 // The ratio of the current _in_use_list count to the ceiling is used
 295 // to determine if we are above MonitorUsedDeflationThreshold and need
 296 // to do an async monitor deflation cycle. The ceiling is increased by
 297 // AvgMonitorsPerThreadEstimate when a thread is added to the system
 298 // and is decreased by AvgMonitorsPerThreadEstimate when a thread is
 299 // removed from the system.
 300 //
 301 // Note: If the _in_use_list max exceeds the ceiling, then
 302 // monitors_used_above_threshold() will use the in_use_list max instead
 303 // of the thread count derived ceiling because we have used more
 304 // ObjectMonitors than the estimated average.
 305 //
 306 // Note: If deflate_idle_monitors() has NoAsyncDeflationProgressMax
 307 // no-progress async monitor deflation cycles in a row, then the ceiling
 308 // is adjusted upwards by monitors_used_above_threshold().
 309 //
 310 // Start the ceiling with the estimate for one thread in initialize()
 311 // which is called after cmd line options are processed.
 312 static size_t _in_use_list_ceiling = 0;
 313 bool volatile ObjectSynchronizer::_is_async_deflation_requested = false;
 314 bool volatile ObjectSynchronizer::_is_final_audit = false;
 315 jlong ObjectSynchronizer::_last_async_deflation_time_ns = 0;
 316 static uintx _no_progress_cnt = 0;
 317 static bool _no_progress_skip_increment = false;
 318 
 319 // =====================> Quick functions
 320 
 321 // The quick_* forms are special fast-path variants used to improve
 322 // performance.  In the simplest case, a "quick_*" implementation could
 323 // simply return false, in which case the caller will perform the necessary
 324 // state transitions and call the slow-path form.
 325 // The fast-path is designed to handle frequently arising cases in an efficient
 326 // manner and is just a degenerate "optimistic" variant of the slow-path.
 327 // returns true  -- to indicate the call was satisfied.
 328 // returns false -- to indicate the call needs the services of the slow-path.
 329 // A no-loitering ordinance is in effect for code in the quick_* family
 330 // operators: safepoints or indefinite blocking (blocking that might span a
 331 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon
 332 // entry.
 333 //
 334 // Consider: An interesting optimization is to have the JIT recognize the
 335 // following common idiom:
 336 //   synchronized (someobj) { .... ; notify(); }
 337 // That is, we find a notify() or notifyAll() call that immediately precedes
 338 // the monitorexit operation.  In that case the JIT could fuse the operations
 339 // into a single notifyAndExit() runtime primitive.
 340 
 341 bool ObjectSynchronizer::quick_notify(oopDesc* obj, JavaThread* current, bool all) {
 342   assert(current->thread_state() == _thread_in_Java, "invariant");
 343   NoSafepointVerifier nsv;
 344   if (obj == nullptr) return false;  // slow-path for invalid obj
 345   const markWord mark = obj->mark();
 346 
 347   if (mark.is_fast_locked() && current->lock_stack().contains(cast_to_oop(obj))) {
 348     // Degenerate notify
 349     // fast-locked by caller so by definition the implied waitset is empty.
 350     return true;
 351   }
 352 
 353   if (mark.has_monitor()) {
 354     ObjectMonitor* const mon = read_monitor(obj, mark);
 355     if (mon == nullptr) {
 356       // Racing with inflation/deflation go slow path
 357       return false;
 358     }
 359     assert(mon->object() == oop(obj), "invariant");
 360     if (!mon->has_owner(current)) return false;  // slow-path for IMS exception
 361 
 362     if (mon->first_waiter() != nullptr) {
 363       // We have one or more waiters. Since this is an inflated monitor
 364       // that we own, we quickly notify them here and now, avoiding the slow-path.
 365       if (all) {
 366         mon->quick_notifyAll(current);
 367       } else {
 368         mon->quick_notify(current);
 369       }
 370     }
 371     return true;
 372   }
 373 
 374   // other IMS exception states take the slow-path
 375   return false;
 376 }
 377 
 378 // Handle notifications when synchronizing on value based classes
 379 void ObjectSynchronizer::handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread) {
 380   assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
 381   frame last_frame = locking_thread->last_frame();
 382   bool bcp_was_adjusted = false;
 383   // Don't decrement bcp if it points to the frame's first instruction.  This happens when
 384   // handle_sync_on_value_based_class() is called because of a synchronized method.  There
 385   // is no actual monitorenter instruction in the byte code in this case.
 386   if (last_frame.is_interpreted_frame() &&
 387       (last_frame.interpreter_frame_method()->code_base() < last_frame.interpreter_frame_bcp())) {
 388     // adjust bcp to point back to monitorenter so that we print the correct line numbers
 389     last_frame.interpreter_frame_set_bcp(last_frame.interpreter_frame_bcp() - 1);
 390     bcp_was_adjusted = true;
 391   }
 392 
 393   if (DiagnoseSyncOnValueBasedClasses == FATAL_EXIT) {
 394     ResourceMark rm;
 395     stringStream ss;
 396     locking_thread->print_active_stack_on(&ss);
 397     char* base = (char*)strstr(ss.base(), "at");
 398     char* newline = (char*)strchr(ss.base(), '\n');
 399     if (newline != nullptr) {
 400       *newline = '\0';
 401     }
 402     fatal("Synchronizing on object " INTPTR_FORMAT " of klass %s %s", p2i(obj()), obj->klass()->external_name(), base);
 403   } else {
 404     assert(DiagnoseSyncOnValueBasedClasses == LOG_WARNING, "invalid value for DiagnoseSyncOnValueBasedClasses");
 405     ResourceMark rm;
 406     Log(valuebasedclasses) vblog;
 407 
 408     vblog.info("Synchronizing on object " INTPTR_FORMAT " of klass %s", p2i(obj()), obj->klass()->external_name());
 409     if (locking_thread->has_last_Java_frame()) {
 410       LogStream info_stream(vblog.info());
 411       locking_thread->print_active_stack_on(&info_stream);
 412     } else {
 413       vblog.info("Cannot find the last Java frame");
 414     }
 415 
 416     EventSyncOnValueBasedClass event;
 417     if (event.should_commit()) {
 418       event.set_valueBasedClass(obj->klass());
 419       event.commit();
 420     }
 421   }
 422 
 423   if (bcp_was_adjusted) {
 424     last_frame.interpreter_frame_set_bcp(last_frame.interpreter_frame_bcp() + 1);
 425   }
 426 }
 427 
 428 // -----------------------------------------------------------------------------
 429 // JNI locks on java objects
 430 // NOTE: must use heavy weight monitor to handle jni monitor enter
 431 void ObjectSynchronizer::jni_enter(Handle obj, JavaThread* current) {
 432   // Top native frames in the stack will not be seen if we attempt
 433   // preemption, since we start walking from the last Java anchor.
 434   NoPreemptMark npm(current);
 435 
 436   if (obj->klass()->is_value_based()) {
 437     handle_sync_on_value_based_class(obj, current);
 438   }
 439 
 440   // the current locking is from JNI instead of Java code
 441   current->set_current_pending_monitor_is_from_java(false);
 442   // An async deflation can race after the inflate() call and before
 443   // enter() can make the ObjectMonitor busy. enter() returns false if
 444   // we have lost the race to async deflation and we simply try again.
 445   while (true) {
 446     BasicLock lock;
 447     if (ObjectSynchronizer::inflate_and_enter(obj(), &lock, inflate_cause_jni_enter, current, current) != nullptr) {
 448       break;
 449     }
 450   }
 451   current->set_current_pending_monitor_is_from_java(true);
 452 }
 453 
 454 // NOTE: must use heavy weight monitor to handle jni monitor exit
 455 void ObjectSynchronizer::jni_exit(oop obj, TRAPS) {
 456   JavaThread* current = THREAD;
 457 
 458   ObjectMonitor* monitor;
 459   monitor = ObjectSynchronizer::inflate_locked_or_imse(obj, inflate_cause_jni_exit, CHECK);
 460   // If this thread has locked the object, exit the monitor. We
 461   // intentionally do not use CHECK on check_owner because we must exit the
 462   // monitor even if an exception was already pending.
 463   if (monitor->check_owner(THREAD)) {
 464     monitor->exit(current);
 465   }
 466 }
 467 
 468 // -----------------------------------------------------------------------------
 469 // Internal VM locks on java objects
 470 // standard constructor, allows locking failures
 471 ObjectLocker::ObjectLocker(Handle obj, TRAPS) : _thread(THREAD), _obj(obj),
 472   _npm(_thread, _thread->at_preemptable_init() /* ignore_mark */), _skip_exit(false) {
 473   assert(!_thread->preempting(), "");
 474 
 475   _thread->check_for_valid_safepoint_state();
 476 
 477   if (_obj() != nullptr) {
 478     ObjectSynchronizer::enter(_obj, &_lock, _thread);
 479 
 480     if (_thread->preempting()) {
 481       // If preemption was cancelled we acquired the monitor after freezing
 482       // the frames. Redoing the vm call laterĀ in thaw will require us to
 483       // release it since the call should look like the original one. We
 484       // do it in ~ObjectLocker to reduce the window of time we hold the
 485       // monitor since we can't do anything useful with it now, and would
 486       // otherwise just force other vthreads to preempt in case they try
 487       // to acquire this monitor.
 488       _skip_exit = !_thread->preemption_cancelled();
 489       ObjectSynchronizer::read_monitor(_obj())->set_object_strong();
 490       _thread->set_pending_preempted_exception();
 491 
 492     }
 493   }
 494 }
 495 
 496 ObjectLocker::~ObjectLocker() {
 497   if (_obj() != nullptr && !_skip_exit) {
 498     ObjectSynchronizer::exit(_obj(), &_lock, _thread);
 499   }
 500 }
 501 
 502 void ObjectLocker::wait_uninterruptibly(TRAPS) {
 503   ObjectSynchronizer::waitUninterruptibly(_obj, 0, _thread);
 504   if (_thread->preempting()) {
 505     _skip_exit = true;
 506     ObjectSynchronizer::read_monitor(_obj())->set_object_strong();
 507     _thread->set_pending_preempted_exception();
 508   }
 509 }
 510 
 511 // -----------------------------------------------------------------------------
 512 //  Wait/Notify/NotifyAll
 513 // NOTE: must use heavy weight monitor to handle wait()
 514 
 515 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
 516   JavaThread* current = THREAD;
 517   if (millis < 0) {
 518     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 519   }
 520 
 521   ObjectMonitor* monitor;
 522   monitor = ObjectSynchronizer::inflate_locked_or_imse(obj(), inflate_cause_wait, CHECK_0);
 523 
 524   DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), current, millis);
 525   monitor->wait(millis, true, THREAD); // Not CHECK as we need following code
 526 
 527   // This dummy call is in place to get around dtrace bug 6254741.  Once
 528   // that's fixed we can uncomment the following line, remove the call
 529   // and change this function back into a "void" func.
 530   // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
 531   int ret_code = dtrace_waited_probe(monitor, obj, THREAD);
 532   return ret_code;
 533 }
 534 
 535 void ObjectSynchronizer::waitUninterruptibly(Handle obj, jlong millis, TRAPS) {
 536   assert(millis >= 0, "timeout value is negative");
 537 
 538   ObjectMonitor* monitor;
 539   monitor = ObjectSynchronizer::inflate_locked_or_imse(obj(), inflate_cause_wait, CHECK);
 540   monitor->wait(millis, false, THREAD);
 541 }
 542 
 543 
 544 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
 545   JavaThread* current = THREAD;
 546 
 547   markWord mark = obj->mark();
 548   if ((mark.is_fast_locked() && current->lock_stack().contains(obj()))) {
 549     // Not inflated so there can't be any waiters to notify.
 550     return;
 551   }
 552   ObjectMonitor* monitor = ObjectSynchronizer::inflate_locked_or_imse(obj(), inflate_cause_notify, CHECK);
 553   monitor->notify(CHECK);
 554 }
 555 
 556 // NOTE: see comment of notify()
 557 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
 558   JavaThread* current = THREAD;
 559 
 560   markWord mark = obj->mark();
 561   if ((mark.is_fast_locked() && current->lock_stack().contains(obj()))) {
 562     // Not inflated so there can't be any waiters to notify.
 563     return;
 564   }
 565 
 566   ObjectMonitor* monitor = ObjectSynchronizer::inflate_locked_or_imse(obj(), inflate_cause_notify, CHECK);
 567   monitor->notifyAll(CHECK);
 568 }
 569 
 570 // -----------------------------------------------------------------------------
 571 // Hash Code handling
 572 
 573 struct SharedGlobals {
 574   char         _pad_prefix[OM_CACHE_LINE_SIZE];
 575   // This is a highly shared mostly-read variable.
 576   // To avoid false-sharing it needs to be the sole occupant of a cache line.
 577   volatile int stw_random;
 578   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 579   // Hot RW variable -- Sequester to avoid false-sharing
 580   volatile int hc_sequence;
 581   DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 582 };
 583 
 584 static SharedGlobals GVars;
 585 
 586 // hashCode() generation :
 587 //
 588 // Possibilities:
 589 // * MD5Digest of {obj,stw_random}
 590 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 591 // * A DES- or AES-style SBox[] mechanism
 592 // * One of the Phi-based schemes, such as:
 593 //   2654435761 = 2^32 * Phi (golden ratio)
 594 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 595 // * A variation of Marsaglia's shift-xor RNG scheme.
 596 // * (obj ^ stw_random) is appealing, but can result
 597 //   in undesirable regularity in the hashCode values of adjacent objects
 598 //   (objects allocated back-to-back, in particular).  This could potentially
 599 //   result in hashtable collisions and reduced hashtable efficiency.
 600 //   There are simple ways to "diffuse" the middle address bits over the
 601 //   generated hashCode values:
 602 
 603 intptr_t ObjectSynchronizer::get_next_hash(Thread* current, oop obj) {
 604   intptr_t value = 0;
 605   if (hashCode == 0) {
 606     // This form uses global Park-Miller RNG.
 607     // On MP system we'll have lots of RW access to a global, so the
 608     // mechanism induces lots of coherency traffic.
 609     value = os::random();
 610   } else if (hashCode == 1) {
 611     // This variation has the property of being stable (idempotent)
 612     // between STW operations.  This can be useful in some of the 1-0
 613     // synchronization schemes.
 614     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 615     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 616   } else if (hashCode == 2) {
 617     value = 1;            // for sensitivity testing
 618   } else if (hashCode == 3) {
 619     value = ++GVars.hc_sequence;
 620   } else if (hashCode == 4) {
 621     value = cast_from_oop<intptr_t>(obj);
 622   } else if (hashCode == 5) {
 623     // Marsaglia's xor-shift scheme with thread-specific state
 624     // This is probably the best overall implementation -- we'll
 625     // likely make this the default in future releases.
 626     unsigned t = current->_hashStateX;
 627     t ^= (t << 11);
 628     current->_hashStateX = current->_hashStateY;
 629     current->_hashStateY = current->_hashStateZ;
 630     current->_hashStateZ = current->_hashStateW;
 631     unsigned v = current->_hashStateW;
 632     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
 633     current->_hashStateW = v;
 634     value = v;
 635   } else {
 636     assert(UseCompactObjectHeaders, "Only with compact i-hash");
 637 #ifdef _LP64
 638     uint64_t val = cast_from_oop<uint64_t>(obj);
 639     uint32_t hash = FastHash::get_hash32((uint32_t)val, (uint32_t)(val >> 32));
 640 #else
 641     uint32_t val = cast_from_oop<uint32_t>(obj);
 642     uint32_t hash = FastHash::get_hash32(val, UCONST64(0xAAAAAAAA));
 643 #endif
 644     value= static_cast<intptr_t>(hash);
 645   }
 646 
 647   value &= markWord::hash_mask;
 648   if (hashCode != 6 && value == 0) value = 0xBAD;
 649   assert(value != markWord::no_hash || hashCode == 6, "invariant");
 650   return value;
 651 }
 652 
 653 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
 654   while (true) {
 655     ObjectMonitor* monitor = nullptr;
 656     markWord temp, test;
 657     intptr_t hash;
 658     markWord mark = obj->mark_acquire();
 659     if (UseCompactObjectHeaders) {
 660       if (mark.is_hashed()) {
 661         return get_hash(mark, obj);
 662       }
 663       intptr_t hash = get_next_hash(current, obj);  // get a new hash
 664       markWord new_mark;
 665       if (mark.is_not_hashed_expanded()) {
 666         new_mark = mark.set_hashed_expanded();
 667         int offset = mark.klass()->hash_offset_in_bytes(obj, mark);
 668         obj->int_field_put(offset, (jint) hash);
 669       } else {
 670         new_mark = mark.set_hashed_not_expanded();
 671       }
 672       markWord old_mark = obj->cas_set_mark(new_mark, mark);
 673       if (old_mark == mark) {
 674         return hash;
 675       }
 676       // CAS failed, retry.
 677       continue;
 678     } else if (UseObjectMonitorTable || !mark.has_monitor()) {
 679       // If UseObjectMonitorTable is set the hash can simply be installed in the
 680       // object header, since the monitor isn't in the object header.
 681       hash = mark.hash();
 682       if (hash != 0) {                     // if it has a hash, just return it
 683         return hash;
 684       }
 685       hash = get_next_hash(current, obj);  // get a new hash
 686       temp = mark.copy_set_hash(hash);     // merge the hash into header
 687                                            // try to install the hash
 688       test = obj->cas_set_mark(temp, mark);
 689       if (test == mark) {                  // if the hash was installed, return it
 690         return hash;
 691       }
 692       // CAS failed, retry
 693       continue;
 694 
 695       // Failed to install the hash. It could be that another thread
 696       // installed the hash just before our attempt or inflation has
 697       // occurred or... so we fall thru to inflate the monitor for
 698       // stability and then install the hash.
 699     } else {
 700       assert(!mark.is_unlocked() && !mark.is_fast_locked(), "invariant");
 701       monitor = mark.monitor();
 702       temp = monitor->header();
 703       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
 704       hash = temp.hash();
 705       if (hash != 0) {
 706         // It has a hash.
 707 
 708         // Separate load of dmw/header above from the loads in
 709         // is_being_async_deflated().
 710 
 711         // dmw/header and _contentions may get written by different threads.
 712         // Make sure to observe them in the same order when having several observers.
 713         OrderAccess::loadload_for_IRIW();
 714 
 715         if (monitor->is_being_async_deflated()) {
 716           // But we can't safely use the hash if we detect that async
 717           // deflation has occurred. So we attempt to restore the
 718           // header/dmw to the object's header so that we only retry
 719           // once if the deflater thread happens to be slow.
 720           monitor->install_displaced_markword_in_object(obj);
 721           continue;
 722         }
 723         return hash;
 724       }
 725       // Fall thru so we only have one place that installs the hash in
 726       // the ObjectMonitor.
 727     }
 728 
 729     // NOTE: an async deflation can race after we get the monitor and
 730     // before we can update the ObjectMonitor's header with the hash
 731     // value below.
 732     assert(mark.has_monitor(), "must be");
 733     monitor = mark.monitor();
 734 
 735     // Load ObjectMonitor's header/dmw field and see if it has a hash.
 736     mark = monitor->header();
 737     assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
 738     hash = mark.hash();
 739     if (hash == 0) {                       // if it does not have a hash
 740       hash = get_next_hash(current, obj);  // get a new hash
 741       temp = mark.copy_set_hash(hash)   ;  // merge the hash into header
 742       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
 743       uintptr_t v = AtomicAccess::cmpxchg(monitor->metadata_addr(), mark.value(), temp.value());
 744       test = markWord(v);
 745       if (test != mark) {
 746         // The attempt to update the ObjectMonitor's header/dmw field
 747         // did not work. This can happen if another thread managed to
 748         // merge in the hash just before our cmpxchg().
 749         // If we add any new usages of the header/dmw field, this code
 750         // will need to be updated.
 751         hash = test.hash();
 752         assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
 753         assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
 754       }
 755       if (monitor->is_being_async_deflated() && !UseObjectMonitorTable) {
 756         // If we detect that async deflation has occurred, then we
 757         // attempt to restore the header/dmw to the object's header
 758         // so that we only retry once if the deflater thread happens
 759         // to be slow.
 760         monitor->install_displaced_markword_in_object(obj);
 761         continue;
 762       }
 763     }
 764     // We finally get the hash.
 765     return hash;
 766   }
 767 }
 768 
 769 
 770 uint32_t ObjectSynchronizer::get_hash(markWord mark, oop obj, Klass* klass) {
 771   assert(UseCompactObjectHeaders, "Only with compact i-hash");
 772   //assert(mark.is_neutral() | mark.is_fast_locked(), "only from neutral or fast-locked mark: " INTPTR_FORMAT, mark.value());
 773   assert(mark.is_hashed(), "only from hashed or copied object");
 774   if (mark.is_hashed_expanded()) {
 775     return obj->int_field(klass->hash_offset_in_bytes(obj, mark));
 776   } else {
 777     assert(mark.is_hashed_not_expanded(), "must be hashed");
 778     assert(hashCode == 6 || hashCode == 2, "must have idempotent hashCode");
 779     // Already marked as hashed, but not yet copied. Recompute hash and return it.
 780     return ObjectSynchronizer::get_next_hash(nullptr, obj); // recompute hash
 781   }
 782 }
 783 
 784 uint32_t ObjectSynchronizer::get_hash(markWord mark, oop obj) {
 785   return get_hash(mark, obj, mark.klass());
 786 }
 787 
 788 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* current,
 789                                                    Handle h_obj) {
 790   assert(current == JavaThread::current(), "Can only be called on current thread");
 791   oop obj = h_obj();
 792 
 793   markWord mark = obj->mark_acquire();
 794 
 795   if (mark.is_fast_locked()) {
 796     // fast-locking case, see if lock is in current's lock stack
 797     return current->lock_stack().contains(h_obj());
 798   }
 799 
 800   while (mark.has_monitor()) {
 801     ObjectMonitor* monitor = read_monitor(obj, mark);
 802     if (monitor != nullptr) {
 803       return monitor->is_entered(current) != 0;
 804     }
 805     // Racing with inflation/deflation, retry
 806     mark = obj->mark_acquire();
 807 
 808     if (mark.is_fast_locked()) {
 809       // Some other thread fast_locked, current could not have held the lock
 810       return false;
 811     }
 812   }
 813 
 814   // Unlocked case, header in place
 815   assert(mark.is_unlocked(), "sanity check");
 816   return false;
 817 }
 818 
 819 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
 820   oop obj = h_obj();
 821   markWord mark = obj->mark_acquire();
 822 
 823   if (mark.is_fast_locked()) {
 824     // fast-locked so get owner from the object.
 825     // owning_thread_from_object() may also return null here:
 826     return Threads::owning_thread_from_object(t_list, h_obj());
 827   }
 828 
 829   while (mark.has_monitor()) {
 830     ObjectMonitor* monitor = read_monitor(obj, mark);
 831     if (monitor != nullptr) {
 832       return Threads::owning_thread_from_monitor(t_list, monitor);
 833     }
 834     // Racing with inflation/deflation, retry
 835     mark = obj->mark_acquire();
 836 
 837     if (mark.is_fast_locked()) {
 838       // Some other thread fast_locked
 839       return Threads::owning_thread_from_object(t_list, h_obj());
 840     }
 841   }
 842 
 843   // Unlocked case, header in place
 844   // Cannot have assertion since this object may have been
 845   // locked by another thread when reaching here.
 846   // assert(mark.is_unlocked(), "sanity check");
 847 
 848   return nullptr;
 849 }
 850 
 851 // Visitors ...
 852 
 853 // Iterate over all ObjectMonitors.
 854 template <typename Function>
 855 void ObjectSynchronizer::monitors_iterate(Function function) {
 856   MonitorList::Iterator iter = _in_use_list.iterator();
 857   while (iter.has_next()) {
 858     ObjectMonitor* monitor = iter.next();
 859     function(monitor);
 860   }
 861 }
 862 
 863 // Iterate ObjectMonitors owned by any thread and where the owner `filter`
 864 // returns true.
 865 template <typename OwnerFilter>
 866 void ObjectSynchronizer::owned_monitors_iterate_filtered(MonitorClosure* closure, OwnerFilter filter) {
 867   monitors_iterate([&](ObjectMonitor* monitor) {
 868     // This function is only called at a safepoint or when the
 869     // target thread is suspended or when the target thread is
 870     // operating on itself. The current closures in use today are
 871     // only interested in an owned ObjectMonitor and ownership
 872     // cannot be dropped under the calling contexts so the
 873     // ObjectMonitor cannot be async deflated.
 874     if (monitor->has_owner() && filter(monitor)) {
 875       assert(!monitor->is_being_async_deflated(), "Owned monitors should not be deflating");
 876 
 877       closure->do_monitor(monitor);
 878     }
 879   });
 880 }
 881 
 882 // Iterate ObjectMonitors where the owner == thread.
 883 void ObjectSynchronizer::owned_monitors_iterate(MonitorClosure* closure, JavaThread* thread) {
 884   int64_t key = ObjectMonitor::owner_id_from(thread);
 885   auto thread_filter = [&](ObjectMonitor* monitor) { return monitor->owner() == key; };
 886   return owned_monitors_iterate_filtered(closure, thread_filter);
 887 }
 888 
 889 void ObjectSynchronizer::owned_monitors_iterate(MonitorClosure* closure, oop vthread) {
 890   int64_t key = ObjectMonitor::owner_id_from(vthread);
 891   auto thread_filter = [&](ObjectMonitor* monitor) { return monitor->owner() == key; };
 892   return owned_monitors_iterate_filtered(closure, thread_filter);
 893 }
 894 
 895 // Iterate ObjectMonitors owned by any thread.
 896 void ObjectSynchronizer::owned_monitors_iterate(MonitorClosure* closure) {
 897   auto all_filter = [&](ObjectMonitor* monitor) { return true; };
 898   return owned_monitors_iterate_filtered(closure, all_filter);
 899 }
 900 
 901 static bool monitors_used_above_threshold(MonitorList* list) {
 902   if (MonitorUsedDeflationThreshold == 0) {  // disabled case is easy
 903     return false;
 904   }
 905   size_t monitors_used = list->count();
 906   if (monitors_used == 0) {  // empty list is easy
 907     return false;
 908   }
 909   size_t old_ceiling = ObjectSynchronizer::in_use_list_ceiling();
 910   // Make sure that we use a ceiling value that is not lower than
 911   // previous, not lower than the recorded max used by the system, and
 912   // not lower than the current number of monitors in use (which can
 913   // race ahead of max). The result is guaranteed > 0.
 914   size_t ceiling = MAX3(old_ceiling, list->max(), monitors_used);
 915 
 916   // Check if our monitor usage is above the threshold:
 917   size_t monitor_usage = (monitors_used * 100LL) / ceiling;
 918   if (int(monitor_usage) > MonitorUsedDeflationThreshold) {
 919     // Deflate monitors if over the threshold percentage, unless no
 920     // progress on previous deflations.
 921     bool is_above_threshold = true;
 922 
 923     // Check if it's time to adjust the in_use_list_ceiling up, due
 924     // to too many async deflation attempts without any progress.
 925     if (NoAsyncDeflationProgressMax != 0 &&
 926         _no_progress_cnt >= NoAsyncDeflationProgressMax) {
 927       double remainder = (100.0 - MonitorUsedDeflationThreshold) / 100.0;
 928       size_t delta = (size_t)(ceiling * remainder) + 1;
 929       size_t new_ceiling = (ceiling > SIZE_MAX - delta)
 930         ? SIZE_MAX         // Overflow, let's clamp new_ceiling.
 931         : ceiling + delta;
 932 
 933       ObjectSynchronizer::set_in_use_list_ceiling(new_ceiling);
 934       log_info(monitorinflation)("Too many deflations without progress; "
 935                                  "bumping in_use_list_ceiling from %zu"
 936                                  " to %zu", old_ceiling, new_ceiling);
 937       _no_progress_cnt = 0;
 938       ceiling = new_ceiling;
 939 
 940       // Check if our monitor usage is still above the threshold:
 941       monitor_usage = (monitors_used * 100LL) / ceiling;
 942       is_above_threshold = int(monitor_usage) > MonitorUsedDeflationThreshold;
 943     }
 944     log_info(monitorinflation)("monitors_used=%zu, ceiling=%zu"
 945                                ", monitor_usage=%zu, threshold=%d",
 946                                monitors_used, ceiling, monitor_usage, MonitorUsedDeflationThreshold);
 947     return is_above_threshold;
 948   }
 949 
 950   return false;
 951 }
 952 
 953 size_t ObjectSynchronizer::in_use_list_count() {
 954   return _in_use_list.count();
 955 }
 956 
 957 size_t ObjectSynchronizer::in_use_list_max() {
 958   return _in_use_list.max();
 959 }
 960 
 961 size_t ObjectSynchronizer::in_use_list_ceiling() {
 962   return _in_use_list_ceiling;
 963 }
 964 
 965 void ObjectSynchronizer::dec_in_use_list_ceiling() {
 966   AtomicAccess::sub(&_in_use_list_ceiling, AvgMonitorsPerThreadEstimate);
 967 }
 968 
 969 void ObjectSynchronizer::inc_in_use_list_ceiling() {
 970   AtomicAccess::add(&_in_use_list_ceiling, AvgMonitorsPerThreadEstimate);
 971 }
 972 
 973 void ObjectSynchronizer::set_in_use_list_ceiling(size_t new_value) {
 974   _in_use_list_ceiling = new_value;
 975 }
 976 
 977 bool ObjectSynchronizer::is_async_deflation_needed() {
 978   if (is_async_deflation_requested()) {
 979     // Async deflation request.
 980     log_info(monitorinflation)("Async deflation needed: explicit request");
 981     return true;
 982   }
 983 
 984   jlong time_since_last = time_since_last_async_deflation_ms();
 985 
 986   if (AsyncDeflationInterval > 0 &&
 987       time_since_last > AsyncDeflationInterval &&
 988       monitors_used_above_threshold(&_in_use_list)) {
 989     // It's been longer than our specified deflate interval and there
 990     // are too many monitors in use. We don't deflate more frequently
 991     // than AsyncDeflationInterval (unless is_async_deflation_requested)
 992     // in order to not swamp the MonitorDeflationThread.
 993     log_info(monitorinflation)("Async deflation needed: monitors used are above the threshold");
 994     return true;
 995   }
 996 
 997   if (GuaranteedAsyncDeflationInterval > 0 &&
 998       time_since_last > GuaranteedAsyncDeflationInterval) {
 999     // It's been longer than our specified guaranteed deflate interval.
1000     // We need to clean up the used monitors even if the threshold is
1001     // not reached, to keep the memory utilization at bay when many threads
1002     // touched many monitors.
1003     log_info(monitorinflation)("Async deflation needed: guaranteed interval (%zd ms) "
1004                                "is greater than time since last deflation (" JLONG_FORMAT " ms)",
1005                                GuaranteedAsyncDeflationInterval, time_since_last);
1006 
1007     // If this deflation has no progress, then it should not affect the no-progress
1008     // tracking, otherwise threshold heuristics would think it was triggered, experienced
1009     // no progress, and needs to backoff more aggressively. In this "no progress" case,
1010     // the generic code would bump the no-progress counter, and we compensate for that
1011     // by telling it to skip the update.
1012     //
1013     // If this deflation has progress, then it should let non-progress tracking
1014     // know about this, otherwise the threshold heuristics would kick in, potentially
1015     // experience no-progress due to aggressive cleanup by this deflation, and think
1016     // it is still in no-progress stride. In this "progress" case, the generic code would
1017     // zero the counter, and we allow it to happen.
1018     _no_progress_skip_increment = true;
1019 
1020     return true;
1021   }
1022 
1023   return false;
1024 }
1025 
1026 void ObjectSynchronizer::request_deflate_idle_monitors() {
1027   MonitorLocker ml(MonitorDeflation_lock, Mutex::_no_safepoint_check_flag);
1028   set_is_async_deflation_requested(true);
1029   ml.notify_all();
1030 }
1031 
1032 bool ObjectSynchronizer::request_deflate_idle_monitors_from_wb() {
1033   JavaThread* current = JavaThread::current();
1034   bool ret_code = false;
1035 
1036   jlong last_time = last_async_deflation_time_ns();
1037 
1038   request_deflate_idle_monitors();
1039 
1040   const int N_CHECKS = 5;
1041   for (int i = 0; i < N_CHECKS; i++) {  // sleep for at most 5 seconds
1042     if (last_async_deflation_time_ns() > last_time) {
1043       log_info(monitorinflation)("Async Deflation happened after %d check(s).", i);
1044       ret_code = true;
1045       break;
1046     }
1047     {
1048       // JavaThread has to honor the blocking protocol.
1049       ThreadBlockInVM tbivm(current);
1050       os::naked_short_sleep(999);  // sleep for almost 1 second
1051     }
1052   }
1053   if (!ret_code) {
1054     log_info(monitorinflation)("Async Deflation DID NOT happen after %d checks.", N_CHECKS);
1055   }
1056 
1057   return ret_code;
1058 }
1059 
1060 jlong ObjectSynchronizer::time_since_last_async_deflation_ms() {
1061   return (os::javaTimeNanos() - last_async_deflation_time_ns()) / (NANOUNITS / MILLIUNITS);
1062 }
1063 
1064 // Walk the in-use list and deflate (at most MonitorDeflationMax) idle
1065 // ObjectMonitors. Returns the number of deflated ObjectMonitors.
1066 //
1067 size_t ObjectSynchronizer::deflate_monitor_list(ObjectMonitorDeflationSafepointer* safepointer) {
1068   MonitorList::Iterator iter = _in_use_list.iterator();
1069   size_t deflated_count = 0;
1070   Thread* current = Thread::current();
1071 
1072   while (iter.has_next()) {
1073     if (deflated_count >= (size_t)MonitorDeflationMax) {
1074       break;
1075     }
1076     ObjectMonitor* mid = iter.next();
1077     if (mid->deflate_monitor(current)) {
1078       deflated_count++;
1079     }
1080 
1081     // Must check for a safepoint/handshake and honor it.
1082     safepointer->block_for_safepoint("deflation", "deflated_count", deflated_count);
1083   }
1084 
1085   return deflated_count;
1086 }
1087 
1088 class DeflationHandshakeClosure : public HandshakeClosure {
1089  public:
1090   DeflationHandshakeClosure() : HandshakeClosure("DeflationHandshakeClosure") {}
1091 
1092   void do_thread(Thread* thread) {
1093     log_trace(monitorinflation)("DeflationHandshakeClosure::do_thread: thread="
1094                                 INTPTR_FORMAT, p2i(thread));
1095     if (thread->is_Java_thread()) {
1096       // Clear OM cache
1097       JavaThread* jt = JavaThread::cast(thread);
1098       jt->om_clear_monitor_cache();
1099     }
1100   }
1101 };
1102 
1103 class VM_RendezvousGCThreads : public VM_Operation {
1104 public:
1105   bool evaluate_at_safepoint() const override { return false; }
1106   VMOp_Type type() const override { return VMOp_RendezvousGCThreads; }
1107   void doit() override {
1108     Universe::heap()->safepoint_synchronize_begin();
1109     Universe::heap()->safepoint_synchronize_end();
1110   };
1111 };
1112 
1113 static size_t delete_monitors(GrowableArray<ObjectMonitor*>* delete_list,
1114                               ObjectMonitorDeflationSafepointer* safepointer) {
1115   NativeHeapTrimmer::SuspendMark sm("monitor deletion");
1116   size_t deleted_count = 0;
1117   for (ObjectMonitor* monitor: *delete_list) {
1118     delete monitor;
1119     deleted_count++;
1120     // A JavaThread must check for a safepoint/handshake and honor it.
1121     safepointer->block_for_safepoint("deletion", "deleted_count", deleted_count);
1122   }
1123   return deleted_count;
1124 }
1125 
1126 class ObjectMonitorDeflationLogging: public StackObj {
1127   LogStreamHandle(Debug, monitorinflation) _debug;
1128   LogStreamHandle(Info, monitorinflation)  _info;
1129   LogStream*                               _stream;
1130   elapsedTimer                             _timer;
1131 
1132   size_t ceiling() const { return ObjectSynchronizer::in_use_list_ceiling(); }
1133   size_t count() const   { return ObjectSynchronizer::in_use_list_count(); }
1134   size_t max() const     { return ObjectSynchronizer::in_use_list_max(); }
1135 
1136 public:
1137   ObjectMonitorDeflationLogging()
1138     : _debug(), _info(), _stream(nullptr) {
1139     if (_debug.is_enabled()) {
1140       _stream = &_debug;
1141     } else if (_info.is_enabled()) {
1142       _stream = &_info;
1143     }
1144   }
1145 
1146   void begin() {
1147     if (_stream != nullptr) {
1148       _stream->print_cr("begin deflating: in_use_list stats: ceiling=%zu, count=%zu, max=%zu",
1149                         ceiling(), count(), max());
1150       _timer.start();
1151     }
1152   }
1153 
1154   void before_handshake(size_t unlinked_count) {
1155     if (_stream != nullptr) {
1156       _timer.stop();
1157       _stream->print_cr("before handshaking: unlinked_count=%zu"
1158                         ", in_use_list stats: ceiling=%zu, count="
1159                         "%zu, max=%zu",
1160                         unlinked_count, ceiling(), count(), max());
1161     }
1162   }
1163 
1164   void after_handshake() {
1165     if (_stream != nullptr) {
1166       _stream->print_cr("after handshaking: in_use_list stats: ceiling="
1167                         "%zu, count=%zu, max=%zu",
1168                         ceiling(), count(), max());
1169       _timer.start();
1170     }
1171   }
1172 
1173   void end(size_t deflated_count, size_t unlinked_count) {
1174     if (_stream != nullptr) {
1175       _timer.stop();
1176       if (deflated_count != 0 || unlinked_count != 0 || _debug.is_enabled()) {
1177         _stream->print_cr("deflated_count=%zu, {unlinked,deleted}_count=%zu monitors in %3.7f secs",
1178                           deflated_count, unlinked_count, _timer.seconds());
1179       }
1180       _stream->print_cr("end deflating: in_use_list stats: ceiling=%zu, count=%zu, max=%zu",
1181                         ceiling(), count(), max());
1182     }
1183   }
1184 
1185   void before_block_for_safepoint(const char* op_name, const char* cnt_name, size_t cnt) {
1186     if (_stream != nullptr) {
1187       _timer.stop();
1188       _stream->print_cr("pausing %s: %s=%zu, in_use_list stats: ceiling="
1189                         "%zu, count=%zu, max=%zu",
1190                         op_name, cnt_name, cnt, ceiling(), count(), max());
1191     }
1192   }
1193 
1194   void after_block_for_safepoint(const char* op_name) {
1195     if (_stream != nullptr) {
1196       _stream->print_cr("resuming %s: in_use_list stats: ceiling=%zu"
1197                         ", count=%zu, max=%zu", op_name,
1198                         ceiling(), count(), max());
1199       _timer.start();
1200     }
1201   }
1202 };
1203 
1204 void ObjectMonitorDeflationSafepointer::block_for_safepoint(const char* op_name, const char* count_name, size_t counter) {
1205   if (!SafepointMechanism::should_process(_current)) {
1206     return;
1207   }
1208 
1209   // A safepoint/handshake has started.
1210   _log->before_block_for_safepoint(op_name, count_name, counter);
1211 
1212   {
1213     // Honor block request.
1214     ThreadBlockInVM tbivm(_current);
1215   }
1216 
1217   _log->after_block_for_safepoint(op_name);
1218 }
1219 
1220 // This function is called by the MonitorDeflationThread to deflate
1221 // ObjectMonitors.
1222 size_t ObjectSynchronizer::deflate_idle_monitors() {
1223   JavaThread* current = JavaThread::current();
1224   assert(current->is_monitor_deflation_thread(), "The only monitor deflater");
1225 
1226   // The async deflation request has been processed.
1227   _last_async_deflation_time_ns = os::javaTimeNanos();
1228   set_is_async_deflation_requested(false);
1229 
1230   ObjectMonitorDeflationLogging log;
1231   ObjectMonitorDeflationSafepointer safepointer(current, &log);
1232 
1233   log.begin();
1234 
1235   // Deflate some idle ObjectMonitors.
1236   size_t deflated_count = deflate_monitor_list(&safepointer);
1237 
1238   // Unlink the deflated ObjectMonitors from the in-use list.
1239   size_t unlinked_count = 0;
1240   size_t deleted_count = 0;
1241   if (deflated_count > 0) {
1242     ResourceMark rm(current);
1243     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1244     unlinked_count = _in_use_list.unlink_deflated(deflated_count, &delete_list, &safepointer);
1245 
1246     GrowableArray<ObjectMonitorTable::Table*> table_delete_list;
1247     if (UseObjectMonitorTable) {
1248       ObjectMonitorTable::rebuild(&table_delete_list);
1249     }
1250 
1251     log.before_handshake(unlinked_count);
1252 
1253     // A JavaThread needs to handshake in order to safely free the
1254     // ObjectMonitors that were deflated in this cycle.
1255     DeflationHandshakeClosure dhc;
1256     Handshake::execute(&dhc);
1257     // Also, we sync and desync GC threads around the handshake, so that they can
1258     // safely read the mark-word and look-through to the object-monitor, without
1259     // being afraid that the object-monitor is going away.
1260     VM_RendezvousGCThreads sync_gc;
1261     VMThread::execute(&sync_gc);
1262 
1263     log.after_handshake();
1264 
1265     // After the handshake, safely free the ObjectMonitors that were
1266     // deflated and unlinked in this cycle.
1267 
1268     // Delete the unlinked ObjectMonitors.
1269     deleted_count = delete_monitors(&delete_list, &safepointer);
1270     if (UseObjectMonitorTable) {
1271       ObjectMonitorTable::destroy(&table_delete_list);
1272     }
1273     assert(unlinked_count == deleted_count, "must be");
1274   }
1275 
1276   log.end(deflated_count, unlinked_count);
1277 
1278   GVars.stw_random = os::random();
1279 
1280   if (deflated_count != 0) {
1281     _no_progress_cnt = 0;
1282   } else if (_no_progress_skip_increment) {
1283     _no_progress_skip_increment = false;
1284   } else {
1285     _no_progress_cnt++;
1286   }
1287 
1288   return deflated_count;
1289 }
1290 
1291 // Monitor cleanup on JavaThread::exit
1292 
1293 // Iterate through monitor cache and attempt to release thread's monitors
1294 class ReleaseJavaMonitorsClosure: public MonitorClosure {
1295  private:
1296   JavaThread* _thread;
1297 
1298  public:
1299   ReleaseJavaMonitorsClosure(JavaThread* thread) : _thread(thread) {}
1300   void do_monitor(ObjectMonitor* mid) {
1301     mid->complete_exit(_thread);
1302   }
1303 };
1304 
1305 // Release all inflated monitors owned by current thread.  Lightweight monitors are
1306 // ignored.  This is meant to be called during JNI thread detach which assumes
1307 // all remaining monitors are heavyweight.  All exceptions are swallowed.
1308 // Scanning the extant monitor list can be time consuming.
1309 // A simple optimization is to add a per-thread flag that indicates a thread
1310 // called jni_monitorenter() during its lifetime.
1311 //
1312 // Instead of NoSafepointVerifier it might be cheaper to
1313 // use an idiom of the form:
1314 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
1315 //   <code that must not run at safepoint>
1316 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1317 // Since the tests are extremely cheap we could leave them enabled
1318 // for normal product builds.
1319 
1320 void ObjectSynchronizer::release_monitors_owned_by_thread(JavaThread* current) {
1321   assert(current == JavaThread::current(), "must be current Java thread");
1322   NoSafepointVerifier nsv;
1323   ReleaseJavaMonitorsClosure rjmc(current);
1324   ObjectSynchronizer::owned_monitors_iterate(&rjmc, current);
1325   assert(!current->has_pending_exception(), "Should not be possible");
1326   current->clear_pending_exception();
1327 }
1328 
1329 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) {
1330   switch (cause) {
1331     case inflate_cause_vm_internal:    return "VM Internal";
1332     case inflate_cause_monitor_enter:  return "Monitor Enter";
1333     case inflate_cause_wait:           return "Monitor Wait";
1334     case inflate_cause_notify:         return "Monitor Notify";
1335     case inflate_cause_jni_enter:      return "JNI Monitor Enter";
1336     case inflate_cause_jni_exit:       return "JNI Monitor Exit";
1337     default:
1338       ShouldNotReachHere();
1339   }
1340   return "Unknown";
1341 }
1342 
1343 //------------------------------------------------------------------------------
1344 // Debugging code
1345 
1346 u_char* ObjectSynchronizer::get_gvars_addr() {
1347   return (u_char*)&GVars;
1348 }
1349 
1350 u_char* ObjectSynchronizer::get_gvars_hc_sequence_addr() {
1351   return (u_char*)&GVars.hc_sequence;
1352 }
1353 
1354 size_t ObjectSynchronizer::get_gvars_size() {
1355   return sizeof(SharedGlobals);
1356 }
1357 
1358 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() {
1359   return (u_char*)&GVars.stw_random;
1360 }
1361 
1362 // Do the final audit and print of ObjectMonitor stats; must be done
1363 // by the VMThread at VM exit time.
1364 void ObjectSynchronizer::do_final_audit_and_print_stats() {
1365   assert(Thread::current()->is_VM_thread(), "sanity check");
1366 
1367   if (is_final_audit()) {  // Only do the audit once.
1368     return;
1369   }
1370   set_is_final_audit();
1371   log_info(monitorinflation)("Starting the final audit.");
1372 
1373   if (log_is_enabled(Info, monitorinflation)) {
1374     LogStreamHandle(Info, monitorinflation) ls;
1375     audit_and_print_stats(&ls, true /* on_exit */);
1376   }
1377 }
1378 
1379 // This function can be called by the MonitorDeflationThread or it can be called when
1380 // we are trying to exit the VM. The list walker functions can run in parallel with
1381 // the other list operations.
1382 // Calls to this function can be added in various places as a debugging
1383 // aid.
1384 //
1385 void ObjectSynchronizer::audit_and_print_stats(outputStream* ls, bool on_exit) {
1386   int error_cnt = 0;
1387 
1388   ls->print_cr("Checking in_use_list:");
1389   chk_in_use_list(ls, &error_cnt);
1390 
1391   if (error_cnt == 0) {
1392     ls->print_cr("No errors found in in_use_list checks.");
1393   } else {
1394     log_error(monitorinflation)("found in_use_list errors: error_cnt=%d", error_cnt);
1395   }
1396 
1397   // When exiting, only log the interesting entries at the Info level.
1398   // When called at intervals by the MonitorDeflationThread, log output
1399   // at the Trace level since there can be a lot of it.
1400   if (!on_exit && log_is_enabled(Trace, monitorinflation)) {
1401     LogStreamHandle(Trace, monitorinflation) ls_tr;
1402     log_in_use_monitor_details(&ls_tr, true /* log_all */);
1403   } else if (on_exit) {
1404     log_in_use_monitor_details(ls, false /* log_all */);
1405   }
1406 
1407   ls->flush();
1408 
1409   guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt);
1410 }
1411 
1412 // Check the in_use_list; log the results of the checks.
1413 void ObjectSynchronizer::chk_in_use_list(outputStream* out, int *error_cnt_p) {
1414   size_t l_in_use_count = _in_use_list.count();
1415   size_t l_in_use_max = _in_use_list.max();
1416   out->print_cr("count=%zu, max=%zu", l_in_use_count,
1417                 l_in_use_max);
1418 
1419   size_t ck_in_use_count = 0;
1420   MonitorList::Iterator iter = _in_use_list.iterator();
1421   while (iter.has_next()) {
1422     ObjectMonitor* mid = iter.next();
1423     chk_in_use_entry(mid, out, error_cnt_p);
1424     ck_in_use_count++;
1425   }
1426 
1427   if (l_in_use_count == ck_in_use_count) {
1428     out->print_cr("in_use_count=%zu equals ck_in_use_count=%zu",
1429                   l_in_use_count, ck_in_use_count);
1430   } else {
1431     out->print_cr("WARNING: in_use_count=%zu is not equal to "
1432                   "ck_in_use_count=%zu", l_in_use_count,
1433                   ck_in_use_count);
1434   }
1435 
1436   size_t ck_in_use_max = _in_use_list.max();
1437   if (l_in_use_max == ck_in_use_max) {
1438     out->print_cr("in_use_max=%zu equals ck_in_use_max=%zu",
1439                   l_in_use_max, ck_in_use_max);
1440   } else {
1441     out->print_cr("WARNING: in_use_max=%zu is not equal to "
1442                   "ck_in_use_max=%zu", l_in_use_max, ck_in_use_max);
1443   }
1444 }
1445 
1446 // Check an in-use monitor entry; log any errors.
1447 void ObjectSynchronizer::chk_in_use_entry(ObjectMonitor* n, outputStream* out,
1448                                           int* error_cnt_p) {
1449   if (n->owner_is_DEFLATER_MARKER()) {
1450     // This could happen when monitor deflation blocks for a safepoint.
1451     return;
1452   }
1453 
1454 
1455   if (n->metadata() == 0) {
1456     out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor must "
1457                   "have non-null _metadata (header/hash) field.", p2i(n));
1458     *error_cnt_p = *error_cnt_p + 1;
1459   }
1460 
1461   const oop obj = n->object_peek();
1462   if (obj == nullptr) {
1463     return;
1464   }
1465 
1466   const markWord mark = obj->mark();
1467   // Note: When using ObjectMonitorTable we may observe an intermediate state,
1468   // where the monitor is globally visible, but no thread has yet transitioned
1469   // the markWord. To avoid reporting a false positive during this transition, we
1470   // skip the `!mark.has_monitor()` test if we are using the ObjectMonitorTable.
1471   if (!UseObjectMonitorTable && !mark.has_monitor()) {
1472     out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
1473                   "object does not think it has a monitor: obj="
1474                   INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n),
1475                   p2i(obj), mark.value());
1476     *error_cnt_p = *error_cnt_p + 1;
1477     return;
1478   }
1479 
1480   ObjectMonitor* const obj_mon = read_monitor(obj, mark);
1481   if (n != obj_mon) {
1482     out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
1483                   "object does not refer to the same monitor: obj="
1484                   INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
1485                   INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
1486     *error_cnt_p = *error_cnt_p + 1;
1487   }
1488 }
1489 
1490 // Log details about ObjectMonitors on the in_use_list. The 'BHL'
1491 // flags indicate why the entry is in-use, 'object' and 'object type'
1492 // indicate the associated object and its type.
1493 void ObjectSynchronizer::log_in_use_monitor_details(outputStream* out, bool log_all) {
1494   if (_in_use_list.count() > 0) {
1495     stringStream ss;
1496     out->print_cr("In-use monitor info%s:", log_all ? "" : " (eliding idle monitors)");
1497     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
1498     out->print_cr("%18s  %s  %18s  %18s",
1499                   "monitor", "BHL", "object", "object type");
1500     out->print_cr("==================  ===  ==================  ==================");
1501 
1502     auto is_interesting = [&](ObjectMonitor* monitor) {
1503       return log_all || monitor->has_owner() || monitor->is_busy();
1504     };
1505 
1506     monitors_iterate([&](ObjectMonitor* monitor) {
1507       if (is_interesting(monitor)) {
1508         const oop obj = monitor->object_peek();
1509         const intptr_t hash = UseObjectMonitorTable ? monitor->hash() : monitor->header().hash();
1510         ResourceMark rm;
1511         out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(monitor),
1512                    monitor->is_busy(), hash != 0, monitor->has_owner(),
1513                    p2i(obj), obj == nullptr ? "" : obj->klass()->external_name());
1514         if (monitor->is_busy()) {
1515           out->print(" (%s)", monitor->is_busy_to_string(&ss));
1516           ss.reset();
1517         }
1518         out->cr();
1519       }
1520     });
1521   }
1522 
1523   out->flush();
1524 }
1525 
1526 ObjectMonitor* ObjectSynchronizer::get_or_insert_monitor_from_table(oop object, bool* inserted) {
1527   ObjectMonitor* monitor = get_monitor_from_table(object);
1528   if (monitor != nullptr) {
1529     *inserted = false;
1530     return monitor;
1531   }
1532 
1533   ObjectMonitor* alloced_monitor = new ObjectMonitor(object);
1534   alloced_monitor->set_anonymous_owner();
1535 
1536   // Try insert monitor
1537   monitor = add_monitor(alloced_monitor, object);
1538 
1539   *inserted = alloced_monitor == monitor;
1540   if (!*inserted) {
1541     delete alloced_monitor;
1542   }
1543 
1544   return monitor;
1545 }
1546 
1547 static void log_inflate(Thread* current, oop object, ObjectSynchronizer::InflateCause cause) {
1548   if (log_is_enabled(Trace, monitorinflation)) {
1549     ResourceMark rm(current);
1550     log_trace(monitorinflation)("inflate: object=" INTPTR_FORMAT ", mark="
1551                                 INTPTR_FORMAT ", type='%s' cause=%s", p2i(object),
1552                                 object->mark().value(), object->klass()->external_name(),
1553                                 ObjectSynchronizer::inflate_cause_name(cause));
1554   }
1555 }
1556 
1557 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1558                                        const oop obj,
1559                                        ObjectSynchronizer::InflateCause cause) {
1560   assert(event != nullptr, "invariant");
1561   const Klass* monitor_klass = obj->klass();
1562   if (ObjectMonitor::is_jfr_excluded(monitor_klass)) {
1563     return;
1564   }
1565   event->set_monitorClass(monitor_klass);
1566   event->set_address((uintptr_t)(void*)obj);
1567   event->set_cause((u1)cause);
1568   event->commit();
1569 }
1570 
1571 ObjectMonitor* ObjectSynchronizer::get_or_insert_monitor(oop object, JavaThread* current, ObjectSynchronizer::InflateCause cause) {
1572   assert(UseObjectMonitorTable, "must be");
1573 
1574   EventJavaMonitorInflate event;
1575 
1576   bool inserted;
1577   ObjectMonitor* monitor = get_or_insert_monitor_from_table(object, &inserted);
1578 
1579   if (inserted) {
1580     log_inflate(current, object, cause);
1581     if (event.should_commit()) {
1582       post_monitor_inflate_event(&event, object, cause);
1583     }
1584 
1585     // The monitor has an anonymous owner so it is safe from async deflation.
1586     ObjectSynchronizer::_in_use_list.add(monitor);
1587   }
1588 
1589   return monitor;
1590 }
1591 
1592 // Add the hashcode to the monitor to match the object and put it in the hashtable.
1593 ObjectMonitor* ObjectSynchronizer::add_monitor(ObjectMonitor* monitor, oop obj) {
1594   assert(UseObjectMonitorTable, "must be");
1595   assert(obj == monitor->object(), "must be");
1596 
1597   markWord mark = obj->mark();
1598   intptr_t hash;
1599   if (UseCompactObjectHeaders) {
1600     hash = static_cast<intptr_t>(get_hash(mark, obj));
1601   } else {
1602     hash = mark.hash();
1603   }
1604   assert(hash != 0, "must be set when claiming the object monitor");
1605   monitor->set_hash(hash);
1606 
1607   return ObjectMonitorTable::monitor_put_get(monitor, obj);
1608 }
1609 
1610 void ObjectSynchronizer::remove_monitor(ObjectMonitor* monitor, oop obj) {
1611   assert(UseObjectMonitorTable, "must be");
1612   assert(monitor->object_peek() == obj, "must be, cleared objects are removed by is_dead");
1613 
1614   ObjectMonitorTable::remove_monitor_entry(monitor);
1615 }
1616 
1617 void ObjectSynchronizer::deflate_mark_word(oop obj) {
1618   assert(UseObjectMonitorTable, "must be");
1619 
1620   markWord mark = obj->mark_acquire();
1621   assert(!mark.has_no_hash(), "obj with inflated monitor must have had a hash");
1622 
1623   while (mark.has_monitor()) {
1624     const markWord new_mark = mark.clear_lock_bits().set_unlocked();
1625     mark = obj->cas_set_mark(new_mark, mark);
1626   }
1627 }
1628 
1629 void ObjectSynchronizer::create_om_table() {
1630   if (!UseObjectMonitorTable) {
1631     return;
1632   }
1633   ObjectMonitorTable::create();
1634 }
1635 
1636 class ObjectSynchronizer::LockStackInflateContendedLocks : private OopClosure {
1637  private:
1638   oop _contended_oops[LockStack::CAPACITY];
1639   int _length;
1640 
1641   void do_oop(oop* o) final {
1642     oop obj = *o;
1643     if (obj->mark_acquire().has_monitor()) {
1644       if (_length > 0 && _contended_oops[_length - 1] == obj) {
1645         // Recursive
1646         return;
1647       }
1648       _contended_oops[_length++] = obj;
1649     }
1650   }
1651 
1652   void do_oop(narrowOop* o) final {
1653     ShouldNotReachHere();
1654   }
1655 
1656  public:
1657   LockStackInflateContendedLocks() :
1658     _contended_oops(),
1659     _length(0) {};
1660 
1661   void inflate(JavaThread* current) {
1662     assert(current == JavaThread::current(), "must be");
1663     current->lock_stack().oops_do(this);
1664     for (int i = 0; i < _length; i++) {
1665       ObjectSynchronizer::
1666         inflate_fast_locked_object(_contended_oops[i], ObjectSynchronizer::inflate_cause_vm_internal, current, current);
1667     }
1668   }
1669 };
1670 
1671 void ObjectSynchronizer::ensure_lock_stack_space(JavaThread* current) {
1672   assert(current == JavaThread::current(), "must be");
1673   LockStack& lock_stack = current->lock_stack();
1674 
1675   // Make room on lock_stack
1676   if (lock_stack.is_full()) {
1677     // Inflate contended objects
1678     LockStackInflateContendedLocks().inflate(current);
1679     if (lock_stack.is_full()) {
1680       // Inflate the oldest object
1681       inflate_fast_locked_object(lock_stack.bottom(), ObjectSynchronizer::inflate_cause_vm_internal, current, current);
1682     }
1683   }
1684 }
1685 
1686 class ObjectSynchronizer::CacheSetter : StackObj {
1687   JavaThread* const _thread;
1688   BasicLock* const _lock;
1689   ObjectMonitor* _monitor;
1690 
1691   NONCOPYABLE(CacheSetter);
1692 
1693  public:
1694   CacheSetter(JavaThread* thread, BasicLock* lock) :
1695     _thread(thread),
1696     _lock(lock),
1697     _monitor(nullptr) {}
1698 
1699   ~CacheSetter() {
1700     // Only use the cache if using the table.
1701     if (UseObjectMonitorTable) {
1702       if (_monitor != nullptr) {
1703         // If the monitor is already in the BasicLock cache then it is most
1704         // likely in the thread cache, do not set it again to avoid reordering.
1705         if (_monitor != _lock->object_monitor_cache()) {
1706           _thread->om_set_monitor_cache(_monitor);
1707           _lock->set_object_monitor_cache(_monitor);
1708         }
1709       } else {
1710         _lock->clear_object_monitor_cache();
1711       }
1712     }
1713   }
1714 
1715   void set_monitor(ObjectMonitor* monitor) {
1716     assert(_monitor == nullptr, "only set once");
1717     _monitor = monitor;
1718   }
1719 
1720 };
1721 
1722 // Reads first from the BasicLock cache then from the OMCache in the current thread.
1723 // C2 fast-path may have put the monitor in the cache in the BasicLock.
1724 inline static ObjectMonitor* read_caches(JavaThread* current, BasicLock* lock, oop object) {
1725   ObjectMonitor* monitor = lock->object_monitor_cache();
1726   if (monitor == nullptr) {
1727     monitor = current->om_get_from_monitor_cache(object);
1728   }
1729   return monitor;
1730 }
1731 
1732 class ObjectSynchronizer::VerifyThreadState {
1733   bool _no_safepoint;
1734 
1735  public:
1736   VerifyThreadState(JavaThread* locking_thread, JavaThread* current) : _no_safepoint(locking_thread != current) {
1737     assert(current == Thread::current(), "must be");
1738     assert(locking_thread == current || locking_thread->is_obj_deopt_suspend(), "locking_thread may not run concurrently");
1739     if (_no_safepoint) {
1740       DEBUG_ONLY(JavaThread::current()->inc_no_safepoint_count();)
1741     }
1742   }
1743   ~VerifyThreadState() {
1744     if (_no_safepoint){
1745       DEBUG_ONLY(JavaThread::current()->dec_no_safepoint_count();)
1746     }
1747   }
1748 };
1749 
1750 inline bool ObjectSynchronizer::fast_lock_try_enter(oop obj, LockStack& lock_stack, JavaThread* current) {
1751   markWord mark = obj->mark();
1752   while (mark.is_unlocked()) {
1753     ensure_lock_stack_space(current);
1754     assert(!lock_stack.is_full(), "must have made room on the lock stack");
1755     assert(!lock_stack.contains(obj), "thread must not already hold the lock");
1756     // Try to swing into 'fast-locked' state.
1757     markWord locked_mark = mark.set_fast_locked();
1758     markWord old_mark = mark;
1759     mark = obj->cas_set_mark(locked_mark, old_mark);
1760     if (old_mark == mark) {
1761       // Successfully fast-locked, push object to lock-stack and return.
1762       lock_stack.push(obj);
1763       return true;
1764     }
1765   }
1766   return false;
1767 }
1768 
1769 bool ObjectSynchronizer::fast_lock_spin_enter(oop obj, LockStack& lock_stack, JavaThread* current, bool observed_deflation) {
1770   assert(UseObjectMonitorTable, "must be");
1771   // Will spin with exponential backoff with an accumulative O(2^spin_limit) spins.
1772   const int log_spin_limit = os::is_MP() ? FastLockingSpins : 1;
1773   const int log_min_safepoint_check_interval = 10;
1774 
1775   markWord mark = obj->mark();
1776   const auto should_spin = [&]() {
1777     if (!mark.has_monitor()) {
1778       // Spin while not inflated.
1779       return true;
1780     } else if (observed_deflation) {
1781       // Spin while monitor is being deflated.
1782       ObjectMonitor* monitor = ObjectSynchronizer::read_monitor(obj, mark);
1783       return monitor == nullptr || monitor->is_being_async_deflated();
1784     }
1785     // Else stop spinning.
1786     return false;
1787   };
1788   // Always attempt to lock once even when safepoint synchronizing.
1789   bool should_process = false;
1790   for (int i = 0; should_spin() && !should_process && i < log_spin_limit; i++) {
1791     // Spin with exponential backoff.
1792     const int total_spin_count = 1 << i;
1793     const int inner_spin_count = MIN2(1 << log_min_safepoint_check_interval, total_spin_count);
1794     const int outer_spin_count = total_spin_count / inner_spin_count;
1795     for (int outer = 0; outer < outer_spin_count; outer++) {
1796       should_process = SafepointMechanism::should_process(current);
1797       if (should_process) {
1798         // Stop spinning for safepoint.
1799         break;
1800       }
1801       for (int inner = 1; inner < inner_spin_count; inner++) {
1802         SpinPause();
1803       }
1804     }
1805 
1806     if (fast_lock_try_enter(obj, lock_stack, current)) return true;
1807   }
1808   return false;
1809 }
1810 
1811 void ObjectSynchronizer::enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread) {
1812   // When called with locking_thread != Thread::current() some mechanism must synchronize
1813   // the locking_thread with respect to the current thread. Currently only used when
1814   // deoptimizing and re-locking locks. See Deoptimization::relock_objects
1815   assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
1816 
1817   assert(!UseObjectMonitorTable || lock->object_monitor_cache() == nullptr, "must be cleared");
1818   JavaThread* current = JavaThread::current();
1819   VerifyThreadState vts(locking_thread, current);
1820 
1821   if (obj->klass()->is_value_based()) {
1822     ObjectSynchronizer::handle_sync_on_value_based_class(obj, locking_thread);
1823   }
1824 
1825   LockStack& lock_stack = locking_thread->lock_stack();
1826 
1827   ObjectMonitor* monitor = nullptr;
1828   if (lock_stack.contains(obj())) {
1829     monitor = inflate_fast_locked_object(obj(), ObjectSynchronizer::inflate_cause_monitor_enter, locking_thread, current);
1830     bool entered = monitor->enter_for(locking_thread);
1831     assert(entered, "recursive ObjectMonitor::enter_for must succeed");
1832   } else {
1833     do {
1834       // It is assumed that enter_for must enter on an object without contention.
1835       monitor = inflate_and_enter(obj(), lock, ObjectSynchronizer::inflate_cause_monitor_enter, locking_thread, current);
1836       // But there may still be a race with deflation.
1837     } while (monitor == nullptr);
1838   }
1839 
1840   assert(monitor != nullptr, "ObjectSynchronizer::enter_for must succeed");
1841   assert(!UseObjectMonitorTable || lock->object_monitor_cache() == nullptr, "unused. already cleared");
1842 }
1843 
1844 void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, JavaThread* current) {
1845   assert(current == JavaThread::current(), "must be");
1846 
1847   if (obj->klass()->is_value_based()) {
1848     ObjectSynchronizer::handle_sync_on_value_based_class(obj, current);
1849   }
1850 
1851   CacheSetter cache_setter(current, lock);
1852 
1853   // Used when deflation is observed. Progress here requires progress
1854   // from the deflator. After observing that the deflator is not
1855   // making progress (after two yields), switch to sleeping.
1856   SpinYield spin_yield(0, 2);
1857   bool observed_deflation = false;
1858 
1859   LockStack& lock_stack = current->lock_stack();
1860 
1861   if (!lock_stack.is_full() && lock_stack.try_recursive_enter(obj())) {
1862     // Recursively fast locked
1863     return;
1864   }
1865 
1866   if (lock_stack.contains(obj())) {
1867     ObjectMonitor* monitor = inflate_fast_locked_object(obj(), ObjectSynchronizer::inflate_cause_monitor_enter, current, current);
1868     bool entered = monitor->enter(current);
1869     assert(entered, "recursive ObjectMonitor::enter must succeed");
1870     cache_setter.set_monitor(monitor);
1871     return;
1872   }
1873 
1874   while (true) {
1875     // Fast-locking does not use the 'lock' argument.
1876     // Fast-lock spinning to avoid inflating for short critical sections.
1877     // The goal is to only inflate when the extra cost of using ObjectMonitors
1878     // is worth it.
1879     // If deflation has been observed we also spin while deflation is ongoing.
1880     if (fast_lock_try_enter(obj(), lock_stack, current)) {
1881       return;
1882     } else if (UseObjectMonitorTable && fast_lock_spin_enter(obj(), lock_stack, current, observed_deflation)) {
1883       return;
1884     }
1885 
1886     if (observed_deflation) {
1887       spin_yield.wait();
1888     }
1889 
1890     ObjectMonitor* monitor = inflate_and_enter(obj(), lock, ObjectSynchronizer::inflate_cause_monitor_enter, current, current);
1891     if (monitor != nullptr) {
1892       cache_setter.set_monitor(monitor);
1893       return;
1894     }
1895 
1896     // If inflate_and_enter returns nullptr it is because a deflated monitor
1897     // was encountered. Fallback to fast locking. The deflater is responsible
1898     // for clearing out the monitor and transitioning the markWord back to
1899     // fast locking.
1900     observed_deflation = true;
1901   }
1902 }
1903 
1904 void ObjectSynchronizer::exit(oop object, BasicLock* lock, JavaThread* current) {
1905   assert(current == Thread::current(), "must be");
1906 
1907   markWord mark = object->mark();
1908   assert(!mark.is_unlocked(), "must be");
1909 
1910   LockStack& lock_stack = current->lock_stack();
1911   if (mark.is_fast_locked()) {
1912     if (lock_stack.try_recursive_exit(object)) {
1913       // This is a recursive exit which succeeded
1914       return;
1915     }
1916     if (lock_stack.is_recursive(object)) {
1917       // Must inflate recursive locks if try_recursive_exit fails
1918       // This happens for un-structured unlocks, could potentially
1919       // fix try_recursive_exit to handle these.
1920       inflate_fast_locked_object(object, ObjectSynchronizer::inflate_cause_vm_internal, current, current);
1921     }
1922   }
1923 
1924   while (mark.is_fast_locked()) {
1925     markWord unlocked_mark = mark.set_unlocked();
1926     markWord old_mark = mark;
1927     mark = object->cas_set_mark(unlocked_mark, old_mark);
1928     if (old_mark == mark) {
1929       // CAS successful, remove from lock_stack
1930       size_t recursion = lock_stack.remove(object) - 1;
1931       assert(recursion == 0, "Should not have unlocked here");
1932       return;
1933     }
1934   }
1935 
1936   assert(mark.has_monitor(), "must be");
1937   // The monitor exists
1938   ObjectMonitor* monitor;
1939   if (UseObjectMonitorTable) {
1940     monitor = read_caches(current, lock, object);
1941     if (monitor == nullptr) {
1942       monitor = get_monitor_from_table(object);
1943     }
1944   } else {
1945     monitor = ObjectSynchronizer::read_monitor(mark);
1946   }
1947   if (monitor->has_anonymous_owner()) {
1948     assert(current->lock_stack().contains(object), "current must have object on its lock stack");
1949     monitor->set_owner_from_anonymous(current);
1950     monitor->set_recursions(current->lock_stack().remove(object) - 1);
1951   }
1952 
1953   monitor->exit(current);
1954 }
1955 
1956 // ObjectSynchronizer::inflate_locked_or_imse is used to get an
1957 // inflated ObjectMonitor* from contexts which require that, such as
1958 // notify/wait and jni_exit. Fast locking keeps the invariant that it
1959 // only inflates if it is already locked by the current thread or the current
1960 // thread is in the process of entering. To maintain this invariant we need to
1961 // throw a java.lang.IllegalMonitorStateException before inflating if the
1962 // current thread is not the owner.
1963 ObjectMonitor* ObjectSynchronizer::inflate_locked_or_imse(oop obj, ObjectSynchronizer::InflateCause cause, TRAPS) {
1964   JavaThread* current = THREAD;
1965 
1966   for (;;) {
1967     markWord mark = obj->mark_acquire();
1968     if (mark.is_unlocked()) {
1969       // No lock, IMSE.
1970       THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1971                  "current thread is not owner", nullptr);
1972     }
1973 
1974     if (mark.is_fast_locked()) {
1975       if (!current->lock_stack().contains(obj)) {
1976         // Fast locked by other thread, IMSE.
1977         THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1978                    "current thread is not owner", nullptr);
1979       } else {
1980         // Current thread owns the lock, must inflate
1981         return inflate_fast_locked_object(obj, cause, current, current);
1982       }
1983     }
1984 
1985     assert(mark.has_monitor(), "must be");
1986     ObjectMonitor* monitor = ObjectSynchronizer::read_monitor(obj, mark);
1987     if (monitor != nullptr) {
1988       if (monitor->has_anonymous_owner()) {
1989         LockStack& lock_stack = current->lock_stack();
1990         if (lock_stack.contains(obj)) {
1991           // Current thread owns the lock but someone else inflated it.
1992           // Fix owner and pop lock stack.
1993           monitor->set_owner_from_anonymous(current);
1994           monitor->set_recursions(lock_stack.remove(obj) - 1);
1995         } else {
1996           // Fast locked (and inflated) by other thread, or deflation in progress, IMSE.
1997           THROW_MSG_(vmSymbols::java_lang_IllegalMonitorStateException(),
1998                      "current thread is not owner", nullptr);
1999         }
2000       }
2001       return monitor;
2002     }
2003   }
2004 }
2005 
2006 ObjectMonitor* ObjectSynchronizer::inflate_into_object_header(oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, Thread* current) {
2007 
2008   // The JavaThread* locking parameter requires that the locking_thread == JavaThread::current,
2009   // or is suspended throughout the call by some other mechanism.
2010   // Even with fast locking the thread might be nullptr when called from a non
2011   // JavaThread. (As may still be the case from FastHashCode). However it is only
2012   // important for the correctness of the fast locking algorithm that the thread
2013   // is set when called from ObjectSynchronizer::enter from the owning thread,
2014   // ObjectSynchronizer::enter_for from any thread, or ObjectSynchronizer::exit.
2015   EventJavaMonitorInflate event;
2016 
2017   for (;;) {
2018     const markWord mark = object->mark_acquire();
2019 
2020     // The mark can be in one of the following states:
2021     // *  inflated     - If the ObjectMonitor owner is anonymous and the
2022     //                   locking_thread owns the object lock, then we make the
2023     //                   locking_thread the ObjectMonitor owner and remove the
2024     //                   lock from the locking_thread's lock stack.
2025     // *  fast-locked  - Coerce it to inflated from fast-locked.
2026     // *  unlocked     - Aggressively inflate the object.
2027 
2028     // CASE: inflated
2029     if (mark.has_monitor()) {
2030       ObjectMonitor* inf = mark.monitor();
2031       markWord dmw = inf->header();
2032       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
2033       if (inf->has_anonymous_owner() &&
2034           locking_thread != nullptr && locking_thread->lock_stack().contains(object)) {
2035         inf->set_owner_from_anonymous(locking_thread);
2036         size_t removed = locking_thread->lock_stack().remove(object);
2037         inf->set_recursions(removed - 1);
2038       }
2039       return inf;
2040     }
2041 
2042     // CASE: fast-locked
2043     // Could be fast-locked either by the locking_thread or by some other thread.
2044     //
2045     // Note that we allocate the ObjectMonitor speculatively, _before_
2046     // attempting to set the object's mark to the new ObjectMonitor. If
2047     // the locking_thread owns the monitor, then we set the ObjectMonitor's
2048     // owner to the locking_thread. Otherwise, we set the ObjectMonitor's owner
2049     // to anonymous. If we lose the race to set the object's mark to the
2050     // new ObjectMonitor, then we just delete it and loop around again.
2051     //
2052     if (mark.is_fast_locked()) {
2053       ObjectMonitor* monitor = new ObjectMonitor(object);
2054       monitor->set_header(mark.set_unlocked());
2055       bool own = locking_thread != nullptr && locking_thread->lock_stack().contains(object);
2056       if (own) {
2057         // Owned by locking_thread.
2058         monitor->set_owner(locking_thread);
2059       } else {
2060         // Owned by somebody else.
2061         monitor->set_anonymous_owner();
2062       }
2063       markWord monitor_mark = markWord::encode(monitor);
2064       markWord old_mark = object->cas_set_mark(monitor_mark, mark);
2065       if (old_mark == mark) {
2066         // Success! Return inflated monitor.
2067         if (own) {
2068           size_t removed = locking_thread->lock_stack().remove(object);
2069           monitor->set_recursions(removed - 1);
2070         }
2071         // Once the ObjectMonitor is configured and object is associated
2072         // with the ObjectMonitor, it is safe to allow async deflation:
2073         ObjectSynchronizer::_in_use_list.add(monitor);
2074 
2075         log_inflate(current, object, cause);
2076         if (event.should_commit()) {
2077           post_monitor_inflate_event(&event, object, cause);
2078         }
2079         return monitor;
2080       } else {
2081         delete monitor;
2082         continue;  // Interference -- just retry
2083       }
2084     }
2085 
2086     // CASE: unlocked
2087     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
2088     // If we know we're inflating for entry it's better to inflate by swinging a
2089     // pre-locked ObjectMonitor pointer into the object header.   A successful
2090     // CAS inflates the object *and* confers ownership to the inflating thread.
2091     // In the current implementation we use a 2-step mechanism where we CAS()
2092     // to inflate and then CAS() again to try to swing _owner from null to current.
2093     // An inflateTry() method that we could call from enter() would be useful.
2094 
2095     assert(mark.is_unlocked(), "invariant: header=" INTPTR_FORMAT, mark.value());
2096     ObjectMonitor* m = new ObjectMonitor(object);
2097     // prepare m for installation - set monitor to initial state
2098     m->set_header(mark);
2099 
2100     if (object->cas_set_mark(markWord::encode(m), mark) != mark) {
2101       delete m;
2102       m = nullptr;
2103       continue;
2104       // interference - the markword changed - just retry.
2105       // The state-transitions are one-way, so there's no chance of
2106       // live-lock -- "Inflated" is an absorbing state.
2107     }
2108 
2109     // Once the ObjectMonitor is configured and object is associated
2110     // with the ObjectMonitor, it is safe to allow async deflation:
2111     ObjectSynchronizer::_in_use_list.add(m);
2112 
2113     log_inflate(current, object, cause);
2114     if (event.should_commit()) {
2115       post_monitor_inflate_event(&event, object, cause);
2116     }
2117     return m;
2118   }
2119 }
2120 
2121 ObjectMonitor* ObjectSynchronizer::inflate_fast_locked_object(oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current) {
2122   VerifyThreadState vts(locking_thread, current);
2123   assert(locking_thread->lock_stack().contains(object), "locking_thread must have object on its lock stack");
2124 
2125   ObjectMonitor* monitor;
2126 
2127   if (!UseObjectMonitorTable) {
2128     return inflate_into_object_header(object, cause, locking_thread, current);
2129   }
2130 
2131   // Inflating requires a hash code
2132   ObjectSynchronizer::FastHashCode(current, object);
2133 
2134   markWord mark = object->mark_acquire();
2135   assert(!mark.is_unlocked(), "Cannot be unlocked");
2136 
2137   for (;;) {
2138     // Fetch the monitor from the table
2139     monitor = get_or_insert_monitor(object, current, cause);
2140 
2141     // ObjectMonitors are always inserted as anonymously owned, this thread is
2142     // the current holder of the monitor. So unless the entry is stale and
2143     // contains a deflating monitor it must be anonymously owned.
2144     if (monitor->has_anonymous_owner()) {
2145       // The monitor must be anonymously owned if it was added
2146       assert(monitor == get_monitor_from_table(object), "The monitor must be found");
2147       // New fresh monitor
2148       break;
2149     }
2150 
2151     // If the monitor was not anonymously owned then we got a deflating monitor
2152     // from the table. We need to let the deflator make progress and remove this
2153     // entry before we are allowed to add a new one.
2154     os::naked_yield();
2155     assert(monitor->is_being_async_deflated(), "Should be the reason");
2156   }
2157 
2158   // Set the mark word; loop to handle concurrent updates to other parts of the mark word
2159   while (mark.is_fast_locked()) {
2160     mark = object->cas_set_mark(mark.set_has_monitor(), mark);
2161   }
2162 
2163   // Indicate that the monitor now has a known owner
2164   monitor->set_owner_from_anonymous(locking_thread);
2165 
2166   // Remove the entry from the thread's lock stack
2167   monitor->set_recursions(locking_thread->lock_stack().remove(object) - 1);
2168 
2169   if (locking_thread == current) {
2170     // Only change the thread local state of the current thread.
2171     locking_thread->om_set_monitor_cache(monitor);
2172   }
2173 
2174   return monitor;
2175 }
2176 
2177 ObjectMonitor* ObjectSynchronizer::inflate_and_enter(oop object, BasicLock* lock, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current) {
2178   VerifyThreadState vts(locking_thread, current);
2179 
2180   // Note: In some paths (deoptimization) the 'current' thread inflates and
2181   // enters the lock on behalf of the 'locking_thread' thread.
2182 
2183   ObjectMonitor* monitor = nullptr;
2184 
2185   if (!UseObjectMonitorTable) {
2186     // Do the old inflate and enter.
2187     monitor = inflate_into_object_header(object, cause, locking_thread, current);
2188 
2189     bool entered;
2190     if (locking_thread == current) {
2191       entered = monitor->enter(locking_thread);
2192     } else {
2193       entered = monitor->enter_for(locking_thread);
2194     }
2195 
2196     // enter returns false for deflation found.
2197     return entered ? monitor : nullptr;
2198   }
2199 
2200   NoSafepointVerifier nsv;
2201 
2202   // Try to get the monitor from the thread-local cache.
2203   // There's no need to use the cache if we are locking
2204   // on behalf of another thread.
2205   if (current == locking_thread) {
2206     monitor = read_caches(current, lock, object);
2207   }
2208 
2209   // Get or create the monitor
2210   if (monitor == nullptr) {
2211     // Lightweight monitors require that hash codes are installed first
2212     ObjectSynchronizer::FastHashCode(locking_thread, object);
2213     monitor = get_or_insert_monitor(object, current, cause);
2214   }
2215 
2216   if (monitor->try_enter(locking_thread)) {
2217     return monitor;
2218   }
2219 
2220   // Holds is_being_async_deflated() stable throughout this function.
2221   ObjectMonitorContentionMark contention_mark(monitor);
2222 
2223   /// First handle the case where the monitor from the table is deflated
2224   if (monitor->is_being_async_deflated()) {
2225     // The MonitorDeflation thread is deflating the monitor. The locking thread
2226     // must spin until further progress has been made.
2227 
2228     // Clear the BasicLock cache as it may contain this monitor.
2229     lock->clear_object_monitor_cache();
2230 
2231     const markWord mark = object->mark_acquire();
2232 
2233     if (mark.has_monitor()) {
2234       // Waiting on the deflation thread to remove the deflated monitor from the table.
2235       os::naked_yield();
2236 
2237     } else if (mark.is_fast_locked()) {
2238       // Some other thread managed to fast-lock the lock, or this is a
2239       // recursive lock from the same thread; yield for the deflation
2240       // thread to remove the deflated monitor from the table.
2241       os::naked_yield();
2242 
2243     } else {
2244       assert(mark.is_unlocked(), "Implied");
2245       // Retry immediately
2246     }
2247 
2248     // Retry
2249     return nullptr;
2250   }
2251 
2252   for (;;) {
2253     const markWord mark = object->mark_acquire();
2254     // The mark can be in one of the following states:
2255     // *  inflated     - If the ObjectMonitor owner is anonymous
2256     //                   and the locking_thread owns the object
2257     //                   lock, then we make the locking_thread
2258     //                   the ObjectMonitor owner and remove the
2259     //                   lock from the locking_thread's lock stack.
2260     // *  fast-locked  - Coerce it to inflated from fast-locked.
2261     // *  neutral      - Inflate the object. Successful CAS is locked
2262 
2263     // CASE: inflated
2264     if (mark.has_monitor()) {
2265       LockStack& lock_stack = locking_thread->lock_stack();
2266       if (monitor->has_anonymous_owner() && lock_stack.contains(object)) {
2267         // The lock is fast-locked by the locking thread,
2268         // convert it to a held monitor with a known owner.
2269         monitor->set_owner_from_anonymous(locking_thread);
2270         monitor->set_recursions(lock_stack.remove(object) - 1);
2271       }
2272 
2273       break; // Success
2274     }
2275 
2276     // CASE: fast-locked
2277     // Could be fast-locked either by locking_thread or by some other thread.
2278     //
2279     if (mark.is_fast_locked()) {
2280       markWord old_mark = object->cas_set_mark(mark.set_has_monitor(), mark);
2281       if (old_mark != mark) {
2282         // CAS failed
2283         continue;
2284       }
2285 
2286       // Success! Return inflated monitor.
2287       LockStack& lock_stack = locking_thread->lock_stack();
2288       if (lock_stack.contains(object)) {
2289         // The lock is fast-locked by the locking thread,
2290         // convert it to a held monitor with a known owner.
2291         monitor->set_owner_from_anonymous(locking_thread);
2292         monitor->set_recursions(lock_stack.remove(object) - 1);
2293       }
2294 
2295       break; // Success
2296     }
2297 
2298     // CASE: neutral (unlocked)
2299 
2300     // Catch if the object's header is not neutral (not locked and
2301     // not marked is what we care about here).
2302     assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
2303     markWord old_mark = object->cas_set_mark(mark.set_has_monitor(), mark);
2304     if (old_mark != mark) {
2305       // CAS failed
2306       continue;
2307     }
2308 
2309     // Transitioned from unlocked to monitor means locking_thread owns the lock.
2310     monitor->set_owner_from_anonymous(locking_thread);
2311 
2312     return monitor;
2313   }
2314 
2315   if (current == locking_thread) {
2316     // One round of spinning
2317     if (monitor->spin_enter(locking_thread)) {
2318       return monitor;
2319     }
2320 
2321     // Monitor is contended, take the time before entering to fix the lock stack.
2322     LockStackInflateContendedLocks().inflate(current);
2323   }
2324 
2325   // enter can block for safepoints; clear the unhandled object oop
2326   PauseNoSafepointVerifier pnsv(&nsv);
2327   object = nullptr;
2328 
2329   if (current == locking_thread) {
2330     monitor->enter_with_contention_mark(locking_thread, contention_mark);
2331   } else {
2332     monitor->enter_for_with_contention_mark(locking_thread, contention_mark);
2333   }
2334 
2335   return monitor;
2336 }
2337 
2338 void ObjectSynchronizer::deflate_monitor(oop obj, ObjectMonitor* monitor) {
2339   if (obj != nullptr) {
2340     deflate_mark_word(obj);
2341     remove_monitor(monitor, obj);
2342   }
2343 }
2344 
2345 ObjectMonitor* ObjectSynchronizer::get_monitor_from_table(oop obj) {
2346   assert(UseObjectMonitorTable, "must be");
2347   return ObjectMonitorTable::monitor_get(obj);
2348 }
2349 
2350 ObjectMonitor* ObjectSynchronizer::read_monitor(markWord mark) {
2351   return mark.monitor();
2352 }
2353 
2354 ObjectMonitor* ObjectSynchronizer::read_monitor(oop obj) {
2355   return ObjectSynchronizer::read_monitor(obj, obj->mark());
2356 }
2357 
2358 ObjectMonitor* ObjectSynchronizer::read_monitor(oop obj, markWord mark) {
2359   if (!UseObjectMonitorTable) {
2360     return read_monitor(mark);
2361   } else {
2362     return ObjectSynchronizer::get_monitor_from_table(obj);
2363   }
2364 }
2365 
2366 bool ObjectSynchronizer::quick_enter_internal(oop obj, BasicLock* lock, JavaThread* current) {
2367   assert(current->thread_state() == _thread_in_Java, "must be");
2368   assert(obj != nullptr, "must be");
2369   NoSafepointVerifier nsv;
2370 
2371   LockStack& lock_stack = current->lock_stack();
2372   if (lock_stack.is_full()) {
2373     // Always go into runtime if the lock stack is full.
2374     return false;
2375   }
2376 
2377   const markWord mark = obj->mark();
2378 
2379 #ifndef _LP64
2380   // Only for 32bit which has limited support for fast locking outside the runtime.
2381   if (lock_stack.try_recursive_enter(obj)) {
2382     // Recursive lock successful.
2383     return true;
2384   }
2385 
2386   if (mark.is_unlocked()) {
2387     markWord locked_mark = mark.set_fast_locked();
2388     if (obj->cas_set_mark(locked_mark, mark) == mark) {
2389       // Successfully fast-locked, push object to lock-stack and return.
2390       lock_stack.push(obj);
2391       return true;
2392     }
2393   }
2394 #endif
2395 
2396   if (mark.has_monitor()) {
2397     ObjectMonitor* monitor;
2398     if (UseObjectMonitorTable) {
2399       monitor = read_caches(current, lock, obj);
2400     } else {
2401       monitor = ObjectSynchronizer::read_monitor(mark);
2402     }
2403 
2404     if (monitor == nullptr) {
2405       // Take the slow-path on a cache miss.
2406       return false;
2407     }
2408 
2409     if (UseObjectMonitorTable) {
2410       // Set the monitor regardless of success.
2411       // Either we successfully lock on the monitor, or we retry with the
2412       // monitor in the slow path. If the monitor gets deflated, it will be
2413       // cleared, either by the CacheSetter if we fast lock in enter or in
2414       // inflate_and_enter when we see that the monitor is deflated.
2415       lock->set_object_monitor_cache(monitor);
2416     }
2417 
2418     if (monitor->spin_enter(current)) {
2419       return true;
2420     }
2421   }
2422 
2423   // Slow-path.
2424   return false;
2425 }
2426 
2427 bool ObjectSynchronizer::quick_enter(oop obj, BasicLock* lock, JavaThread* current) {
2428   assert(current->thread_state() == _thread_in_Java, "invariant");
2429   NoSafepointVerifier nsv;
2430   if (obj == nullptr) return false;       // Need to throw NPE
2431 
2432   if (obj->klass()->is_value_based()) {
2433     return false;
2434   }
2435 
2436   return ObjectSynchronizer::quick_enter_internal(obj, lock, current);
2437 }