src/hotspot/share/runtime/thread.cpp
changeset 52302 912b79d983d9
parent 52297 99962c340e73
child 52341 2b58b8e1d28f
equal deleted inserted replaced
52301:fbfcdc5bf694 52302:912b79d983d9
   336 #endif
   336 #endif
   337   ThreadLocalStorage::set_thread(NULL);
   337   ThreadLocalStorage::set_thread(NULL);
   338 }
   338 }
   339 
   339 
   340 void Thread::record_stack_base_and_size() {
   340 void Thread::record_stack_base_and_size() {
       
   341   // Note: at this point, Thread object is not yet initialized. Do not rely on
       
   342   // any members being initialized. Do not rely on Thread::current() being set.
       
   343   // If possible, refrain from doing anything which may crash or assert since
       
   344   // quite probably those crash dumps will be useless.
   341   set_stack_base(os::current_stack_base());
   345   set_stack_base(os::current_stack_base());
   342   set_stack_size(os::current_stack_size());
   346   set_stack_size(os::current_stack_size());
   343   // CR 7190089: on Solaris, primordial thread's stack is adjusted
   347 
   344   // in initialize_thread(). Without the adjustment, stack size is
   348 #ifdef SOLARIS
   345   // incorrect if stack is set to unlimited (ulimit -s unlimited).
   349   if (os::is_primordial_thread()) {
   346   // So far, only Solaris has real implementation of initialize_thread().
   350     os::Solaris::correct_stack_boundaries_for_primordial_thread(this);
   347   //
   351   }
   348   // set up any platform-specific state.
   352 #endif
   349   os::initialize_thread(this);
       
   350 
   353 
   351   // Set stack limits after thread is initialized.
   354   // Set stack limits after thread is initialized.
   352   if (is_Java_thread()) {
   355   if (is_Java_thread()) {
   353     ((JavaThread*) this)->set_stack_overflow_limit();
   356     ((JavaThread*) this)->set_stack_overflow_limit();
   354     ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
   357     ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
   355   }
   358   }
       
   359 }
       
   360 
   356 #if INCLUDE_NMT
   361 #if INCLUDE_NMT
   357   // record thread's native stack, stack grows downward
   362 void Thread::register_thread_stack_with_NMT() {
   358   MemTracker::record_thread_stack(stack_end(), stack_size());
   363   MemTracker::record_thread_stack(stack_end(), stack_size());
       
   364 }
   359 #endif // INCLUDE_NMT
   365 #endif // INCLUDE_NMT
       
   366 
       
   367 void Thread::call_run() {
       
   368   // At this point, Thread object should be fully initialized and
       
   369   // Thread::current() should be set.
       
   370 
       
   371   register_thread_stack_with_NMT();
       
   372 
   360   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
   373   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
   361     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
   374     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
   362     os::current_thread_id(), p2i(stack_base() - stack_size()),
   375     os::current_thread_id(), p2i(stack_base() - stack_size()),
   363     p2i(stack_base()), stack_size()/1024);
   376     p2i(stack_base()), stack_size()/1024);
   364 }
   377 
   365 
   378   // Invoke <ChildClass>::run()
       
   379   this->run();
       
   380   // Returned from <ChildClass>::run(). Thread finished.
       
   381 
       
   382   // Note: at this point the thread object may already have deleted itself.
       
   383   // So from here on do not dereference *this*.
       
   384 
       
   385   // If a thread has not deleted itself ("delete this") as part of its
       
   386   // termination sequence, we have to ensure thread-local-storage is
       
   387   // cleared before we actually terminate. No threads should ever be
       
   388   // deleted asynchronously with respect to their termination.
       
   389   if (Thread::current_or_null_safe() != NULL) {
       
   390     assert(Thread::current_or_null_safe() == this, "current thread is wrong");
       
   391     Thread::clear_thread_current();
       
   392   }
       
   393 
       
   394 }
   366 
   395 
   367 Thread::~Thread() {
   396 Thread::~Thread() {
   368   JFR_ONLY(Jfr::on_thread_destruct(this);)
   397   JFR_ONLY(Jfr::on_thread_destruct(this);)
   369 
   398 
   370   // Notify the barrier set that a thread is being destroyed. Note that a barrier
   399   // Notify the barrier set that a thread is being destroyed. Note that a barrier
   415   if (osthread() != NULL) os::free_thread(osthread());
   444   if (osthread() != NULL) os::free_thread(osthread());
   416 
   445 
   417   // clear Thread::current if thread is deleting itself.
   446   // clear Thread::current if thread is deleting itself.
   418   // Needed to ensure JNI correctly detects non-attached threads.
   447   // Needed to ensure JNI correctly detects non-attached threads.
   419   if (this == Thread::current()) {
   448   if (this == Thread::current()) {
   420     clear_thread_current();
   449     Thread::clear_thread_current();
   421   }
   450   }
   422 
   451 
   423   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
   452   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
   424 }
       
   425 
       
   426 // NOTE: dummy function for assertion purpose.
       
   427 void Thread::run() {
       
   428   ShouldNotReachHere();
       
   429 }
   453 }
   430 
   454 
   431 #ifdef ASSERT
   455 #ifdef ASSERT
   432 // A JavaThread is considered "dangling" if it is not the current
   456 // A JavaThread is considered "dangling" if it is not the current
   433 // thread, has been added the Threads list, the system is not at a
   457 // thread, has been added the Threads list, the system is not at a
  1372 }
  1396 }
  1373 
  1397 
  1374 void WatcherThread::run() {
  1398 void WatcherThread::run() {
  1375   assert(this == watcher_thread(), "just checking");
  1399   assert(this == watcher_thread(), "just checking");
  1376 
  1400 
  1377   this->record_stack_base_and_size();
       
  1378   this->set_native_thread_name(this->name());
  1401   this->set_native_thread_name(this->name());
  1379   this->set_active_handles(JNIHandleBlock::allocate_block());
  1402   this->set_active_handles(JNIHandleBlock::allocate_block());
  1380   while (true) {
  1403   while (true) {
  1381     assert(watcher_thread() == Thread::current(), "thread consistency check");
  1404     assert(watcher_thread() == Thread::current(), "thread consistency check");
  1382     assert(watcher_thread() == this, "thread consistency check");
  1405     assert(watcher_thread() == this, "thread consistency check");
  1738   this->initialize_tlab();
  1761   this->initialize_tlab();
  1739 
  1762 
  1740   // used to test validity of stack trace backs
  1763   // used to test validity of stack trace backs
  1741   this->record_base_of_stack_pointer();
  1764   this->record_base_of_stack_pointer();
  1742 
  1765 
  1743   // Record real stack base and size.
       
  1744   this->record_stack_base_and_size();
       
  1745 
       
  1746   this->create_stack_guard_pages();
  1766   this->create_stack_guard_pages();
  1747 
  1767 
  1748   this->cache_global_variables();
  1768   this->cache_global_variables();
  1749 
  1769 
  1750   // Thread is now sufficient initialized to be handled by the safepoint code as being
  1770   // Thread is now sufficient initialized to be handled by the safepoint code as being
  3707   JavaThread* main_thread = new JavaThread();
  3727   JavaThread* main_thread = new JavaThread();
  3708   main_thread->set_thread_state(_thread_in_vm);
  3728   main_thread->set_thread_state(_thread_in_vm);
  3709   main_thread->initialize_thread_current();
  3729   main_thread->initialize_thread_current();
  3710   // must do this before set_active_handles
  3730   // must do this before set_active_handles
  3711   main_thread->record_stack_base_and_size();
  3731   main_thread->record_stack_base_and_size();
       
  3732   main_thread->register_thread_stack_with_NMT();
  3712   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
  3733   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
  3713 
  3734 
  3714   if (!main_thread->set_as_starting_thread()) {
  3735   if (!main_thread->set_as_starting_thread()) {
  3715     vm_shutdown_during_initialization(
  3736     vm_shutdown_during_initialization(
  3716                                       "Failed necessary internal allocation. Out of swap space");
  3737                                       "Failed necessary internal allocation. Out of swap space");