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
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 (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_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ").",
374 os::current_thread_id());
375 log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
376 PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
377 os::current_thread_id(), p2i(stack_base() - stack_size()),
378 p2i(stack_base()), stack_size()/1024);
379
380 this->run();
381
382 // Note: at this point the thread object may already have deleted itself.
383 // So from here on do not refer to this.
384
385 log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ").",
386 os::current_thread_id());
387 }
388
389 Thread::~Thread() {
390 JFR_ONLY(Jfr::on_thread_destruct(this);)
391
392 // Notify the barrier set that a thread is being destroyed. Note that a barrier
393 // set might not be available if we encountered errors during bootstrapping.
394 BarrierSet* const barrier_set = BarrierSet::barrier_set();
395 if (barrier_set != NULL) {
396 barrier_set->on_thread_destroy(this);
397 }
398
399
400 // stack_base can be NULL if the thread is never started or exited before
401 // record_stack_base_and_size called. Although, we would like to ensure
402 // that all started threads do call record_stack_base_and_size(), there is
403 // not proper way to enforce that.
404 #if INCLUDE_NMT
405 if (_stack_base != NULL) {
406 MemTracker::release_thread_stack(stack_end(), stack_size());
407 #ifdef ASSERT
428 delete handle_area();
429 delete metadata_handles();
430
431 // SR_handler uses this as a termination indicator -
432 // needs to happen before os::free_thread()
433 delete _SR_lock;
434 _SR_lock = NULL;
435
436 // osthread() can be NULL, if creation of thread failed.
437 if (osthread() != NULL) os::free_thread(osthread());
438
439 // clear Thread::current if thread is deleting itself.
440 // Needed to ensure JNI correctly detects non-attached threads.
441 if (this == Thread::current()) {
442 clear_thread_current();
443 }
444
445 CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
446 }
447
448 #ifdef ASSERT
449 // A JavaThread is considered "dangling" if it is not the current
450 // thread, has been added the Threads list, the system is not at a
451 // safepoint and the Thread is not "protected".
452 //
453 void Thread::check_for_dangling_thread_pointer(Thread *thread) {
454 assert(!thread->is_Java_thread() || Thread::current() == thread ||
455 !((JavaThread *) thread)->on_thread_list() ||
456 SafepointSynchronize::is_at_safepoint() ||
457 ThreadsSMRSupport::is_a_protected_JavaThread_with_lock((JavaThread *) thread),
458 "possibility of dangling Thread pointer");
459 }
460 #endif
461
462 ThreadPriority Thread::get_priority(const Thread* const thread) {
463 ThreadPriority priority;
464 // Can return an error!
465 (void)os::get_priority(thread, priority);
466 assert(MinPriority <= priority && priority <= MaxPriority, "non-Java priority found");
467 return priority;
1374
1375 remaining = PeriodicTask::time_to_wait();
1376 if (remaining == 0) {
1377 // Last task was just disenrolled so loop around and wait until
1378 // another task gets enrolled
1379 continue;
1380 }
1381
1382 remaining -= time_slept;
1383 if (remaining <= 0) {
1384 break;
1385 }
1386 }
1387
1388 return time_slept;
1389 }
1390
1391 void WatcherThread::run() {
1392 assert(this == watcher_thread(), "just checking");
1393
1394 this->set_native_thread_name(this->name());
1395 this->set_active_handles(JNIHandleBlock::allocate_block());
1396 while (true) {
1397 assert(watcher_thread() == Thread::current(), "thread consistency check");
1398 assert(watcher_thread() == this, "thread consistency check");
1399
1400 // Calculate how long it'll be until the next PeriodicTask work
1401 // should be done, and sleep that amount of time.
1402 int time_waited = sleep();
1403
1404 if (VMError::is_error_reported()) {
1405 // A fatal error has happened, the error handler(VMError::report_and_die)
1406 // should abort JVM after creating an error log file. However in some
1407 // rare cases, the error handler itself might deadlock. Here periodically
1408 // check for error reporting timeouts, and if it happens, just proceed to
1409 // abort the VM.
1410
1411 // This code is in WatcherThread because WatcherThread wakes up
1412 // periodically so the fatal error handler doesn't need to do anything;
1413 // also because the WatcherThread is less likely to crash than other
1739 if (JVMCICounterSize > 0) {
1740 if (jvmci_counters_include(this)) {
1741 for (int i = 0; i < JVMCICounterSize; i++) {
1742 _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1743 }
1744 }
1745 FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1746 }
1747 #endif // INCLUDE_JVMCI
1748 }
1749
1750
1751 // The first routine called by a new Java thread
1752 void JavaThread::run() {
1753 // initialize thread-local alloc buffer related fields
1754 this->initialize_tlab();
1755
1756 // used to test validity of stack trace backs
1757 this->record_base_of_stack_pointer();
1758
1759 this->create_stack_guard_pages();
1760
1761 this->cache_global_variables();
1762
1763 // Thread is now sufficient initialized to be handled by the safepoint code as being
1764 // in the VM. Change thread state from _thread_new to _thread_in_vm
1765 ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1766
1767 assert(JavaThread::current() == this, "sanity check");
1768 assert(!Thread::current()->owns_locks(), "sanity check");
1769
1770 DTRACE_THREAD_PROBE(start, this);
1771
1772 // This operation might block. We call that after all safepoint checks for a new thread has
1773 // been completed.
1774 this->set_active_handles(JNIHandleBlock::allocate_block());
1775
1776 if (JvmtiExport::should_post_thread_life()) {
1777 JvmtiExport::post_thread_start(this);
1778 }
3701 _number_of_non_daemon_threads = 0;
3702
3703 // Initialize global data structures and create system classes in heap
3704 vm_init_globals();
3705
3706 #if INCLUDE_JVMCI
3707 if (JVMCICounterSize > 0) {
3708 JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
3709 memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
3710 } else {
3711 JavaThread::_jvmci_old_thread_counters = NULL;
3712 }
3713 #endif // INCLUDE_JVMCI
3714
3715 // Attach the main thread to this os thread
3716 JavaThread* main_thread = new JavaThread();
3717 main_thread->set_thread_state(_thread_in_vm);
3718 main_thread->initialize_thread_current();
3719 // must do this before set_active_handles
3720 main_thread->record_stack_base_and_size();
3721 main_thread->register_thread_stack_with_NMT();
3722 main_thread->set_active_handles(JNIHandleBlock::allocate_block());
3723
3724 if (!main_thread->set_as_starting_thread()) {
3725 vm_shutdown_during_initialization(
3726 "Failed necessary internal allocation. Out of swap space");
3727 main_thread->smr_delete();
3728 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3729 return JNI_ENOMEM;
3730 }
3731
3732 // Enable guard page *after* os::create_main_thread(), otherwise it would
3733 // crash Linux VM, see notes in os_linux.cpp.
3734 main_thread->create_stack_guard_pages();
3735
3736 // Initialize Java-Level synchronization subsystem
3737 ObjectMonitor::Initialize();
3738
3739 // Initialize global modules
3740 jint status = init_globals();
3741 if (status != JNI_OK) {
|