329 os::initialize_thread(this); |
329 os::initialize_thread(this); |
330 |
330 |
331 #if INCLUDE_NMT |
331 #if INCLUDE_NMT |
332 // record thread's native stack, stack grows downward |
332 // record thread's native stack, stack grows downward |
333 address stack_low_addr = stack_base() - stack_size(); |
333 address stack_low_addr = stack_base() - stack_size(); |
334 MemTracker::record_thread_stack(stack_low_addr, stack_size(), this, |
334 MemTracker::record_thread_stack(stack_low_addr, stack_size()); |
335 CURRENT_PC); |
|
336 #endif // INCLUDE_NMT |
335 #endif // INCLUDE_NMT |
337 } |
336 } |
338 |
337 |
339 |
338 |
340 Thread::~Thread() { |
339 Thread::~Thread() { |
348 // that all started threads do call record_stack_base_and_size(), there is |
347 // that all started threads do call record_stack_base_and_size(), there is |
349 // not proper way to enforce that. |
348 // not proper way to enforce that. |
350 #if INCLUDE_NMT |
349 #if INCLUDE_NMT |
351 if (_stack_base != NULL) { |
350 if (_stack_base != NULL) { |
352 address low_stack_addr = stack_base() - stack_size(); |
351 address low_stack_addr = stack_base() - stack_size(); |
353 MemTracker::release_thread_stack(low_stack_addr, stack_size(), this); |
352 MemTracker::release_thread_stack(low_stack_addr, stack_size()); |
354 #ifdef ASSERT |
353 #ifdef ASSERT |
355 set_stack_base(NULL); |
354 set_stack_base(NULL); |
356 #endif |
355 #endif |
357 } |
356 } |
358 #endif // INCLUDE_NMT |
357 #endif // INCLUDE_NMT |
1440 set_deopt_nmethod(NULL); |
1439 set_deopt_nmethod(NULL); |
1441 clear_must_deopt_id(); |
1440 clear_must_deopt_id(); |
1442 set_monitor_chunks(NULL); |
1441 set_monitor_chunks(NULL); |
1443 set_next(NULL); |
1442 set_next(NULL); |
1444 set_thread_state(_thread_new); |
1443 set_thread_state(_thread_new); |
1445 #if INCLUDE_NMT |
|
1446 set_recorder(NULL); |
|
1447 #endif |
|
1448 _terminated = _not_terminated; |
1444 _terminated = _not_terminated; |
1449 _privileged_stack_top = NULL; |
1445 _privileged_stack_top = NULL; |
1450 _array_for_gc = NULL; |
1446 _array_for_gc = NULL; |
1451 _suspend_equivalent = false; |
1447 _suspend_equivalent = false; |
1452 _in_deopt_handler = 0; |
1448 _in_deopt_handler = 0; |
1517 _jni_attach_state = _attaching_via_jni; |
1513 _jni_attach_state = _attaching_via_jni; |
1518 } else { |
1514 } else { |
1519 _jni_attach_state = _not_attaching_via_jni; |
1515 _jni_attach_state = _not_attaching_via_jni; |
1520 } |
1516 } |
1521 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor"); |
1517 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor"); |
1522 _safepoint_visible = false; |
|
1523 } |
1518 } |
1524 |
1519 |
1525 bool JavaThread::reguard_stack(address cur_sp) { |
1520 bool JavaThread::reguard_stack(address cur_sp) { |
1526 if (_stack_guard_state != stack_guard_yellow_disabled) { |
1521 if (_stack_guard_state != stack_guard_yellow_disabled) { |
1527 return true; // Stack already guarded or guard pages not needed. |
1522 return true; // Stack already guarded or guard pages not needed. |
1580 // %note runtime_23 |
1575 // %note runtime_23 |
1581 os::ThreadType thr_type = os::java_thread; |
1576 os::ThreadType thr_type = os::java_thread; |
1582 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : |
1577 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : |
1583 os::java_thread; |
1578 os::java_thread; |
1584 os::create_thread(this, thr_type, stack_sz); |
1579 os::create_thread(this, thr_type, stack_sz); |
1585 _safepoint_visible = false; |
|
1586 // The _osthread may be NULL here because we ran out of memory (too many threads active). |
1580 // The _osthread may be NULL here because we ran out of memory (too many threads active). |
1587 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller |
1581 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller |
1588 // may hold a lock and all locks must be unlocked before throwing the exception (throwing |
1582 // may hold a lock and all locks must be unlocked before throwing the exception (throwing |
1589 // the exception consists of creating the exception object & initializing it, initialization |
1583 // the exception consists of creating the exception object & initializing it, initialization |
1590 // will leave the VM via a JavaCall and then all locks must be unlocked). |
1584 // will leave the VM via a JavaCall and then all locks must be unlocked). |
1597 |
1591 |
1598 JavaThread::~JavaThread() { |
1592 JavaThread::~JavaThread() { |
1599 if (TraceThreadEvents) { |
1593 if (TraceThreadEvents) { |
1600 tty->print_cr("terminate thread %p", this); |
1594 tty->print_cr("terminate thread %p", this); |
1601 } |
1595 } |
1602 |
|
1603 // By now, this thread should already be invisible to safepoint, |
|
1604 // and its per-thread recorder also collected. |
|
1605 assert(!is_safepoint_visible(), "wrong state"); |
|
1606 #if INCLUDE_NMT |
|
1607 assert(get_recorder() == NULL, "Already collected"); |
|
1608 #endif // INCLUDE_NMT |
|
1609 |
1596 |
1610 // JSR166 -- return the parker to the free list |
1597 // JSR166 -- return the parker to the free list |
1611 Parker::Release(_parker); |
1598 Parker::Release(_parker); |
1612 _parker = NULL ; |
1599 _parker = NULL ; |
1613 |
1600 |
3368 if (adjust_after_os_result != JNI_OK) return adjust_after_os_result; |
3355 if (adjust_after_os_result != JNI_OK) return adjust_after_os_result; |
3369 |
3356 |
3370 // intialize TLS |
3357 // intialize TLS |
3371 ThreadLocalStorage::init(); |
3358 ThreadLocalStorage::init(); |
3372 |
3359 |
3373 // Bootstrap native memory tracking, so it can start recording memory |
|
3374 // activities before worker thread is started. This is the first phase |
|
3375 // of bootstrapping, VM is currently running in single-thread mode. |
|
3376 MemTracker::bootstrap_single_thread(); |
|
3377 |
|
3378 // Initialize output stream logging |
3360 // Initialize output stream logging |
3379 ostream_init_log(); |
3361 ostream_init_log(); |
3380 |
3362 |
3381 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad |
3363 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad |
3382 // Must be before create_vm_init_agents() |
3364 // Must be before create_vm_init_agents() |
3423 main_thread->create_stack_guard_pages(); |
3405 main_thread->create_stack_guard_pages(); |
3424 |
3406 |
3425 // Initialize Java-Level synchronization subsystem |
3407 // Initialize Java-Level synchronization subsystem |
3426 ObjectMonitor::Initialize() ; |
3408 ObjectMonitor::Initialize() ; |
3427 |
3409 |
3428 // Second phase of bootstrapping, VM is about entering multi-thread mode |
|
3429 MemTracker::bootstrap_multi_thread(); |
|
3430 |
|
3431 // Initialize global modules |
3410 // Initialize global modules |
3432 jint status = init_globals(); |
3411 jint status = init_globals(); |
3433 if (status != JNI_OK) { |
3412 if (status != JNI_OK) { |
3434 delete main_thread; |
3413 delete main_thread; |
3435 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again |
3414 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again |
3446 } |
3425 } |
3447 |
3426 |
3448 // Any JVMTI raw monitors entered in onload will transition into |
3427 // Any JVMTI raw monitors entered in onload will transition into |
3449 // real raw monitor. VM is setup enough here for raw monitor enter. |
3428 // real raw monitor. VM is setup enough here for raw monitor enter. |
3450 JvmtiExport::transition_pending_onload_raw_monitors(); |
3429 JvmtiExport::transition_pending_onload_raw_monitors(); |
3451 |
|
3452 // Fully start NMT |
|
3453 MemTracker::start(); |
|
3454 |
3430 |
3455 // Create the VMThread |
3431 // Create the VMThread |
3456 { TraceTime timer("Start VMThread", TraceStartupTime); |
3432 { TraceTime timer("Start VMThread", TraceStartupTime); |
3457 VMThread::create(); |
3433 VMThread::create(); |
3458 Thread* vmthread = VMThread::vm_thread(); |
3434 Thread* vmthread = VMThread::vm_thread(); |
4087 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) { |
4063 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) { |
4088 _number_of_non_daemon_threads++; |
4064 _number_of_non_daemon_threads++; |
4089 daemon = false; |
4065 daemon = false; |
4090 } |
4066 } |
4091 |
4067 |
4092 p->set_safepoint_visible(true); |
|
4093 |
|
4094 ThreadService::add_thread(p, daemon); |
4068 ThreadService::add_thread(p, daemon); |
4095 |
4069 |
4096 // Possible GC point. |
4070 // Possible GC point. |
4097 Events::log(p, "Thread added: " INTPTR_FORMAT, p); |
4071 Events::log(p, "Thread added: " INTPTR_FORMAT, p); |
4098 } |
4072 } |
4134 // Make sure that safepoint code disregard this thread. This is needed since |
4108 // Make sure that safepoint code disregard this thread. This is needed since |
4135 // the thread might mess around with locks after this point. This can cause it |
4109 // the thread might mess around with locks after this point. This can cause it |
4136 // to do callbacks into the safepoint code. However, the safepoint code is not aware |
4110 // to do callbacks into the safepoint code. However, the safepoint code is not aware |
4137 // of this thread since it is removed from the queue. |
4111 // of this thread since it is removed from the queue. |
4138 p->set_terminated_value(); |
4112 p->set_terminated_value(); |
4139 |
|
4140 // Now, this thread is not visible to safepoint |
|
4141 p->set_safepoint_visible(false); |
|
4142 // once the thread becomes safepoint invisible, we can not use its per-thread |
|
4143 // recorder. And Threads::do_threads() no longer walks this thread, so we have |
|
4144 // to release its per-thread recorder here. |
|
4145 MemTracker::thread_exiting(p); |
|
4146 } // unlock Threads_lock |
4113 } // unlock Threads_lock |
4147 |
4114 |
4148 // Since Events::log uses a lock, we grab it outside the Threads_lock |
4115 // Since Events::log uses a lock, we grab it outside the Threads_lock |
4149 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); |
4116 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); |
4150 } |
4117 } |