1 /*
   2  * Copyright (c) 1998, 2014, 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 "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "jfr/jfrEvents.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/markOop.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/biasedLocking.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/interfaceSupport.hpp"
  34 #include "runtime/mutexLocker.hpp"
  35 #include "runtime/objectMonitor.hpp"
  36 #include "runtime/objectMonitor.inline.hpp"
  37 #include "runtime/osThread.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "runtime/synchronizer.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "utilities/dtrace.hpp"
  42 #include "utilities/events.hpp"
  43 #include "utilities/preserveException.hpp"
  44 #ifdef TARGET_OS_FAMILY_linux
  45 # include "os_linux.inline.hpp"
  46 #endif
  47 #ifdef TARGET_OS_FAMILY_solaris
  48 # include "os_solaris.inline.hpp"
  49 #endif
  50 #ifdef TARGET_OS_FAMILY_windows
  51 # include "os_windows.inline.hpp"
  52 #endif
  53 #ifdef TARGET_OS_FAMILY_bsd
  54 # include "os_bsd.inline.hpp"
  55 #endif
  56 
  57 #if defined(__GNUC__) && !defined(PPC64)
  58   // Need to inhibit inlining for older versions of GCC to avoid build-time failures
  59   #define ATTR __attribute__((noinline))
  60 #else
  61   #define ATTR
  62 #endif
  63 
  64 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  65 
  66 // The "core" versions of monitor enter and exit reside in this file.
  67 // The interpreter and compilers contain specialized transliterated
  68 // variants of the enter-exit fast-path operations.  See i486.ad fast_lock(),
  69 // for instance.  If you make changes here, make sure to modify the
  70 // interpreter, and both C1 and C2 fast-path inline locking code emission.
  71 //
  72 //
  73 // -----------------------------------------------------------------------------
  74 
  75 #ifdef DTRACE_ENABLED
  76 
  77 // Only bother with this argument setup if dtrace is available
  78 // TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
  79 
  80 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
  81   char* bytes = NULL;                                                      \
  82   int len = 0;                                                             \
  83   jlong jtid = SharedRuntime::get_java_tid(thread);                        \
  84   Symbol* klassname = ((oop)(obj))->klass()->name();                       \
  85   if (klassname != NULL) {                                                 \
  86     bytes = (char*)klassname->bytes();                                     \
  87     len = klassname->utf8_length();                                        \
  88   }
  89 
  90 #ifndef USDT2
  91 HS_DTRACE_PROBE_DECL5(hotspot, monitor__wait,
  92   jlong, uintptr_t, char*, int, long);
  93 HS_DTRACE_PROBE_DECL4(hotspot, monitor__waited,
  94   jlong, uintptr_t, char*, int);
  95 
  96 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis)            \
  97   {                                                                        \
  98     if (DTraceMonitorProbes) {                                            \
  99       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 100       HS_DTRACE_PROBE5(hotspot, monitor__wait, jtid,                       \
 101                        (monitor), bytes, len, (millis));                   \
 102     }                                                                      \
 103   }
 104 
 105 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread)                  \
 106   {                                                                        \
 107     if (DTraceMonitorProbes) {                                            \
 108       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 109       HS_DTRACE_PROBE4(hotspot, monitor__##probe, jtid,                    \
 110                        (uintptr_t)(monitor), bytes, len);                  \
 111     }                                                                      \
 112   }
 113 
 114 #else /* USDT2 */
 115 
 116 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis)            \
 117   {                                                                        \
 118     if (DTraceMonitorProbes) {                                            \
 119       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 120       HOTSPOT_MONITOR_WAIT(jtid,                                           \
 121                            (uintptr_t)(monitor), bytes, len, (millis));  \
 122     }                                                                      \
 123   }
 124 
 125 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED
 126 
 127 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread)                  \
 128   {                                                                        \
 129     if (DTraceMonitorProbes) {                                            \
 130       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 131       HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */             \
 132                        (uintptr_t)(monitor), bytes, len);                  \
 133     }                                                                      \
 134   }
 135 
 136 #endif /* USDT2 */
 137 #else //  ndef DTRACE_ENABLED
 138 
 139 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
 140 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
 141 
 142 #endif // ndef DTRACE_ENABLED
 143 
 144 // This exists only as a workaround of dtrace bug 6254741
 145 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
 146   DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
 147   return 0;
 148 }
 149 
 150 #define NINFLATIONLOCKS 256
 151 static volatile intptr_t InflationLocks [NINFLATIONLOCKS] ;
 152 
 153 ObjectMonitor * volatile ObjectSynchronizer::gBlockList = NULL;
 154 ObjectMonitor * volatile ObjectSynchronizer::gFreeList  = NULL ;
 155 ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList  = NULL ;
 156 int ObjectSynchronizer::gOmInUseCount = 0;
 157 static volatile intptr_t ListLock = 0 ;      // protects global monitor free-list cache
 158 static volatile int MonitorFreeCount  = 0 ;      // # on gFreeList
 159 static volatile int MonitorPopulation = 0 ;      // # Extant -- in circulation
 160 #define CHAINMARKER (cast_to_oop<intptr_t>(-1))
 161 
 162 // -----------------------------------------------------------------------------
 163 //  Fast Monitor Enter/Exit
 164 // This the fast monitor enter. The interpreter and compiler use
 165 // some assembly copies of this code. Make sure update those code
 166 // if the following function is changed. The implementation is
 167 // extremely sensitive to race condition. Be careful.
 168 
 169 void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS) {
 170  if (UseBiasedLocking) {
 171     if (!SafepointSynchronize::is_at_safepoint()) {
 172       BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
 173       if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
 174         return;
 175       }
 176     } else {
 177       assert(!attempt_rebias, "can not rebias toward VM thread");
 178       BiasedLocking::revoke_at_safepoint(obj);
 179     }
 180     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 181  }
 182 
 183  slow_enter (obj, lock, THREAD) ;
 184 }
 185 
 186 void ObjectSynchronizer::fast_exit(oop object, BasicLock* lock, TRAPS) {
 187   assert(!object->mark()->has_bias_pattern(), "should not see bias pattern here");
 188   // if displaced header is null, the previous enter is recursive enter, no-op
 189   markOop dhw = lock->displaced_header();
 190   markOop mark ;
 191   if (dhw == NULL) {
 192      // Recursive stack-lock.
 193      // Diagnostics -- Could be: stack-locked, inflating, inflated.
 194      mark = object->mark() ;
 195      assert (!mark->is_neutral(), "invariant") ;
 196      if (mark->has_locker() && mark != markOopDesc::INFLATING()) {
 197         assert(THREAD->is_lock_owned((address)mark->locker()), "invariant") ;
 198      }
 199      if (mark->has_monitor()) {
 200         ObjectMonitor * m = mark->monitor() ;
 201         assert(((oop)(m->object()))->mark() == mark, "invariant") ;
 202         assert(m->is_entered(THREAD), "invariant") ;
 203      }
 204      return ;
 205   }
 206 
 207   mark = object->mark() ;
 208 
 209   // If the object is stack-locked by the current thread, try to
 210   // swing the displaced header from the box back to the mark.
 211   if (mark == (markOop) lock) {
 212      assert (dhw->is_neutral(), "invariant") ;
 213      if ((markOop) Atomic::cmpxchg_ptr (dhw, object->mark_addr(), mark) == mark) {
 214         TEVENT (fast_exit: release stacklock) ;
 215         return;
 216      }
 217   }
 218 
 219   ObjectSynchronizer::inflate(THREAD, object)->exit (true, THREAD) ;
 220 }
 221 
 222 // -----------------------------------------------------------------------------
 223 // Interpreter/Compiler Slow Case
 224 // This routine is used to handle interpreter/compiler slow case
 225 // We don't need to use fast path here, because it must have been
 226 // failed in the interpreter/compiler code.
 227 void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
 228   markOop mark = obj->mark();
 229   assert(!mark->has_bias_pattern(), "should not see bias pattern here");
 230 
 231   if (mark->is_neutral()) {
 232     // Anticipate successful CAS -- the ST of the displaced mark must
 233     // be visible <= the ST performed by the CAS.
 234     lock->set_displaced_header(mark);
 235     if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) {
 236       TEVENT (slow_enter: release stacklock) ;
 237       return ;
 238     }
 239     // Fall through to inflate() ...
 240   } else
 241   if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
 242     assert(lock != mark->locker(), "must not re-lock the same lock");
 243     assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
 244     lock->set_displaced_header(NULL);
 245     return;
 246   }
 247 
 248 #if 0
 249   // The following optimization isn't particularly useful.
 250   if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) {
 251     lock->set_displaced_header (NULL) ;
 252     return ;
 253   }
 254 #endif
 255 
 256   // The object header will never be displaced to this lock,
 257   // so it does not matter what the value is, except that it
 258   // must be non-zero to avoid looking like a re-entrant lock,
 259   // and must not look locked either.
 260   lock->set_displaced_header(markOopDesc::unused_mark());
 261   ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
 262 }
 263 
 264 // This routine is used to handle interpreter/compiler slow case
 265 // We don't need to use fast path here, because it must have
 266 // failed in the interpreter/compiler code. Simply use the heavy
 267 // weight monitor should be ok, unless someone find otherwise.
 268 void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) {
 269   fast_exit (object, lock, THREAD) ;
 270 }
 271 
 272 // -----------------------------------------------------------------------------
 273 // Class Loader  support to workaround deadlocks on the class loader lock objects
 274 // Also used by GC
 275 // complete_exit()/reenter() are used to wait on a nested lock
 276 // i.e. to give up an outer lock completely and then re-enter
 277 // Used when holding nested locks - lock acquisition order: lock1 then lock2
 278 //  1) complete_exit lock1 - saving recursion count
 279 //  2) wait on lock2
 280 //  3) when notified on lock2, unlock lock2
 281 //  4) reenter lock1 with original recursion count
 282 //  5) lock lock2
 283 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 284 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
 285   TEVENT (complete_exit) ;
 286   if (UseBiasedLocking) {
 287     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 288     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 289   }
 290 
 291   ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
 292 
 293   return monitor->complete_exit(THREAD);
 294 }
 295 
 296 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 297 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
 298   TEVENT (reenter) ;
 299   if (UseBiasedLocking) {
 300     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 301     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 302   }
 303 
 304   ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
 305 
 306   monitor->reenter(recursion, THREAD);
 307 }
 308 // -----------------------------------------------------------------------------
 309 // JNI locks on java objects
 310 // NOTE: must use heavy weight monitor to handle jni monitor enter
 311 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) { // possible entry from jni enter
 312   // the current locking is from JNI instead of Java code
 313   TEVENT (jni_enter) ;
 314   if (UseBiasedLocking) {
 315     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 316     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 317   }
 318   THREAD->set_current_pending_monitor_is_from_java(false);
 319   ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
 320   THREAD->set_current_pending_monitor_is_from_java(true);
 321 }
 322 
 323 // NOTE: must use heavy weight monitor to handle jni monitor enter
 324 bool ObjectSynchronizer::jni_try_enter(Handle obj, Thread* THREAD) {
 325   if (UseBiasedLocking) {
 326     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 327     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 328   }
 329 
 330   ObjectMonitor* monitor = ObjectSynchronizer::inflate_helper(obj());
 331   return monitor->try_enter(THREAD);
 332 }
 333 
 334 
 335 // NOTE: must use heavy weight monitor to handle jni monitor exit
 336 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
 337   TEVENT (jni_exit) ;
 338   if (UseBiasedLocking) {
 339     Handle h_obj(THREAD, obj);
 340     BiasedLocking::revoke_and_rebias(h_obj, false, THREAD);
 341     obj = h_obj();
 342   }
 343   assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 344 
 345   ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj);
 346   // If this thread has locked the object, exit the monitor.  Note:  can't use
 347   // monitor->check(CHECK); must exit even if an exception is pending.
 348   if (monitor->check(THREAD)) {
 349      monitor->exit(true, THREAD);
 350   }
 351 }
 352 
 353 // -----------------------------------------------------------------------------
 354 // Internal VM locks on java objects
 355 // standard constructor, allows locking failures
 356 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) {
 357   _dolock = doLock;
 358   _thread = thread;
 359   debug_only(if (StrictSafepointChecks) _thread->check_for_valid_safepoint_state(false);)
 360   _obj = obj;
 361 
 362   if (_dolock) {
 363     TEVENT (ObjectLocker) ;
 364 
 365     ObjectSynchronizer::fast_enter(_obj, &_lock, false, _thread);
 366   }
 367 }
 368 
 369 ObjectLocker::~ObjectLocker() {
 370   if (_dolock) {
 371     ObjectSynchronizer::fast_exit(_obj(), &_lock, _thread);
 372   }
 373 }
 374 
 375 
 376 // -----------------------------------------------------------------------------
 377 //  Wait/Notify/NotifyAll
 378 // NOTE: must use heavy weight monitor to handle wait()
 379 void ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
 380   if (UseBiasedLocking) {
 381     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 382     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 383   }
 384   if (millis < 0) {
 385     TEVENT (wait - throw IAX) ;
 386     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 387   }
 388   ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj());
 389   DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
 390   monitor->wait(millis, true, THREAD);
 391 
 392   /* This dummy call is in place to get around dtrace bug 6254741.  Once
 393      that's fixed we can uncomment the following line and remove the call */
 394   // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
 395   dtrace_waited_probe(monitor, obj, THREAD);
 396 }
 397 
 398 void ObjectSynchronizer::waitUninterruptibly (Handle obj, jlong millis, TRAPS) {
 399   if (UseBiasedLocking) {
 400     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 401     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 402   }
 403   if (millis < 0) {
 404     TEVENT (wait - throw IAX) ;
 405     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 406   }
 407   ObjectSynchronizer::inflate(THREAD, obj()) -> wait(millis, false, THREAD) ;
 408 }
 409 
 410 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
 411  if (UseBiasedLocking) {
 412     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 413     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 414   }
 415 
 416   markOop mark = obj->mark();
 417   if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
 418     return;
 419   }
 420   ObjectSynchronizer::inflate(THREAD, obj())->notify(THREAD);
 421 }
 422 
 423 // NOTE: see comment of notify()
 424 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
 425   if (UseBiasedLocking) {
 426     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 427     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 428   }
 429 
 430   markOop mark = obj->mark();
 431   if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
 432     return;
 433   }
 434   ObjectSynchronizer::inflate(THREAD, obj())->notifyAll(THREAD);
 435 }
 436 
 437 // -----------------------------------------------------------------------------
 438 // Hash Code handling
 439 //
 440 // Performance concern:
 441 // OrderAccess::storestore() calls release() which at one time stored 0
 442 // into the global volatile OrderAccess::dummy variable. This store was
 443 // unnecessary for correctness. Many threads storing into a common location
 444 // causes considerable cache migration or "sloshing" on large SMP systems.
 445 // As such, I avoided using OrderAccess::storestore(). In some cases
 446 // OrderAccess::fence() -- which incurs local latency on the executing
 447 // processor -- is a better choice as it scales on SMP systems.
 448 //
 449 // See http://blogs.oracle.com/dave/entry/biased_locking_in_hotspot for
 450 // a discussion of coherency costs. Note that all our current reference
 451 // platforms provide strong ST-ST order, so the issue is moot on IA32,
 452 // x64, and SPARC.
 453 //
 454 // As a general policy we use "volatile" to control compiler-based reordering
 455 // and explicit fences (barriers) to control for architectural reordering
 456 // performed by the CPU(s) or platform.
 457 
 458 struct SharedGlobals {
 459     // These are highly shared mostly-read variables.
 460     // To avoid false-sharing they need to be the sole occupants of a $ line.
 461     double padPrefix [8];
 462     volatile int stwRandom ;
 463     volatile int stwCycle ;
 464 
 465     // Hot RW variables -- Sequester to avoid false-sharing
 466     double padSuffix [16];
 467     volatile int hcSequence ;
 468     double padFinal [8] ;
 469 } ;
 470 
 471 static SharedGlobals GVars ;
 472 static int MonitorScavengeThreshold = 1000000 ;
 473 static volatile int ForceMonitorScavenge = 0 ; // Scavenge required and pending
 474 
 475 static markOop ReadStableMark (oop obj) {
 476   markOop mark = obj->mark() ;
 477   if (!mark->is_being_inflated()) {
 478     return mark ;       // normal fast-path return
 479   }
 480 
 481   int its = 0 ;
 482   for (;;) {
 483     markOop mark = obj->mark() ;
 484     if (!mark->is_being_inflated()) {
 485       return mark ;    // normal fast-path return
 486     }
 487 
 488     // The object is being inflated by some other thread.
 489     // The caller of ReadStableMark() must wait for inflation to complete.
 490     // Avoid live-lock
 491     // TODO: consider calling SafepointSynchronize::do_call_back() while
 492     // spinning to see if there's a safepoint pending.  If so, immediately
 493     // yielding or blocking would be appropriate.  Avoid spinning while
 494     // there is a safepoint pending.
 495     // TODO: add inflation contention performance counters.
 496     // TODO: restrict the aggregate number of spinners.
 497 
 498     ++its ;
 499     if (its > 10000 || !os::is_MP()) {
 500        if (its & 1) {
 501          os::NakedYield() ;
 502          TEVENT (Inflate: INFLATING - yield) ;
 503        } else {
 504          // Note that the following code attenuates the livelock problem but is not
 505          // a complete remedy.  A more complete solution would require that the inflating
 506          // thread hold the associated inflation lock.  The following code simply restricts
 507          // the number of spinners to at most one.  We'll have N-2 threads blocked
 508          // on the inflationlock, 1 thread holding the inflation lock and using
 509          // a yield/park strategy, and 1 thread in the midst of inflation.
 510          // A more refined approach would be to change the encoding of INFLATING
 511          // to allow encapsulation of a native thread pointer.  Threads waiting for
 512          // inflation to complete would use CAS to push themselves onto a singly linked
 513          // list rooted at the markword.  Once enqueued, they'd loop, checking a per-thread flag
 514          // and calling park().  When inflation was complete the thread that accomplished inflation
 515          // would detach the list and set the markword to inflated with a single CAS and
 516          // then for each thread on the list, set the flag and unpark() the thread.
 517          // This is conceptually similar to muxAcquire-muxRelease, except that muxRelease
 518          // wakes at most one thread whereas we need to wake the entire list.
 519          int ix = (cast_from_oop<intptr_t>(obj) >> 5) & (NINFLATIONLOCKS-1) ;
 520          int YieldThenBlock = 0 ;
 521          assert (ix >= 0 && ix < NINFLATIONLOCKS, "invariant") ;
 522          assert ((NINFLATIONLOCKS & (NINFLATIONLOCKS-1)) == 0, "invariant") ;
 523          Thread::muxAcquire (InflationLocks + ix, "InflationLock") ;
 524          while (obj->mark() == markOopDesc::INFLATING()) {
 525            // Beware: NakedYield() is advisory and has almost no effect on some platforms
 526            // so we periodically call Self->_ParkEvent->park(1).
 527            // We use a mixed spin/yield/block mechanism.
 528            if ((YieldThenBlock++) >= 16) {
 529               Thread::current()->_ParkEvent->park(1) ;
 530            } else {
 531               os::NakedYield() ;
 532            }
 533          }
 534          Thread::muxRelease (InflationLocks + ix ) ;
 535          TEVENT (Inflate: INFLATING - yield/park) ;
 536        }
 537     } else {
 538        SpinPause() ;       // SMP-polite spinning
 539     }
 540   }
 541 }
 542 
 543 // hashCode() generation :
 544 //
 545 // Possibilities:
 546 // * MD5Digest of {obj,stwRandom}
 547 // * CRC32 of {obj,stwRandom} or any linear-feedback shift register function.
 548 // * A DES- or AES-style SBox[] mechanism
 549 // * One of the Phi-based schemes, such as:
 550 //   2654435761 = 2^32 * Phi (golden ratio)
 551 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ;
 552 // * A variation of Marsaglia's shift-xor RNG scheme.
 553 // * (obj ^ stwRandom) is appealing, but can result
 554 //   in undesirable regularity in the hashCode values of adjacent objects
 555 //   (objects allocated back-to-back, in particular).  This could potentially
 556 //   result in hashtable collisions and reduced hashtable efficiency.
 557 //   There are simple ways to "diffuse" the middle address bits over the
 558 //   generated hashCode values:
 559 //
 560 
 561 static inline intptr_t get_next_hash(Thread * Self, oop obj) {
 562   intptr_t value = 0 ;
 563   if (hashCode == 0) {
 564      // This form uses an unguarded global Park-Miller RNG,
 565      // so it's possible for two threads to race and generate the same RNG.
 566      // On MP system we'll have lots of RW access to a global, so the
 567      // mechanism induces lots of coherency traffic.
 568      value = os::random() ;
 569   } else
 570   if (hashCode == 1) {
 571      // This variation has the property of being stable (idempotent)
 572      // between STW operations.  This can be useful in some of the 1-0
 573      // synchronization schemes.
 574      intptr_t addrBits = cast_from_oop<intptr_t>(obj) >> 3 ;
 575      value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;
 576   } else
 577   if (hashCode == 2) {
 578      value = 1 ;            // for sensitivity testing
 579   } else
 580   if (hashCode == 3) {
 581      value = ++GVars.hcSequence ;
 582   } else
 583   if (hashCode == 4) {
 584      value = cast_from_oop<intptr_t>(obj) ;
 585   } else {
 586      // Marsaglia's xor-shift scheme with thread-specific state
 587      // This is probably the best overall implementation -- we'll
 588      // likely make this the default in future releases.
 589      unsigned t = Self->_hashStateX ;
 590      t ^= (t << 11) ;
 591      Self->_hashStateX = Self->_hashStateY ;
 592      Self->_hashStateY = Self->_hashStateZ ;
 593      Self->_hashStateZ = Self->_hashStateW ;
 594      unsigned v = Self->_hashStateW ;
 595      v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;
 596      Self->_hashStateW = v ;
 597      value = v ;
 598   }
 599 
 600   value &= markOopDesc::hash_mask;
 601   if (value == 0) value = 0xBAD ;
 602   assert (value != markOopDesc::no_hash, "invariant") ;
 603   TEVENT (hashCode: GENERATE) ;
 604   return value;
 605 }
 606 //
 607 intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
 608   if (UseBiasedLocking) {
 609     // NOTE: many places throughout the JVM do not expect a safepoint
 610     // to be taken here, in particular most operations on perm gen
 611     // objects. However, we only ever bias Java instances and all of
 612     // the call sites of identity_hash that might revoke biases have
 613     // been checked to make sure they can handle a safepoint. The
 614     // added check of the bias pattern is to avoid useless calls to
 615     // thread-local storage.
 616     if (obj->mark()->has_bias_pattern()) {
 617       // Box and unbox the raw reference just in case we cause a STW safepoint.
 618       Handle hobj (Self, obj) ;
 619       // Relaxing assertion for bug 6320749.
 620       assert (Universe::verify_in_progress() ||
 621               !SafepointSynchronize::is_at_safepoint(),
 622              "biases should not be seen by VM thread here");
 623       BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
 624       obj = hobj() ;
 625       assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 626     }
 627   }
 628 
 629   // hashCode() is a heap mutator ...
 630   // Relaxing assertion for bug 6320749.
 631   assert (Universe::verify_in_progress() ||
 632           !SafepointSynchronize::is_at_safepoint(), "invariant") ;
 633   assert (Universe::verify_in_progress() ||
 634           Self->is_Java_thread() , "invariant") ;
 635   assert (Universe::verify_in_progress() ||
 636          ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;
 637 
 638   ObjectMonitor* monitor = NULL;
 639   markOop temp, test;
 640   intptr_t hash;
 641   markOop mark = ReadStableMark (obj);
 642 
 643   // object should remain ineligible for biased locking
 644   assert (!mark->has_bias_pattern(), "invariant") ;
 645 
 646   if (mark->is_neutral()) {
 647     hash = mark->hash();              // this is a normal header
 648     if (hash) {                       // if it has hash, just return it
 649       return hash;
 650     }
 651     hash = get_next_hash(Self, obj);  // allocate a new hash code
 652     temp = mark->copy_set_hash(hash); // merge the hash code into header
 653     // use (machine word version) atomic operation to install the hash
 654     test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);
 655     if (test == mark) {
 656       return hash;
 657     }
 658     // If atomic operation failed, we must inflate the header
 659     // into heavy weight monitor. We could add more code here
 660     // for fast path, but it does not worth the complexity.
 661   } else if (mark->has_monitor()) {
 662     monitor = mark->monitor();
 663     temp = monitor->header();
 664     assert (temp->is_neutral(), "invariant") ;
 665     hash = temp->hash();
 666     if (hash) {
 667       return hash;
 668     }
 669     // Skip to the following code to reduce code size
 670   } else if (Self->is_lock_owned((address)mark->locker())) {
 671     temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
 672     assert (temp->is_neutral(), "invariant") ;
 673     hash = temp->hash();              // by current thread, check if the displaced
 674     if (hash) {                       // header contains hash code
 675       return hash;
 676     }
 677     // WARNING:
 678     //   The displaced header is strictly immutable.
 679     // It can NOT be changed in ANY cases. So we have
 680     // to inflate the header into heavyweight monitor
 681     // even the current thread owns the lock. The reason
 682     // is the BasicLock (stack slot) will be asynchronously
 683     // read by other threads during the inflate() function.
 684     // Any change to stack may not propagate to other threads
 685     // correctly.
 686   }
 687 
 688   // Inflate the monitor to set hash code
 689   monitor = ObjectSynchronizer::inflate(Self, obj);
 690   // Load displaced header and check it has hash code
 691   mark = monitor->header();
 692   assert (mark->is_neutral(), "invariant") ;
 693   hash = mark->hash();
 694   if (hash == 0) {
 695     hash = get_next_hash(Self, obj);
 696     temp = mark->copy_set_hash(hash); // merge hash code into header
 697     assert (temp->is_neutral(), "invariant") ;
 698     test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);
 699     if (test != mark) {
 700       // The only update to the header in the monitor (outside GC)
 701       // is install the hash code. If someone add new usage of
 702       // displaced header, please update this code
 703       hash = test->hash();
 704       assert (test->is_neutral(), "invariant") ;
 705       assert (hash != 0, "Trivial unexpected object/monitor header usage.");
 706     }
 707   }
 708   // We finally get the hash
 709   return hash;
 710 }
 711 
 712 // Deprecated -- use FastHashCode() instead.
 713 
 714 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
 715   return FastHashCode (Thread::current(), obj()) ;
 716 }
 717 
 718 
 719 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
 720                                                    Handle h_obj) {
 721   if (UseBiasedLocking) {
 722     BiasedLocking::revoke_and_rebias(h_obj, false, thread);
 723     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 724   }
 725 
 726   assert(thread == JavaThread::current(), "Can only be called on current thread");
 727   oop obj = h_obj();
 728 
 729   markOop mark = ReadStableMark (obj) ;
 730 
 731   // Uncontended case, header points to stack
 732   if (mark->has_locker()) {
 733     return thread->is_lock_owned((address)mark->locker());
 734   }
 735   // Contended case, header points to ObjectMonitor (tagged pointer)
 736   if (mark->has_monitor()) {
 737     ObjectMonitor* monitor = mark->monitor();
 738     return monitor->is_entered(thread) != 0 ;
 739   }
 740   // Unlocked case, header in place
 741   assert(mark->is_neutral(), "sanity check");
 742   return false;
 743 }
 744 
 745 // Be aware of this method could revoke bias of the lock object.
 746 // This method querys the ownership of the lock handle specified by 'h_obj'.
 747 // If the current thread owns the lock, it returns owner_self. If no
 748 // thread owns the lock, it returns owner_none. Otherwise, it will return
 749 // ower_other.
 750 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
 751 (JavaThread *self, Handle h_obj) {
 752   // The caller must beware this method can revoke bias, and
 753   // revocation can result in a safepoint.
 754   assert (!SafepointSynchronize::is_at_safepoint(), "invariant") ;
 755   assert (self->thread_state() != _thread_blocked , "invariant") ;
 756 
 757   // Possible mark states: neutral, biased, stack-locked, inflated
 758 
 759   if (UseBiasedLocking && h_obj()->mark()->has_bias_pattern()) {
 760     // CASE: biased
 761     BiasedLocking::revoke_and_rebias(h_obj, false, self);
 762     assert(!h_obj->mark()->has_bias_pattern(),
 763            "biases should be revoked by now");
 764   }
 765 
 766   assert(self == JavaThread::current(), "Can only be called on current thread");
 767   oop obj = h_obj();
 768   markOop mark = ReadStableMark (obj) ;
 769 
 770   // CASE: stack-locked.  Mark points to a BasicLock on the owner's stack.
 771   if (mark->has_locker()) {
 772     return self->is_lock_owned((address)mark->locker()) ?
 773       owner_self : owner_other;
 774   }
 775 
 776   // CASE: inflated. Mark (tagged pointer) points to an objectMonitor.
 777   // The Object:ObjectMonitor relationship is stable as long as we're
 778   // not at a safepoint.
 779   if (mark->has_monitor()) {
 780     void * owner = mark->monitor()->_owner ;
 781     if (owner == NULL) return owner_none ;
 782     return (owner == self ||
 783             self->is_lock_owned((address)owner)) ? owner_self : owner_other;
 784   }
 785 
 786   // CASE: neutral
 787   assert(mark->is_neutral(), "sanity check");
 788   return owner_none ;           // it's unlocked
 789 }
 790 
 791 // FIXME: jvmti should call this
 792 JavaThread* ObjectSynchronizer::get_lock_owner(Handle h_obj, bool doLock) {
 793   if (UseBiasedLocking) {
 794     if (SafepointSynchronize::is_at_safepoint()) {
 795       BiasedLocking::revoke_at_safepoint(h_obj);
 796     } else {
 797       BiasedLocking::revoke_and_rebias(h_obj, false, JavaThread::current());
 798     }
 799     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 800   }
 801 
 802   oop obj = h_obj();
 803   address owner = NULL;
 804 
 805   markOop mark = ReadStableMark (obj) ;
 806 
 807   // Uncontended case, header points to stack
 808   if (mark->has_locker()) {
 809     owner = (address) mark->locker();
 810   }
 811 
 812   // Contended case, header points to ObjectMonitor (tagged pointer)
 813   if (mark->has_monitor()) {
 814     ObjectMonitor* monitor = mark->monitor();
 815     assert(monitor != NULL, "monitor should be non-null");
 816     owner = (address) monitor->owner();
 817   }
 818 
 819   if (owner != NULL) {
 820     // owning_thread_from_monitor_owner() may also return NULL here
 821     return Threads::owning_thread_from_monitor_owner(owner, doLock);
 822   }
 823 
 824   // Unlocked case, header in place
 825   // Cannot have assertion since this object may have been
 826   // locked by another thread when reaching here.
 827   // assert(mark->is_neutral(), "sanity check");
 828 
 829   return NULL;
 830 }
 831 // Visitors ...
 832 
 833 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
 834   ObjectMonitor* block =
 835     (ObjectMonitor*)OrderAccess::load_ptr_acquire(&gBlockList);
 836   while (block != NULL) {
 837     assert(block->object() == CHAINMARKER, "must be a block header");
 838     for (int i = _BLOCKSIZE - 1; i > 0; i--) {
 839       ObjectMonitor* mid = (ObjectMonitor *)(block + i);
 840       oop object = (oop)mid->object();
 841       if (object != NULL) {
 842         closure->do_monitor(mid);
 843       }
 844     }
 845     block = (ObjectMonitor*)block->FreeNext;
 846   }
 847 }
 848 
 849 // Get the next block in the block list.
 850 static inline ObjectMonitor* next(ObjectMonitor* block) {
 851   assert(block->object() == CHAINMARKER, "must be a block header");
 852   block = block->FreeNext ;
 853   assert(block == NULL || block->object() == CHAINMARKER, "must be a block header");
 854   return block;
 855 }
 856 
 857 
 858 void ObjectSynchronizer::oops_do(OopClosure* f) {
 859   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 860   ObjectMonitor* block =
 861     (ObjectMonitor*)OrderAccess::load_ptr_acquire(&gBlockList);
 862   for (; block != NULL; block = (ObjectMonitor *)next(block)) {
 863     assert(block->object() == CHAINMARKER, "must be a block header");
 864     for (int i = 1; i < _BLOCKSIZE; i++) {
 865       ObjectMonitor* mid = &block[i];
 866       if (mid->object() != NULL) {
 867         f->do_oop((oop*)mid->object_addr());
 868       }
 869     }
 870   }
 871 }
 872 
 873 
 874 // -----------------------------------------------------------------------------
 875 // ObjectMonitor Lifecycle
 876 // -----------------------
 877 // Inflation unlinks monitors from the global gFreeList and
 878 // associates them with objects.  Deflation -- which occurs at
 879 // STW-time -- disassociates idle monitors from objects.  Such
 880 // scavenged monitors are returned to the gFreeList.
 881 //
 882 // The global list is protected by ListLock.  All the critical sections
 883 // are short and operate in constant-time.
 884 //
 885 // ObjectMonitors reside in type-stable memory (TSM) and are immortal.
 886 //
 887 // Lifecycle:
 888 // --   unassigned and on the global free list
 889 // --   unassigned and on a thread's private omFreeList
 890 // --   assigned to an object.  The object is inflated and the mark refers
 891 //      to the objectmonitor.
 892 //
 893 
 894 
 895 // Constraining monitor pool growth via MonitorBound ...
 896 //
 897 // The monitor pool is grow-only.  We scavenge at STW safepoint-time, but the
 898 // the rate of scavenging is driven primarily by GC.  As such,  we can find
 899 // an inordinate number of monitors in circulation.
 900 // To avoid that scenario we can artificially induce a STW safepoint
 901 // if the pool appears to be growing past some reasonable bound.
 902 // Generally we favor time in space-time tradeoffs, but as there's no
 903 // natural back-pressure on the # of extant monitors we need to impose some
 904 // type of limit.  Beware that if MonitorBound is set to too low a value
 905 // we could just loop. In addition, if MonitorBound is set to a low value
 906 // we'll incur more safepoints, which are harmful to performance.
 907 // See also: GuaranteedSafepointInterval
 908 //
 909 // The current implementation uses asynchronous VM operations.
 910 //
 911 
 912 static void InduceScavenge (Thread * Self, const char * Whence) {
 913   // Induce STW safepoint to trim monitors
 914   // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
 915   // More precisely, trigger an asynchronous STW safepoint as the number
 916   // of active monitors passes the specified threshold.
 917   // TODO: assert thread state is reasonable
 918 
 919   if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
 920     if (ObjectMonitor::Knob_Verbose) {
 921       ::printf ("Monitor scavenge - Induced STW @%s (%d)\n", Whence, ForceMonitorScavenge) ;
 922       ::fflush(stdout) ;
 923     }
 924     // Induce a 'null' safepoint to scavenge monitors
 925     // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
 926     // to the VMthread and have a lifespan longer than that of this activation record.
 927     // The VMThread will delete the op when completed.
 928     VMThread::execute (new VM_ForceAsyncSafepoint()) ;
 929 
 930     if (ObjectMonitor::Knob_Verbose) {
 931       ::printf ("Monitor scavenge - STW posted @%s (%d)\n", Whence, ForceMonitorScavenge) ;
 932       ::fflush(stdout) ;
 933     }
 934   }
 935 }
 936 /* Too slow for general assert or debug
 937 void ObjectSynchronizer::verifyInUse (Thread *Self) {
 938    ObjectMonitor* mid;
 939    int inusetally = 0;
 940    for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
 941      inusetally ++;
 942    }
 943    assert(inusetally == Self->omInUseCount, "inuse count off");
 944 
 945    int freetally = 0;
 946    for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
 947      freetally ++;
 948    }
 949    assert(freetally == Self->omFreeCount, "free count off");
 950 }
 951 */
 952 ObjectMonitor * ATTR ObjectSynchronizer::omAlloc (Thread * Self) {
 953     // A large MAXPRIVATE value reduces both list lock contention
 954     // and list coherency traffic, but also tends to increase the
 955     // number of objectMonitors in circulation as well as the STW
 956     // scavenge costs.  As usual, we lean toward time in space-time
 957     // tradeoffs.
 958     const int MAXPRIVATE = 1024 ;
 959     for (;;) {
 960         ObjectMonitor * m ;
 961 
 962         // 1: try to allocate from the thread's local omFreeList.
 963         // Threads will attempt to allocate first from their local list, then
 964         // from the global list, and only after those attempts fail will the thread
 965         // attempt to instantiate new monitors.   Thread-local free lists take
 966         // heat off the ListLock and improve allocation latency, as well as reducing
 967         // coherency traffic on the shared global list.
 968         m = Self->omFreeList ;
 969         if (m != NULL) {
 970            Self->omFreeList = m->FreeNext ;
 971            Self->omFreeCount -- ;
 972            // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
 973            guarantee (m->object() == NULL, "invariant") ;
 974            if (MonitorInUseLists) {
 975              m->FreeNext = Self->omInUseList;
 976              Self->omInUseList = m;
 977              Self->omInUseCount ++;
 978              // verifyInUse(Self);
 979            } else {
 980              m->FreeNext = NULL;
 981            }
 982            return m ;
 983         }
 984 
 985         // 2: try to allocate from the global gFreeList
 986         // CONSIDER: use muxTry() instead of muxAcquire().
 987         // If the muxTry() fails then drop immediately into case 3.
 988         // If we're using thread-local free lists then try
 989         // to reprovision the caller's free list.
 990         if (gFreeList != NULL) {
 991             // Reprovision the thread's omFreeList.
 992             // Use bulk transfers to reduce the allocation rate and heat
 993             // on various locks.
 994             Thread::muxAcquire (&ListLock, "omAlloc") ;
 995             for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL; ) {
 996                 MonitorFreeCount --;
 997                 ObjectMonitor * take = gFreeList ;
 998                 gFreeList = take->FreeNext ;
 999                 guarantee (take->object() == NULL, "invariant") ;
1000                 guarantee (!take->is_busy(), "invariant") ;
1001                 take->Recycle() ;
1002                 omRelease (Self, take, false) ;
1003             }
1004             Thread::muxRelease (&ListLock) ;
1005             Self->omFreeProvision += 1 + (Self->omFreeProvision/2) ;
1006             if (Self->omFreeProvision > MAXPRIVATE ) Self->omFreeProvision = MAXPRIVATE ;
1007             TEVENT (omFirst - reprovision) ;
1008 
1009             const int mx = MonitorBound ;
1010             if (mx > 0 && (MonitorPopulation-MonitorFreeCount) > mx) {
1011               // We can't safely induce a STW safepoint from omAlloc() as our thread
1012               // state may not be appropriate for such activities and callers may hold
1013               // naked oops, so instead we defer the action.
1014               InduceScavenge (Self, "omAlloc") ;
1015             }
1016             continue;
1017         }
1018 
1019         // 3: allocate a block of new ObjectMonitors
1020         // Both the local and global free lists are empty -- resort to malloc().
1021         // In the current implementation objectMonitors are TSM - immortal.
1022         assert (_BLOCKSIZE > 1, "invariant") ;
1023         ObjectMonitor * temp = new ObjectMonitor[_BLOCKSIZE];
1024 
1025         // NOTE: (almost) no way to recover if allocation failed.
1026         // We might be able to induce a STW safepoint and scavenge enough
1027         // objectMonitors to permit progress.
1028         if (temp == NULL) {
1029             vm_exit_out_of_memory (sizeof (ObjectMonitor[_BLOCKSIZE]), OOM_MALLOC_ERROR,
1030                                    "Allocate ObjectMonitors");
1031         }
1032 
1033         // Format the block.
1034         // initialize the linked list, each monitor points to its next
1035         // forming the single linked free list, the very first monitor
1036         // will points to next block, which forms the block list.
1037         // The trick of using the 1st element in the block as gBlockList
1038         // linkage should be reconsidered.  A better implementation would
1039         // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
1040 
1041         for (int i = 1; i < _BLOCKSIZE ; i++) {
1042            temp[i].FreeNext = &temp[i+1];
1043         }
1044 
1045         // terminate the last monitor as the end of list
1046         temp[_BLOCKSIZE - 1].FreeNext = NULL ;
1047 
1048         // Element [0] is reserved for global list linkage
1049         temp[0].set_object(CHAINMARKER);
1050 
1051         // Consider carving out this thread's current request from the
1052         // block in hand.  This avoids some lock traffic and redundant
1053         // list activity.
1054 
1055         // Acquire the ListLock to manipulate BlockList and FreeList.
1056         // An Oyama-Taura-Yonezawa scheme might be more efficient.
1057         Thread::muxAcquire (&ListLock, "omAlloc [2]") ;
1058         MonitorPopulation += _BLOCKSIZE-1;
1059         MonitorFreeCount += _BLOCKSIZE-1;
1060 
1061         // Add the new block to the list of extant blocks (gBlockList).
1062         // The very first objectMonitor in a block is reserved and dedicated.
1063         // It serves as blocklist "next" linkage.
1064         temp[0].FreeNext = gBlockList;
1065         // There are lock-free uses of gBlockList so make sure that
1066         // the previous stores happen before we update gBlockList.
1067         OrderAccess::release_store_ptr(&gBlockList, temp);
1068 
1069         // Add the new string of objectMonitors to the global free list
1070         temp[_BLOCKSIZE - 1].FreeNext = gFreeList ;
1071         gFreeList = temp + 1;
1072         Thread::muxRelease (&ListLock) ;
1073         TEVENT (Allocate block of monitors) ;
1074     }
1075 }
1076 
1077 // Place "m" on the caller's private per-thread omFreeList.
1078 // In practice there's no need to clamp or limit the number of
1079 // monitors on a thread's omFreeList as the only time we'll call
1080 // omRelease is to return a monitor to the free list after a CAS
1081 // attempt failed.  This doesn't allow unbounded #s of monitors to
1082 // accumulate on a thread's free list.
1083 //
1084 
1085 void ObjectSynchronizer::omRelease (Thread * Self, ObjectMonitor * m, bool fromPerThreadAlloc) {
1086     guarantee (m->object() == NULL, "invariant") ;
1087 
1088     // Remove from omInUseList
1089     if (MonitorInUseLists && fromPerThreadAlloc) {
1090       ObjectMonitor* curmidinuse = NULL;
1091       for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; ) {
1092        if (m == mid) {
1093          // extract from per-thread in-use-list
1094          if (mid == Self->omInUseList) {
1095            Self->omInUseList = mid->FreeNext;
1096          } else if (curmidinuse != NULL) {
1097            curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
1098          }
1099          Self->omInUseCount --;
1100          // verifyInUse(Self);
1101          break;
1102        } else {
1103          curmidinuse = mid;
1104          mid = mid->FreeNext;
1105       }
1106     }
1107   }
1108 
1109   // FreeNext is used for both onInUseList and omFreeList, so clear old before setting new
1110   m->FreeNext = Self->omFreeList ;
1111   Self->omFreeList = m ;
1112   Self->omFreeCount ++ ;
1113 }
1114 
1115 // Return the monitors of a moribund thread's local free list to
1116 // the global free list.  Typically a thread calls omFlush() when
1117 // it's dying.  We could also consider having the VM thread steal
1118 // monitors from threads that have not run java code over a few
1119 // consecutive STW safepoints.  Relatedly, we might decay
1120 // omFreeProvision at STW safepoints.
1121 //
1122 // Also return the monitors of a moribund thread"s omInUseList to
1123 // a global gOmInUseList under the global list lock so these
1124 // will continue to be scanned.
1125 //
1126 // We currently call omFlush() from the Thread:: dtor _after the thread
1127 // has been excised from the thread list and is no longer a mutator.
1128 // That means that omFlush() can run concurrently with a safepoint and
1129 // the scavenge operator.  Calling omFlush() from JavaThread::exit() might
1130 // be a better choice as we could safely reason that that the JVM is
1131 // not at a safepoint at the time of the call, and thus there could
1132 // be not inopportune interleavings between omFlush() and the scavenge
1133 // operator.
1134 
1135 void ObjectSynchronizer::omFlush (Thread * Self) {
1136     ObjectMonitor * List = Self->omFreeList ;  // Null-terminated SLL
1137     Self->omFreeList = NULL ;
1138     ObjectMonitor * Tail = NULL ;
1139     int Tally = 0;
1140     if (List != NULL) {
1141       ObjectMonitor * s ;
1142       for (s = List ; s != NULL ; s = s->FreeNext) {
1143           Tally ++ ;
1144           Tail = s ;
1145           guarantee (s->object() == NULL, "invariant") ;
1146           guarantee (!s->is_busy(), "invariant") ;
1147           s->set_owner (NULL) ;   // redundant but good hygiene
1148           TEVENT (omFlush - Move one) ;
1149       }
1150       guarantee (Tail != NULL && List != NULL, "invariant") ;
1151     }
1152 
1153     ObjectMonitor * InUseList = Self->omInUseList;
1154     ObjectMonitor * InUseTail = NULL ;
1155     int InUseTally = 0;
1156     if (InUseList != NULL) {
1157       Self->omInUseList = NULL;
1158       ObjectMonitor *curom;
1159       for (curom = InUseList; curom != NULL; curom = curom->FreeNext) {
1160         InUseTail = curom;
1161         InUseTally++;
1162       }
1163 // TODO debug
1164       assert(Self->omInUseCount == InUseTally, "inuse count off");
1165       Self->omInUseCount = 0;
1166       guarantee (InUseTail != NULL && InUseList != NULL, "invariant");
1167     }
1168 
1169     Thread::muxAcquire (&ListLock, "omFlush") ;
1170     if (Tail != NULL) {
1171       Tail->FreeNext = gFreeList ;
1172       gFreeList = List ;
1173       MonitorFreeCount += Tally;
1174     }
1175 
1176     if (InUseTail != NULL) {
1177       InUseTail->FreeNext = gOmInUseList;
1178       gOmInUseList = InUseList;
1179       gOmInUseCount += InUseTally;
1180     }
1181 
1182     Thread::muxRelease (&ListLock) ;
1183     TEVENT (omFlush) ;
1184 }
1185 
1186 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1187                                        const oop obj) {
1188   assert(event != NULL, "invariant");
1189   assert(event->should_commit(), "invariant");
1190   event->set_monitorClass(obj->klass());
1191   event->set_address((uintptr_t)(void*)obj);
1192   event->commit();
1193 }
1194 
1195 // Fast path code shared by multiple functions
1196 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
1197   markOop mark = obj->mark();
1198   if (mark->has_monitor()) {
1199     assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1200     assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
1201     return mark->monitor();
1202   }
1203   return ObjectSynchronizer::inflate(Thread::current(), obj);
1204 }
1205 
1206 
1207 // Note that we could encounter some performance loss through false-sharing as
1208 // multiple locks occupy the same $ line.  Padding might be appropriate.
1209 
1210 
1211 ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
1212   // Inflate mutates the heap ...
1213   // Relaxing assertion for bug 6320749.
1214   assert (Universe::verify_in_progress() ||
1215           !SafepointSynchronize::is_at_safepoint(), "invariant") ;
1216 
1217   EventJavaMonitorInflate event;
1218 
1219   for (;;) {
1220       const markOop mark = object->mark() ;
1221       assert (!mark->has_bias_pattern(), "invariant") ;
1222 
1223       // The mark can be in one of the following states:
1224       // *  Inflated     - just return
1225       // *  Stack-locked - coerce it to inflated
1226       // *  INFLATING    - busy wait for conversion to complete
1227       // *  Neutral      - aggressively inflate the object.
1228       // *  BIASED       - Illegal.  We should never see this
1229 
1230       // CASE: inflated
1231       if (mark->has_monitor()) {
1232           ObjectMonitor * inf = mark->monitor() ;
1233           assert (inf->header()->is_neutral(), "invariant");
1234           assert (inf->object() == object, "invariant") ;
1235           assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1236           return inf ;
1237       }
1238 
1239       // CASE: inflation in progress - inflating over a stack-lock.
1240       // Some other thread is converting from stack-locked to inflated.
1241       // Only that thread can complete inflation -- other threads must wait.
1242       // The INFLATING value is transient.
1243       // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1244       // We could always eliminate polling by parking the thread on some auxiliary list.
1245       if (mark == markOopDesc::INFLATING()) {
1246          TEVENT (Inflate: spin while INFLATING) ;
1247          ReadStableMark(object) ;
1248          continue ;
1249       }
1250 
1251       // CASE: stack-locked
1252       // Could be stack-locked either by this thread or by some other thread.
1253       //
1254       // Note that we allocate the objectmonitor speculatively, _before_ attempting
1255       // to install INFLATING into the mark word.  We originally installed INFLATING,
1256       // allocated the objectmonitor, and then finally STed the address of the
1257       // objectmonitor into the mark.  This was correct, but artificially lengthened
1258       // the interval in which INFLATED appeared in the mark, thus increasing
1259       // the odds of inflation contention.
1260       //
1261       // We now use per-thread private objectmonitor free lists.
1262       // These list are reprovisioned from the global free list outside the
1263       // critical INFLATING...ST interval.  A thread can transfer
1264       // multiple objectmonitors en-mass from the global free list to its local free list.
1265       // This reduces coherency traffic and lock contention on the global free list.
1266       // Using such local free lists, it doesn't matter if the omAlloc() call appears
1267       // before or after the CAS(INFLATING) operation.
1268       // See the comments in omAlloc().
1269 
1270       if (mark->has_locker()) {
1271           ObjectMonitor * m = omAlloc (Self) ;
1272           // Optimistically prepare the objectmonitor - anticipate successful CAS
1273           // We do this before the CAS in order to minimize the length of time
1274           // in which INFLATING appears in the mark.
1275           m->Recycle();
1276           m->_Responsible  = NULL ;
1277           m->OwnerIsThread = 0 ;
1278           m->_recursions   = 0 ;
1279           m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;   // Consider: maintain by type/class
1280 
1281           markOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ;
1282           if (cmp != mark) {
1283              omRelease (Self, m, true) ;
1284              continue ;       // Interference -- just retry
1285           }
1286 
1287           // We've successfully installed INFLATING (0) into the mark-word.
1288           // This is the only case where 0 will appear in a mark-work.
1289           // Only the singular thread that successfully swings the mark-word
1290           // to 0 can perform (or more precisely, complete) inflation.
1291           //
1292           // Why do we CAS a 0 into the mark-word instead of just CASing the
1293           // mark-word from the stack-locked value directly to the new inflated state?
1294           // Consider what happens when a thread unlocks a stack-locked object.
1295           // It attempts to use CAS to swing the displaced header value from the
1296           // on-stack basiclock back into the object header.  Recall also that the
1297           // header value (hashcode, etc) can reside in (a) the object header, or
1298           // (b) a displaced header associated with the stack-lock, or (c) a displaced
1299           // header in an objectMonitor.  The inflate() routine must copy the header
1300           // value from the basiclock on the owner's stack to the objectMonitor, all
1301           // the while preserving the hashCode stability invariants.  If the owner
1302           // decides to release the lock while the value is 0, the unlock will fail
1303           // and control will eventually pass from slow_exit() to inflate.  The owner
1304           // will then spin, waiting for the 0 value to disappear.   Put another way,
1305           // the 0 causes the owner to stall if the owner happens to try to
1306           // drop the lock (restoring the header from the basiclock to the object)
1307           // while inflation is in-progress.  This protocol avoids races that might
1308           // would otherwise permit hashCode values to change or "flicker" for an object.
1309           // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
1310           // 0 serves as a "BUSY" inflate-in-progress indicator.
1311 
1312 
1313           // fetch the displaced mark from the owner's stack.
1314           // The owner can't die or unwind past the lock while our INFLATING
1315           // object is in the mark.  Furthermore the owner can't complete
1316           // an unlock on the object, either.
1317           markOop dmw = mark->displaced_mark_helper() ;
1318           assert (dmw->is_neutral(), "invariant") ;
1319 
1320           // Setup monitor fields to proper values -- prepare the monitor
1321           m->set_header(dmw) ;
1322 
1323           // Optimization: if the mark->locker stack address is associated
1324           // with this thread we could simply set m->_owner = Self and
1325           // m->OwnerIsThread = 1. Note that a thread can inflate an object
1326           // that it has stack-locked -- as might happen in wait() -- directly
1327           // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1328           m->set_owner(mark->locker());
1329           m->set_object(object);
1330           // TODO-FIXME: assert BasicLock->dhw != 0.
1331 
1332           // Must preserve store ordering. The monitor state must
1333           // be stable at the time of publishing the monitor address.
1334           guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
1335           object->release_set_mark(markOopDesc::encode(m));
1336 
1337           // Hopefully the performance counters are allocated on distinct cache lines
1338           // to avoid false sharing on MP systems ...
1339           if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1340           TEVENT(Inflate: overwrite stacklock) ;
1341           if (TraceMonitorInflation) {
1342             if (object->is_instance()) {
1343               ResourceMark rm;
1344               tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1345                 (void *) object, (intptr_t) object->mark(),
1346                 object->klass()->external_name());
1347             }
1348           }
1349           if (event.should_commit()) {
1350             post_monitor_inflate_event(&event, object);
1351           }
1352           return m ;
1353       }
1354 
1355       // CASE: neutral
1356       // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1357       // If we know we're inflating for entry it's better to inflate by swinging a
1358       // pre-locked objectMonitor pointer into the object header.   A successful
1359       // CAS inflates the object *and* confers ownership to the inflating thread.
1360       // In the current implementation we use a 2-step mechanism where we CAS()
1361       // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1362       // An inflateTry() method that we could call from fast_enter() and slow_enter()
1363       // would be useful.
1364 
1365       assert (mark->is_neutral(), "invariant");
1366       ObjectMonitor * m = omAlloc (Self) ;
1367       // prepare m for installation - set monitor to initial state
1368       m->Recycle();
1369       m->set_header(mark);
1370       m->set_owner(NULL);
1371       m->set_object(object);
1372       m->OwnerIsThread = 1 ;
1373       m->_recursions   = 0 ;
1374       m->_Responsible  = NULL ;
1375       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;       // consider: keep metastats by type/class
1376 
1377       if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) {
1378           m->set_object (NULL) ;
1379           m->set_owner  (NULL) ;
1380           m->OwnerIsThread = 0 ;
1381           m->Recycle() ;
1382           omRelease (Self, m, true) ;
1383           m = NULL ;
1384           continue ;
1385           // interference - the markword changed - just retry.
1386           // The state-transitions are one-way, so there's no chance of
1387           // live-lock -- "Inflated" is an absorbing state.
1388       }
1389 
1390       // Hopefully the performance counters are allocated on distinct
1391       // cache lines to avoid false sharing on MP systems ...
1392       if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1393       TEVENT(Inflate: overwrite neutral) ;
1394       if (TraceMonitorInflation) {
1395         if (object->is_instance()) {
1396           ResourceMark rm;
1397           tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1398             (void *) object, (intptr_t) object->mark(),
1399             object->klass()->external_name());
1400         }
1401       }
1402       if (event.should_commit()) {
1403         post_monitor_inflate_event(&event, object);
1404       }
1405       return m ;
1406   }
1407 }
1408 
1409 // Note that we could encounter some performance loss through false-sharing as
1410 // multiple locks occupy the same $ line.  Padding might be appropriate.
1411 
1412 
1413 // Deflate_idle_monitors() is called at all safepoints, immediately
1414 // after all mutators are stopped, but before any objects have moved.
1415 // It traverses the list of known monitors, deflating where possible.
1416 // The scavenged monitor are returned to the monitor free list.
1417 //
1418 // Beware that we scavenge at *every* stop-the-world point.
1419 // Having a large number of monitors in-circulation negatively
1420 // impacts the performance of some applications (e.g., PointBase).
1421 // Broadly, we want to minimize the # of monitors in circulation.
1422 //
1423 // We have added a flag, MonitorInUseLists, which creates a list
1424 // of active monitors for each thread. deflate_idle_monitors()
1425 // only scans the per-thread inuse lists. omAlloc() puts all
1426 // assigned monitors on the per-thread list. deflate_idle_monitors()
1427 // returns the non-busy monitors to the global free list.
1428 // When a thread dies, omFlush() adds the list of active monitors for
1429 // that thread to a global gOmInUseList acquiring the
1430 // global list lock. deflate_idle_monitors() acquires the global
1431 // list lock to scan for non-busy monitors to the global free list.
1432 // An alternative could have used a single global inuse list. The
1433 // downside would have been the additional cost of acquiring the global list lock
1434 // for every omAlloc().
1435 //
1436 // Perversely, the heap size -- and thus the STW safepoint rate --
1437 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
1438 // which in turn can mean large(r) numbers of objectmonitors in circulation.
1439 // This is an unfortunate aspect of this design.
1440 //
1441 
1442 enum ManifestConstants {
1443     ClearResponsibleAtSTW   = 0,
1444     MaximumRecheckInterval  = 1000
1445 } ;
1446 
1447 // Deflate a single monitor if not in use
1448 // Return true if deflated, false if in use
1449 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1450                                          ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
1451   bool deflated;
1452   // Normal case ... The monitor is associated with obj.
1453   guarantee (obj->mark() == markOopDesc::encode(mid), "invariant") ;
1454   guarantee (mid == obj->mark()->monitor(), "invariant");
1455   guarantee (mid->header()->is_neutral(), "invariant");
1456 
1457   if (mid->is_busy()) {
1458      if (ClearResponsibleAtSTW) mid->_Responsible = NULL ;
1459      deflated = false;
1460   } else {
1461      // Deflate the monitor if it is no longer being used
1462      // It's idle - scavenge and return to the global free list
1463      // plain old deflation ...
1464      TEVENT (deflate_idle_monitors - scavenge1) ;
1465      if (TraceMonitorInflation) {
1466        if (obj->is_instance()) {
1467          ResourceMark rm;
1468            tty->print_cr("Deflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1469                 (void *) obj, (intptr_t) obj->mark(), obj->klass()->external_name());
1470        }
1471      }
1472 
1473      // Restore the header back to obj
1474      obj->release_set_mark(mid->header());
1475      mid->clear();
1476 
1477      assert (mid->object() == NULL, "invariant") ;
1478 
1479      // Move the object to the working free list defined by FreeHead,FreeTail.
1480      if (*FreeHeadp == NULL) *FreeHeadp = mid;
1481      if (*FreeTailp != NULL) {
1482        ObjectMonitor * prevtail = *FreeTailp;
1483        assert(prevtail->FreeNext == NULL, "cleaned up deflated?"); // TODO KK
1484        prevtail->FreeNext = mid;
1485       }
1486      *FreeTailp = mid;
1487      deflated = true;
1488   }
1489   return deflated;
1490 }
1491 
1492 // Caller acquires ListLock
1493 int ObjectSynchronizer::walk_monitor_list(ObjectMonitor** listheadp,
1494                                           ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
1495   ObjectMonitor* mid;
1496   ObjectMonitor* next;
1497   ObjectMonitor* curmidinuse = NULL;
1498   int deflatedcount = 0;
1499 
1500   for (mid = *listheadp; mid != NULL; ) {
1501      oop obj = (oop) mid->object();
1502      bool deflated = false;
1503      if (obj != NULL) {
1504        deflated = deflate_monitor(mid, obj, FreeHeadp, FreeTailp);
1505      }
1506      if (deflated) {
1507        // extract from per-thread in-use-list
1508        if (mid == *listheadp) {
1509          *listheadp = mid->FreeNext;
1510        } else if (curmidinuse != NULL) {
1511          curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
1512        }
1513        next = mid->FreeNext;
1514        mid->FreeNext = NULL;  // This mid is current tail in the FreeHead list
1515        mid = next;
1516        deflatedcount++;
1517      } else {
1518        curmidinuse = mid;
1519        mid = mid->FreeNext;
1520     }
1521   }
1522   return deflatedcount;
1523 }
1524 
1525 void ObjectSynchronizer::deflate_idle_monitors() {
1526   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1527   int nInuse = 0 ;              // currently associated with objects
1528   int nInCirculation = 0 ;      // extant
1529   int nScavenged = 0 ;          // reclaimed
1530   bool deflated = false;
1531 
1532   ObjectMonitor * FreeHead = NULL ;  // Local SLL of scavenged monitors
1533   ObjectMonitor * FreeTail = NULL ;
1534 
1535   TEVENT (deflate_idle_monitors) ;
1536   // Prevent omFlush from changing mids in Thread dtor's during deflation
1537   // And in case the vm thread is acquiring a lock during a safepoint
1538   // See e.g. 6320749
1539   Thread::muxAcquire (&ListLock, "scavenge - return") ;
1540 
1541   if (MonitorInUseLists) {
1542     int inUse = 0;
1543     for (JavaThread* cur = Threads::first(); cur != NULL; cur = cur->next()) {
1544       nInCirculation+= cur->omInUseCount;
1545       int deflatedcount = walk_monitor_list(cur->omInUseList_addr(), &FreeHead, &FreeTail);
1546       cur->omInUseCount-= deflatedcount;
1547       // verifyInUse(cur);
1548       nScavenged += deflatedcount;
1549       nInuse += cur->omInUseCount;
1550      }
1551 
1552    // For moribund threads, scan gOmInUseList
1553    if (gOmInUseList) {
1554      nInCirculation += gOmInUseCount;
1555      int deflatedcount = walk_monitor_list((ObjectMonitor **)&gOmInUseList, &FreeHead, &FreeTail);
1556      gOmInUseCount-= deflatedcount;
1557      nScavenged += deflatedcount;
1558      nInuse += gOmInUseCount;
1559     }
1560 
1561   } else {
1562     ObjectMonitor* block =
1563       (ObjectMonitor*)OrderAccess::load_ptr_acquire(&gBlockList);
1564     for (; block != NULL; block = (ObjectMonitor*)next(block)) {
1565       // Iterate over all extant monitors - Scavenge all idle monitors.
1566       assert(block->object() == CHAINMARKER, "must be a block header");
1567       nInCirculation += _BLOCKSIZE;
1568       for (int i = 1; i < _BLOCKSIZE; i++) {
1569         ObjectMonitor* mid = (ObjectMonitor*)&block[i];
1570         oop obj = (oop)mid->object();
1571 
1572         if (obj == NULL) {
1573           // The monitor is not associated with an object.
1574           // The monitor should either be a thread-specific private
1575           // free list or the global free list.
1576           // obj == NULL IMPLIES mid->is_busy() == 0
1577           guarantee(!mid->is_busy(), "invariant");
1578           continue;
1579         }
1580         deflated = deflate_monitor(mid, obj, &FreeHead, &FreeTail);
1581 
1582         if (deflated) {
1583           mid->FreeNext = NULL;
1584           nScavenged++;
1585         } else {
1586           nInuse++;
1587         }
1588       }
1589     }
1590   }
1591 
1592   MonitorFreeCount += nScavenged;
1593 
1594   // Consider: audit gFreeList to ensure that MonitorFreeCount and list agree.
1595 
1596   if (ObjectMonitor::Knob_Verbose) {
1597     ::printf ("Deflate: InCirc=%d InUse=%d Scavenged=%d ForceMonitorScavenge=%d : pop=%d free=%d\n",
1598         nInCirculation, nInuse, nScavenged, ForceMonitorScavenge,
1599         MonitorPopulation, MonitorFreeCount) ;
1600     ::fflush(stdout) ;
1601   }
1602 
1603   ForceMonitorScavenge = 0;    // Reset
1604 
1605   // Move the scavenged monitors back to the global free list.
1606   if (FreeHead != NULL) {
1607      guarantee (FreeTail != NULL && nScavenged > 0, "invariant") ;
1608      assert (FreeTail->FreeNext == NULL, "invariant") ;
1609      // constant-time list splice - prepend scavenged segment to gFreeList
1610      FreeTail->FreeNext = gFreeList ;
1611      gFreeList = FreeHead ;
1612   }
1613   Thread::muxRelease (&ListLock) ;
1614 
1615   if (ObjectMonitor::_sync_Deflations != NULL) ObjectMonitor::_sync_Deflations->inc(nScavenged) ;
1616   if (ObjectMonitor::_sync_MonExtant  != NULL) ObjectMonitor::_sync_MonExtant ->set_value(nInCirculation);
1617 
1618   // TODO: Add objectMonitor leak detection.
1619   // Audit/inventory the objectMonitors -- make sure they're all accounted for.
1620   GVars.stwRandom = os::random() ;
1621   GVars.stwCycle ++ ;
1622 }
1623 
1624 // Monitor cleanup on JavaThread::exit
1625 
1626 // Iterate through monitor cache and attempt to release thread's monitors
1627 // Gives up on a particular monitor if an exception occurs, but continues
1628 // the overall iteration, swallowing the exception.
1629 class ReleaseJavaMonitorsClosure: public MonitorClosure {
1630 private:
1631   TRAPS;
1632 
1633 public:
1634   ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {}
1635   void do_monitor(ObjectMonitor* mid) {
1636     if (mid->owner() == THREAD) {
1637       (void)mid->complete_exit(CHECK);
1638     }
1639   }
1640 };
1641 
1642 // Release all inflated monitors owned by THREAD.  Lightweight monitors are
1643 // ignored.  This is meant to be called during JNI thread detach which assumes
1644 // all remaining monitors are heavyweight.  All exceptions are swallowed.
1645 // Scanning the extant monitor list can be time consuming.
1646 // A simple optimization is to add a per-thread flag that indicates a thread
1647 // called jni_monitorenter() during its lifetime.
1648 //
1649 // Instead of No_Savepoint_Verifier it might be cheaper to
1650 // use an idiom of the form:
1651 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
1652 //   <code that must not run at safepoint>
1653 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1654 // Since the tests are extremely cheap we could leave them enabled
1655 // for normal product builds.
1656 
1657 void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) {
1658   assert(THREAD == JavaThread::current(), "must be current Java thread");
1659   No_Safepoint_Verifier nsv ;
1660   ReleaseJavaMonitorsClosure rjmc(THREAD);
1661   Thread::muxAcquire(&ListLock, "release_monitors_owned_by_thread");
1662   ObjectSynchronizer::monitors_iterate(&rjmc);
1663   Thread::muxRelease(&ListLock);
1664   THREAD->clear_pending_exception();
1665 }
1666 
1667 //------------------------------------------------------------------------------
1668 // Debugging code
1669 
1670 void ObjectSynchronizer::sanity_checks(const bool verbose,
1671                                        const uint cache_line_size,
1672                                        int *error_cnt_ptr,
1673                                        int *warning_cnt_ptr) {
1674   u_char *addr_begin      = (u_char*)&GVars;
1675   u_char *addr_stwRandom  = (u_char*)&GVars.stwRandom;
1676   u_char *addr_hcSequence = (u_char*)&GVars.hcSequence;
1677 
1678   if (verbose) {
1679     tty->print_cr("INFO: sizeof(SharedGlobals)=" SIZE_FORMAT,
1680                   sizeof(SharedGlobals));
1681   }
1682 
1683   uint offset_stwRandom = (uint)(addr_stwRandom - addr_begin);
1684   if (verbose) tty->print_cr("INFO: offset(stwRandom)=%u", offset_stwRandom);
1685 
1686   uint offset_hcSequence = (uint)(addr_hcSequence - addr_begin);
1687   if (verbose) {
1688     tty->print_cr("INFO: offset(_hcSequence)=%u", offset_hcSequence);
1689   }
1690 
1691   if (cache_line_size != 0) {
1692     // We were able to determine the L1 data cache line size so
1693     // do some cache line specific sanity checks
1694 
1695     if (offset_stwRandom < cache_line_size) {
1696       tty->print_cr("WARNING: the SharedGlobals.stwRandom field is closer "
1697                     "to the struct beginning than a cache line which permits "
1698                     "false sharing.");
1699       (*warning_cnt_ptr)++;
1700     }
1701 
1702     if ((offset_hcSequence - offset_stwRandom) < cache_line_size) {
1703       tty->print_cr("WARNING: the SharedGlobals.stwRandom and "
1704                     "SharedGlobals.hcSequence fields are closer than a cache "
1705                     "line which permits false sharing.");
1706       (*warning_cnt_ptr)++;
1707     }
1708 
1709     if ((sizeof(SharedGlobals) - offset_hcSequence) < cache_line_size) {
1710       tty->print_cr("WARNING: the SharedGlobals.hcSequence field is closer "
1711                     "to the struct end than a cache line which permits false "
1712                     "sharing.");
1713       (*warning_cnt_ptr)++;
1714     }
1715   }
1716 }
1717 
1718 #ifndef PRODUCT
1719 
1720 // Verify all monitors in the monitor cache, the verification is weak.
1721 void ObjectSynchronizer::verify() {
1722   ObjectMonitor* block =
1723     (ObjectMonitor *)OrderAccess::load_ptr_acquire(&gBlockList);
1724   while (block != NULL) {
1725     assert(block->object() == CHAINMARKER, "must be a block header");
1726     for (int i = 1; i < _BLOCKSIZE; i++) {
1727       ObjectMonitor* mid = (ObjectMonitor *)(block + i);
1728       oop object = (oop)mid->object();
1729       if (object != NULL) {
1730         mid->verify();
1731       }
1732     }
1733     block = (ObjectMonitor*) block->FreeNext;
1734   }
1735 }
1736 
1737 // Check if monitor belongs to the monitor cache
1738 // The list is grow-only so it's *relatively* safe to traverse
1739 // the list of extant blocks without taking a lock.
1740 
1741 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) {
1742   ObjectMonitor* block =
1743     (ObjectMonitor*)OrderAccess::load_ptr_acquire(&gBlockList);
1744   while (block != NULL) {
1745     assert(block->object() == CHAINMARKER, "must be a block header");
1746     if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) {
1747       address mon = (address)monitor;
1748       address blk = (address)block;
1749       size_t diff = mon - blk;
1750       assert((diff % sizeof(ObjectMonitor)) == 0, "must be aligned");
1751       return 1;
1752     }
1753     block = (ObjectMonitor*)block->FreeNext;
1754   }
1755   return 0;
1756 }
1757 
1758 #endif