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 Atomic::inc(&_contentions);
298 if (AsyncDeflateIdleMonitors && 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 Atomic::dec(&_contentions);
310 return false;
311 }
312
313 JFR_ONLY(JfrConditionalFlushWithStacktrace<EventJavaMonitorEnter> flush(jt);)
314 EventJavaMonitorEnter event;
315 if (event.should_commit()) {
316 event.set_monitorClass(((oop)this->object())->klass());
317 event.set_address((uintptr_t)(this->object_addr()));
318 }
319
320 { // Change java thread status to indicate blocked on monitor enter.
321 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this);
322
323 Self->set_current_pending_monitor(this);
324
325 DTRACE_MONITOR_PROBE(contended__enter, this, object(), jt);
326 if (JvmtiExport::should_post_monitor_contended_enter()) {
327 JvmtiExport::post_monitor_contended_enter(jt, this);
328
329 // The current thread does not yet own the monitor and does not
352 // thread that suspended us.
353 //
354 _recursions = 0;
355 _succ = NULL;
356 exit(false, Self);
357
358 jt->java_suspend_self();
359 }
360 Self->set_current_pending_monitor(NULL);
361
362 // We cleared the pending monitor info since we've just gotten past
363 // the enter-check-for-suspend dance and we now own the monitor free
364 // and clear, i.e., it is no longer pending. The ThreadBlockInVM
365 // destructor can go to a safepoint at the end of this block. If we
366 // do a thread dump during that safepoint, then this thread will show
367 // as having "-locked" the monitor, but the OS and java.lang.Thread
368 // states will still report that the thread is blocked trying to
369 // acquire it.
370 }
371
372 Atomic::dec(&_contentions);
373 assert(contentions() >= 0, "must not be negative: contentions=%d", contentions());
374 Self->_Stalled = 0;
375
376 // Must either set _recursions = 0 or ASSERT _recursions == 0.
377 assert(_recursions == 0, "invariant");
378 assert(_owner == Self, "invariant");
379 assert(_succ != Self, "invariant");
380 assert(((oop)(object()))->mark() == markWord::encode(this), "invariant");
381
382 // The thread -- now the owner -- is back in vm mode.
383 // Report the glorious news via TI,DTrace and jvmstat.
384 // The probe effect is non-trivial. All the reportage occurs
385 // while we hold the monitor, increasing the length of the critical
386 // section. Amdahl's parallel speedup law comes vividly into play.
387 //
388 // Another option might be to aggregate the events (thread local or
389 // per-monitor aggregation) and defer reporting until a more opportune
390 // time -- such as next time some thread encounters contention but has
391 // yet to acquire the lock. While spinning that thread could
392 // spinning we could increment JVMStat counters, etc.
436 // && contentions <= 0), but we can't guarantee that here because
437 // those values could change when the ObjectMonitor gets moved from
438 // the global free list to a per-thread free list.
439
440 guarantee(obj != NULL, "must be non-NULL");
441
442 const oop l_object = (oop)object();
443 if (l_object == NULL) {
444 // ObjectMonitor's object ref has already been cleared by async
445 // deflation so we're done here.
446 return;
447 }
448 ADIM_guarantee(l_object == obj, "object=" INTPTR_FORMAT " must equal obj="
449 INTPTR_FORMAT, p2i(l_object), p2i(obj));
450
451 markWord dmw = header();
452 // The dmw has to be neutral (not NULL, not locked and not marked).
453 ADIM_guarantee(dmw.is_neutral(), "must be neutral: dmw=" INTPTR_FORMAT, dmw.value());
454
455 // Install displaced mark word if the object's header still points
456 // to this ObjectMonitor. All racing callers to this function will
457 // reach this point, but only one can win.
458 markWord res = obj->cas_set_mark(dmw, markWord::encode(this));
459 if (res != markWord::encode(this)) {
460 // This should be rare so log at the Info level when it happens.
461 log_info(monitorinflation)("install_displaced_markword_in_object: "
462 "failed cas_set_mark: new_mark=" INTPTR_FORMAT
463 ", old_mark=" INTPTR_FORMAT ", res=" INTPTR_FORMAT,
464 dmw.value(), markWord::encode(this).value(),
465 res.value());
466 }
467
468 // Note: It does not matter which thread restored the header/dmw
469 // into the object's header. The thread deflating the monitor just
470 // wanted the object's header restored and it is. The threads that
471 // detected a race with the deflation process also wanted the
472 // object's header restored before they retry their operation and
473 // because it is restored they will only retry once.
474 }
475
476 // Convert the fields used by is_busy() to a string that can be
477 // used for diagnostic output.
|
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((jint)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((jint)-1);
310 return false;
311 }
312
313 JFR_ONLY(JfrConditionalFlushWithStacktrace<EventJavaMonitorEnter> flush(jt);)
314 EventJavaMonitorEnter event;
315 if (event.should_commit()) {
316 event.set_monitorClass(((oop)this->object())->klass());
317 event.set_address((uintptr_t)(this->object_addr()));
318 }
319
320 { // Change java thread status to indicate blocked on monitor enter.
321 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this);
322
323 Self->set_current_pending_monitor(this);
324
325 DTRACE_MONITOR_PROBE(contended__enter, this, object(), jt);
326 if (JvmtiExport::should_post_monitor_contended_enter()) {
327 JvmtiExport::post_monitor_contended_enter(jt, this);
328
329 // The current thread does not yet own the monitor and does not
352 // thread that suspended us.
353 //
354 _recursions = 0;
355 _succ = NULL;
356 exit(false, Self);
357
358 jt->java_suspend_self();
359 }
360 Self->set_current_pending_monitor(NULL);
361
362 // We cleared the pending monitor info since we've just gotten past
363 // the enter-check-for-suspend dance and we now own the monitor free
364 // and clear, i.e., it is no longer pending. The ThreadBlockInVM
365 // destructor can go to a safepoint at the end of this block. If we
366 // do a thread dump during that safepoint, then this thread will show
367 // as having "-locked" the monitor, but the OS and java.lang.Thread
368 // states will still report that the thread is blocked trying to
369 // acquire it.
370 }
371
372 add_to_contentions((jint)-1);
373 assert(contentions() >= 0, "must not be negative: contentions=%d", contentions());
374 Self->_Stalled = 0;
375
376 // Must either set _recursions = 0 or ASSERT _recursions == 0.
377 assert(_recursions == 0, "invariant");
378 assert(_owner == Self, "invariant");
379 assert(_succ != Self, "invariant");
380 assert(((oop)(object()))->mark() == markWord::encode(this), "invariant");
381
382 // The thread -- now the owner -- is back in vm mode.
383 // Report the glorious news via TI,DTrace and jvmstat.
384 // The probe effect is non-trivial. All the reportage occurs
385 // while we hold the monitor, increasing the length of the critical
386 // section. Amdahl's parallel speedup law comes vividly into play.
387 //
388 // Another option might be to aggregate the events (thread local or
389 // per-monitor aggregation) and defer reporting until a more opportune
390 // time -- such as next time some thread encounters contention but has
391 // yet to acquire the lock. While spinning that thread could
392 // spinning we could increment JVMStat counters, etc.
436 // && contentions <= 0), but we can't guarantee that here because
437 // those values could change when the ObjectMonitor gets moved from
438 // the global free list to a per-thread free list.
439
440 guarantee(obj != NULL, "must be non-NULL");
441
442 const oop l_object = (oop)object();
443 if (l_object == NULL) {
444 // ObjectMonitor's object ref has already been cleared by async
445 // deflation so we're done here.
446 return;
447 }
448 ADIM_guarantee(l_object == obj, "object=" INTPTR_FORMAT " must equal obj="
449 INTPTR_FORMAT, p2i(l_object), p2i(obj));
450
451 markWord dmw = header();
452 // The dmw has to be neutral (not NULL, not locked and not marked).
453 ADIM_guarantee(dmw.is_neutral(), "must be neutral: dmw=" INTPTR_FORMAT, dmw.value());
454
455 // Install displaced mark word if the object's header still points
456 // to this ObjectMonitor. More than one racing caller to this function
457 // can rarely reach this point, but only one can win.
458 markWord res = obj->cas_set_mark(dmw, markWord::encode(this));
459 if (res != markWord::encode(this)) {
460 // This should be rare so log at the Info level when it happens.
461 log_info(monitorinflation)("install_displaced_markword_in_object: "
462 "failed cas_set_mark: new_mark=" INTPTR_FORMAT
463 ", old_mark=" INTPTR_FORMAT ", res=" INTPTR_FORMAT,
464 dmw.value(), markWord::encode(this).value(),
465 res.value());
466 }
467
468 // Note: It does not matter which thread restored the header/dmw
469 // into the object's header. The thread deflating the monitor just
470 // wanted the object's header restored and it is. The threads that
471 // detected a race with the deflation process also wanted the
472 // object's header restored before they retry their operation and
473 // because it is restored they will only retry once.
474 }
475
476 // Convert the fields used by is_busy() to a string that can be
477 // used for diagnostic output.
|