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