annotate src/share/vm/runtime/synchronizer.cpp @ 4834:74d14a44c398

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