78 #include "runtime/threadCritical.hpp"
79 #include "runtime/threadLocalStorage.hpp"
80 #include "runtime/vframe.hpp"
81 #include "runtime/vframeArray.hpp"
82 #include "runtime/vframe_hp.hpp"
83 #include "runtime/vmThread.hpp"
84 #include "runtime/vm_operations.hpp"
85 #include "runtime/vm_version.hpp"
86 #include "services/attachListener.hpp"
87 #include "services/management.hpp"
88 #include "services/memTracker.hpp"
89 #include "services/threadService.hpp"
90 #include "trace/traceMacros.hpp"
91 #include "trace/tracing.hpp"
92 #include "utilities/defaultStream.hpp"
93 #include "utilities/dtrace.hpp"
94 #include "utilities/events.hpp"
95 #include "utilities/macros.hpp"
96 #include "utilities/preserveException.hpp"
97 #if INCLUDE_ALL_GCS
98 #include "gc/cms/concurrentMarkSweepThread.hpp"
99 #include "gc/g1/concurrentMarkThread.inline.hpp"
100 #include "gc/parallel/pcTasks.hpp"
101 #endif // INCLUDE_ALL_GCS
102 #ifdef COMPILER1
103 #include "c1/c1_Compiler.hpp"
104 #endif
105 #ifdef COMPILER2
106 #include "opto/c2compiler.hpp"
107 #include "opto/idealGraphPrinter.hpp"
108 #endif
109 #if INCLUDE_RTM_OPT
110 #include "runtime/rtmLocking.hpp"
111 #endif
112
113 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
114
115 #ifdef DTRACE_ENABLED
116
117 // Only bother with this argument setup if dtrace is available
245 _hashStateZ = 0x8767; // (int)(3579807591LL & 0xffff) ;
246 _hashStateW = 273326509;
247
248 _OnTrap = 0;
249 _schedctl = NULL;
250 _Stalled = 0;
251 _TypeTag = 0x2BAD;
252
253 // Many of the following fields are effectively final - immutable
254 // Note that nascent threads can't use the Native Monitor-Mutex
255 // construct until the _MutexEvent is initialized ...
256 // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents
257 // we might instead use a stack of ParkEvents that we could provision on-demand.
258 // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
259 // and ::Release()
260 _ParkEvent = ParkEvent::Allocate(this);
261 _SleepEvent = ParkEvent::Allocate(this);
262 _MutexEvent = ParkEvent::Allocate(this);
263 _MuxEvent = ParkEvent::Allocate(this);
264
265 #ifdef CHECK_UNHANDLED_OOPS
266 if (CheckUnhandledOops) {
267 _unhandled_oops = new UnhandledOops(this);
268 }
269 #endif // CHECK_UNHANDLED_OOPS
270 #ifdef ASSERT
271 if (UseBiasedLocking) {
272 assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
273 assert(this == _real_malloc_address ||
274 this == (void*) align_size_up((intptr_t) _real_malloc_address, markOopDesc::biased_lock_alignment),
275 "bug in forced alignment of thread objects");
276 }
277 #endif // ASSERT
278 }
279
280 // Non-inlined version to be used where thread.inline.hpp shouldn't be included.
281 Thread* Thread::current_noinline() {
282 return Thread::current();
283 }
284
1454 set_thread_profiler(pp);
1455 }
1456
1457 // Setup safepoint state info for this thread
1458 ThreadSafepointState::create(this);
1459
1460 debug_only(_java_call_counter = 0);
1461
1462 // JVMTI PopFrame support
1463 _popframe_condition = popframe_inactive;
1464 _popframe_preserved_args = NULL;
1465 _popframe_preserved_args_size = 0;
1466 _frames_to_pop_failed_realloc = 0;
1467
1468 pd_initialize();
1469 }
1470
1471 #if INCLUDE_ALL_GCS
1472 SATBMarkQueueSet JavaThread::_satb_mark_queue_set;
1473 DirtyCardQueueSet JavaThread::_dirty_card_queue_set;
1474 #endif // INCLUDE_ALL_GCS
1475
1476 JavaThread::JavaThread(bool is_attaching_via_jni) :
1477 Thread()
1478 #if INCLUDE_ALL_GCS
1479 , _satb_mark_queue(&_satb_mark_queue_set),
1480 _dirty_card_queue(&_dirty_card_queue_set)
1481 #endif // INCLUDE_ALL_GCS
1482 {
1483 initialize();
1484 if (is_attaching_via_jni) {
1485 _jni_attach_state = _attaching_via_jni;
1486 } else {
1487 _jni_attach_state = _not_attaching_via_jni;
1488 }
1489 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1490 }
1491
1492 bool JavaThread::reguard_stack(address cur_sp) {
1493 if (_stack_guard_state != stack_guard_yellow_disabled) {
1494 return true; // Stack already guarded or guard pages not needed.
1495 }
1496
1497 if (register_stack_overflow()) {
1498 // For those architectures which have separate register and
1499 // memory stacks, we must check the register stack to see if
1500 // it has overflowed.
1518
1519
1520 void JavaThread::block_if_vm_exited() {
1521 if (_terminated == _vm_exited) {
1522 // _vm_exited is set at safepoint, and Threads_lock is never released
1523 // we will block here forever
1524 Threads_lock->lock_without_safepoint_check();
1525 ShouldNotReachHere();
1526 }
1527 }
1528
1529
1530 // Remove this ifdef when C1 is ported to the compiler interface.
1531 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1532 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1533
1534 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1535 Thread()
1536 #if INCLUDE_ALL_GCS
1537 , _satb_mark_queue(&_satb_mark_queue_set),
1538 _dirty_card_queue(&_dirty_card_queue_set)
1539 #endif // INCLUDE_ALL_GCS
1540 {
1541 initialize();
1542 _jni_attach_state = _not_attaching_via_jni;
1543 set_entry_point(entry_point);
1544 // Create the native thread itself.
1545 // %note runtime_23
1546 os::ThreadType thr_type = os::java_thread;
1547 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1548 os::java_thread;
1549 os::create_thread(this, thr_type, stack_sz);
1550 // The _osthread may be NULL here because we ran out of memory (too many threads active).
1551 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1552 // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1553 // the exception consists of creating the exception object & initializing it, initialization
1554 // will leave the VM via a JavaCall and then all locks must be unlocked).
1555 //
1556 // The thread is still suspended when we reach here. Thread must be explicit started
1557 // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1558 // by calling Threads:add. The reason why this is not done here, is because the thread
1653 // Note: Due to JVM_StopThread we can have pending exceptions already!
1654 if (!this->has_pending_exception() &&
1655 !java_lang_Thread::is_stillborn(this->threadObj())) {
1656 {
1657 ResourceMark rm(this);
1658 this->set_native_thread_name(this->get_thread_name());
1659 }
1660 HandleMark hm(this);
1661 this->entry_point()(this, this);
1662 }
1663
1664 DTRACE_THREAD_PROBE(stop, this);
1665
1666 this->exit(false);
1667 delete this;
1668 }
1669
1670
1671 static void ensure_join(JavaThread* thread) {
1672 // We do not need to grap the Threads_lock, since we are operating on ourself.
1673 Handle threadObj(thread, thread->threadObj());
1674 assert(threadObj.not_null(), "java thread object must exist");
1675 ObjectLocker lock(threadObj, thread);
1676 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1677 thread->clear_pending_exception();
1678 // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
1679 java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1680 // Clear the native thread instance - this makes isAlive return false and allows the join()
1681 // to complete once we've done the notify_all below
1682 java_lang_Thread::set_thread(threadObj(), NULL);
1683 lock.notify_all(thread);
1684 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1685 thread->clear_pending_exception();
1686 }
1687
1688
1689 // For any new cleanup additions, please check to see if they need to be applied to
1690 // cleanup_failed_attach_current_thread as well.
1691 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1692 assert(this == JavaThread::current(), "thread consistency check");
1693
1847 remove_stack_guard_pages();
1848
1849 if (UseTLAB) {
1850 tlab().make_parsable(true); // retire TLAB
1851 }
1852
1853 if (JvmtiEnv::environments_might_exist()) {
1854 JvmtiExport::cleanup_thread(this);
1855 }
1856
1857 // We must flush any deferred card marks before removing a thread from
1858 // the list of active threads.
1859 Universe::heap()->flush_deferred_store_barrier(this);
1860 assert(deferred_card_mark().is_empty(), "Should have been flushed");
1861
1862 #if INCLUDE_ALL_GCS
1863 // We must flush the G1-related buffers before removing a thread
1864 // from the list of active threads. We must do this after any deferred
1865 // card marks have been flushed (above) so that any entries that are
1866 // added to the thread's dirty card queue as a result are not lost.
1867 if (UseG1GC) {
1868 flush_barrier_queues();
1869 }
1870 #endif // INCLUDE_ALL_GCS
1871
1872 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1873 Threads::remove(this);
1874 }
1875
1876 #if INCLUDE_ALL_GCS
1877 // Flush G1-related queues.
1878 void JavaThread::flush_barrier_queues() {
1879 satb_mark_queue().flush();
1880 dirty_card_queue().flush();
1881 }
1882
1883 void JavaThread::initialize_queues() {
1884 assert(!SafepointSynchronize::is_at_safepoint(),
1885 "we should not be at a safepoint");
1886
1887 ObjPtrQueue& satb_queue = satb_mark_queue();
1888 SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1889 // The SATB queue should have been constructed with its active
1890 // field set to false.
1891 assert(!satb_queue.is_active(), "SATB queue should not be active");
1892 assert(satb_queue.is_empty(), "SATB queue should be empty");
1893 // If we are creating the thread during a marking cycle, we should
1894 // set the active field of the SATB queue to true.
1895 if (satb_queue_set.is_active()) {
1896 satb_queue.set_active(true);
1897 }
1898
1899 DirtyCardQueue& dirty_queue = dirty_card_queue();
1900 // The dirty card queue should have been constructed with its
1901 // active field set to true.
1902 assert(dirty_queue.is_active(), "dirty card queue should be active");
1903 }
1904 #endif // INCLUDE_ALL_GCS
1905
1906 void JavaThread::cleanup_failed_attach_current_thread() {
1907 if (get_thread_profiler() != NULL) {
1908 get_thread_profiler()->disengage();
1909 ResourceMark rm;
1910 get_thread_profiler()->print(get_thread_name());
1911 }
1912
1913 if (active_handles() != NULL) {
1914 JNIHandleBlock* block = active_handles();
1915 set_active_handles(NULL);
1916 JNIHandleBlock::release_block(block);
1917 }
1918
1919 if (free_handle_block() != NULL) {
1920 JNIHandleBlock* block = free_handle_block();
1921 set_free_handle_block(NULL);
1922 JNIHandleBlock::release_block(block);
1923 }
1924
1925 // These have to be removed while this is still a valid thread.
1926 remove_stack_guard_pages();
1927
1928 if (UseTLAB) {
1929 tlab().make_parsable(true); // retire TLAB, if any
1930 }
1931
1932 #if INCLUDE_ALL_GCS
1933 if (UseG1GC) {
1934 flush_barrier_queues();
1935 }
1936 #endif // INCLUDE_ALL_GCS
1937
1938 Threads::remove(this);
1939 delete this;
1940 }
1941
1942
1943
1944
1945 JavaThread* JavaThread::active() {
1946 Thread* thread = ThreadLocalStorage::thread();
1947 assert(thread != NULL, "just checking");
1948 if (thread->is_Java_thread()) {
1949 return (JavaThread*) thread;
1950 } else {
1951 assert(thread->is_VM_thread(), "this must be a vm thread");
1952 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1953 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1954 assert(ret->is_Java_thread(), "must be a Java thread");
1955 return ret;
2864 } else if (is_attaching_via_jni()) { // workaround for 6412693 - see 6404306
2865 name_str = "<no-name - thread is attaching>";
2866 } else {
2867 name_str = Thread::name();
2868 }
2869 } else {
2870 name_str = Thread::name();
2871 }
2872 assert(name_str != NULL, "unexpected NULL thread name");
2873 return name_str;
2874 }
2875
2876
2877 const char* JavaThread::get_threadgroup_name() const {
2878 debug_only(if (JavaThread::current() != this) assert_locked_or_safepoint(Threads_lock);)
2879 oop thread_obj = threadObj();
2880 if (thread_obj != NULL) {
2881 oop thread_group = java_lang_Thread::threadGroup(thread_obj);
2882 if (thread_group != NULL) {
2883 typeArrayOop name = java_lang_ThreadGroup::name(thread_group);
2884 // ThreadGroup.name can be null
2885 if (name != NULL) {
2886 const char* str = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
2887 return str;
2888 }
2889 }
2890 }
2891 return NULL;
2892 }
2893
2894 const char* JavaThread::get_parent_name() const {
2895 debug_only(if (JavaThread::current() != this) assert_locked_or_safepoint(Threads_lock);)
2896 oop thread_obj = threadObj();
2897 if (thread_obj != NULL) {
2898 oop thread_group = java_lang_Thread::threadGroup(thread_obj);
2899 if (thread_group != NULL) {
2900 oop parent = java_lang_ThreadGroup::parent(thread_group);
2901 if (parent != NULL) {
2902 typeArrayOop name = java_lang_ThreadGroup::name(parent);
2903 // ThreadGroup.name can be null
2904 if (name != NULL) {
2905 const char* str = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
2906 return str;
2907 }
2908 }
2909 }
2910 }
2911 return NULL;
2912 }
2913
2914 ThreadPriority JavaThread::java_priority() const {
2915 oop thr_oop = threadObj();
2916 if (thr_oop == NULL) return NormPriority; // Bootstrapping
2917 ThreadPriority priority = java_lang_Thread::priority(thr_oop);
2918 assert(MinPriority <= priority && priority <= MaxPriority, "sanity check");
2919 return priority;
2920 }
2921
2922 void JavaThread::prepare(jobject jni_thread, ThreadPriority prio) {
3495 Metaspace::post_initialize();
3496
3497 HOTSPOT_VM_INIT_END();
3498
3499 // record VM initialization completion time
3500 #if INCLUDE_MANAGEMENT
3501 Management::record_vm_init_completed();
3502 #endif // INCLUDE_MANAGEMENT
3503
3504 // Compute system loader. Note that this has to occur after set_init_completed, since
3505 // valid exceptions may be thrown in the process.
3506 // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
3507 // set_init_completed has just been called, causing exceptions not to be shortcut
3508 // anymore. We call vm_exit_during_initialization directly instead.
3509 SystemDictionary::compute_java_system_loader(CHECK_JNI_ERR);
3510
3511 #if INCLUDE_ALL_GCS
3512 // Support for ConcurrentMarkSweep. This should be cleaned up
3513 // and better encapsulated. The ugly nested if test would go away
3514 // once things are properly refactored. XXX YSR
3515 if (UseConcMarkSweepGC || UseG1GC) {
3516 if (UseConcMarkSweepGC) {
3517 ConcurrentMarkSweepThread::makeSurrogateLockerThread(CHECK_JNI_ERR);
3518 } else {
3519 ConcurrentMarkThread::makeSurrogateLockerThread(CHECK_JNI_ERR);
3520 }
3521 }
3522 #endif // INCLUDE_ALL_GCS
3523
3524 // Always call even when there are not JVMTI environments yet, since environments
3525 // may be attached late and JVMTI must track phases of VM execution
3526 JvmtiExport::enter_live_phase();
3527
3528 // Signal Dispatcher needs to be started before VMInit event is posted
3529 os::signal_init();
3530
3531 // Start Attach Listener if +StartAttachListener or it can't be started lazily
3532 if (!DisableAttachMechanism) {
3533 AttachListener::vm_start();
3534 if (StartAttachListener || AttachListener::init_at_startup()) {
3535 AttachListener::init();
3536 }
3537 }
3905 // complete. wait_for_ext_suspend_completion() will eventually
3906 // timeout, but that takes time. Making this wait a suspend-
3907 // equivalent condition solves that timeout problem.
3908 //
3909 Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
3910 Mutex::_as_suspend_equivalent_flag);
3911 }
3912
3913 // Hang forever on exit if we are reporting an error.
3914 if (ShowMessageBoxOnError && is_error_reported()) {
3915 os::infinite_sleep();
3916 }
3917 os::wait_for_keypress_at_exit();
3918
3919 // run Java level shutdown hooks
3920 thread->invoke_shutdown_hooks();
3921
3922 before_exit(thread);
3923
3924 thread->exit(true);
3925
3926 // Stop VM thread.
3927 {
3928 // 4945125 The vm thread comes to a safepoint during exit.
3929 // GC vm_operations can get caught at the safepoint, and the
3930 // heap is unparseable if they are caught. Grab the Heap_lock
3931 // to prevent this. The GC vm_operations will not be able to
3932 // queue until after the vm thread is dead. After this point,
3933 // we'll never emerge out of the safepoint before the VM exits.
3934
3935 MutexLocker ml(Heap_lock);
3936
3937 VMThread::wait_for_vm_thread_exit();
3938 assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
3939 VMThread::destroy();
3940 }
3941
3942 // clean up ideal graph printers
3943 #if defined(COMPILER2) && !defined(PRODUCT)
3944 IdealGraphPrinter::clean_up();
|
78 #include "runtime/threadCritical.hpp"
79 #include "runtime/threadLocalStorage.hpp"
80 #include "runtime/vframe.hpp"
81 #include "runtime/vframeArray.hpp"
82 #include "runtime/vframe_hp.hpp"
83 #include "runtime/vmThread.hpp"
84 #include "runtime/vm_operations.hpp"
85 #include "runtime/vm_version.hpp"
86 #include "services/attachListener.hpp"
87 #include "services/management.hpp"
88 #include "services/memTracker.hpp"
89 #include "services/threadService.hpp"
90 #include "trace/traceMacros.hpp"
91 #include "trace/tracing.hpp"
92 #include "utilities/defaultStream.hpp"
93 #include "utilities/dtrace.hpp"
94 #include "utilities/events.hpp"
95 #include "utilities/macros.hpp"
96 #include "utilities/preserveException.hpp"
97 #if INCLUDE_ALL_GCS
98 #include "gc/shenandoah/shenandoahConcurrentThread.hpp"
99 #include "gc/cms/concurrentMarkSweepThread.hpp"
100 #include "gc/g1/concurrentMarkThread.inline.hpp"
101 #include "gc/parallel/pcTasks.hpp"
102 #endif // INCLUDE_ALL_GCS
103 #ifdef COMPILER1
104 #include "c1/c1_Compiler.hpp"
105 #endif
106 #ifdef COMPILER2
107 #include "opto/c2compiler.hpp"
108 #include "opto/idealGraphPrinter.hpp"
109 #endif
110 #if INCLUDE_RTM_OPT
111 #include "runtime/rtmLocking.hpp"
112 #endif
113
114 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
115
116 #ifdef DTRACE_ENABLED
117
118 // Only bother with this argument setup if dtrace is available
246 _hashStateZ = 0x8767; // (int)(3579807591LL & 0xffff) ;
247 _hashStateW = 273326509;
248
249 _OnTrap = 0;
250 _schedctl = NULL;
251 _Stalled = 0;
252 _TypeTag = 0x2BAD;
253
254 // Many of the following fields are effectively final - immutable
255 // Note that nascent threads can't use the Native Monitor-Mutex
256 // construct until the _MutexEvent is initialized ...
257 // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents
258 // we might instead use a stack of ParkEvents that we could provision on-demand.
259 // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
260 // and ::Release()
261 _ParkEvent = ParkEvent::Allocate(this);
262 _SleepEvent = ParkEvent::Allocate(this);
263 _MutexEvent = ParkEvent::Allocate(this);
264 _MuxEvent = ParkEvent::Allocate(this);
265
266 _evacuating = false;
267
268 #ifdef CHECK_UNHANDLED_OOPS
269 if (CheckUnhandledOops) {
270 _unhandled_oops = new UnhandledOops(this);
271 }
272 #endif // CHECK_UNHANDLED_OOPS
273 #ifdef ASSERT
274 if (UseBiasedLocking) {
275 assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
276 assert(this == _real_malloc_address ||
277 this == (void*) align_size_up((intptr_t) _real_malloc_address, markOopDesc::biased_lock_alignment),
278 "bug in forced alignment of thread objects");
279 }
280 #endif // ASSERT
281 }
282
283 // Non-inlined version to be used where thread.inline.hpp shouldn't be included.
284 Thread* Thread::current_noinline() {
285 return Thread::current();
286 }
287
1457 set_thread_profiler(pp);
1458 }
1459
1460 // Setup safepoint state info for this thread
1461 ThreadSafepointState::create(this);
1462
1463 debug_only(_java_call_counter = 0);
1464
1465 // JVMTI PopFrame support
1466 _popframe_condition = popframe_inactive;
1467 _popframe_preserved_args = NULL;
1468 _popframe_preserved_args_size = 0;
1469 _frames_to_pop_failed_realloc = 0;
1470
1471 pd_initialize();
1472 }
1473
1474 #if INCLUDE_ALL_GCS
1475 SATBMarkQueueSet JavaThread::_satb_mark_queue_set;
1476 DirtyCardQueueSet JavaThread::_dirty_card_queue_set;
1477 bool JavaThread::_evacuation_in_progress_global = false;
1478 #endif // INCLUDE_ALL_GCS
1479
1480 JavaThread::JavaThread(bool is_attaching_via_jni) :
1481 Thread()
1482 #if INCLUDE_ALL_GCS
1483 , _satb_mark_queue(&_satb_mark_queue_set),
1484 _dirty_card_queue(&_dirty_card_queue_set),
1485 _evacuation_in_progress(_evacuation_in_progress_global)
1486 #endif // INCLUDE_ALL_GCS
1487 {
1488 initialize();
1489 if (is_attaching_via_jni) {
1490 _jni_attach_state = _attaching_via_jni;
1491 } else {
1492 _jni_attach_state = _not_attaching_via_jni;
1493 }
1494 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1495 }
1496
1497 bool JavaThread::reguard_stack(address cur_sp) {
1498 if (_stack_guard_state != stack_guard_yellow_disabled) {
1499 return true; // Stack already guarded or guard pages not needed.
1500 }
1501
1502 if (register_stack_overflow()) {
1503 // For those architectures which have separate register and
1504 // memory stacks, we must check the register stack to see if
1505 // it has overflowed.
1523
1524
1525 void JavaThread::block_if_vm_exited() {
1526 if (_terminated == _vm_exited) {
1527 // _vm_exited is set at safepoint, and Threads_lock is never released
1528 // we will block here forever
1529 Threads_lock->lock_without_safepoint_check();
1530 ShouldNotReachHere();
1531 }
1532 }
1533
1534
1535 // Remove this ifdef when C1 is ported to the compiler interface.
1536 static void compiler_thread_entry(JavaThread* thread, TRAPS);
1537 static void sweeper_thread_entry(JavaThread* thread, TRAPS);
1538
1539 JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) :
1540 Thread()
1541 #if INCLUDE_ALL_GCS
1542 , _satb_mark_queue(&_satb_mark_queue_set),
1543 _dirty_card_queue(&_dirty_card_queue_set),
1544 _evacuation_in_progress(_evacuation_in_progress_global)
1545 #endif // INCLUDE_ALL_GCS
1546 {
1547 initialize();
1548 _jni_attach_state = _not_attaching_via_jni;
1549 set_entry_point(entry_point);
1550 // Create the native thread itself.
1551 // %note runtime_23
1552 os::ThreadType thr_type = os::java_thread;
1553 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1554 os::java_thread;
1555 os::create_thread(this, thr_type, stack_sz);
1556 // The _osthread may be NULL here because we ran out of memory (too many threads active).
1557 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1558 // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1559 // the exception consists of creating the exception object & initializing it, initialization
1560 // will leave the VM via a JavaCall and then all locks must be unlocked).
1561 //
1562 // The thread is still suspended when we reach here. Thread must be explicit started
1563 // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1564 // by calling Threads:add. The reason why this is not done here, is because the thread
1659 // Note: Due to JVM_StopThread we can have pending exceptions already!
1660 if (!this->has_pending_exception() &&
1661 !java_lang_Thread::is_stillborn(this->threadObj())) {
1662 {
1663 ResourceMark rm(this);
1664 this->set_native_thread_name(this->get_thread_name());
1665 }
1666 HandleMark hm(this);
1667 this->entry_point()(this, this);
1668 }
1669
1670 DTRACE_THREAD_PROBE(stop, this);
1671
1672 this->exit(false);
1673 delete this;
1674 }
1675
1676
1677 static void ensure_join(JavaThread* thread) {
1678 // We do not need to grap the Threads_lock, since we are operating on ourself.
1679 Handle threadObj(thread, oopDesc::bs()->write_barrier(thread->threadObj()));
1680 assert(threadObj.not_null(), "java thread object must exist");
1681 ObjectLocker lock(threadObj, thread);
1682 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1683 thread->clear_pending_exception();
1684 // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
1685 java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1686 // Clear the native thread instance - this makes isAlive return false and allows the join()
1687 // to complete once we've done the notify_all below
1688 java_lang_Thread::set_thread(threadObj(), NULL);
1689 lock.notify_all(thread);
1690 // Ignore pending exception (ThreadDeath), since we are exiting anyway
1691 thread->clear_pending_exception();
1692 }
1693
1694
1695 // For any new cleanup additions, please check to see if they need to be applied to
1696 // cleanup_failed_attach_current_thread as well.
1697 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1698 assert(this == JavaThread::current(), "thread consistency check");
1699
1853 remove_stack_guard_pages();
1854
1855 if (UseTLAB) {
1856 tlab().make_parsable(true); // retire TLAB
1857 }
1858
1859 if (JvmtiEnv::environments_might_exist()) {
1860 JvmtiExport::cleanup_thread(this);
1861 }
1862
1863 // We must flush any deferred card marks before removing a thread from
1864 // the list of active threads.
1865 Universe::heap()->flush_deferred_store_barrier(this);
1866 assert(deferred_card_mark().is_empty(), "Should have been flushed");
1867
1868 #if INCLUDE_ALL_GCS
1869 // We must flush the G1-related buffers before removing a thread
1870 // from the list of active threads. We must do this after any deferred
1871 // card marks have been flushed (above) so that any entries that are
1872 // added to the thread's dirty card queue as a result are not lost.
1873 if (UseG1GC || UseShenandoahGC) {
1874 flush_barrier_queues();
1875 }
1876 if (UseShenandoahGC && UseTLAB) {
1877 gclab().make_parsable(true);
1878 }
1879 #endif // INCLUDE_ALL_GCS
1880
1881 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
1882 Threads::remove(this);
1883 }
1884
1885 #if INCLUDE_ALL_GCS
1886 // Flush G1-related queues.
1887 void JavaThread::flush_barrier_queues() {
1888 satb_mark_queue().flush();
1889 dirty_card_queue().flush();
1890 }
1891
1892 void JavaThread::initialize_queues() {
1893 assert(!SafepointSynchronize::is_at_safepoint(),
1894 "we should not be at a safepoint");
1895
1896 ObjPtrQueue& satb_queue = satb_mark_queue();
1897 SATBMarkQueueSet& satb_queue_set = satb_mark_queue_set();
1898 // The SATB queue should have been constructed with its active
1899 // field set to false.
1900 assert(!satb_queue.is_active(), "SATB queue should not be active");
1901 assert(satb_queue.is_empty(), "SATB queue should be empty");
1902 // If we are creating the thread during a marking cycle, we should
1903 // set the active field of the SATB queue to true.
1904 if (satb_queue_set.is_active()) {
1905 satb_queue.set_active(true);
1906 }
1907
1908 DirtyCardQueue& dirty_queue = dirty_card_queue();
1909 // The dirty card queue should have been constructed with its
1910 // active field set to true.
1911 assert(dirty_queue.is_active(), "dirty card queue should be active");
1912 }
1913
1914 bool JavaThread::evacuation_in_progress() const {
1915 return _evacuation_in_progress;
1916 }
1917
1918 void JavaThread::set_evacuation_in_progress(bool in_prog) {
1919 _evacuation_in_progress = in_prog;
1920 }
1921
1922 void JavaThread::set_evacuation_in_progress_all_threads(bool in_prog) {
1923 _evacuation_in_progress_global = in_prog;
1924 for (JavaThread* t = Threads::first(); t; t = t->next()) {
1925 t->set_evacuation_in_progress(in_prog);
1926 }
1927 }
1928 #endif // INCLUDE_ALL_GCS
1929
1930 void JavaThread::cleanup_failed_attach_current_thread() {
1931 if (get_thread_profiler() != NULL) {
1932 get_thread_profiler()->disengage();
1933 ResourceMark rm;
1934 get_thread_profiler()->print(get_thread_name());
1935 }
1936
1937 if (active_handles() != NULL) {
1938 JNIHandleBlock* block = active_handles();
1939 set_active_handles(NULL);
1940 JNIHandleBlock::release_block(block);
1941 }
1942
1943 if (free_handle_block() != NULL) {
1944 JNIHandleBlock* block = free_handle_block();
1945 set_free_handle_block(NULL);
1946 JNIHandleBlock::release_block(block);
1947 }
1948
1949 // These have to be removed while this is still a valid thread.
1950 remove_stack_guard_pages();
1951
1952 if (UseTLAB) {
1953 tlab().make_parsable(true); // retire TLAB, if any
1954 }
1955
1956 #if INCLUDE_ALL_GCS
1957 if (UseG1GC || UseShenandoahGC) {
1958 flush_barrier_queues();
1959 }
1960 if (UseShenandoahGC && UseTLAB) {
1961 gclab().make_parsable(true);
1962 }
1963 #endif // INCLUDE_ALL_GCS
1964
1965 Threads::remove(this);
1966 delete this;
1967 }
1968
1969
1970
1971
1972 JavaThread* JavaThread::active() {
1973 Thread* thread = ThreadLocalStorage::thread();
1974 assert(thread != NULL, "just checking");
1975 if (thread->is_Java_thread()) {
1976 return (JavaThread*) thread;
1977 } else {
1978 assert(thread->is_VM_thread(), "this must be a vm thread");
1979 VM_Operation* op = ((VMThread*) thread)->vm_operation();
1980 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread();
1981 assert(ret->is_Java_thread(), "must be a Java thread");
1982 return ret;
2891 } else if (is_attaching_via_jni()) { // workaround for 6412693 - see 6404306
2892 name_str = "<no-name - thread is attaching>";
2893 } else {
2894 name_str = Thread::name();
2895 }
2896 } else {
2897 name_str = Thread::name();
2898 }
2899 assert(name_str != NULL, "unexpected NULL thread name");
2900 return name_str;
2901 }
2902
2903
2904 const char* JavaThread::get_threadgroup_name() const {
2905 debug_only(if (JavaThread::current() != this) assert_locked_or_safepoint(Threads_lock);)
2906 oop thread_obj = threadObj();
2907 if (thread_obj != NULL) {
2908 oop thread_group = java_lang_Thread::threadGroup(thread_obj);
2909 if (thread_group != NULL) {
2910 typeArrayOop name = java_lang_ThreadGroup::name(thread_group);
2911 name = typeArrayOop(oopDesc::bs()->read_barrier(name));
2912 // ThreadGroup.name can be null
2913 if (name != NULL) {
2914 const char* str = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
2915 return str;
2916 }
2917 }
2918 }
2919 return NULL;
2920 }
2921
2922 const char* JavaThread::get_parent_name() const {
2923 debug_only(if (JavaThread::current() != this) assert_locked_or_safepoint(Threads_lock);)
2924 oop thread_obj = threadObj();
2925 if (thread_obj != NULL) {
2926 oop thread_group = java_lang_Thread::threadGroup(thread_obj);
2927 if (thread_group != NULL) {
2928 oop parent = java_lang_ThreadGroup::parent(thread_group);
2929 if (parent != NULL) {
2930 typeArrayOop name = java_lang_ThreadGroup::name(parent);
2931 name = typeArrayOop(oopDesc::bs()->read_barrier(name));
2932 // ThreadGroup.name can be null
2933 if (name != NULL) {
2934 const char* str = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
2935 return str;
2936 }
2937 }
2938 }
2939 }
2940 return NULL;
2941 }
2942
2943 ThreadPriority JavaThread::java_priority() const {
2944 oop thr_oop = threadObj();
2945 if (thr_oop == NULL) return NormPriority; // Bootstrapping
2946 ThreadPriority priority = java_lang_Thread::priority(thr_oop);
2947 assert(MinPriority <= priority && priority <= MaxPriority, "sanity check");
2948 return priority;
2949 }
2950
2951 void JavaThread::prepare(jobject jni_thread, ThreadPriority prio) {
3524 Metaspace::post_initialize();
3525
3526 HOTSPOT_VM_INIT_END();
3527
3528 // record VM initialization completion time
3529 #if INCLUDE_MANAGEMENT
3530 Management::record_vm_init_completed();
3531 #endif // INCLUDE_MANAGEMENT
3532
3533 // Compute system loader. Note that this has to occur after set_init_completed, since
3534 // valid exceptions may be thrown in the process.
3535 // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
3536 // set_init_completed has just been called, causing exceptions not to be shortcut
3537 // anymore. We call vm_exit_during_initialization directly instead.
3538 SystemDictionary::compute_java_system_loader(CHECK_JNI_ERR);
3539
3540 #if INCLUDE_ALL_GCS
3541 // Support for ConcurrentMarkSweep. This should be cleaned up
3542 // and better encapsulated. The ugly nested if test would go away
3543 // once things are properly refactored. XXX YSR
3544 if (UseConcMarkSweepGC || UseG1GC || UseShenandoahGC) {
3545 if (UseConcMarkSweepGC) {
3546 ConcurrentMarkSweepThread::makeSurrogateLockerThread(CHECK_JNI_ERR);
3547 } else if (UseShenandoahGC) {
3548 ShenandoahConcurrentThread::makeSurrogateLockerThread(CHECK_JNI_ERR);
3549 } else {
3550 ConcurrentMarkThread::makeSurrogateLockerThread(CHECK_JNI_ERR);
3551 }
3552 }
3553 #endif // INCLUDE_ALL_GCS
3554
3555 // Always call even when there are not JVMTI environments yet, since environments
3556 // may be attached late and JVMTI must track phases of VM execution
3557 JvmtiExport::enter_live_phase();
3558
3559 // Signal Dispatcher needs to be started before VMInit event is posted
3560 os::signal_init();
3561
3562 // Start Attach Listener if +StartAttachListener or it can't be started lazily
3563 if (!DisableAttachMechanism) {
3564 AttachListener::vm_start();
3565 if (StartAttachListener || AttachListener::init_at_startup()) {
3566 AttachListener::init();
3567 }
3568 }
3936 // complete. wait_for_ext_suspend_completion() will eventually
3937 // timeout, but that takes time. Making this wait a suspend-
3938 // equivalent condition solves that timeout problem.
3939 //
3940 Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
3941 Mutex::_as_suspend_equivalent_flag);
3942 }
3943
3944 // Hang forever on exit if we are reporting an error.
3945 if (ShowMessageBoxOnError && is_error_reported()) {
3946 os::infinite_sleep();
3947 }
3948 os::wait_for_keypress_at_exit();
3949
3950 // run Java level shutdown hooks
3951 thread->invoke_shutdown_hooks();
3952
3953 before_exit(thread);
3954
3955 thread->exit(true);
3956
3957 // Stop GC threads.
3958 Universe::heap()->shutdown();
3959
3960 // Stop VM thread.
3961 {
3962 // 4945125 The vm thread comes to a safepoint during exit.
3963 // GC vm_operations can get caught at the safepoint, and the
3964 // heap is unparseable if they are caught. Grab the Heap_lock
3965 // to prevent this. The GC vm_operations will not be able to
3966 // queue until after the vm thread is dead. After this point,
3967 // we'll never emerge out of the safepoint before the VM exits.
3968
3969 MutexLocker ml(Heap_lock);
3970
3971 VMThread::wait_for_vm_thread_exit();
3972 assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint");
3973 VMThread::destroy();
3974 }
3975
3976 // clean up ideal graph printers
3977 #if defined(COMPILER2) && !defined(PRODUCT)
3978 IdealGraphPrinter::clean_up();
|