273 // transitions. The following spin is strictly optional ...
274 // Note that if we acquire the monitor from an initial spin
275 // we forgo posting JVMTI events and firing DTRACE probes.
276 if (TrySpin(Self) > 0) {
277 assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
278 assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
279 assert(((oop)object())->mark() == markWord::encode(this),
280 "object mark must match encoded this: mark=" INTPTR_FORMAT
281 ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
282 markWord::encode(this).value());
283 Self->_Stalled = 0;
284 return true;
285 }
286
287 assert(_owner != Self, "invariant");
288 assert(_succ != Self, "invariant");
289 assert(Self->is_Java_thread(), "invariant");
290 JavaThread * jt = (JavaThread *) Self;
291 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
292 assert(jt->thread_state() != _thread_blocked, "invariant");
293 assert(AsyncDeflateIdleMonitors || this->object() != NULL, "invariant");
294 assert(AsyncDeflateIdleMonitors || contentions() >= 0, "must not be negative: contentions=%d", contentions());
295
296 // Keep track of contention for JVM/TI and M&M queries.
297 add_to_contentions(1);
298 if (is_being_async_deflated()) {
299 // Async deflation is in progress and our contentions increment
300 // above lost the race to async deflation. Undo the work and
301 // force the caller to retry.
302 const oop l_object = (oop)object();
303 if (l_object != NULL) {
304 // Attempt to restore the header/dmw to the object's header so that
305 // we only retry once if the deflater thread happens to be slow.
306 install_displaced_markword_in_object(l_object);
307 }
308 Self->_Stalled = 0;
309 add_to_contentions(-1);
310 return false;
311 }
312
313 JFR_ONLY(JfrConditionalFlushWithStacktrace<EventJavaMonitorEnter> flush(jt);)
314 EventJavaMonitorEnter event;
438 // the global free list to a per-thread free list.
439
440 guarantee(obj != NULL, "must be non-NULL");
441
442 // Separate loads in is_being_async_deflated(), which is almost always
443 // called before this function, from the load of dmw/header below.
444 if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
445 // A non-multiple copy atomic (nMCA) machine needs a bigger
446 // hammer to separate the loads before and the load below.
447 OrderAccess::fence();
448 } else {
449 OrderAccess::loadload();
450 }
451
452 const oop l_object = (oop)object();
453 if (l_object == NULL) {
454 // ObjectMonitor's object ref has already been cleared by async
455 // deflation so we're done here.
456 return;
457 }
458 ADIM_guarantee(l_object == obj, "object=" INTPTR_FORMAT " must equal obj="
459 INTPTR_FORMAT, p2i(l_object), p2i(obj));
460
461 markWord dmw = header();
462 // The dmw has to be neutral (not NULL, not locked and not marked).
463 ADIM_guarantee(dmw.is_neutral(), "must be neutral: dmw=" INTPTR_FORMAT, dmw.value());
464
465 // Install displaced mark word if the object's header still points
466 // to this ObjectMonitor. More than one racing caller to this function
467 // can rarely reach this point, but only one can win.
468 markWord res = obj->cas_set_mark(dmw, markWord::encode(this));
469 if (res != markWord::encode(this)) {
470 // This should be rare so log at the Info level when it happens.
471 log_info(monitorinflation)("install_displaced_markword_in_object: "
472 "failed cas_set_mark: new_mark=" INTPTR_FORMAT
473 ", old_mark=" INTPTR_FORMAT ", res=" INTPTR_FORMAT,
474 dmw.value(), markWord::encode(this).value(),
475 res.value());
476 }
477
478 // Note: It does not matter which thread restored the header/dmw
479 // into the object's header. The thread deflating the monitor just
480 // wanted the object's header restored and it is. The threads that
481 // detected a race with the deflation process also wanted the
482 // object's header restored before they retry their operation and
483 // because it is restored they will only retry once.
484 }
485
486 // Convert the fields used by is_busy() to a string that can be
487 // used for diagnostic output.
488 const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
489 ss->print("is_busy: waiters=%d, ", _waiters);
490 if (!AsyncDeflateIdleMonitors) {
491 ss->print("contentions=%d, ", contentions());
492 ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
493 } else {
494 if (contentions() > 0) {
495 ss->print("contentions=%d, ", contentions());
496 } else {
497 ss->print("contentions=0");
498 }
499 if (_owner != DEFLATER_MARKER) {
500 ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
501 } else {
502 // We report NULL instead of DEFLATER_MARKER here because is_busy()
503 // ignores DEFLATER_MARKER values.
504 ss->print("owner=" INTPTR_FORMAT, NULL);
505 }
506 }
507 ss->print(", cxq=" INTPTR_FORMAT ", EntryList=" INTPTR_FORMAT, p2i(_cxq),
508 p2i(_EntryList));
509 return ss->base();
510 }
511
512 #define MAX_RECHECK_INTERVAL 1000
513
514 void ObjectMonitor::EnterI(TRAPS) {
515 Thread * const Self = THREAD;
516 assert(Self->is_Java_thread(), "invariant");
517 assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
518
519 // Try the lock - TATAS
520 if (TryLock (Self) > 0) {
521 assert(_succ != Self, "invariant");
522 assert(_owner == Self, "invariant");
523 assert(_Responsible != Self, "invariant");
524 return;
525 }
526
527 if (AsyncDeflateIdleMonitors &&
528 try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
529 // Cancelled the in-progress async deflation by changing owner from
530 // DEFLATER_MARKER to Self. As part of the contended enter protocol,
531 // contentions was incremented to a positive value before EnterI()
532 // was called and that prevents the deflater thread from winning the
533 // last part of the 2-part async deflation protocol. After EnterI()
534 // returns to enter(), contentions is decremented because the caller
535 // now owns the monitor. We bump contentions an extra time here to
536 // prevent the deflater thread from winning the last part of the
537 // 2-part async deflation protocol after the regular decrement
538 // occurs in enter(). The deflater thread will decrement contentions
539 // after it recognizes that the async deflation was cancelled.
540 add_to_contentions(1);
541 assert(_succ != Self, "invariant");
542 assert(_Responsible != Self, "invariant");
543 return;
544 }
545
546 assert(InitDone, "Unexpectedly not initialized");
547
548 // We try one round of spinning *before* enqueueing Self.
642
643 for (;;) {
644
645 if (TryLock(Self) > 0) break;
646 assert(_owner != Self, "invariant");
647
648 // park self
649 if (_Responsible == Self) {
650 Self->_ParkEvent->park((jlong) recheckInterval);
651 // Increase the recheckInterval, but clamp the value.
652 recheckInterval *= 8;
653 if (recheckInterval > MAX_RECHECK_INTERVAL) {
654 recheckInterval = MAX_RECHECK_INTERVAL;
655 }
656 } else {
657 Self->_ParkEvent->park();
658 }
659
660 if (TryLock(Self) > 0) break;
661
662 if (AsyncDeflateIdleMonitors &&
663 try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
664 // Cancelled the in-progress async deflation by changing owner from
665 // DEFLATER_MARKER to Self. As part of the contended enter protocol,
666 // contentions was incremented to a positive value before EnterI()
667 // was called and that prevents the deflater thread from winning the
668 // last part of the 2-part async deflation protocol. After EnterI()
669 // returns to enter(), contentions is decremented because the caller
670 // now owns the monitor. We bump contentions an extra time here to
671 // prevent the deflater thread from winning the last part of the
672 // 2-part async deflation protocol after the regular decrement
673 // occurs in enter(). The deflater thread will decrement contentions
674 // after it recognizes that the async deflation was cancelled.
675 add_to_contentions(1);
676 break;
677 }
678
679 // The lock is still contested.
680 // Keep a tally of the # of futile wakeups.
681 // Note that the counter is not protected by a lock or updated by atomics.
682 // That is by design - we trade "lossy" counters which are exposed to
683 // races during updates for a lower probe effect.
990 if (THREAD->is_lock_owned((address)cur)) {
991 assert(_recursions == 0, "invariant");
992 set_owner_from_BasicLock(cur, Self); // Convert from BasicLock* to Thread*.
993 _recursions = 0;
994 } else {
995 // Apparent unbalanced locking ...
996 // Naively we'd like to throw IllegalMonitorStateException.
997 // As a practical matter we can neither allocate nor throw an
998 // exception as ::exit() can be called from leaf routines.
999 // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
1000 // Upon deeper reflection, however, in a properly run JVM the only
1001 // way we should encounter this situation is in the presence of
1002 // unbalanced JNI locking. TODO: CheckJNICalls.
1003 // See also: CR4414101
1004 #ifdef ASSERT
1005 LogStreamHandle(Error, monitorinflation) lsh;
1006 lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
1007 " is exiting an ObjectMonitor it does not own.", p2i(THREAD));
1008 lsh.print_cr("The imbalance is possibly caused by JNI locking.");
1009 print_debug_style_on(&lsh);
1010 #endif
1011 assert(false, "Non-balanced monitor enter/exit!");
1012 return;
1013 }
1014 }
1015
1016 if (_recursions != 0) {
1017 _recursions--; // this is simple recursive enter
1018 return;
1019 }
1020
1021 // Invariant: after setting Responsible=null an thread must execute
1022 // a MEMBAR or other serializing instruction before fetching EntryList|cxq.
1023 _Responsible = NULL;
1024
1025 #if INCLUDE_JFR
1026 // get the owner's thread id for the MonitorEnter event
1027 // if it is enabled and the thread isn't suspended
1028 if (not_suspended && EventJavaMonitorEnter::is_enabled()) {
1029 _previous_owner_tid = JFR_THREAD_ID(Self);
1030 }
1031 #endif
|
273 // transitions. The following spin is strictly optional ...
274 // Note that if we acquire the monitor from an initial spin
275 // we forgo posting JVMTI events and firing DTRACE probes.
276 if (TrySpin(Self) > 0) {
277 assert(_owner == Self, "must be Self: owner=" INTPTR_FORMAT, p2i(_owner));
278 assert(_recursions == 0, "must be 0: recursions=" INTX_FORMAT, _recursions);
279 assert(((oop)object())->mark() == markWord::encode(this),
280 "object mark must match encoded this: mark=" INTPTR_FORMAT
281 ", encoded this=" INTPTR_FORMAT, ((oop)object())->mark().value(),
282 markWord::encode(this).value());
283 Self->_Stalled = 0;
284 return true;
285 }
286
287 assert(_owner != Self, "invariant");
288 assert(_succ != Self, "invariant");
289 assert(Self->is_Java_thread(), "invariant");
290 JavaThread * jt = (JavaThread *) Self;
291 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
292 assert(jt->thread_state() != _thread_blocked, "invariant");
293
294 // Keep track of contention for JVM/TI and M&M queries.
295 add_to_contentions(1);
296 if (is_being_async_deflated()) {
297 // Async deflation is in progress and our contentions increment
298 // above lost the race to async deflation. Undo the work and
299 // force the caller to retry.
300 const oop l_object = (oop)object();
301 if (l_object != NULL) {
302 // Attempt to restore the header/dmw to the object's header so that
303 // we only retry once if the deflater thread happens to be slow.
304 install_displaced_markword_in_object(l_object);
305 }
306 Self->_Stalled = 0;
307 add_to_contentions(-1);
308 return false;
309 }
310
311 JFR_ONLY(JfrConditionalFlushWithStacktrace<EventJavaMonitorEnter> flush(jt);)
312 EventJavaMonitorEnter event;
436 // the global free list to a per-thread free list.
437
438 guarantee(obj != NULL, "must be non-NULL");
439
440 // Separate loads in is_being_async_deflated(), which is almost always
441 // called before this function, from the load of dmw/header below.
442 if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
443 // A non-multiple copy atomic (nMCA) machine needs a bigger
444 // hammer to separate the loads before and the load below.
445 OrderAccess::fence();
446 } else {
447 OrderAccess::loadload();
448 }
449
450 const oop l_object = (oop)object();
451 if (l_object == NULL) {
452 // ObjectMonitor's object ref has already been cleared by async
453 // deflation so we're done here.
454 return;
455 }
456 assert(l_object == obj, "object=" INTPTR_FORMAT " must equal obj="
457 INTPTR_FORMAT, p2i(l_object), p2i(obj));
458
459 markWord dmw = header();
460 // The dmw has to be neutral (not NULL, not locked and not marked).
461 assert(dmw.is_neutral(), "must be neutral: dmw=" INTPTR_FORMAT, dmw.value());
462
463 // Install displaced mark word if the object's header still points
464 // to this ObjectMonitor. More than one racing caller to this function
465 // can rarely reach this point, but only one can win.
466 markWord res = obj->cas_set_mark(dmw, markWord::encode(this));
467 if (res != markWord::encode(this)) {
468 // This should be rare so log at the Info level when it happens.
469 log_info(monitorinflation)("install_displaced_markword_in_object: "
470 "failed cas_set_mark: new_mark=" INTPTR_FORMAT
471 ", old_mark=" INTPTR_FORMAT ", res=" INTPTR_FORMAT,
472 dmw.value(), markWord::encode(this).value(),
473 res.value());
474 }
475
476 // Note: It does not matter which thread restored the header/dmw
477 // into the object's header. The thread deflating the monitor just
478 // wanted the object's header restored and it is. The threads that
479 // detected a race with the deflation process also wanted the
480 // object's header restored before they retry their operation and
481 // because it is restored they will only retry once.
482 }
483
484 // Convert the fields used by is_busy() to a string that can be
485 // used for diagnostic output.
486 const char* ObjectMonitor::is_busy_to_string(stringStream* ss) {
487 ss->print("is_busy: waiters=%d, ", _waiters);
488 if (contentions() > 0) {
489 ss->print("contentions=%d, ", contentions());
490 } else {
491 ss->print("contentions=0");
492 }
493 if (_owner != DEFLATER_MARKER) {
494 ss->print("owner=" INTPTR_FORMAT, p2i(_owner));
495 } else {
496 // We report NULL instead of DEFLATER_MARKER here because is_busy()
497 // ignores DEFLATER_MARKER values.
498 ss->print("owner=" INTPTR_FORMAT, NULL);
499 }
500 ss->print(", cxq=" INTPTR_FORMAT ", EntryList=" INTPTR_FORMAT, p2i(_cxq),
501 p2i(_EntryList));
502 return ss->base();
503 }
504
505 #define MAX_RECHECK_INTERVAL 1000
506
507 void ObjectMonitor::EnterI(TRAPS) {
508 Thread * const Self = THREAD;
509 assert(Self->is_Java_thread(), "invariant");
510 assert(((JavaThread *) Self)->thread_state() == _thread_blocked, "invariant");
511
512 // Try the lock - TATAS
513 if (TryLock (Self) > 0) {
514 assert(_succ != Self, "invariant");
515 assert(_owner == Self, "invariant");
516 assert(_Responsible != Self, "invariant");
517 return;
518 }
519
520 if (try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
521 // Cancelled the in-progress async deflation by changing owner from
522 // DEFLATER_MARKER to Self. As part of the contended enter protocol,
523 // contentions was incremented to a positive value before EnterI()
524 // was called and that prevents the deflater thread from winning the
525 // last part of the 2-part async deflation protocol. After EnterI()
526 // returns to enter(), contentions is decremented because the caller
527 // now owns the monitor. We bump contentions an extra time here to
528 // prevent the deflater thread from winning the last part of the
529 // 2-part async deflation protocol after the regular decrement
530 // occurs in enter(). The deflater thread will decrement contentions
531 // after it recognizes that the async deflation was cancelled.
532 add_to_contentions(1);
533 assert(_succ != Self, "invariant");
534 assert(_Responsible != Self, "invariant");
535 return;
536 }
537
538 assert(InitDone, "Unexpectedly not initialized");
539
540 // We try one round of spinning *before* enqueueing Self.
634
635 for (;;) {
636
637 if (TryLock(Self) > 0) break;
638 assert(_owner != Self, "invariant");
639
640 // park self
641 if (_Responsible == Self) {
642 Self->_ParkEvent->park((jlong) recheckInterval);
643 // Increase the recheckInterval, but clamp the value.
644 recheckInterval *= 8;
645 if (recheckInterval > MAX_RECHECK_INTERVAL) {
646 recheckInterval = MAX_RECHECK_INTERVAL;
647 }
648 } else {
649 Self->_ParkEvent->park();
650 }
651
652 if (TryLock(Self) > 0) break;
653
654 if (try_set_owner_from(DEFLATER_MARKER, Self) == DEFLATER_MARKER) {
655 // Cancelled the in-progress async deflation by changing owner from
656 // DEFLATER_MARKER to Self. As part of the contended enter protocol,
657 // contentions was incremented to a positive value before EnterI()
658 // was called and that prevents the deflater thread from winning the
659 // last part of the 2-part async deflation protocol. After EnterI()
660 // returns to enter(), contentions is decremented because the caller
661 // now owns the monitor. We bump contentions an extra time here to
662 // prevent the deflater thread from winning the last part of the
663 // 2-part async deflation protocol after the regular decrement
664 // occurs in enter(). The deflater thread will decrement contentions
665 // after it recognizes that the async deflation was cancelled.
666 add_to_contentions(1);
667 break;
668 }
669
670 // The lock is still contested.
671 // Keep a tally of the # of futile wakeups.
672 // Note that the counter is not protected by a lock or updated by atomics.
673 // That is by design - we trade "lossy" counters which are exposed to
674 // races during updates for a lower probe effect.
981 if (THREAD->is_lock_owned((address)cur)) {
982 assert(_recursions == 0, "invariant");
983 set_owner_from_BasicLock(cur, Self); // Convert from BasicLock* to Thread*.
984 _recursions = 0;
985 } else {
986 // Apparent unbalanced locking ...
987 // Naively we'd like to throw IllegalMonitorStateException.
988 // As a practical matter we can neither allocate nor throw an
989 // exception as ::exit() can be called from leaf routines.
990 // see x86_32.ad Fast_Unlock() and the I1 and I2 properties.
991 // Upon deeper reflection, however, in a properly run JVM the only
992 // way we should encounter this situation is in the presence of
993 // unbalanced JNI locking. TODO: CheckJNICalls.
994 // See also: CR4414101
995 #ifdef ASSERT
996 LogStreamHandle(Error, monitorinflation) lsh;
997 lsh.print_cr("ERROR: ObjectMonitor::exit(): thread=" INTPTR_FORMAT
998 " is exiting an ObjectMonitor it does not own.", p2i(THREAD));
999 lsh.print_cr("The imbalance is possibly caused by JNI locking.");
1000 print_debug_style_on(&lsh);
1001 assert(false, "Non-balanced monitor enter/exit!");
1002 #endif
1003 return;
1004 }
1005 }
1006
1007 if (_recursions != 0) {
1008 _recursions--; // this is simple recursive enter
1009 return;
1010 }
1011
1012 // Invariant: after setting Responsible=null an thread must execute
1013 // a MEMBAR or other serializing instruction before fetching EntryList|cxq.
1014 _Responsible = NULL;
1015
1016 #if INCLUDE_JFR
1017 // get the owner's thread id for the MonitorEnter event
1018 // if it is enabled and the thread isn't suspended
1019 if (not_suspended && EventJavaMonitorEnter::is_enabled()) {
1020 _previous_owner_tid = JFR_THREAD_ID(Self);
1021 }
1022 #endif
|