src/hotspot/share/runtime/thread.cpp
branchJEP-349-branch
changeset 58343 ca19b94eac7a
parent 58249 e17143e28542
parent 58291 a013100f7a35
child 58478 78de3ec29f1e
equal deleted inserted replaced
58271:e47423f1318b 58343:ca19b94eac7a
   971 // Checks safepoint allowed and clears unhandled oops at potential safepoints.
   971 // Checks safepoint allowed and clears unhandled oops at potential safepoints.
   972 void Thread::check_possible_safepoint() {
   972 void Thread::check_possible_safepoint() {
   973   if (!is_Java_thread()) return;
   973   if (!is_Java_thread()) return;
   974 
   974 
   975   if (_no_safepoint_count > 0) {
   975   if (_no_safepoint_count > 0) {
       
   976     print_owned_locks();
   976     fatal("Possible safepoint reached by thread that does not allow it");
   977     fatal("Possible safepoint reached by thread that does not allow it");
   977   }
   978   }
   978 #ifdef CHECK_UNHANDLED_OOPS
   979 #ifdef CHECK_UNHANDLED_OOPS
   979   // Clear unhandled oops in JavaThreads so we get a crash right away.
   980   // Clear unhandled oops in JavaThreads so we get a crash right away.
   980   clear_unhandled_oops();
   981   clear_unhandled_oops();
   981 #endif // CHECK_UNHANDLED_OOPS
   982 #endif // CHECK_UNHANDLED_OOPS
   982 }
   983 }
   983 
   984 
   984 // The flag: potential_vm_operation notifies if this particular safepoint state could potentially
   985 void Thread::check_for_valid_safepoint_state() {
   985 // invoke the vm-thread (e.g., an oop allocation). In that case, we also have to make sure that
       
   986 // no locks which allow_vm_block's are held
       
   987 void Thread::check_for_valid_safepoint_state(bool potential_vm_operation) {
       
   988   if (!is_Java_thread()) return;
   986   if (!is_Java_thread()) return;
   989 
   987 
       
   988   // Check NoSafepointVerifier, which is implied by locks taken that can be
       
   989   // shared with the VM thread.  This makes sure that no locks with allow_vm_block
       
   990   // are held.
   990   check_possible_safepoint();
   991   check_possible_safepoint();
   991 
   992 
   992   if (((JavaThread*)this)->thread_state() != _thread_in_vm) {
   993   if (((JavaThread*)this)->thread_state() != _thread_in_vm) {
   993     fatal("LEAF method calling lock?");
   994     fatal("LEAF method calling lock?");
   994   }
       
   995 
       
   996   if (potential_vm_operation && !Universe::is_bootstrapping()) {
       
   997     // Make sure we do not hold any locks that the VM thread also uses.
       
   998     // This could potentially lead to deadlocks
       
   999     for (Mutex* cur = _owned_locks; cur; cur = cur->next()) {
       
  1000       // Threads_lock is special, since the safepoint synchronization will not start before this is
       
  1001       // acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock,
       
  1002       // since it is used to transfer control between JavaThreads and the VMThread
       
  1003       // Do not *exclude* any locks unless you are absolutely sure it is correct. Ask someone else first!
       
  1004       if ((cur->allow_vm_block() &&
       
  1005            cur != Threads_lock &&
       
  1006            cur != Compile_lock &&               // Temporary: should not be necessary when we get separate compilation
       
  1007            cur != VMOperationRequest_lock &&
       
  1008            cur != VMOperationQueue_lock) ||
       
  1009            cur->rank() == Mutex::special) {
       
  1010         fatal("Thread holding lock at safepoint that vm can block on: %s", cur->name());
       
  1011       }
       
  1012     }
       
  1013   }
   995   }
  1014 
   996 
  1015   if (GCALotAtAllSafepoints) {
   997   if (GCALotAtAllSafepoints) {
  1016     // We could enter a safepoint here and thus have a gc
   998     // We could enter a safepoint here and thus have a gc
  1017     InterfaceSupport::check_gc_alot();
   999     InterfaceSupport::check_gc_alot();
  1551 }
  1533 }
  1552 
  1534 
  1553 // Attempt to enlarge the array for per thread counters.
  1535 // Attempt to enlarge the array for per thread counters.
  1554 jlong* resize_counters_array(jlong* old_counters, int current_size, int new_size) {
  1536 jlong* resize_counters_array(jlong* old_counters, int current_size, int new_size) {
  1555   jlong* new_counters = NEW_C_HEAP_ARRAY(jlong, new_size, mtJVMCI);
  1537   jlong* new_counters = NEW_C_HEAP_ARRAY(jlong, new_size, mtJVMCI);
  1556   if (new_counters == NULL) {
       
  1557     return NULL;
       
  1558   }
       
  1559   if (old_counters == NULL) {
  1538   if (old_counters == NULL) {
  1560     old_counters = new_counters;
  1539     old_counters = new_counters;
  1561     memset(old_counters, 0, sizeof(jlong) * new_size);
  1540     memset(old_counters, 0, sizeof(jlong) * new_size);
  1562   } else {
  1541   } else {
  1563     for (int i = 0; i < MIN2((int) current_size, new_size); i++) {
  1542     for (int i = 0; i < MIN2((int) current_size, new_size); i++) {
  1570   }
  1549   }
  1571   return new_counters;
  1550   return new_counters;
  1572 }
  1551 }
  1573 
  1552 
  1574 // Attempt to enlarge the array for per thread counters.
  1553 // Attempt to enlarge the array for per thread counters.
  1575 bool JavaThread::resize_counters(int current_size, int new_size) {
  1554 void JavaThread::resize_counters(int current_size, int new_size) {
  1576   jlong* new_counters = resize_counters_array(_jvmci_counters, current_size, new_size);
  1555   _jvmci_counters = resize_counters_array(_jvmci_counters, current_size, new_size);
  1577   if (new_counters == NULL) {
       
  1578     return false;
       
  1579   } else {
       
  1580     _jvmci_counters = new_counters;
       
  1581     return true;
       
  1582   }
       
  1583 }
  1556 }
  1584 
  1557 
  1585 class VM_JVMCIResizeCounters : public VM_Operation {
  1558 class VM_JVMCIResizeCounters : public VM_Operation {
  1586  private:
  1559  private:
  1587   int _new_size;
  1560   int _new_size;
  1588   bool _failed;
       
  1589 
  1561 
  1590  public:
  1562  public:
  1591   VM_JVMCIResizeCounters(int new_size) : _new_size(new_size), _failed(false) { }
  1563   VM_JVMCIResizeCounters(int new_size) : _new_size(new_size) { }
  1592   VMOp_Type type()                  const        { return VMOp_JVMCIResizeCounters; }
  1564   VMOp_Type type()                  const        { return VMOp_JVMCIResizeCounters; }
  1593   bool allow_nested_vm_operations() const        { return true; }
  1565   bool allow_nested_vm_operations() const        { return true; }
  1594   void doit() {
  1566   void doit() {
  1595     // Resize the old thread counters array
  1567     // Resize the old thread counters array
  1596     jlong* new_counters = resize_counters_array(JavaThread::_jvmci_old_thread_counters, JVMCICounterSize, _new_size);
  1568     jlong* new_counters = resize_counters_array(JavaThread::_jvmci_old_thread_counters, JVMCICounterSize, _new_size);
  1597     if (new_counters == NULL) {
  1569     JavaThread::_jvmci_old_thread_counters = new_counters;
  1598       _failed = true;
       
  1599       return;
       
  1600     } else {
       
  1601       JavaThread::_jvmci_old_thread_counters = new_counters;
       
  1602     }
       
  1603 
  1570 
  1604     // Now resize each threads array
  1571     // Now resize each threads array
  1605     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
  1572     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *tp = jtiwh.next(); ) {
  1606       if (!tp->resize_counters(JVMCICounterSize, _new_size)) {
  1573       tp->resize_counters(JVMCICounterSize, _new_size);
  1607         _failed = true;
  1574     }
  1608         break;
  1575     JVMCICounterSize = _new_size;
  1609       }
  1576   }
  1610     }
       
  1611     if (!_failed) {
       
  1612       JVMCICounterSize = _new_size;
       
  1613     }
       
  1614   }
       
  1615 
       
  1616   bool failed() { return _failed; }
       
  1617 };
  1577 };
  1618 
  1578 
  1619 bool JavaThread::resize_all_jvmci_counters(int new_size) {
  1579 void JavaThread::resize_all_jvmci_counters(int new_size) {
  1620   VM_JVMCIResizeCounters op(new_size);
  1580   VM_JVMCIResizeCounters op(new_size);
  1621   VMThread::execute(&op);
  1581   VMThread::execute(&op);
  1622   return !op.failed();
       
  1623 }
  1582 }
  1624 
  1583 
  1625 #endif // INCLUDE_JVMCI
  1584 #endif // INCLUDE_JVMCI
  1626 
  1585 
  1627 // A JavaThread is a normal Java thread
  1586 // A JavaThread is a normal Java thread
  1801 
  1760 
  1802 
  1761 
  1803 void JavaThread::block_if_vm_exited() {
  1762 void JavaThread::block_if_vm_exited() {
  1804   if (_terminated == _vm_exited) {
  1763   if (_terminated == _vm_exited) {
  1805     // _vm_exited is set at safepoint, and Threads_lock is never released
  1764     // _vm_exited is set at safepoint, and Threads_lock is never released
  1806     // we will block here forever
  1765     // we will block here forever.
       
  1766     // Here we can be doing a jump from a safe state to an unsafe state without
       
  1767     // proper transition, but it happens after the final safepoint has begun.
       
  1768     set_thread_state(_thread_in_vm);
  1807     Threads_lock->lock();
  1769     Threads_lock->lock();
  1808     ShouldNotReachHere();
  1770     ShouldNotReachHere();
  1809   }
  1771   }
  1810 }
  1772 }
  1811 
  1773