hotspot/src/share/vm/runtime/thread.cpp
changeset 2526 39a58a50be35
parent 2336 427ce3320578
child 2995 d8283445992a
equal deleted inserted replaced
2364:7c9f46e9d37c 2526:39a58a50be35
   126   // plain initialization
   126   // plain initialization
   127   debug_only(_owned_locks = NULL;)
   127   debug_only(_owned_locks = NULL;)
   128   debug_only(_allow_allocation_count = 0;)
   128   debug_only(_allow_allocation_count = 0;)
   129   NOT_PRODUCT(_allow_safepoint_count = 0;)
   129   NOT_PRODUCT(_allow_safepoint_count = 0;)
   130   CHECK_UNHANDLED_OOPS_ONLY(_gc_locked_out_count = 0;)
   130   CHECK_UNHANDLED_OOPS_ONLY(_gc_locked_out_count = 0;)
   131   _highest_lock = NULL;
       
   132   _jvmti_env_iteration_count = 0;
   131   _jvmti_env_iteration_count = 0;
   133   _vm_operation_started_count = 0;
   132   _vm_operation_started_count = 0;
   134   _vm_operation_completed_count = 0;
   133   _vm_operation_completed_count = 0;
   135   _current_pending_monitor = NULL;
   134   _current_pending_monitor = NULL;
   136   _current_pending_monitor_is_from_java = true;
   135   _current_pending_monitor_is_from_java = true;
   788 
   787 
   789 #endif
   788 #endif
   790 }
   789 }
   791 #endif
   790 #endif
   792 
   791 
   793 bool Thread::lock_is_in_stack(address adr) const {
       
   794   assert(Thread::current() == this, "lock_is_in_stack can only be called from current thread");
       
   795   // High limit: highest_lock is set during thread execution
       
   796   // Low  limit: address of the local variable dummy, rounded to 4K boundary.
       
   797   // (The rounding helps finding threads in unsafe mode, even if the particular stack
       
   798   // frame has been popped already.  Correct as long as stacks are at least 4K long and aligned.)
       
   799   address end = os::current_stack_pointer();
       
   800   if (_highest_lock >= adr && adr >= end) return true;
       
   801 
       
   802   return false;
       
   803 }
       
   804 
       
   805 
       
   806 bool Thread::is_in_stack(address adr) const {
   792 bool Thread::is_in_stack(address adr) const {
   807   assert(Thread::current() == this, "is_in_stack can only be called from current thread");
   793   assert(Thread::current() == this, "is_in_stack can only be called from current thread");
   808   address end = os::current_stack_pointer();
   794   address end = os::current_stack_pointer();
   809   if (stack_base() >= adr && adr >= end) return true;
   795   if (stack_base() >= adr && adr >= end) return true;
   810 
   796 
   816 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
   802 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
   817 // used for compilation in the future. If that change is made, the need for these methods
   803 // used for compilation in the future. If that change is made, the need for these methods
   818 // should be revisited, and they should be removed if possible.
   804 // should be revisited, and they should be removed if possible.
   819 
   805 
   820 bool Thread::is_lock_owned(address adr) const {
   806 bool Thread::is_lock_owned(address adr) const {
   821   if (lock_is_in_stack(adr) ) return true;
   807   return (_stack_base >= adr && adr >= (_stack_base - _stack_size));
   822   return false;
       
   823 }
   808 }
   824 
   809 
   825 bool Thread::set_as_starting_thread() {
   810 bool Thread::set_as_starting_thread() {
   826  // NOTE: this must be called inside the main thread.
   811  // NOTE: this must be called inside the main thread.
   827   return os::create_main_thread((JavaThread*)this);
   812   return os::create_main_thread((JavaThread*)this);
  1662     return ret;
  1647     return ret;
  1663   }
  1648   }
  1664 }
  1649 }
  1665 
  1650 
  1666 bool JavaThread::is_lock_owned(address adr) const {
  1651 bool JavaThread::is_lock_owned(address adr) const {
  1667   if (lock_is_in_stack(adr)) return true;
  1652   if (Thread::is_lock_owned(adr)) return true;
  1668 
  1653 
  1669   for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
  1654   for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
  1670     if (chunk->contains(adr)) return true;
  1655     if (chunk->contains(adr)) return true;
  1671   }
  1656   }
  1672 
  1657 
  2441   st->print("\"%s\" ", get_thread_name());
  2426   st->print("\"%s\" ", get_thread_name());
  2442   oop thread_oop = threadObj();
  2427   oop thread_oop = threadObj();
  2443   if (thread_oop != NULL && java_lang_Thread::is_daemon(thread_oop))  st->print("daemon ");
  2428   if (thread_oop != NULL && java_lang_Thread::is_daemon(thread_oop))  st->print("daemon ");
  2444   Thread::print_on(st);
  2429   Thread::print_on(st);
  2445   // print guess for valid stack memory region (assume 4K pages); helps lock debugging
  2430   // print guess for valid stack memory region (assume 4K pages); helps lock debugging
  2446   st->print_cr("[" INTPTR_FORMAT ".." INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12), highest_lock());
  2431   st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
  2447   if (thread_oop != NULL && JDK_Version::is_gte_jdk15x_version()) {
  2432   if (thread_oop != NULL && JDK_Version::is_gte_jdk15x_version()) {
  2448     st->print_cr("   java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
  2433     st->print_cr("   java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
  2449   }
  2434   }
  2450 #ifndef PRODUCT
  2435 #ifndef PRODUCT
  2451   print_thread_state_on(st);
  2436   print_thread_state_on(st);
  3731   //
  3716   //
  3732   // If we didn't find a matching Java thread and we didn't force use of
  3717   // If we didn't find a matching Java thread and we didn't force use of
  3733   // heavyweight monitors, then the owner is the stack address of the
  3718   // heavyweight monitors, then the owner is the stack address of the
  3734   // Lock Word in the owning Java thread's stack.
  3719   // Lock Word in the owning Java thread's stack.
  3735   //
  3720   //
  3736   // We can't use Thread::is_lock_owned() or Thread::lock_is_in_stack() because
       
  3737   // those routines rely on the "current" stack pointer. That would be our
       
  3738   // stack pointer which is not relevant to the question. Instead we use the
       
  3739   // highest lock ever entered by the thread and find the thread that is
       
  3740   // higher than and closest to our target stack address.
       
  3741   //
       
  3742   address    least_diff = 0;
       
  3743   bool       least_diff_initialized = false;
       
  3744   JavaThread* the_owner = NULL;
  3721   JavaThread* the_owner = NULL;
  3745   {
  3722   {
  3746     MutexLockerEx ml(doLock ? Threads_lock : NULL);
  3723     MutexLockerEx ml(doLock ? Threads_lock : NULL);
  3747     ALL_JAVA_THREADS(q) {
  3724     ALL_JAVA_THREADS(q) {
  3748       address addr = q->highest_lock();
  3725       if (q->is_lock_owned(owner)) {
  3749       if (addr == NULL || addr < owner) continue;  // thread has entered no monitors or is too low
       
  3750       address diff = (address)(addr - owner);
       
  3751       if (!least_diff_initialized || diff < least_diff) {
       
  3752         least_diff_initialized = true;
       
  3753         least_diff = diff;
       
  3754         the_owner = q;
  3726         the_owner = q;
       
  3727         break;
  3755       }
  3728       }
  3756     }
  3729     }
  3757   }
  3730   }
  3758   assert(the_owner != NULL, "Did not find owning Java thread for lock word address");
  3731   assert(the_owner != NULL, "Did not find owning Java thread for lock word address");
  3759   return the_owner;
  3732   return the_owner;