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 * 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 = gBlockList;
 835   ObjectMonitor* mid;
 836   while (block) {
 837     assert(block->object() == CHAINMARKER, "must be a block header");
 838     for (int i = _BLOCKSIZE - 1; i > 0; i--) {
 839       mid = 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   for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) {
 861     assert(block->object() == CHAINMARKER, "must be a block header");
 862     for (int i = 1; i < _BLOCKSIZE; i++) {
 863       ObjectMonitor* mid = &block[i];
 864       if (mid->object() != NULL) {
 865         f->do_oop((oop*)mid->object_addr());
 866       }
 867     }
 868   }
 869 }
 870 
 871 
 872 // -----------------------------------------------------------------------------
 873 // ObjectMonitor Lifecycle
 874 // -----------------------
 875 // Inflation unlinks monitors from the global gFreeList and
 876 // associates them with objects.  Deflation -- which occurs at
 877 // STW-time -- disassociates idle monitors from objects.  Such
 878 // scavenged monitors are returned to the gFreeList.
 879 //
 880 // The global list is protected by ListLock.  All the critical sections
 881 // are short and operate in constant-time.
 882 //
 883 // ObjectMonitors reside in type-stable memory (TSM) and are immortal.
 884 //
 885 // Lifecycle:
 886 // --   unassigned and on the global free list
 887 // --   unassigned and on a thread's private omFreeList
 888 // --   assigned to an object.  The object is inflated and the mark refers
 889 //      to the objectmonitor.
 890 //
 891 
 892 
 893 // Constraining monitor pool growth via MonitorBound ...
 894 //
 895 // The monitor pool is grow-only.  We scavenge at STW safepoint-time, but the
 896 // the rate of scavenging is driven primarily by GC.  As such,  we can find
 897 // an inordinate number of monitors in circulation.
 898 // To avoid that scenario we can artificially induce a STW safepoint
 899 // if the pool appears to be growing past some reasonable bound.
 900 // Generally we favor time in space-time tradeoffs, but as there's no
 901 // natural back-pressure on the # of extant monitors we need to impose some
 902 // type of limit.  Beware that if MonitorBound is set to too low a value
 903 // we could just loop. In addition, if MonitorBound is set to a low value
 904 // we'll incur more safepoints, which are harmful to performance.
 905 // See also: GuaranteedSafepointInterval
 906 //
 907 // The current implementation uses asynchronous VM operations.
 908 //
 909 
 910 static void InduceScavenge (Thread * Self, const char * Whence) {
 911   // Induce STW safepoint to trim monitors
 912   // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
 913   // More precisely, trigger an asynchronous STW safepoint as the number
 914   // of active monitors passes the specified threshold.
 915   // TODO: assert thread state is reasonable
 916 
 917   if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
 918     if (ObjectMonitor::Knob_Verbose) {
 919       ::printf ("Monitor scavenge - Induced STW @%s (%d)\n", Whence, ForceMonitorScavenge) ;
 920       ::fflush(stdout) ;
 921     }
 922     // Induce a 'null' safepoint to scavenge monitors
 923     // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
 924     // to the VMthread and have a lifespan longer than that of this activation record.
 925     // The VMThread will delete the op when completed.
 926     VMThread::execute (new VM_ForceAsyncSafepoint()) ;
 927 
 928     if (ObjectMonitor::Knob_Verbose) {
 929       ::printf ("Monitor scavenge - STW posted @%s (%d)\n", Whence, ForceMonitorScavenge) ;
 930       ::fflush(stdout) ;
 931     }
 932   }
 933 }
 934 /* Too slow for general assert or debug
 935 void ObjectSynchronizer::verifyInUse (Thread *Self) {
 936    ObjectMonitor* mid;
 937    int inusetally = 0;
 938    for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
 939      inusetally ++;
 940    }
 941    assert(inusetally == Self->omInUseCount, "inuse count off");
 942 
 943    int freetally = 0;
 944    for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
 945      freetally ++;
 946    }
 947    assert(freetally == Self->omFreeCount, "free count off");
 948 }
 949 */
 950 ObjectMonitor * ATTR ObjectSynchronizer::omAlloc (Thread * Self) {
 951     // A large MAXPRIVATE value reduces both list lock contention
 952     // and list coherency traffic, but also tends to increase the
 953     // number of objectMonitors in circulation as well as the STW
 954     // scavenge costs.  As usual, we lean toward time in space-time
 955     // tradeoffs.
 956     const int MAXPRIVATE = 1024 ;
 957     for (;;) {
 958         ObjectMonitor * m ;
 959 
 960         // 1: try to allocate from the thread's local omFreeList.
 961         // Threads will attempt to allocate first from their local list, then
 962         // from the global list, and only after those attempts fail will the thread
 963         // attempt to instantiate new monitors.   Thread-local free lists take
 964         // heat off the ListLock and improve allocation latency, as well as reducing
 965         // coherency traffic on the shared global list.
 966         m = Self->omFreeList ;
 967         if (m != NULL) {
 968            Self->omFreeList = m->FreeNext ;
 969            Self->omFreeCount -- ;
 970            // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
 971            guarantee (m->object() == NULL, "invariant") ;
 972            if (MonitorInUseLists) {
 973              m->FreeNext = Self->omInUseList;
 974              Self->omInUseList = m;
 975              Self->omInUseCount ++;
 976              // verifyInUse(Self);
 977            } else {
 978              m->FreeNext = NULL;
 979            }
 980            return m ;
 981         }
 982 
 983         // 2: try to allocate from the global gFreeList
 984         // CONSIDER: use muxTry() instead of muxAcquire().
 985         // If the muxTry() fails then drop immediately into case 3.
 986         // If we're using thread-local free lists then try
 987         // to reprovision the caller's free list.
 988         if (gFreeList != NULL) {
 989             // Reprovision the thread's omFreeList.
 990             // Use bulk transfers to reduce the allocation rate and heat
 991             // on various locks.
 992             Thread::muxAcquire (&ListLock, "omAlloc") ;
 993             for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL; ) {
 994                 MonitorFreeCount --;
 995                 ObjectMonitor * take = gFreeList ;
 996                 gFreeList = take->FreeNext ;
 997                 guarantee (take->object() == NULL, "invariant") ;
 998                 guarantee (!take->is_busy(), "invariant") ;
 999                 take->Recycle() ;
1000                 omRelease (Self, take, false) ;
1001             }
1002             Thread::muxRelease (&ListLock) ;
1003             Self->omFreeProvision += 1 + (Self->omFreeProvision/2) ;
1004             if (Self->omFreeProvision > MAXPRIVATE ) Self->omFreeProvision = MAXPRIVATE ;
1005             TEVENT (omFirst - reprovision) ;
1006 
1007             const int mx = MonitorBound ;
1008             if (mx > 0 && (MonitorPopulation-MonitorFreeCount) > mx) {
1009               // We can't safely induce a STW safepoint from omAlloc() as our thread
1010               // state may not be appropriate for such activities and callers may hold
1011               // naked oops, so instead we defer the action.
1012               InduceScavenge (Self, "omAlloc") ;
1013             }
1014             continue;
1015         }
1016 
1017         // 3: allocate a block of new ObjectMonitors
1018         // Both the local and global free lists are empty -- resort to malloc().
1019         // In the current implementation objectMonitors are TSM - immortal.
1020         assert (_BLOCKSIZE > 1, "invariant") ;
1021         ObjectMonitor * temp = new ObjectMonitor[_BLOCKSIZE];
1022 
1023         // NOTE: (almost) no way to recover if allocation failed.
1024         // We might be able to induce a STW safepoint and scavenge enough
1025         // objectMonitors to permit progress.
1026         if (temp == NULL) {
1027             vm_exit_out_of_memory (sizeof (ObjectMonitor[_BLOCKSIZE]), OOM_MALLOC_ERROR,
1028                                    "Allocate ObjectMonitors");
1029         }
1030 
1031         // Format the block.
1032         // initialize the linked list, each monitor points to its next
1033         // forming the single linked free list, the very first monitor
1034         // will points to next block, which forms the block list.
1035         // The trick of using the 1st element in the block as gBlockList
1036         // linkage should be reconsidered.  A better implementation would
1037         // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
1038 
1039         for (int i = 1; i < _BLOCKSIZE ; i++) {
1040            temp[i].FreeNext = &temp[i+1];
1041         }
1042 
1043         // terminate the last monitor as the end of list
1044         temp[_BLOCKSIZE - 1].FreeNext = NULL ;
1045 
1046         // Element [0] is reserved for global list linkage
1047         temp[0].set_object(CHAINMARKER);
1048 
1049         // Consider carving out this thread's current request from the
1050         // block in hand.  This avoids some lock traffic and redundant
1051         // list activity.
1052 
1053         // Acquire the ListLock to manipulate BlockList and FreeList.
1054         // An Oyama-Taura-Yonezawa scheme might be more efficient.
1055         Thread::muxAcquire (&ListLock, "omAlloc [2]") ;
1056         MonitorPopulation += _BLOCKSIZE-1;
1057         MonitorFreeCount += _BLOCKSIZE-1;
1058 
1059         // Add the new block to the list of extant blocks (gBlockList).
1060         // The very first objectMonitor in a block is reserved and dedicated.
1061         // It serves as blocklist "next" linkage.
1062         temp[0].FreeNext = gBlockList;
1063         gBlockList = temp;
1064 
1065         // Add the new string of objectMonitors to the global free list
1066         temp[_BLOCKSIZE - 1].FreeNext = gFreeList ;
1067         gFreeList = temp + 1;
1068         Thread::muxRelease (&ListLock) ;
1069         TEVENT (Allocate block of monitors) ;
1070     }
1071 }
1072 
1073 // Place "m" on the caller's private per-thread omFreeList.
1074 // In practice there's no need to clamp or limit the number of
1075 // monitors on a thread's omFreeList as the only time we'll call
1076 // omRelease is to return a monitor to the free list after a CAS
1077 // attempt failed.  This doesn't allow unbounded #s of monitors to
1078 // accumulate on a thread's free list.
1079 //
1080 
1081 void ObjectSynchronizer::omRelease (Thread * Self, ObjectMonitor * m, bool fromPerThreadAlloc) {
1082     guarantee (m->object() == NULL, "invariant") ;
1083 
1084     // Remove from omInUseList
1085     if (MonitorInUseLists && fromPerThreadAlloc) {
1086       ObjectMonitor* curmidinuse = NULL;
1087       for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; ) {
1088        if (m == mid) {
1089          // extract from per-thread in-use-list
1090          if (mid == Self->omInUseList) {
1091            Self->omInUseList = mid->FreeNext;
1092          } else if (curmidinuse != NULL) {
1093            curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
1094          }
1095          Self->omInUseCount --;
1096          // verifyInUse(Self);
1097          break;
1098        } else {
1099          curmidinuse = mid;
1100          mid = mid->FreeNext;
1101       }
1102     }
1103   }
1104 
1105   // FreeNext is used for both onInUseList and omFreeList, so clear old before setting new
1106   m->FreeNext = Self->omFreeList ;
1107   Self->omFreeList = m ;
1108   Self->omFreeCount ++ ;
1109 }
1110 
1111 // Return the monitors of a moribund thread's local free list to
1112 // the global free list.  Typically a thread calls omFlush() when
1113 // it's dying.  We could also consider having the VM thread steal
1114 // monitors from threads that have not run java code over a few
1115 // consecutive STW safepoints.  Relatedly, we might decay
1116 // omFreeProvision at STW safepoints.
1117 //
1118 // Also return the monitors of a moribund thread"s omInUseList to
1119 // a global gOmInUseList under the global list lock so these
1120 // will continue to be scanned.
1121 //
1122 // We currently call omFlush() from the Thread:: dtor _after the thread
1123 // has been excised from the thread list and is no longer a mutator.
1124 // That means that omFlush() can run concurrently with a safepoint and
1125 // the scavenge operator.  Calling omFlush() from JavaThread::exit() might
1126 // be a better choice as we could safely reason that that the JVM is
1127 // not at a safepoint at the time of the call, and thus there could
1128 // be not inopportune interleavings between omFlush() and the scavenge
1129 // operator.
1130 
1131 void ObjectSynchronizer::omFlush (Thread * Self) {
1132     ObjectMonitor * List = Self->omFreeList ;  // Null-terminated SLL
1133     Self->omFreeList = NULL ;
1134     ObjectMonitor * Tail = NULL ;
1135     int Tally = 0;
1136     if (List != NULL) {
1137       ObjectMonitor * s ;
1138       for (s = List ; s != NULL ; s = s->FreeNext) {
1139           Tally ++ ;
1140           Tail = s ;
1141           guarantee (s->object() == NULL, "invariant") ;
1142           guarantee (!s->is_busy(), "invariant") ;
1143           s->set_owner (NULL) ;   // redundant but good hygiene
1144           TEVENT (omFlush - Move one) ;
1145       }
1146       guarantee (Tail != NULL && List != NULL, "invariant") ;
1147     }
1148 
1149     ObjectMonitor * InUseList = Self->omInUseList;
1150     ObjectMonitor * InUseTail = NULL ;
1151     int InUseTally = 0;
1152     if (InUseList != NULL) {
1153       Self->omInUseList = NULL;
1154       ObjectMonitor *curom;
1155       for (curom = InUseList; curom != NULL; curom = curom->FreeNext) {
1156         InUseTail = curom;
1157         InUseTally++;
1158       }
1159 // TODO debug
1160       assert(Self->omInUseCount == InUseTally, "inuse count off");
1161       Self->omInUseCount = 0;
1162       guarantee (InUseTail != NULL && InUseList != NULL, "invariant");
1163     }
1164 
1165     Thread::muxAcquire (&ListLock, "omFlush") ;
1166     if (Tail != NULL) {
1167       Tail->FreeNext = gFreeList ;
1168       gFreeList = List ;
1169       MonitorFreeCount += Tally;
1170     }
1171 
1172     if (InUseTail != NULL) {
1173       InUseTail->FreeNext = gOmInUseList;
1174       gOmInUseList = InUseList;
1175       gOmInUseCount += InUseTally;
1176     }
1177 
1178     Thread::muxRelease (&ListLock) ;
1179     TEVENT (omFlush) ;
1180 }
1181 
1182 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1183                                        const oop obj) {
1184   assert(event != NULL, "invariant");
1185   assert(event->should_commit(), "invariant");
1186   event->set_monitorClass(obj->klass());
1187   event->set_address((uintptr_t)(void*)obj);
1188   event->commit();
1189 }
1190 
1191 // Fast path code shared by multiple functions
1192 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
1193   markOop mark = obj->mark();
1194   if (mark->has_monitor()) {
1195     assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1196     assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
1197     return mark->monitor();
1198   }
1199   return ObjectSynchronizer::inflate(Thread::current(), obj);
1200 }
1201 
1202 
1203 // Note that we could encounter some performance loss through false-sharing as
1204 // multiple locks occupy the same $ line.  Padding might be appropriate.
1205 
1206 
1207 ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
1208   // Inflate mutates the heap ...
1209   // Relaxing assertion for bug 6320749.
1210   assert (Universe::verify_in_progress() ||
1211           !SafepointSynchronize::is_at_safepoint(), "invariant") ;
1212 
1213   EventJavaMonitorInflate event;
1214 
1215   for (;;) {
1216       const markOop mark = object->mark() ;
1217       assert (!mark->has_bias_pattern(), "invariant") ;
1218 
1219       // The mark can be in one of the following states:
1220       // *  Inflated     - just return
1221       // *  Stack-locked - coerce it to inflated
1222       // *  INFLATING    - busy wait for conversion to complete
1223       // *  Neutral      - aggressively inflate the object.
1224       // *  BIASED       - Illegal.  We should never see this
1225 
1226       // CASE: inflated
1227       if (mark->has_monitor()) {
1228           ObjectMonitor * inf = mark->monitor() ;
1229           assert (inf->header()->is_neutral(), "invariant");
1230           assert (inf->object() == object, "invariant") ;
1231           assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1232           return inf ;
1233       }
1234 
1235       // CASE: inflation in progress - inflating over a stack-lock.
1236       // Some other thread is converting from stack-locked to inflated.
1237       // Only that thread can complete inflation -- other threads must wait.
1238       // The INFLATING value is transient.
1239       // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1240       // We could always eliminate polling by parking the thread on some auxiliary list.
1241       if (mark == markOopDesc::INFLATING()) {
1242          TEVENT (Inflate: spin while INFLATING) ;
1243          ReadStableMark(object) ;
1244          continue ;
1245       }
1246 
1247       // CASE: stack-locked
1248       // Could be stack-locked either by this thread or by some other thread.
1249       //
1250       // Note that we allocate the objectmonitor speculatively, _before_ attempting
1251       // to install INFLATING into the mark word.  We originally installed INFLATING,
1252       // allocated the objectmonitor, and then finally STed the address of the
1253       // objectmonitor into the mark.  This was correct, but artificially lengthened
1254       // the interval in which INFLATED appeared in the mark, thus increasing
1255       // the odds of inflation contention.
1256       //
1257       // We now use per-thread private objectmonitor free lists.
1258       // These list are reprovisioned from the global free list outside the
1259       // critical INFLATING...ST interval.  A thread can transfer
1260       // multiple objectmonitors en-mass from the global free list to its local free list.
1261       // This reduces coherency traffic and lock contention on the global free list.
1262       // Using such local free lists, it doesn't matter if the omAlloc() call appears
1263       // before or after the CAS(INFLATING) operation.
1264       // See the comments in omAlloc().
1265 
1266       if (mark->has_locker()) {
1267           ObjectMonitor * m = omAlloc (Self) ;
1268           // Optimistically prepare the objectmonitor - anticipate successful CAS
1269           // We do this before the CAS in order to minimize the length of time
1270           // in which INFLATING appears in the mark.
1271           m->Recycle();
1272           m->_Responsible  = NULL ;
1273           m->OwnerIsThread = 0 ;
1274           m->_recursions   = 0 ;
1275           m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;   // Consider: maintain by type/class
1276 
1277           markOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ;
1278           if (cmp != mark) {
1279              omRelease (Self, m, true) ;
1280              continue ;       // Interference -- just retry
1281           }
1282 
1283           // We've successfully installed INFLATING (0) into the mark-word.
1284           // This is the only case where 0 will appear in a mark-work.
1285           // Only the singular thread that successfully swings the mark-word
1286           // to 0 can perform (or more precisely, complete) inflation.
1287           //
1288           // Why do we CAS a 0 into the mark-word instead of just CASing the
1289           // mark-word from the stack-locked value directly to the new inflated state?
1290           // Consider what happens when a thread unlocks a stack-locked object.
1291           // It attempts to use CAS to swing the displaced header value from the
1292           // on-stack basiclock back into the object header.  Recall also that the
1293           // header value (hashcode, etc) can reside in (a) the object header, or
1294           // (b) a displaced header associated with the stack-lock, or (c) a displaced
1295           // header in an objectMonitor.  The inflate() routine must copy the header
1296           // value from the basiclock on the owner's stack to the objectMonitor, all
1297           // the while preserving the hashCode stability invariants.  If the owner
1298           // decides to release the lock while the value is 0, the unlock will fail
1299           // and control will eventually pass from slow_exit() to inflate.  The owner
1300           // will then spin, waiting for the 0 value to disappear.   Put another way,
1301           // the 0 causes the owner to stall if the owner happens to try to
1302           // drop the lock (restoring the header from the basiclock to the object)
1303           // while inflation is in-progress.  This protocol avoids races that might
1304           // would otherwise permit hashCode values to change or "flicker" for an object.
1305           // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
1306           // 0 serves as a "BUSY" inflate-in-progress indicator.
1307 
1308 
1309           // fetch the displaced mark from the owner's stack.
1310           // The owner can't die or unwind past the lock while our INFLATING
1311           // object is in the mark.  Furthermore the owner can't complete
1312           // an unlock on the object, either.
1313           markOop dmw = mark->displaced_mark_helper() ;
1314           assert (dmw->is_neutral(), "invariant") ;
1315 
1316           // Setup monitor fields to proper values -- prepare the monitor
1317           m->set_header(dmw) ;
1318 
1319           // Optimization: if the mark->locker stack address is associated
1320           // with this thread we could simply set m->_owner = Self and
1321           // m->OwnerIsThread = 1. Note that a thread can inflate an object
1322           // that it has stack-locked -- as might happen in wait() -- directly
1323           // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1324           m->set_owner(mark->locker());
1325           m->set_object(object);
1326           // TODO-FIXME: assert BasicLock->dhw != 0.
1327 
1328           // Must preserve store ordering. The monitor state must
1329           // be stable at the time of publishing the monitor address.
1330           guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
1331           object->release_set_mark(markOopDesc::encode(m));
1332 
1333           // Hopefully the performance counters are allocated on distinct cache lines
1334           // to avoid false sharing on MP systems ...
1335           if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1336           TEVENT(Inflate: overwrite stacklock) ;
1337           if (TraceMonitorInflation) {
1338             if (object->is_instance()) {
1339               ResourceMark rm;
1340               tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1341                 (void *) object, (intptr_t) object->mark(),
1342                 object->klass()->external_name());
1343             }
1344           }
1345           if (event.should_commit()) {
1346             post_monitor_inflate_event(&event, object);
1347           }
1348           return m ;
1349       }
1350 
1351       // CASE: neutral
1352       // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1353       // If we know we're inflating for entry it's better to inflate by swinging a
1354       // pre-locked objectMonitor pointer into the object header.   A successful
1355       // CAS inflates the object *and* confers ownership to the inflating thread.
1356       // In the current implementation we use a 2-step mechanism where we CAS()
1357       // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1358       // An inflateTry() method that we could call from fast_enter() and slow_enter()
1359       // would be useful.
1360 
1361       assert (mark->is_neutral(), "invariant");
1362       ObjectMonitor * m = omAlloc (Self) ;
1363       // prepare m for installation - set monitor to initial state
1364       m->Recycle();
1365       m->set_header(mark);
1366       m->set_owner(NULL);
1367       m->set_object(object);
1368       m->OwnerIsThread = 1 ;
1369       m->_recursions   = 0 ;
1370       m->_Responsible  = NULL ;
1371       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;       // consider: keep metastats by type/class
1372 
1373       if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) {
1374           m->set_object (NULL) ;
1375           m->set_owner  (NULL) ;
1376           m->OwnerIsThread = 0 ;
1377           m->Recycle() ;
1378           omRelease (Self, m, true) ;
1379           m = NULL ;
1380           continue ;
1381           // interference - the markword changed - just retry.
1382           // The state-transitions are one-way, so there's no chance of
1383           // live-lock -- "Inflated" is an absorbing state.
1384       }
1385 
1386       // Hopefully the performance counters are allocated on distinct
1387       // cache lines to avoid false sharing on MP systems ...
1388       if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1389       TEVENT(Inflate: overwrite neutral) ;
1390       if (TraceMonitorInflation) {
1391         if (object->is_instance()) {
1392           ResourceMark rm;
1393           tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1394             (void *) object, (intptr_t) object->mark(),
1395             object->klass()->external_name());
1396         }
1397       }
1398       if (event.should_commit()) {
1399         post_monitor_inflate_event(&event, object);
1400       }
1401       return m ;
1402   }
1403 }
1404 
1405 // Note that we could encounter some performance loss through false-sharing as
1406 // multiple locks occupy the same $ line.  Padding might be appropriate.
1407 
1408 
1409 // Deflate_idle_monitors() is called at all safepoints, immediately
1410 // after all mutators are stopped, but before any objects have moved.
1411 // It traverses the list of known monitors, deflating where possible.
1412 // The scavenged monitor are returned to the monitor free list.
1413 //
1414 // Beware that we scavenge at *every* stop-the-world point.
1415 // Having a large number of monitors in-circulation negatively
1416 // impacts the performance of some applications (e.g., PointBase).
1417 // Broadly, we want to minimize the # of monitors in circulation.
1418 //
1419 // We have added a flag, MonitorInUseLists, which creates a list
1420 // of active monitors for each thread. deflate_idle_monitors()
1421 // only scans the per-thread inuse lists. omAlloc() puts all
1422 // assigned monitors on the per-thread list. deflate_idle_monitors()
1423 // returns the non-busy monitors to the global free list.
1424 // When a thread dies, omFlush() adds the list of active monitors for
1425 // that thread to a global gOmInUseList acquiring the
1426 // global list lock. deflate_idle_monitors() acquires the global
1427 // list lock to scan for non-busy monitors to the global free list.
1428 // An alternative could have used a single global inuse list. The
1429 // downside would have been the additional cost of acquiring the global list lock
1430 // for every omAlloc().
1431 //
1432 // Perversely, the heap size -- and thus the STW safepoint rate --
1433 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
1434 // which in turn can mean large(r) numbers of objectmonitors in circulation.
1435 // This is an unfortunate aspect of this design.
1436 //
1437 
1438 enum ManifestConstants {
1439     ClearResponsibleAtSTW   = 0,
1440     MaximumRecheckInterval  = 1000
1441 } ;
1442 
1443 // Deflate a single monitor if not in use
1444 // Return true if deflated, false if in use
1445 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1446                                          ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
1447   bool deflated;
1448   // Normal case ... The monitor is associated with obj.
1449   guarantee (obj->mark() == markOopDesc::encode(mid), "invariant") ;
1450   guarantee (mid == obj->mark()->monitor(), "invariant");
1451   guarantee (mid->header()->is_neutral(), "invariant");
1452 
1453   if (mid->is_busy()) {
1454      if (ClearResponsibleAtSTW) mid->_Responsible = NULL ;
1455      deflated = false;
1456   } else {
1457      // Deflate the monitor if it is no longer being used
1458      // It's idle - scavenge and return to the global free list
1459      // plain old deflation ...
1460      TEVENT (deflate_idle_monitors - scavenge1) ;
1461      if (TraceMonitorInflation) {
1462        if (obj->is_instance()) {
1463          ResourceMark rm;
1464            tty->print_cr("Deflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1465                 (void *) obj, (intptr_t) obj->mark(), obj->klass()->external_name());
1466        }
1467      }
1468 
1469      // Restore the header back to obj
1470      obj->release_set_mark(mid->header());
1471      mid->clear();
1472 
1473      assert (mid->object() == NULL, "invariant") ;
1474 
1475      // Move the object to the working free list defined by FreeHead,FreeTail.
1476      if (*FreeHeadp == NULL) *FreeHeadp = mid;
1477      if (*FreeTailp != NULL) {
1478        ObjectMonitor * prevtail = *FreeTailp;
1479        assert(prevtail->FreeNext == NULL, "cleaned up deflated?"); // TODO KK
1480        prevtail->FreeNext = mid;
1481       }
1482      *FreeTailp = mid;
1483      deflated = true;
1484   }
1485   return deflated;
1486 }
1487 
1488 // Caller acquires ListLock
1489 int ObjectSynchronizer::walk_monitor_list(ObjectMonitor** listheadp,
1490                                           ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) {
1491   ObjectMonitor* mid;
1492   ObjectMonitor* next;
1493   ObjectMonitor* curmidinuse = NULL;
1494   int deflatedcount = 0;
1495 
1496   for (mid = *listheadp; mid != NULL; ) {
1497      oop obj = (oop) mid->object();
1498      bool deflated = false;
1499      if (obj != NULL) {
1500        deflated = deflate_monitor(mid, obj, FreeHeadp, FreeTailp);
1501      }
1502      if (deflated) {
1503        // extract from per-thread in-use-list
1504        if (mid == *listheadp) {
1505          *listheadp = mid->FreeNext;
1506        } else if (curmidinuse != NULL) {
1507          curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist
1508        }
1509        next = mid->FreeNext;
1510        mid->FreeNext = NULL;  // This mid is current tail in the FreeHead list
1511        mid = next;
1512        deflatedcount++;
1513      } else {
1514        curmidinuse = mid;
1515        mid = mid->FreeNext;
1516     }
1517   }
1518   return deflatedcount;
1519 }
1520 
1521 void ObjectSynchronizer::deflate_idle_monitors() {
1522   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1523   int nInuse = 0 ;              // currently associated with objects
1524   int nInCirculation = 0 ;      // extant
1525   int nScavenged = 0 ;          // reclaimed
1526   bool deflated = false;
1527 
1528   ObjectMonitor * FreeHead = NULL ;  // Local SLL of scavenged monitors
1529   ObjectMonitor * FreeTail = NULL ;
1530 
1531   TEVENT (deflate_idle_monitors) ;
1532   // Prevent omFlush from changing mids in Thread dtor's during deflation
1533   // And in case the vm thread is acquiring a lock during a safepoint
1534   // See e.g. 6320749
1535   Thread::muxAcquire (&ListLock, "scavenge - return") ;
1536 
1537   if (MonitorInUseLists) {
1538     int inUse = 0;
1539     for (JavaThread* cur = Threads::first(); cur != NULL; cur = cur->next()) {
1540       nInCirculation+= cur->omInUseCount;
1541       int deflatedcount = walk_monitor_list(cur->omInUseList_addr(), &FreeHead, &FreeTail);
1542       cur->omInUseCount-= deflatedcount;
1543       // verifyInUse(cur);
1544       nScavenged += deflatedcount;
1545       nInuse += cur->omInUseCount;
1546      }
1547 
1548    // For moribund threads, scan gOmInUseList
1549    if (gOmInUseList) {
1550      nInCirculation += gOmInUseCount;
1551      int deflatedcount = walk_monitor_list((ObjectMonitor **)&gOmInUseList, &FreeHead, &FreeTail);
1552      gOmInUseCount-= deflatedcount;
1553      nScavenged += deflatedcount;
1554      nInuse += gOmInUseCount;
1555     }
1556 
1557   } else for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) {
1558   // Iterate over all extant monitors - Scavenge all idle monitors.
1559     assert(block->object() == CHAINMARKER, "must be a block header");
1560     nInCirculation += _BLOCKSIZE ;
1561     for (int i = 1 ; i < _BLOCKSIZE; i++) {
1562       ObjectMonitor* mid = &block[i];
1563       oop obj = (oop) mid->object();
1564 
1565       if (obj == NULL) {
1566         // The monitor is not associated with an object.
1567         // The monitor should either be a thread-specific private
1568         // free list or the global free list.
1569         // obj == NULL IMPLIES mid->is_busy() == 0
1570         guarantee (!mid->is_busy(), "invariant") ;
1571         continue ;
1572       }
1573       deflated = deflate_monitor(mid, obj, &FreeHead, &FreeTail);
1574 
1575       if (deflated) {
1576         mid->FreeNext = NULL ;
1577         nScavenged ++ ;
1578       } else {
1579         nInuse ++;
1580       }
1581     }
1582   }
1583 
1584   MonitorFreeCount += nScavenged;
1585 
1586   // Consider: audit gFreeList to ensure that MonitorFreeCount and list agree.
1587 
1588   if (ObjectMonitor::Knob_Verbose) {
1589     ::printf ("Deflate: InCirc=%d InUse=%d Scavenged=%d ForceMonitorScavenge=%d : pop=%d free=%d\n",
1590         nInCirculation, nInuse, nScavenged, ForceMonitorScavenge,
1591         MonitorPopulation, MonitorFreeCount) ;
1592     ::fflush(stdout) ;
1593   }
1594 
1595   ForceMonitorScavenge = 0;    // Reset
1596 
1597   // Move the scavenged monitors back to the global free list.
1598   if (FreeHead != NULL) {
1599      guarantee (FreeTail != NULL && nScavenged > 0, "invariant") ;
1600      assert (FreeTail->FreeNext == NULL, "invariant") ;
1601      // constant-time list splice - prepend scavenged segment to gFreeList
1602      FreeTail->FreeNext = gFreeList ;
1603      gFreeList = FreeHead ;
1604   }
1605   Thread::muxRelease (&ListLock) ;
1606 
1607   if (ObjectMonitor::_sync_Deflations != NULL) ObjectMonitor::_sync_Deflations->inc(nScavenged) ;
1608   if (ObjectMonitor::_sync_MonExtant  != NULL) ObjectMonitor::_sync_MonExtant ->set_value(nInCirculation);
1609 
1610   // TODO: Add objectMonitor leak detection.
1611   // Audit/inventory the objectMonitors -- make sure they're all accounted for.
1612   GVars.stwRandom = os::random() ;
1613   GVars.stwCycle ++ ;
1614 }
1615 
1616 // Monitor cleanup on JavaThread::exit
1617 
1618 // Iterate through monitor cache and attempt to release thread's monitors
1619 // Gives up on a particular monitor if an exception occurs, but continues
1620 // the overall iteration, swallowing the exception.
1621 class ReleaseJavaMonitorsClosure: public MonitorClosure {
1622 private:
1623   TRAPS;
1624 
1625 public:
1626   ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {}
1627   void do_monitor(ObjectMonitor* mid) {
1628     if (mid->owner() == THREAD) {
1629       (void)mid->complete_exit(CHECK);
1630     }
1631   }
1632 };
1633 
1634 // Release all inflated monitors owned by THREAD.  Lightweight monitors are
1635 // ignored.  This is meant to be called during JNI thread detach which assumes
1636 // all remaining monitors are heavyweight.  All exceptions are swallowed.
1637 // Scanning the extant monitor list can be time consuming.
1638 // A simple optimization is to add a per-thread flag that indicates a thread
1639 // called jni_monitorenter() during its lifetime.
1640 //
1641 // Instead of No_Savepoint_Verifier it might be cheaper to
1642 // use an idiom of the form:
1643 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
1644 //   <code that must not run at safepoint>
1645 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1646 // Since the tests are extremely cheap we could leave them enabled
1647 // for normal product builds.
1648 
1649 void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) {
1650   assert(THREAD == JavaThread::current(), "must be current Java thread");
1651   No_Safepoint_Verifier nsv ;
1652   ReleaseJavaMonitorsClosure rjmc(THREAD);
1653   Thread::muxAcquire(&ListLock, "release_monitors_owned_by_thread");
1654   ObjectSynchronizer::monitors_iterate(&rjmc);
1655   Thread::muxRelease(&ListLock);
1656   THREAD->clear_pending_exception();
1657 }
1658 
1659 //------------------------------------------------------------------------------
1660 // Debugging code
1661 
1662 void ObjectSynchronizer::sanity_checks(const bool verbose,
1663                                        const uint cache_line_size,
1664                                        int *error_cnt_ptr,
1665                                        int *warning_cnt_ptr) {
1666   u_char *addr_begin      = (u_char*)&GVars;
1667   u_char *addr_stwRandom  = (u_char*)&GVars.stwRandom;
1668   u_char *addr_hcSequence = (u_char*)&GVars.hcSequence;
1669 
1670   if (verbose) {
1671     tty->print_cr("INFO: sizeof(SharedGlobals)=" SIZE_FORMAT,
1672                   sizeof(SharedGlobals));
1673   }
1674 
1675   uint offset_stwRandom = (uint)(addr_stwRandom - addr_begin);
1676   if (verbose) tty->print_cr("INFO: offset(stwRandom)=%u", offset_stwRandom);
1677 
1678   uint offset_hcSequence = (uint)(addr_hcSequence - addr_begin);
1679   if (verbose) {
1680     tty->print_cr("INFO: offset(_hcSequence)=%u", offset_hcSequence);
1681   }
1682 
1683   if (cache_line_size != 0) {
1684     // We were able to determine the L1 data cache line size so
1685     // do some cache line specific sanity checks
1686 
1687     if (offset_stwRandom < cache_line_size) {
1688       tty->print_cr("WARNING: the SharedGlobals.stwRandom field is closer "
1689                     "to the struct beginning than a cache line which permits "
1690                     "false sharing.");
1691       (*warning_cnt_ptr)++;
1692     }
1693 
1694     if ((offset_hcSequence - offset_stwRandom) < cache_line_size) {
1695       tty->print_cr("WARNING: the SharedGlobals.stwRandom and "
1696                     "SharedGlobals.hcSequence fields are closer than a cache "
1697                     "line which permits false sharing.");
1698       (*warning_cnt_ptr)++;
1699     }
1700 
1701     if ((sizeof(SharedGlobals) - offset_hcSequence) < cache_line_size) {
1702       tty->print_cr("WARNING: the SharedGlobals.hcSequence field is closer "
1703                     "to the struct end than a cache line which permits false "
1704                     "sharing.");
1705       (*warning_cnt_ptr)++;
1706     }
1707   }
1708 }
1709 
1710 #ifndef PRODUCT
1711 
1712 // Verify all monitors in the monitor cache, the verification is weak.
1713 void ObjectSynchronizer::verify() {
1714   ObjectMonitor* block = gBlockList;
1715   ObjectMonitor* mid;
1716   while (block) {
1717     assert(block->object() == CHAINMARKER, "must be a block header");
1718     for (int i = 1; i < _BLOCKSIZE; i++) {
1719       mid = block + i;
1720       oop object = (oop) mid->object();
1721       if (object != NULL) {
1722         mid->verify();
1723       }
1724     }
1725     block = (ObjectMonitor*) block->FreeNext;
1726   }
1727 }
1728 
1729 // Check if monitor belongs to the monitor cache
1730 // The list is grow-only so it's *relatively* safe to traverse
1731 // the list of extant blocks without taking a lock.
1732 
1733 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) {
1734   ObjectMonitor* block = gBlockList;
1735 
1736   while (block) {
1737     assert(block->object() == CHAINMARKER, "must be a block header");
1738     if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) {
1739       address mon = (address) monitor;
1740       address blk = (address) block;
1741       size_t diff = mon - blk;
1742       assert((diff % sizeof(ObjectMonitor)) == 0, "check");
1743       return 1;
1744     }
1745     block = (ObjectMonitor*) block->FreeNext;
1746   }
1747   return 0;
1748 }
1749 
1750 #endif