< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page
rev 52211 : [mq]: tinit


 321 
 322 void Thread::initialize_thread_current() {
 323 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 324   assert(_thr_current == NULL, "Thread::current already initialized");
 325   _thr_current = this;
 326 #endif
 327   assert(ThreadLocalStorage::thread() == NULL, "ThreadLocalStorage::thread already initialized");
 328   ThreadLocalStorage::set_thread(this);
 329   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 330 }
 331 
 332 void Thread::clear_thread_current() {
 333   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 334 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 335   _thr_current = NULL;
 336 #endif
 337   ThreadLocalStorage::set_thread(NULL);
 338 }
 339 
 340 void Thread::record_stack_base_and_size() {




 341   set_stack_base(os::current_stack_base());
 342   set_stack_size(os::current_stack_size());
 343   // CR 7190089: on Solaris, primordial thread's stack is adjusted
 344   // in initialize_thread(). Without the adjustment, stack size is
 345   // incorrect if stack is set to unlimited (ulimit -s unlimited).
 346   // So far, only Solaris has real implementation of initialize_thread().
 347   //
 348   // set up any platform-specific state.
 349   os::initialize_thread(this);
 350 
 351   // Set stack limits after thread is initialized.
 352   if (is_Java_thread()) {
 353     ((JavaThread*) this)->set_stack_overflow_limit();
 354     ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
 355   }


 356 #if INCLUDE_NMT
 357   // record thread's native stack, stack grows downward
 358   MemTracker::record_thread_stack(stack_end(), stack_size());

 359 #endif // INCLUDE_NMT







 360   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
 361     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
 362     os::current_thread_id(), p2i(stack_base() - stack_size()),
 363     p2i(stack_base()), stack_size()/1024);
 364 }
 365 

















 366 
 367 Thread::~Thread() {
 368   JFR_ONLY(Jfr::on_thread_destruct(this);)
 369 
 370   // Notify the barrier set that a thread is being destroyed. Note that a barrier
 371   // set might not be available if we encountered errors during bootstrapping.
 372   BarrierSet* const barrier_set = BarrierSet::barrier_set();
 373   if (barrier_set != NULL) {
 374     barrier_set->on_thread_destroy(this);
 375   }
 376 
 377 
 378   // stack_base can be NULL if the thread is never started or exited before
 379   // record_stack_base_and_size called. Although, we would like to ensure
 380   // that all started threads do call record_stack_base_and_size(), there is
 381   // not proper way to enforce that.
 382 #if INCLUDE_NMT
 383   if (_stack_base != NULL) {
 384     MemTracker::release_thread_stack(stack_end(), stack_size());
 385 #ifdef ASSERT


 400   // We NULL out the fields for good hygiene.
 401   ParkEvent::Release(_ParkEvent); _ParkEvent   = NULL;
 402   ParkEvent::Release(_SleepEvent); _SleepEvent  = NULL;
 403   ParkEvent::Release(_MutexEvent); _MutexEvent  = NULL;
 404   ParkEvent::Release(_MuxEvent); _MuxEvent    = NULL;
 405 
 406   delete handle_area();
 407   delete metadata_handles();
 408 
 409   // SR_handler uses this as a termination indicator -
 410   // needs to happen before os::free_thread()
 411   delete _SR_lock;
 412   _SR_lock = NULL;
 413 
 414   // osthread() can be NULL, if creation of thread failed.
 415   if (osthread() != NULL) os::free_thread(osthread());
 416 
 417   // clear Thread::current if thread is deleting itself.
 418   // Needed to ensure JNI correctly detects non-attached threads.
 419   if (this == Thread::current()) {
 420     clear_thread_current();
 421   }
 422 
 423   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 }
 430 
 431 #ifdef ASSERT
 432 // 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
 434 // safepoint and the Thread is not "protected".
 435 //
 436 void Thread::check_for_dangling_thread_pointer(Thread *thread) {
 437   assert(!thread->is_Java_thread() || Thread::current() == thread ||
 438          !((JavaThread *) thread)->on_thread_list() ||
 439          SafepointSynchronize::is_at_safepoint() ||
 440          ThreadsSMRSupport::is_a_protected_JavaThread_with_lock((JavaThread *) thread),
 441          "possibility of dangling Thread pointer");
 442 }
 443 #endif
 444 
 445 ThreadPriority Thread::get_priority(const Thread* const thread) {
 446   ThreadPriority priority;
 447   // Can return an error!
 448   (void)os::get_priority(thread, priority);
 449   assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
 450   return priority;


1357 
1358     remaining = PeriodicTask::time_to_wait();
1359     if (remaining == 0) {
1360       // Last task was just disenrolled so loop around and wait until
1361       // another task gets enrolled
1362       continue;
1363     }
1364 
1365     remaining -= time_slept;
1366     if (remaining <= 0) {
1367       break;
1368     }
1369   }
1370 
1371   return time_slept;
1372 }
1373 
1374 void WatcherThread::run() {
1375   assert(this == watcher_thread(), "just checking");
1376 
1377   this->record_stack_base_and_size();
1378   this->set_native_thread_name(this->name());
1379   this->set_active_handles(JNIHandleBlock::allocate_block());
1380   while (true) {
1381     assert(watcher_thread() == Thread::current(), "thread consistency check");
1382     assert(watcher_thread() == this, "thread consistency check");
1383 
1384     // Calculate how long it'll be until the next PeriodicTask work
1385     // should be done, and sleep that amount of time.
1386     int time_waited = sleep();
1387 
1388     if (VMError::is_error_reported()) {
1389       // A fatal error has happened, the error handler(VMError::report_and_die)
1390       // should abort JVM after creating an error log file. However in some
1391       // rare cases, the error handler itself might deadlock. Here periodically
1392       // check for error reporting timeouts, and if it happens, just proceed to
1393       // abort the VM.
1394 
1395       // This code is in WatcherThread because WatcherThread wakes up
1396       // periodically so the fatal error handler doesn't need to do anything;
1397       // also because the WatcherThread is less likely to crash than other


1723   if (JVMCICounterSize > 0) {
1724     if (jvmci_counters_include(this)) {
1725       for (int i = 0; i < JVMCICounterSize; i++) {
1726         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1727       }
1728     }
1729     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1730   }
1731 #endif // INCLUDE_JVMCI
1732 }
1733 
1734 
1735 // The first routine called by a new Java thread
1736 void JavaThread::run() {
1737   // initialize thread-local alloc buffer related fields
1738   this->initialize_tlab();
1739 
1740   // used to test validity of stack trace backs
1741   this->record_base_of_stack_pointer();
1742 
1743   // Record real stack base and size.
1744   this->record_stack_base_and_size();
1745 
1746   this->create_stack_guard_pages();
1747 
1748   this->cache_global_variables();
1749 
1750   // Thread is now sufficient initialized to be handled by the safepoint code as being
1751   // in the VM. Change thread state from _thread_new to _thread_in_vm
1752   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1753 
1754   assert(JavaThread::current() == this, "sanity check");
1755   assert(!Thread::current()->owns_locks(), "sanity check");
1756 
1757   DTRACE_THREAD_PROBE(start, this);
1758 
1759   // This operation might block. We call that after all safepoint checks for a new thread has
1760   // been completed.
1761   this->set_active_handles(JNIHandleBlock::allocate_block());
1762 
1763   if (JvmtiExport::should_post_thread_life()) {
1764     JvmtiExport::post_thread_start(this);
1765   }


3688   _number_of_non_daemon_threads = 0;
3689 
3690   // Initialize global data structures and create system classes in heap
3691   vm_init_globals();
3692 
3693 #if INCLUDE_JVMCI
3694   if (JVMCICounterSize > 0) {
3695     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3696     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3697   } else {
3698     JavaThread::_jvmci_old_thread_counters = NULL;
3699   }
3700 #endif // INCLUDE_JVMCI
3701 
3702   // Attach the main thread to this os thread
3703   JavaThread* main_thread = new JavaThread();
3704   main_thread->set_thread_state(_thread_in_vm);
3705   main_thread->initialize_thread_current();
3706   // must do this before set_active_handles
3707   main_thread->record_stack_base_and_size();

3708   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3709 
3710   if (!main_thread->set_as_starting_thread()) {
3711     vm_shutdown_during_initialization(
3712                                       "Failed necessary internal allocation. Out of swap space");
3713     main_thread->smr_delete();
3714     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3715     return JNI_ENOMEM;
3716   }
3717 
3718   // Enable guard page *after* os::create_main_thread(), otherwise it would
3719   // crash Linux VM, see notes in os_linux.cpp.
3720   main_thread->create_stack_guard_pages();
3721 
3722   // Initialize Java-Level synchronization subsystem
3723   ObjectMonitor::Initialize();
3724 
3725   // Initialize global modules
3726   jint status = init_globals();
3727   if (status != JNI_OK) {




 321 
 322 void Thread::initialize_thread_current() {
 323 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 324   assert(_thr_current == NULL, "Thread::current already initialized");
 325   _thr_current = this;
 326 #endif
 327   assert(ThreadLocalStorage::thread() == NULL, "ThreadLocalStorage::thread already initialized");
 328   ThreadLocalStorage::set_thread(this);
 329   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 330 }
 331 
 332 void Thread::clear_thread_current() {
 333   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
 334 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 335   _thr_current = NULL;
 336 #endif
 337   ThreadLocalStorage::set_thread(NULL);
 338 }
 339 
 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.
 345   set_stack_base(os::current_stack_base());
 346   set_stack_size(os::current_stack_size());
 347 
 348 #ifdef SOLARIS
 349   if (os::is_primordial_thread()) {
 350     os::Solaris::correct_stack_boundaries_for_primordial_thread(this);
 351   }
 352 #endif

 353 
 354   // Set stack limits after thread is initialized.
 355   if (is_Java_thread()) {
 356     ((JavaThread*) this)->set_stack_overflow_limit();
 357     ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
 358   }
 359 }
 360 
 361 #if INCLUDE_NMT
 362 void Thread::register_thread_stack_with_NMT() {
 363   MemTracker::record_thread_stack(stack_end(), stack_size());
 364 }
 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 
 373   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
 374     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
 375     os::current_thread_id(), p2i(stack_base() - stack_size()),
 376     p2i(stack_base()), stack_size()/1024);

 377 
 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 }
 395 
 396 Thread::~Thread() {
 397   JFR_ONLY(Jfr::on_thread_destruct(this);)
 398 
 399   // Notify the barrier set that a thread is being destroyed. Note that a barrier
 400   // set might not be available if we encountered errors during bootstrapping.
 401   BarrierSet* const barrier_set = BarrierSet::barrier_set();
 402   if (barrier_set != NULL) {
 403     barrier_set->on_thread_destroy(this);
 404   }
 405 
 406 
 407   // stack_base can be NULL if the thread is never started or exited before
 408   // record_stack_base_and_size called. Although, we would like to ensure
 409   // that all started threads do call record_stack_base_and_size(), there is
 410   // not proper way to enforce that.
 411 #if INCLUDE_NMT
 412   if (_stack_base != NULL) {
 413     MemTracker::release_thread_stack(stack_end(), stack_size());
 414 #ifdef ASSERT


 429   // We NULL out the fields for good hygiene.
 430   ParkEvent::Release(_ParkEvent); _ParkEvent   = NULL;
 431   ParkEvent::Release(_SleepEvent); _SleepEvent  = NULL;
 432   ParkEvent::Release(_MutexEvent); _MutexEvent  = NULL;
 433   ParkEvent::Release(_MuxEvent); _MuxEvent    = NULL;
 434 
 435   delete handle_area();
 436   delete metadata_handles();
 437 
 438   // SR_handler uses this as a termination indicator -
 439   // needs to happen before os::free_thread()
 440   delete _SR_lock;
 441   _SR_lock = NULL;
 442 
 443   // osthread() can be NULL, if creation of thread failed.
 444   if (osthread() != NULL) os::free_thread(osthread());
 445 
 446   // clear Thread::current if thread is deleting itself.
 447   // Needed to ensure JNI correctly detects non-attached threads.
 448   if (this == Thread::current()) {
 449     Thread::clear_thread_current();
 450   }
 451 
 452   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
 453 }
 454 





 455 #ifdef ASSERT
 456 // A JavaThread is considered "dangling" if it is not the current
 457 // thread, has been added the Threads list, the system is not at a
 458 // safepoint and the Thread is not "protected".
 459 //
 460 void Thread::check_for_dangling_thread_pointer(Thread *thread) {
 461   assert(!thread->is_Java_thread() || Thread::current() == thread ||
 462          !((JavaThread *) thread)->on_thread_list() ||
 463          SafepointSynchronize::is_at_safepoint() ||
 464          ThreadsSMRSupport::is_a_protected_JavaThread_with_lock((JavaThread *) thread),
 465          "possibility of dangling Thread pointer");
 466 }
 467 #endif
 468 
 469 ThreadPriority Thread::get_priority(const Thread* const thread) {
 470   ThreadPriority priority;
 471   // Can return an error!
 472   (void)os::get_priority(thread, priority);
 473   assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
 474   return priority;


1381 
1382     remaining = PeriodicTask::time_to_wait();
1383     if (remaining == 0) {
1384       // Last task was just disenrolled so loop around and wait until
1385       // another task gets enrolled
1386       continue;
1387     }
1388 
1389     remaining -= time_slept;
1390     if (remaining <= 0) {
1391       break;
1392     }
1393   }
1394 
1395   return time_slept;
1396 }
1397 
1398 void WatcherThread::run() {
1399   assert(this == watcher_thread(), "just checking");
1400 

1401   this->set_native_thread_name(this->name());
1402   this->set_active_handles(JNIHandleBlock::allocate_block());
1403   while (true) {
1404     assert(watcher_thread() == Thread::current(), "thread consistency check");
1405     assert(watcher_thread() == this, "thread consistency check");
1406 
1407     // Calculate how long it'll be until the next PeriodicTask work
1408     // should be done, and sleep that amount of time.
1409     int time_waited = sleep();
1410 
1411     if (VMError::is_error_reported()) {
1412       // A fatal error has happened, the error handler(VMError::report_and_die)
1413       // should abort JVM after creating an error log file. However in some
1414       // rare cases, the error handler itself might deadlock. Here periodically
1415       // check for error reporting timeouts, and if it happens, just proceed to
1416       // abort the VM.
1417 
1418       // This code is in WatcherThread because WatcherThread wakes up
1419       // periodically so the fatal error handler doesn't need to do anything;
1420       // also because the WatcherThread is less likely to crash than other


1746   if (JVMCICounterSize > 0) {
1747     if (jvmci_counters_include(this)) {
1748       for (int i = 0; i < JVMCICounterSize; i++) {
1749         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1750       }
1751     }
1752     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1753   }
1754 #endif // INCLUDE_JVMCI
1755 }
1756 
1757 
1758 // The first routine called by a new Java thread
1759 void JavaThread::run() {
1760   // initialize thread-local alloc buffer related fields
1761   this->initialize_tlab();
1762 
1763   // used to test validity of stack trace backs
1764   this->record_base_of_stack_pointer();
1765 



1766   this->create_stack_guard_pages();
1767 
1768   this->cache_global_variables();
1769 
1770   // Thread is now sufficient initialized to be handled by the safepoint code as being
1771   // in the VM. Change thread state from _thread_new to _thread_in_vm
1772   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1773 
1774   assert(JavaThread::current() == this, "sanity check");
1775   assert(!Thread::current()->owns_locks(), "sanity check");
1776 
1777   DTRACE_THREAD_PROBE(start, this);
1778 
1779   // This operation might block. We call that after all safepoint checks for a new thread has
1780   // been completed.
1781   this->set_active_handles(JNIHandleBlock::allocate_block());
1782 
1783   if (JvmtiExport::should_post_thread_life()) {
1784     JvmtiExport::post_thread_start(this);
1785   }


3708   _number_of_non_daemon_threads = 0;
3709 
3710   // Initialize global data structures and create system classes in heap
3711   vm_init_globals();
3712 
3713 #if INCLUDE_JVMCI
3714   if (JVMCICounterSize > 0) {
3715     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3716     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3717   } else {
3718     JavaThread::_jvmci_old_thread_counters = NULL;
3719   }
3720 #endif // INCLUDE_JVMCI
3721 
3722   // Attach the main thread to this os thread
3723   JavaThread* main_thread = new JavaThread();
3724   main_thread->set_thread_state(_thread_in_vm);
3725   main_thread->initialize_thread_current();
3726   // must do this before set_active_handles
3727   main_thread->record_stack_base_and_size();
3728   main_thread->register_thread_stack_with_NMT();
3729   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3730 
3731   if (!main_thread->set_as_starting_thread()) {
3732     vm_shutdown_during_initialization(
3733                                       "Failed necessary internal allocation. Out of swap space");
3734     main_thread->smr_delete();
3735     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3736     return JNI_ENOMEM;
3737   }
3738 
3739   // Enable guard page *after* os::create_main_thread(), otherwise it would
3740   // crash Linux VM, see notes in os_linux.cpp.
3741   main_thread->create_stack_guard_pages();
3742 
3743   // Initialize Java-Level synchronization subsystem
3744   ObjectMonitor::Initialize();
3745 
3746   // Initialize global modules
3747   jint status = init_globals();
3748   if (status != JNI_OK) {


< prev index next >