src/share/vm/runtime/synchronizer.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

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

mercurial