240 // and last are the inflated Java Monitor (ObjectMonitor) checks.
241 lock->set_displaced_header(markWord::unused_mark());
242
243 if (owner == NULL && Atomic::replace_if_null(Self, &(m->_owner))) {
244 assert(m->_recursions == 0, "invariant");
245 return true;
246 }
247 }
248
249 // Note that we could inflate in quick_enter.
250 // This is likely a useful optimization
251 // Critically, in quick_enter() we must not:
252 // -- perform bias revocation, or
253 // -- block indefinitely, or
254 // -- reach a safepoint
255
256 return false; // revert to slow-path
257 }
258
259 // -----------------------------------------------------------------------------
260 // Fast Monitor Enter/Exit
261 // This the fast monitor enter. The interpreter and compiler use
262 // some assembly copies of this code. Make sure update those code
263 // if the following function is changed. The implementation is
264 // extremely sensitive to race condition. Be careful.
265
266 void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock,
267 bool attempt_rebias, TRAPS) {
268 if (UseBiasedLocking) {
269 if (!SafepointSynchronize::is_at_safepoint()) {
270 BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
271 if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
272 return;
273 }
274 } else {
275 assert(!attempt_rebias, "can not rebias toward VM thread");
276 BiasedLocking::revoke_at_safepoint(obj);
277 }
278 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
279 }
280
281 slow_enter(obj, lock, THREAD);
282 }
283
284 void ObjectSynchronizer::fast_exit(oop object, BasicLock* lock, TRAPS) {
285 markWord mark = object->mark();
286 // We cannot check for Biased Locking if we are racing an inflation.
287 assert(mark == markWord::INFLATING() ||
288 !mark.has_bias_pattern(), "should not see bias pattern here");
289
290 markWord dhw = lock->displaced_header();
291 if (dhw.value() == 0) {
292 // If the displaced header is NULL, then this exit matches up with
293 // a recursive enter. No real work to do here except for diagnostics.
294 #ifndef PRODUCT
295 if (mark != markWord::INFLATING()) {
296 // Only do diagnostics if we are not racing an inflation. Simply
297 // exiting a recursive enter of a Java Monitor that is being
298 // inflated is safe; see the has_monitor() comment below.
299 assert(!mark.is_neutral(), "invariant");
300 assert(!mark.has_locker() ||
301 THREAD->is_lock_owned((address)mark.locker()), "invariant");
302 if (mark.has_monitor()) {
303 // The BasicLock's displaced_header is marked as a recursive
304 // enter and we have an inflated Java Monitor (ObjectMonitor).
314 }
315 }
316 #endif
317 return;
318 }
319
320 if (mark == markWord::from_pointer(lock)) {
321 // If the object is stack-locked by the current thread, try to
322 // swing the displaced header from the BasicLock back to the mark.
323 assert(dhw.is_neutral(), "invariant");
324 if (object->cas_set_mark(dhw, mark) == mark) {
325 return;
326 }
327 }
328
329 // We have to take the slow-path of possible inflation and then exit.
330 inflate(THREAD, object, inflate_cause_vm_internal)->exit(true, THREAD);
331 }
332
333 // -----------------------------------------------------------------------------
334 // Interpreter/Compiler Slow Case
335 // This routine is used to handle interpreter/compiler slow case
336 // We don't need to use fast path here, because it must have been
337 // failed in the interpreter/compiler code.
338 void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
339 markWord mark = obj->mark();
340 assert(!mark.has_bias_pattern(), "should not see bias pattern here");
341
342 if (mark.is_neutral()) {
343 // Anticipate successful CAS -- the ST of the displaced mark must
344 // be visible <= the ST performed by the CAS.
345 lock->set_displaced_header(mark);
346 if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) {
347 return;
348 }
349 // Fall through to inflate() ...
350 } else if (mark.has_locker() &&
351 THREAD->is_lock_owned((address)mark.locker())) {
352 assert(lock != mark.locker(), "must not re-lock the same lock");
353 assert(lock != (BasicLock*)obj->mark().value(), "don't relock with same BasicLock");
354 lock->set_displaced_header(markWord::from_pointer(NULL));
355 return;
356 }
357
358 // The object header will never be displaced to this lock,
359 // so it does not matter what the value is, except that it
360 // must be non-zero to avoid looking like a re-entrant lock,
361 // and must not look locked either.
362 lock->set_displaced_header(markWord::unused_mark());
363 inflate(THREAD, obj(), inflate_cause_monitor_enter)->enter(THREAD);
364 }
365
366 // This routine is used to handle interpreter/compiler slow case
367 // We don't need to use fast path here, because it must have
368 // failed in the interpreter/compiler code. Simply use the heavy
369 // weight monitor should be ok, unless someone find otherwise.
370 void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) {
371 fast_exit(object, lock, THREAD);
372 }
373
374 // -----------------------------------------------------------------------------
375 // Class Loader support to workaround deadlocks on the class loader lock objects
376 // Also used by GC
377 // complete_exit()/reenter() are used to wait on a nested lock
378 // i.e. to give up an outer lock completely and then re-enter
379 // Used when holding nested locks - lock acquisition order: lock1 then lock2
380 // 1) complete_exit lock1 - saving recursion count
381 // 2) wait on lock2
382 // 3) when notified on lock2, unlock lock2
383 // 4) reenter lock1 with original recursion count
384 // 5) lock lock2
385 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
386 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
387 if (UseBiasedLocking) {
388 BiasedLocking::revoke_and_rebias(obj, false, THREAD);
389 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
390 }
391
392 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
393
394 return monitor->complete_exit(THREAD);
395 }
396
397 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
398 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
399 if (UseBiasedLocking) {
400 BiasedLocking::revoke_and_rebias(obj, false, THREAD);
401 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
402 }
403
404 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
405
406 monitor->reenter(recursion, THREAD);
407 }
408 // -----------------------------------------------------------------------------
409 // JNI locks on java objects
410 // NOTE: must use heavy weight monitor to handle jni monitor enter
411 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) {
412 // the current locking is from JNI instead of Java code
413 if (UseBiasedLocking) {
414 BiasedLocking::revoke_and_rebias(obj, false, THREAD);
415 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
416 }
417 THREAD->set_current_pending_monitor_is_from_java(false);
418 inflate(THREAD, obj(), inflate_cause_jni_enter)->enter(THREAD);
419 THREAD->set_current_pending_monitor_is_from_java(true);
420 }
421
422 // NOTE: must use heavy weight monitor to handle jni monitor exit
423 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
424 if (UseBiasedLocking) {
425 Handle h_obj(THREAD, obj);
426 BiasedLocking::revoke_and_rebias(h_obj, false, THREAD);
427 obj = h_obj();
428 }
429 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
430
431 ObjectMonitor* monitor = inflate(THREAD, obj, inflate_cause_jni_exit);
432 // If this thread has locked the object, exit the monitor. We
433 // intentionally do not use CHECK here because we must exit the
434 // monitor even if an exception is pending.
435 if (monitor->check_owner(THREAD)) {
436 monitor->exit(true, THREAD);
437 }
438 }
439
440 // -----------------------------------------------------------------------------
441 // Internal VM locks on java objects
442 // standard constructor, allows locking failures
443 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) {
444 _dolock = doLock;
445 _thread = thread;
446 _thread->check_for_valid_safepoint_state(false);
447 _obj = obj;
448
449 if (_dolock) {
450 ObjectSynchronizer::fast_enter(_obj, &_lock, false, _thread);
451 }
452 }
453
454 ObjectLocker::~ObjectLocker() {
455 if (_dolock) {
456 ObjectSynchronizer::fast_exit(_obj(), &_lock, _thread);
457 }
458 }
459
460
461 // -----------------------------------------------------------------------------
462 // Wait/Notify/NotifyAll
463 // NOTE: must use heavy weight monitor to handle wait()
464 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
465 if (UseBiasedLocking) {
466 BiasedLocking::revoke_and_rebias(obj, false, THREAD);
467 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
468 }
469 if (millis < 0) {
470 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
471 }
472 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_wait);
473
474 DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
475 monitor->wait(millis, true, THREAD);
476
477 // This dummy call is in place to get around dtrace bug 6254741. Once
478 // that's fixed we can uncomment the following line, remove the call
479 // and change this function back into a "void" func.
480 // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
481 return dtrace_waited_probe(monitor, obj, THREAD);
482 }
483
484 void ObjectSynchronizer::waitUninterruptibly(Handle obj, jlong millis, TRAPS) {
485 if (UseBiasedLocking) {
486 BiasedLocking::revoke_and_rebias(obj, false, THREAD);
487 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
488 }
489 if (millis < 0) {
490 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
491 }
492 inflate(THREAD, obj(), inflate_cause_wait)->wait(millis, false, THREAD);
493 }
494
495 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
496 if (UseBiasedLocking) {
497 BiasedLocking::revoke_and_rebias(obj, false, THREAD);
498 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
499 }
500
501 markWord mark = obj->mark();
502 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
503 return;
504 }
505 inflate(THREAD, obj(), inflate_cause_notify)->notify(THREAD);
506 }
507
508 // NOTE: see comment of notify()
509 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
510 if (UseBiasedLocking) {
511 BiasedLocking::revoke_and_rebias(obj, false, THREAD);
512 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
513 }
514
515 markWord mark = obj->mark();
516 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
517 return;
518 }
519 inflate(THREAD, obj(), inflate_cause_notify)->notifyAll(THREAD);
520 }
521
522 // -----------------------------------------------------------------------------
523 // Hash Code handling
524 //
525 // Performance concern:
526 // OrderAccess::storestore() calls release() which at one time stored 0
527 // into the global volatile OrderAccess::dummy variable. This store was
528 // unnecessary for correctness. Many threads storing into a common location
529 // causes considerable cache migration or "sloshing" on large SMP systems.
530 // As such, I avoided using OrderAccess::storestore(). In some cases
531 // OrderAccess::fence() -- which incurs local latency on the executing
678 assert(value != markWord::no_hash, "invariant");
679 return value;
680 }
681
682 intptr_t ObjectSynchronizer::FastHashCode(Thread * Self, oop obj) {
683 if (UseBiasedLocking) {
684 // NOTE: many places throughout the JVM do not expect a safepoint
685 // to be taken here, in particular most operations on perm gen
686 // objects. However, we only ever bias Java instances and all of
687 // the call sites of identity_hash that might revoke biases have
688 // been checked to make sure they can handle a safepoint. The
689 // added check of the bias pattern is to avoid useless calls to
690 // thread-local storage.
691 if (obj->mark().has_bias_pattern()) {
692 // Handle for oop obj in case of STW safepoint
693 Handle hobj(Self, obj);
694 // Relaxing assertion for bug 6320749.
695 assert(Universe::verify_in_progress() ||
696 !SafepointSynchronize::is_at_safepoint(),
697 "biases should not be seen by VM thread here");
698 BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
699 obj = hobj();
700 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
701 }
702 }
703
704 // hashCode() is a heap mutator ...
705 // Relaxing assertion for bug 6320749.
706 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
707 !SafepointSynchronize::is_at_safepoint(), "invariant");
708 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
709 Self->is_Java_thread() , "invariant");
710 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
711 ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
712
713 ObjectMonitor* monitor = NULL;
714 markWord temp, test;
715 intptr_t hash;
716 markWord mark = ReadStableMark(obj);
717
718 // object should remain ineligible for biased locking
777 // of the header/dmw field, please update this code.
778 hash = test.hash();
779 assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
780 assert(hash != 0, "Trivial unexpected object/monitor header usage.");
781 }
782 }
783 // We finally get the hash
784 return hash;
785 }
786
787 // Deprecated -- use FastHashCode() instead.
788
789 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
790 return FastHashCode(Thread::current(), obj());
791 }
792
793
794 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
795 Handle h_obj) {
796 if (UseBiasedLocking) {
797 BiasedLocking::revoke_and_rebias(h_obj, false, thread);
798 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
799 }
800
801 assert(thread == JavaThread::current(), "Can only be called on current thread");
802 oop obj = h_obj();
803
804 markWord mark = ReadStableMark(obj);
805
806 // Uncontended case, header points to stack
807 if (mark.has_locker()) {
808 return thread->is_lock_owned((address)mark.locker());
809 }
810 // Contended case, header points to ObjectMonitor (tagged pointer)
811 if (mark.has_monitor()) {
812 ObjectMonitor* monitor = mark.monitor();
813 return monitor->is_entered(thread) != 0;
814 }
815 // Unlocked case, header in place
816 assert(mark.is_neutral(), "sanity check");
817 return false;
818 }
819
820 // Be aware of this method could revoke bias of the lock object.
821 // This method queries the ownership of the lock handle specified by 'h_obj'.
822 // If the current thread owns the lock, it returns owner_self. If no
823 // thread owns the lock, it returns owner_none. Otherwise, it will return
824 // owner_other.
825 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
826 (JavaThread *self, Handle h_obj) {
827 // The caller must beware this method can revoke bias, and
828 // revocation can result in a safepoint.
829 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
830 assert(self->thread_state() != _thread_blocked, "invariant");
831
832 // Possible mark states: neutral, biased, stack-locked, inflated
833
834 if (UseBiasedLocking && h_obj()->mark().has_bias_pattern()) {
835 // CASE: biased
836 BiasedLocking::revoke_and_rebias(h_obj, false, self);
837 assert(!h_obj->mark().has_bias_pattern(),
838 "biases should be revoked by now");
839 }
840
841 assert(self == JavaThread::current(), "Can only be called on current thread");
842 oop obj = h_obj();
843 markWord mark = ReadStableMark(obj);
844
845 // CASE: stack-locked. Mark points to a BasicLock on the owner's stack.
846 if (mark.has_locker()) {
847 return self->is_lock_owned((address)mark.locker()) ?
848 owner_self : owner_other;
849 }
850
851 // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor.
852 // The Object:ObjectMonitor relationship is stable as long as we're
853 // not at a safepoint.
854 if (mark.has_monitor()) {
855 void * owner = mark.monitor()->_owner;
856 if (owner == NULL) return owner_none;
857 return (owner == self ||
858 self->is_lock_owned((address)owner)) ? owner_self : owner_other;
859 }
860
861 // CASE: neutral
862 assert(mark.is_neutral(), "sanity check");
863 return owner_none; // it's unlocked
864 }
865
866 // FIXME: jvmti should call this
867 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
868 if (UseBiasedLocking) {
869 if (SafepointSynchronize::is_at_safepoint()) {
870 BiasedLocking::revoke_at_safepoint(h_obj);
871 } else {
872 BiasedLocking::revoke_and_rebias(h_obj, false, JavaThread::current());
873 }
874 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
875 }
876
877 oop obj = h_obj();
878 address owner = NULL;
879
880 markWord mark = ReadStableMark(obj);
881
882 // Uncontended case, header points to stack
883 if (mark.has_locker()) {
884 owner = (address) mark.locker();
885 }
886
887 // Contended case, header points to ObjectMonitor (tagged pointer)
888 else if (mark.has_monitor()) {
889 ObjectMonitor* monitor = mark.monitor();
890 assert(monitor != NULL, "monitor should be non-null");
891 owner = (address) monitor->owner();
892 }
1446 OM_PERFDATA_OP(Inflations, inc());
1447 if (log_is_enabled(Trace, monitorinflation)) {
1448 ResourceMark rm(Self);
1449 lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1450 INTPTR_FORMAT ", type='%s'", p2i(object),
1451 object->mark().value(), object->klass()->external_name());
1452 }
1453 if (event.should_commit()) {
1454 post_monitor_inflate_event(&event, object, cause);
1455 }
1456 return m;
1457 }
1458
1459 // CASE: neutral
1460 // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1461 // If we know we're inflating for entry it's better to inflate by swinging a
1462 // pre-locked objectMonitor pointer into the object header. A successful
1463 // CAS inflates the object *and* confers ownership to the inflating thread.
1464 // In the current implementation we use a 2-step mechanism where we CAS()
1465 // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1466 // An inflateTry() method that we could call from fast_enter() and slow_enter()
1467 // would be useful.
1468
1469 // Catch if the object's header is not neutral (not locked and
1470 // not marked is what we care about here).
1471 assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1472 ObjectMonitor * m = omAlloc(Self);
1473 // prepare m for installation - set monitor to initial state
1474 m->Recycle();
1475 m->set_header(mark);
1476 m->set_object(object);
1477 m->_Responsible = NULL;
1478 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // consider: keep metastats by type/class
1479
1480 if (object->cas_set_mark(markWord::encode(m), mark) != mark) {
1481 m->set_header(markWord::zero());
1482 m->set_object(NULL);
1483 m->Recycle();
1484 omRelease(Self, m, true);
1485 m = NULL;
1486 continue;
1487 // interference - the markword changed - just retry.
|
240 // and last are the inflated Java Monitor (ObjectMonitor) checks.
241 lock->set_displaced_header(markWord::unused_mark());
242
243 if (owner == NULL && Atomic::replace_if_null(Self, &(m->_owner))) {
244 assert(m->_recursions == 0, "invariant");
245 return true;
246 }
247 }
248
249 // Note that we could inflate in quick_enter.
250 // This is likely a useful optimization
251 // Critically, in quick_enter() we must not:
252 // -- perform bias revocation, or
253 // -- block indefinitely, or
254 // -- reach a safepoint
255
256 return false; // revert to slow-path
257 }
258
259 // -----------------------------------------------------------------------------
260 // Monitor Enter/Exit
261 // The interpreter and compiler use some assembly copies of this code. Make sure
262 // update those code if the following function is changed. The implementation
263 // is extremely sensitive to race condition. Be careful.
264
265 void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, TRAPS) {
266 if (UseBiasedLocking) {
267 if (!SafepointSynchronize::is_at_safepoint()) {
268 BiasedLocking::revoke(obj, THREAD);
269 } else {
270 BiasedLocking::revoke_at_safepoint(obj);
271 }
272 }
273
274 markWord mark = obj->mark();
275 assert(!mark.has_bias_pattern(), "should not see bias pattern here");
276
277 if (mark.is_neutral()) {
278 // Anticipate successful CAS -- the ST of the displaced mark must
279 // be visible <= the ST performed by the CAS.
280 lock->set_displaced_header(mark);
281 if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) {
282 return;
283 }
284 // Fall through to inflate() ...
285 } else if (mark.has_locker() &&
286 THREAD->is_lock_owned((address)mark.locker())) {
287 assert(lock != mark.locker(), "must not re-lock the same lock");
288 assert(lock != (BasicLock*)obj->mark().value(), "don't relock with same BasicLock");
289 lock->set_displaced_header(markWord::from_pointer(NULL));
290 return;
291 }
292
293 // The object header will never be displaced to this lock,
294 // so it does not matter what the value is, except that it
295 // must be non-zero to avoid looking like a re-entrant lock,
296 // and must not look locked either.
297 lock->set_displaced_header(markWord::unused_mark());
298 inflate(THREAD, obj(), inflate_cause_monitor_enter)->enter(THREAD);
299 }
300
301 void ObjectSynchronizer::exit(oop object, BasicLock* lock, TRAPS) {
302 markWord mark = object->mark();
303 // We cannot check for Biased Locking if we are racing an inflation.
304 assert(mark == markWord::INFLATING() ||
305 !mark.has_bias_pattern(), "should not see bias pattern here");
306
307 markWord dhw = lock->displaced_header();
308 if (dhw.value() == 0) {
309 // If the displaced header is NULL, then this exit matches up with
310 // a recursive enter. No real work to do here except for diagnostics.
311 #ifndef PRODUCT
312 if (mark != markWord::INFLATING()) {
313 // Only do diagnostics if we are not racing an inflation. Simply
314 // exiting a recursive enter of a Java Monitor that is being
315 // inflated is safe; see the has_monitor() comment below.
316 assert(!mark.is_neutral(), "invariant");
317 assert(!mark.has_locker() ||
318 THREAD->is_lock_owned((address)mark.locker()), "invariant");
319 if (mark.has_monitor()) {
320 // The BasicLock's displaced_header is marked as a recursive
321 // enter and we have an inflated Java Monitor (ObjectMonitor).
331 }
332 }
333 #endif
334 return;
335 }
336
337 if (mark == markWord::from_pointer(lock)) {
338 // If the object is stack-locked by the current thread, try to
339 // swing the displaced header from the BasicLock back to the mark.
340 assert(dhw.is_neutral(), "invariant");
341 if (object->cas_set_mark(dhw, mark) == mark) {
342 return;
343 }
344 }
345
346 // We have to take the slow-path of possible inflation and then exit.
347 inflate(THREAD, object, inflate_cause_vm_internal)->exit(true, THREAD);
348 }
349
350 // -----------------------------------------------------------------------------
351 // Class Loader support to workaround deadlocks on the class loader lock objects
352 // Also used by GC
353 // complete_exit()/reenter() are used to wait on a nested lock
354 // i.e. to give up an outer lock completely and then re-enter
355 // Used when holding nested locks - lock acquisition order: lock1 then lock2
356 // 1) complete_exit lock1 - saving recursion count
357 // 2) wait on lock2
358 // 3) when notified on lock2, unlock lock2
359 // 4) reenter lock1 with original recursion count
360 // 5) lock lock2
361 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
362 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
363 if (UseBiasedLocking) {
364 BiasedLocking::revoke(obj, THREAD);
365 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
366 }
367
368 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
369
370 return monitor->complete_exit(THREAD);
371 }
372
373 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
374 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
375 if (UseBiasedLocking) {
376 BiasedLocking::revoke(obj, THREAD);
377 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
378 }
379
380 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
381
382 monitor->reenter(recursion, THREAD);
383 }
384 // -----------------------------------------------------------------------------
385 // JNI locks on java objects
386 // NOTE: must use heavy weight monitor to handle jni monitor enter
387 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) {
388 // the current locking is from JNI instead of Java code
389 if (UseBiasedLocking) {
390 BiasedLocking::revoke(obj, THREAD);
391 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
392 }
393 THREAD->set_current_pending_monitor_is_from_java(false);
394 inflate(THREAD, obj(), inflate_cause_jni_enter)->enter(THREAD);
395 THREAD->set_current_pending_monitor_is_from_java(true);
396 }
397
398 // NOTE: must use heavy weight monitor to handle jni monitor exit
399 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
400 if (UseBiasedLocking) {
401 Handle h_obj(THREAD, obj);
402 BiasedLocking::revoke(h_obj, THREAD);
403 obj = h_obj();
404 }
405 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
406
407 ObjectMonitor* monitor = inflate(THREAD, obj, inflate_cause_jni_exit);
408 // If this thread has locked the object, exit the monitor. We
409 // intentionally do not use CHECK here because we must exit the
410 // monitor even if an exception is pending.
411 if (monitor->check_owner(THREAD)) {
412 monitor->exit(true, THREAD);
413 }
414 }
415
416 // -----------------------------------------------------------------------------
417 // Internal VM locks on java objects
418 // standard constructor, allows locking failures
419 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) {
420 _dolock = doLock;
421 _thread = thread;
422 _thread->check_for_valid_safepoint_state(false);
423 _obj = obj;
424
425 if (_dolock) {
426 ObjectSynchronizer::enter(_obj, &_lock, _thread);
427 }
428 }
429
430 ObjectLocker::~ObjectLocker() {
431 if (_dolock) {
432 ObjectSynchronizer::exit(_obj(), &_lock, _thread);
433 }
434 }
435
436
437 // -----------------------------------------------------------------------------
438 // Wait/Notify/NotifyAll
439 // NOTE: must use heavy weight monitor to handle wait()
440 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
441 if (UseBiasedLocking) {
442 BiasedLocking::revoke(obj, THREAD);
443 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
444 }
445 if (millis < 0) {
446 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
447 }
448 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_wait);
449
450 DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
451 monitor->wait(millis, true, THREAD);
452
453 // This dummy call is in place to get around dtrace bug 6254741. Once
454 // that's fixed we can uncomment the following line, remove the call
455 // and change this function back into a "void" func.
456 // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
457 return dtrace_waited_probe(monitor, obj, THREAD);
458 }
459
460 void ObjectSynchronizer::waitUninterruptibly(Handle obj, jlong millis, TRAPS) {
461 if (UseBiasedLocking) {
462 BiasedLocking::revoke(obj, THREAD);
463 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
464 }
465 if (millis < 0) {
466 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
467 }
468 inflate(THREAD, obj(), inflate_cause_wait)->wait(millis, false, THREAD);
469 }
470
471 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
472 if (UseBiasedLocking) {
473 BiasedLocking::revoke(obj, THREAD);
474 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
475 }
476
477 markWord mark = obj->mark();
478 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
479 return;
480 }
481 inflate(THREAD, obj(), inflate_cause_notify)->notify(THREAD);
482 }
483
484 // NOTE: see comment of notify()
485 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
486 if (UseBiasedLocking) {
487 BiasedLocking::revoke(obj, THREAD);
488 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
489 }
490
491 markWord mark = obj->mark();
492 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
493 return;
494 }
495 inflate(THREAD, obj(), inflate_cause_notify)->notifyAll(THREAD);
496 }
497
498 // -----------------------------------------------------------------------------
499 // Hash Code handling
500 //
501 // Performance concern:
502 // OrderAccess::storestore() calls release() which at one time stored 0
503 // into the global volatile OrderAccess::dummy variable. This store was
504 // unnecessary for correctness. Many threads storing into a common location
505 // causes considerable cache migration or "sloshing" on large SMP systems.
506 // As such, I avoided using OrderAccess::storestore(). In some cases
507 // OrderAccess::fence() -- which incurs local latency on the executing
654 assert(value != markWord::no_hash, "invariant");
655 return value;
656 }
657
658 intptr_t ObjectSynchronizer::FastHashCode(Thread * Self, oop obj) {
659 if (UseBiasedLocking) {
660 // NOTE: many places throughout the JVM do not expect a safepoint
661 // to be taken here, in particular most operations on perm gen
662 // objects. However, we only ever bias Java instances and all of
663 // the call sites of identity_hash that might revoke biases have
664 // been checked to make sure they can handle a safepoint. The
665 // added check of the bias pattern is to avoid useless calls to
666 // thread-local storage.
667 if (obj->mark().has_bias_pattern()) {
668 // Handle for oop obj in case of STW safepoint
669 Handle hobj(Self, obj);
670 // Relaxing assertion for bug 6320749.
671 assert(Universe::verify_in_progress() ||
672 !SafepointSynchronize::is_at_safepoint(),
673 "biases should not be seen by VM thread here");
674 BiasedLocking::revoke(hobj, JavaThread::current());
675 obj = hobj();
676 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
677 }
678 }
679
680 // hashCode() is a heap mutator ...
681 // Relaxing assertion for bug 6320749.
682 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
683 !SafepointSynchronize::is_at_safepoint(), "invariant");
684 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
685 Self->is_Java_thread() , "invariant");
686 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
687 ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
688
689 ObjectMonitor* monitor = NULL;
690 markWord temp, test;
691 intptr_t hash;
692 markWord mark = ReadStableMark(obj);
693
694 // object should remain ineligible for biased locking
753 // of the header/dmw field, please update this code.
754 hash = test.hash();
755 assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
756 assert(hash != 0, "Trivial unexpected object/monitor header usage.");
757 }
758 }
759 // We finally get the hash
760 return hash;
761 }
762
763 // Deprecated -- use FastHashCode() instead.
764
765 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
766 return FastHashCode(Thread::current(), obj());
767 }
768
769
770 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
771 Handle h_obj) {
772 if (UseBiasedLocking) {
773 BiasedLocking::revoke(h_obj, thread);
774 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
775 }
776
777 assert(thread == JavaThread::current(), "Can only be called on current thread");
778 oop obj = h_obj();
779
780 markWord mark = ReadStableMark(obj);
781
782 // Uncontended case, header points to stack
783 if (mark.has_locker()) {
784 return thread->is_lock_owned((address)mark.locker());
785 }
786 // Contended case, header points to ObjectMonitor (tagged pointer)
787 if (mark.has_monitor()) {
788 ObjectMonitor* monitor = mark.monitor();
789 return monitor->is_entered(thread) != 0;
790 }
791 // Unlocked case, header in place
792 assert(mark.is_neutral(), "sanity check");
793 return false;
794 }
795
796 // Be aware of this method could revoke bias of the lock object.
797 // This method queries the ownership of the lock handle specified by 'h_obj'.
798 // If the current thread owns the lock, it returns owner_self. If no
799 // thread owns the lock, it returns owner_none. Otherwise, it will return
800 // owner_other.
801 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
802 (JavaThread *self, Handle h_obj) {
803 // The caller must beware this method can revoke bias, and
804 // revocation can result in a safepoint.
805 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
806 assert(self->thread_state() != _thread_blocked, "invariant");
807
808 // Possible mark states: neutral, biased, stack-locked, inflated
809
810 if (UseBiasedLocking && h_obj()->mark().has_bias_pattern()) {
811 // CASE: biased
812 BiasedLocking::revoke(h_obj, self);
813 assert(!h_obj->mark().has_bias_pattern(),
814 "biases should be revoked by now");
815 }
816
817 assert(self == JavaThread::current(), "Can only be called on current thread");
818 oop obj = h_obj();
819 markWord mark = ReadStableMark(obj);
820
821 // CASE: stack-locked. Mark points to a BasicLock on the owner's stack.
822 if (mark.has_locker()) {
823 return self->is_lock_owned((address)mark.locker()) ?
824 owner_self : owner_other;
825 }
826
827 // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor.
828 // The Object:ObjectMonitor relationship is stable as long as we're
829 // not at a safepoint.
830 if (mark.has_monitor()) {
831 void * owner = mark.monitor()->_owner;
832 if (owner == NULL) return owner_none;
833 return (owner == self ||
834 self->is_lock_owned((address)owner)) ? owner_self : owner_other;
835 }
836
837 // CASE: neutral
838 assert(mark.is_neutral(), "sanity check");
839 return owner_none; // it's unlocked
840 }
841
842 // FIXME: jvmti should call this
843 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
844 if (UseBiasedLocking) {
845 if (SafepointSynchronize::is_at_safepoint()) {
846 BiasedLocking::revoke_at_safepoint(h_obj);
847 } else {
848 BiasedLocking::revoke(h_obj, JavaThread::current());
849 }
850 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
851 }
852
853 oop obj = h_obj();
854 address owner = NULL;
855
856 markWord mark = ReadStableMark(obj);
857
858 // Uncontended case, header points to stack
859 if (mark.has_locker()) {
860 owner = (address) mark.locker();
861 }
862
863 // Contended case, header points to ObjectMonitor (tagged pointer)
864 else if (mark.has_monitor()) {
865 ObjectMonitor* monitor = mark.monitor();
866 assert(monitor != NULL, "monitor should be non-null");
867 owner = (address) monitor->owner();
868 }
1422 OM_PERFDATA_OP(Inflations, inc());
1423 if (log_is_enabled(Trace, monitorinflation)) {
1424 ResourceMark rm(Self);
1425 lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1426 INTPTR_FORMAT ", type='%s'", p2i(object),
1427 object->mark().value(), object->klass()->external_name());
1428 }
1429 if (event.should_commit()) {
1430 post_monitor_inflate_event(&event, object, cause);
1431 }
1432 return m;
1433 }
1434
1435 // CASE: neutral
1436 // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1437 // If we know we're inflating for entry it's better to inflate by swinging a
1438 // pre-locked objectMonitor pointer into the object header. A successful
1439 // CAS inflates the object *and* confers ownership to the inflating thread.
1440 // In the current implementation we use a 2-step mechanism where we CAS()
1441 // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1442 // An inflateTry() method that we could call from enter() would be useful.
1443
1444 // Catch if the object's header is not neutral (not locked and
1445 // not marked is what we care about here).
1446 assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1447 ObjectMonitor * m = omAlloc(Self);
1448 // prepare m for installation - set monitor to initial state
1449 m->Recycle();
1450 m->set_header(mark);
1451 m->set_object(object);
1452 m->_Responsible = NULL;
1453 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // consider: keep metastats by type/class
1454
1455 if (object->cas_set_mark(markWord::encode(m), mark) != mark) {
1456 m->set_header(markWord::zero());
1457 m->set_object(NULL);
1458 m->Recycle();
1459 omRelease(Self, m, true);
1460 m = NULL;
1461 continue;
1462 // interference - the markword changed - just retry.
|