1 /*
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
368 assert (_succ != Self , "invariant") ;
369 assert (Self->is_Java_thread() , "invariant") ;
370 JavaThread * jt = (JavaThread *) Self ;
371 assert (!SafepointSynchronize::is_at_safepoint(), "invariant") ;
372 assert (jt->thread_state() != _thread_blocked , "invariant") ;
373 assert (this->object() != NULL , "invariant") ;
374 assert (_count >= 0, "invariant") ;
375
376 // Prevent deflation at STW-time. See deflate_idle_monitors() and is_busy().
377 // Ensure the object-monitor relationship remains stable while there's contention.
378 Atomic::inc_ptr(&_count);
379
380 EventJavaMonitorEnter event;
381
382 { // Change java thread status to indicate blocked on monitor enter.
383 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this);
384
385 DTRACE_MONITOR_PROBE(contended__enter, this, object(), jt);
386 if (JvmtiExport::should_post_monitor_contended_enter()) {
387 JvmtiExport::post_monitor_contended_enter(jt, this);
388 }
389
390 OSThreadContendState osts(Self->osthread());
391 ThreadBlockInVM tbivm(jt);
392
393 Self->set_current_pending_monitor(this);
394
395 // TODO-FIXME: change the following for(;;) loop to straight-line code.
396 for (;;) {
397 jt->set_suspend_equivalent();
398 // cleared by handle_special_suspend_equivalent_condition()
399 // or java_suspend_self()
400
401 EnterI (THREAD) ;
402
403 if (!ExitSuspendEquivalent(jt)) break ;
404
405 //
406 // We have acquired the contended monitor, but while we were
407 // waiting another thread suspended us. We don't want to enter
425 assert (_recursions == 0 , "invariant") ;
426 assert (_owner == Self , "invariant") ;
427 assert (_succ != Self , "invariant") ;
428 assert (((oop)(object()))->mark() == markOopDesc::encode(this), "invariant") ;
429
430 // The thread -- now the owner -- is back in vm mode.
431 // Report the glorious news via TI,DTrace and jvmstat.
432 // The probe effect is non-trivial. All the reportage occurs
433 // while we hold the monitor, increasing the length of the critical
434 // section. Amdahl's parallel speedup law comes vividly into play.
435 //
436 // Another option might be to aggregate the events (thread local or
437 // per-monitor aggregation) and defer reporting until a more opportune
438 // time -- such as next time some thread encounters contention but has
439 // yet to acquire the lock. While spinning that thread could
440 // spinning we could increment JVMStat counters, etc.
441
442 DTRACE_MONITOR_PROBE(contended__entered, this, object(), jt);
443 if (JvmtiExport::should_post_monitor_contended_entered()) {
444 JvmtiExport::post_monitor_contended_entered(jt, this);
445 }
446
447 if (event.should_commit()) {
448 event.set_klass(((oop)this->object())->klass());
449 event.set_previousOwner((TYPE_JAVALANGTHREAD)_previous_owner_tid);
450 event.set_address((TYPE_ADDRESS)(uintptr_t)(this->object_addr()));
451 event.commit();
452 }
453
454 if (ObjectMonitor::_sync_ContendedLockAttempts != NULL) {
455 ObjectMonitor::_sync_ContendedLockAttempts->inc() ;
456 }
457 }
458
459
460 // Caveat: TryLock() is not necessarily serializing if it returns failure.
461 // Callers must compensate as needed.
462
463 int ObjectMonitor::TryLock (Thread * Self) {
464 for (;;) {
1442 // will need to be replicated in complete_exit above
1443 void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
1444 Thread * const Self = THREAD ;
1445 assert(Self->is_Java_thread(), "Must be Java thread!");
1446 JavaThread *jt = (JavaThread *)THREAD;
1447
1448 DeferredInitialize () ;
1449
1450 // Throw IMSX or IEX.
1451 CHECK_OWNER();
1452
1453 EventJavaMonitorWait event;
1454
1455 // check for a pending interrupt
1456 if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) {
1457 // post monitor waited event. Note that this is past-tense, we are done waiting.
1458 if (JvmtiExport::should_post_monitor_waited()) {
1459 // Note: 'false' parameter is passed here because the
1460 // wait was not timed out due to thread interrupt.
1461 JvmtiExport::post_monitor_waited(jt, this, false);
1462 }
1463 if (event.should_commit()) {
1464 post_monitor_wait_event(&event, 0, millis, false);
1465 }
1466 TEVENT (Wait - Throw IEX) ;
1467 THROW(vmSymbols::java_lang_InterruptedException());
1468 return ;
1469 }
1470
1471 TEVENT (Wait) ;
1472
1473 assert (Self->_Stalled == 0, "invariant") ;
1474 Self->_Stalled = intptr_t(this) ;
1475 jt->set_current_waiting_monitor(this);
1476
1477 // create a node to be put into the queue
1478 // Critically, after we reset() the event but prior to park(), we must check
1479 // for a pending interrupt.
1480 ObjectWaiter node(Self);
1481 node.TState = ObjectWaiter::TS_WAIT ;
1485 // Enter the waiting queue, which is a circular doubly linked list in this case
1486 // but it could be a priority queue or any data structure.
1487 // _WaitSetLock protects the wait queue. Normally the wait queue is accessed only
1488 // by the the owner of the monitor *except* in the case where park()
1489 // returns because of a timeout of interrupt. Contention is exceptionally rare
1490 // so we use a simple spin-lock instead of a heavier-weight blocking lock.
1491
1492 Thread::SpinAcquire (&_WaitSetLock, "WaitSet - add") ;
1493 AddWaiter (&node) ;
1494 Thread::SpinRelease (&_WaitSetLock) ;
1495
1496 if ((SyncFlags & 4) == 0) {
1497 _Responsible = NULL ;
1498 }
1499 intptr_t save = _recursions; // record the old recursion count
1500 _waiters++; // increment the number of waiters
1501 _recursions = 0; // set the recursion level to be 1
1502 exit (true, Self) ; // exit the monitor
1503 guarantee (_owner != Self, "invariant") ;
1504
1505 // As soon as the ObjectMonitor's ownership is dropped in the exit()
1506 // call above, another thread can enter() the ObjectMonitor, do the
1507 // notify(), and exit() the ObjectMonitor. If the other thread's
1508 // exit() call chooses this thread as the successor and the unpark()
1509 // call happens to occur while this thread is posting a
1510 // MONITOR_CONTENDED_EXIT event, then we run the risk of the event
1511 // handler using RawMonitors and consuming the unpark().
1512 //
1513 // To avoid the problem, we re-post the event. This does no harm
1514 // even if the original unpark() was not consumed because we are the
1515 // chosen successor for this monitor.
1516 if (node._notified != 0 && _succ == Self) {
1517 node._event->unpark();
1518 }
1519
1520 // The thread is on the WaitSet list - now park() it.
1521 // On MP systems it's conceivable that a brief spin before we park
1522 // could be profitable.
1523 //
1524 // TODO-FIXME: change the following logic to a loop of the form
1525 // while (!timeout && !interrupted && _notified == 0) park()
1526
1527 int ret = OS_OK ;
1528 int WasNotified = 0 ;
1529 { // State transition wrappers
1530 OSThread* osthread = Self->osthread();
1531 OSThreadWaitState osts(osthread, true);
1532 {
1533 ThreadBlockInVM tbivm(jt);
1534 // Thread is in thread_blocked state and oop access is unsafe.
1535 jt->set_suspend_equivalent();
1536
1537 if (interruptible && (Thread::is_interrupted(THREAD, false) || HAS_PENDING_EXCEPTION)) {
1538 // Intentionally empty
1539 } else
1583 // on the EntryList (TS_ENTER), or on the cxq (TS_CXQ).
1584 // The Node's TState variable is stable from the perspective of this thread.
1585 // No other threads will asynchronously modify TState.
1586 guarantee (node.TState != ObjectWaiter::TS_WAIT, "invariant") ;
1587 OrderAccess::loadload() ;
1588 if (_succ == Self) _succ = NULL ;
1589 WasNotified = node._notified ;
1590
1591 // Reentry phase -- reacquire the monitor.
1592 // re-enter contended monitor after object.wait().
1593 // retain OBJECT_WAIT state until re-enter successfully completes
1594 // Thread state is thread_in_vm and oop access is again safe,
1595 // although the raw address of the object may have changed.
1596 // (Don't cache naked oops over safepoints, of course).
1597
1598 // post monitor waited event. Note that this is past-tense, we are done waiting.
1599 if (JvmtiExport::should_post_monitor_waited()) {
1600 JvmtiExport::post_monitor_waited(jt, this, ret == OS_TIMEOUT);
1601 }
1602
1603 if (event.should_commit()) {
1604 post_monitor_wait_event(&event, node._notifier_tid, millis, ret == OS_TIMEOUT);
1605 }
1606
1607 OrderAccess::fence() ;
1608
1609 assert (Self->_Stalled != 0, "invariant") ;
1610 Self->_Stalled = 0 ;
1611
1612 assert (_owner != Self, "invariant") ;
1613 ObjectWaiter::TStates v = node.TState ;
1614 if (v == ObjectWaiter::TS_RUN) {
1615 enter (Self) ;
1616 } else {
1617 guarantee (v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant") ;
1618 ReenterI (Self, &node) ;
1619 node.wait_reenter_end(this);
1620 }
1621
1622 // Self has reacquired the lock.
|
1 /*
2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
368 assert (_succ != Self , "invariant") ;
369 assert (Self->is_Java_thread() , "invariant") ;
370 JavaThread * jt = (JavaThread *) Self ;
371 assert (!SafepointSynchronize::is_at_safepoint(), "invariant") ;
372 assert (jt->thread_state() != _thread_blocked , "invariant") ;
373 assert (this->object() != NULL , "invariant") ;
374 assert (_count >= 0, "invariant") ;
375
376 // Prevent deflation at STW-time. See deflate_idle_monitors() and is_busy().
377 // Ensure the object-monitor relationship remains stable while there's contention.
378 Atomic::inc_ptr(&_count);
379
380 EventJavaMonitorEnter event;
381
382 { // Change java thread status to indicate blocked on monitor enter.
383 JavaThreadBlockedOnMonitorEnterState jtbmes(jt, this);
384
385 DTRACE_MONITOR_PROBE(contended__enter, this, object(), jt);
386 if (JvmtiExport::should_post_monitor_contended_enter()) {
387 JvmtiExport::post_monitor_contended_enter(jt, this);
388
389 // The current thread does not yet own the monitor and does not
390 // yet appear on any queues that would get it made the successor.
391 // This means that the JVMTI_EVENT_MONITOR_CONTENDED_ENTER event
392 // handler cannot accidentally consume an unpark() meant for the
393 // ParkEvent associated with this ObjectMonitor.
394 }
395
396 OSThreadContendState osts(Self->osthread());
397 ThreadBlockInVM tbivm(jt);
398
399 Self->set_current_pending_monitor(this);
400
401 // TODO-FIXME: change the following for(;;) loop to straight-line code.
402 for (;;) {
403 jt->set_suspend_equivalent();
404 // cleared by handle_special_suspend_equivalent_condition()
405 // or java_suspend_self()
406
407 EnterI (THREAD) ;
408
409 if (!ExitSuspendEquivalent(jt)) break ;
410
411 //
412 // We have acquired the contended monitor, but while we were
413 // waiting another thread suspended us. We don't want to enter
431 assert (_recursions == 0 , "invariant") ;
432 assert (_owner == Self , "invariant") ;
433 assert (_succ != Self , "invariant") ;
434 assert (((oop)(object()))->mark() == markOopDesc::encode(this), "invariant") ;
435
436 // The thread -- now the owner -- is back in vm mode.
437 // Report the glorious news via TI,DTrace and jvmstat.
438 // The probe effect is non-trivial. All the reportage occurs
439 // while we hold the monitor, increasing the length of the critical
440 // section. Amdahl's parallel speedup law comes vividly into play.
441 //
442 // Another option might be to aggregate the events (thread local or
443 // per-monitor aggregation) and defer reporting until a more opportune
444 // time -- such as next time some thread encounters contention but has
445 // yet to acquire the lock. While spinning that thread could
446 // spinning we could increment JVMStat counters, etc.
447
448 DTRACE_MONITOR_PROBE(contended__entered, this, object(), jt);
449 if (JvmtiExport::should_post_monitor_contended_entered()) {
450 JvmtiExport::post_monitor_contended_entered(jt, this);
451
452 // The current thread already owns the monitor and is not going to
453 // call park() for the remainder of the monitor enter protocol. So
454 // it doesn't matter if the JVMTI_EVENT_MONITOR_CONTENDED_ENTERED
455 // event handler consumed an unpark() issued by the thread that
456 // just exited the monitor.
457 }
458
459 if (event.should_commit()) {
460 event.set_klass(((oop)this->object())->klass());
461 event.set_previousOwner((TYPE_JAVALANGTHREAD)_previous_owner_tid);
462 event.set_address((TYPE_ADDRESS)(uintptr_t)(this->object_addr()));
463 event.commit();
464 }
465
466 if (ObjectMonitor::_sync_ContendedLockAttempts != NULL) {
467 ObjectMonitor::_sync_ContendedLockAttempts->inc() ;
468 }
469 }
470
471
472 // Caveat: TryLock() is not necessarily serializing if it returns failure.
473 // Callers must compensate as needed.
474
475 int ObjectMonitor::TryLock (Thread * Self) {
476 for (;;) {
1454 // will need to be replicated in complete_exit above
1455 void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) {
1456 Thread * const Self = THREAD ;
1457 assert(Self->is_Java_thread(), "Must be Java thread!");
1458 JavaThread *jt = (JavaThread *)THREAD;
1459
1460 DeferredInitialize () ;
1461
1462 // Throw IMSX or IEX.
1463 CHECK_OWNER();
1464
1465 EventJavaMonitorWait event;
1466
1467 // check for a pending interrupt
1468 if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) {
1469 // post monitor waited event. Note that this is past-tense, we are done waiting.
1470 if (JvmtiExport::should_post_monitor_waited()) {
1471 // Note: 'false' parameter is passed here because the
1472 // wait was not timed out due to thread interrupt.
1473 JvmtiExport::post_monitor_waited(jt, this, false);
1474
1475 // In this short circuit of the monitor wait protocol, the
1476 // current thread never drops ownership of the monitor and
1477 // never gets added to the wait queue so the current thread
1478 // cannot be made the successor. This means that the
1479 // JVMTI_EVENT_MONITOR_WAITED event handler cannot accidentally
1480 // consume an unpark() meant for the ParkEvent associated with
1481 // this ObjectMonitor.
1482 }
1483 if (event.should_commit()) {
1484 post_monitor_wait_event(&event, 0, millis, false);
1485 }
1486 TEVENT (Wait - Throw IEX) ;
1487 THROW(vmSymbols::java_lang_InterruptedException());
1488 return ;
1489 }
1490
1491 TEVENT (Wait) ;
1492
1493 assert (Self->_Stalled == 0, "invariant") ;
1494 Self->_Stalled = intptr_t(this) ;
1495 jt->set_current_waiting_monitor(this);
1496
1497 // create a node to be put into the queue
1498 // Critically, after we reset() the event but prior to park(), we must check
1499 // for a pending interrupt.
1500 ObjectWaiter node(Self);
1501 node.TState = ObjectWaiter::TS_WAIT ;
1505 // Enter the waiting queue, which is a circular doubly linked list in this case
1506 // but it could be a priority queue or any data structure.
1507 // _WaitSetLock protects the wait queue. Normally the wait queue is accessed only
1508 // by the the owner of the monitor *except* in the case where park()
1509 // returns because of a timeout of interrupt. Contention is exceptionally rare
1510 // so we use a simple spin-lock instead of a heavier-weight blocking lock.
1511
1512 Thread::SpinAcquire (&_WaitSetLock, "WaitSet - add") ;
1513 AddWaiter (&node) ;
1514 Thread::SpinRelease (&_WaitSetLock) ;
1515
1516 if ((SyncFlags & 4) == 0) {
1517 _Responsible = NULL ;
1518 }
1519 intptr_t save = _recursions; // record the old recursion count
1520 _waiters++; // increment the number of waiters
1521 _recursions = 0; // set the recursion level to be 1
1522 exit (true, Self) ; // exit the monitor
1523 guarantee (_owner != Self, "invariant") ;
1524
1525 // The thread is on the WaitSet list - now park() it.
1526 // On MP systems it's conceivable that a brief spin before we park
1527 // could be profitable.
1528 //
1529 // TODO-FIXME: change the following logic to a loop of the form
1530 // while (!timeout && !interrupted && _notified == 0) park()
1531
1532 int ret = OS_OK ;
1533 int WasNotified = 0 ;
1534 { // State transition wrappers
1535 OSThread* osthread = Self->osthread();
1536 OSThreadWaitState osts(osthread, true);
1537 {
1538 ThreadBlockInVM tbivm(jt);
1539 // Thread is in thread_blocked state and oop access is unsafe.
1540 jt->set_suspend_equivalent();
1541
1542 if (interruptible && (Thread::is_interrupted(THREAD, false) || HAS_PENDING_EXCEPTION)) {
1543 // Intentionally empty
1544 } else
1588 // on the EntryList (TS_ENTER), or on the cxq (TS_CXQ).
1589 // The Node's TState variable is stable from the perspective of this thread.
1590 // No other threads will asynchronously modify TState.
1591 guarantee (node.TState != ObjectWaiter::TS_WAIT, "invariant") ;
1592 OrderAccess::loadload() ;
1593 if (_succ == Self) _succ = NULL ;
1594 WasNotified = node._notified ;
1595
1596 // Reentry phase -- reacquire the monitor.
1597 // re-enter contended monitor after object.wait().
1598 // retain OBJECT_WAIT state until re-enter successfully completes
1599 // Thread state is thread_in_vm and oop access is again safe,
1600 // although the raw address of the object may have changed.
1601 // (Don't cache naked oops over safepoints, of course).
1602
1603 // post monitor waited event. Note that this is past-tense, we are done waiting.
1604 if (JvmtiExport::should_post_monitor_waited()) {
1605 JvmtiExport::post_monitor_waited(jt, this, ret == OS_TIMEOUT);
1606 }
1607
1608 // Without the fix for 8028280, it is possible for the above call:
1609 //
1610 // Thread::SpinAcquire (&_WaitSetLock, "WaitSet - unlink") ;
1611 //
1612 // to consume the unpark() that was done when the successor was set.
1613 // The solution for this very rare possibility is to redo the unpark()
1614 // outside of the JvmtiExport::should_post_monitor_waited() check.
1615 //
1616 if (node._notified != 0 && _succ == Self) {
1617 // In this part of the monitor wait-notify-reenter protocol it
1618 // is possible (and normal) for another thread to do a fastpath
1619 // monitor enter-exit while this thread is still trying to get
1620 // to the reenter portion of the protocol.
1621 //
1622 // The ObjectMonitor was notified and the current thread is
1623 // the successor which also means that an unpark() has already
1624 // been done. The JVMTI_EVENT_MONITOR_WAITED event handler can
1625 // consume the unpark() that was done when the successor was
1626 // set because the same ParkEvent is shared between Java
1627 // monitors and JVM/TI RawMonitors (for now).
1628 //
1629 // We redo the unpark() to ensure forward progress, i.e., we
1630 // don't want all pending threads hanging (parked) with none
1631 // entering the unlocked monitor.
1632 node._event->unpark();
1633 }
1634
1635 if (event.should_commit()) {
1636 post_monitor_wait_event(&event, node._notifier_tid, millis, ret == OS_TIMEOUT);
1637 }
1638
1639 OrderAccess::fence() ;
1640
1641 assert (Self->_Stalled != 0, "invariant") ;
1642 Self->_Stalled = 0 ;
1643
1644 assert (_owner != Self, "invariant") ;
1645 ObjectWaiter::TStates v = node.TState ;
1646 if (v == ObjectWaiter::TS_RUN) {
1647 enter (Self) ;
1648 } else {
1649 guarantee (v == ObjectWaiter::TS_ENTER || v == ObjectWaiter::TS_CXQ, "invariant") ;
1650 ReenterI (Self, &node) ;
1651 node.wait_reenter_end(this);
1652 }
1653
1654 // Self has reacquired the lock.
|