295 os::initialize_thread(this); |
295 os::initialize_thread(this); |
296 |
296 |
297 #if INCLUDE_NMT |
297 #if INCLUDE_NMT |
298 // record thread's native stack, stack grows downward |
298 // record thread's native stack, stack grows downward |
299 address stack_low_addr = stack_base() - stack_size(); |
299 address stack_low_addr = stack_base() - stack_size(); |
300 MemTracker::record_thread_stack(stack_low_addr, stack_size(), this, |
300 MemTracker::record_thread_stack(stack_low_addr, stack_size()); |
301 CURRENT_PC); |
|
302 #endif // INCLUDE_NMT |
301 #endif // INCLUDE_NMT |
303 } |
302 } |
304 |
303 |
305 |
304 |
306 Thread::~Thread() { |
305 Thread::~Thread() { |
314 // that all started threads do call record_stack_base_and_size(), there is |
313 // that all started threads do call record_stack_base_and_size(), there is |
315 // not proper way to enforce that. |
314 // not proper way to enforce that. |
316 #if INCLUDE_NMT |
315 #if INCLUDE_NMT |
317 if (_stack_base != NULL) { |
316 if (_stack_base != NULL) { |
318 address low_stack_addr = stack_base() - stack_size(); |
317 address low_stack_addr = stack_base() - stack_size(); |
319 MemTracker::release_thread_stack(low_stack_addr, stack_size(), this); |
318 MemTracker::release_thread_stack(low_stack_addr, stack_size()); |
320 #ifdef ASSERT |
319 #ifdef ASSERT |
321 set_stack_base(NULL); |
320 set_stack_base(NULL); |
322 #endif |
321 #endif |
323 } |
322 } |
324 #endif // INCLUDE_NMT |
323 #endif // INCLUDE_NMT |
1423 set_deopt_nmethod(NULL); |
1422 set_deopt_nmethod(NULL); |
1424 clear_must_deopt_id(); |
1423 clear_must_deopt_id(); |
1425 set_monitor_chunks(NULL); |
1424 set_monitor_chunks(NULL); |
1426 set_next(NULL); |
1425 set_next(NULL); |
1427 set_thread_state(_thread_new); |
1426 set_thread_state(_thread_new); |
1428 #if INCLUDE_NMT |
|
1429 set_recorder(NULL); |
|
1430 #endif |
|
1431 _terminated = _not_terminated; |
1427 _terminated = _not_terminated; |
1432 _privileged_stack_top = NULL; |
1428 _privileged_stack_top = NULL; |
1433 _array_for_gc = NULL; |
1429 _array_for_gc = NULL; |
1434 _suspend_equivalent = false; |
1430 _suspend_equivalent = false; |
1435 _in_deopt_handler = 0; |
1431 _in_deopt_handler = 0; |
1501 _jni_attach_state = _attaching_via_jni; |
1497 _jni_attach_state = _attaching_via_jni; |
1502 } else { |
1498 } else { |
1503 _jni_attach_state = _not_attaching_via_jni; |
1499 _jni_attach_state = _not_attaching_via_jni; |
1504 } |
1500 } |
1505 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor"); |
1501 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor"); |
1506 _safepoint_visible = false; |
|
1507 } |
1502 } |
1508 |
1503 |
1509 bool JavaThread::reguard_stack(address cur_sp) { |
1504 bool JavaThread::reguard_stack(address cur_sp) { |
1510 if (_stack_guard_state != stack_guard_yellow_disabled) { |
1505 if (_stack_guard_state != stack_guard_yellow_disabled) { |
1511 return true; // Stack already guarded or guard pages not needed. |
1506 return true; // Stack already guarded or guard pages not needed. |
1564 // %note runtime_23 |
1559 // %note runtime_23 |
1565 os::ThreadType thr_type = os::java_thread; |
1560 os::ThreadType thr_type = os::java_thread; |
1566 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : |
1561 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : |
1567 os::java_thread; |
1562 os::java_thread; |
1568 os::create_thread(this, thr_type, stack_sz); |
1563 os::create_thread(this, thr_type, stack_sz); |
1569 _safepoint_visible = false; |
|
1570 // The _osthread may be NULL here because we ran out of memory (too many threads active). |
1564 // The _osthread may be NULL here because we ran out of memory (too many threads active). |
1571 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller |
1565 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller |
1572 // may hold a lock and all locks must be unlocked before throwing the exception (throwing |
1566 // may hold a lock and all locks must be unlocked before throwing the exception (throwing |
1573 // the exception consists of creating the exception object & initializing it, initialization |
1567 // the exception consists of creating the exception object & initializing it, initialization |
1574 // will leave the VM via a JavaCall and then all locks must be unlocked). |
1568 // will leave the VM via a JavaCall and then all locks must be unlocked). |
1581 |
1575 |
1582 JavaThread::~JavaThread() { |
1576 JavaThread::~JavaThread() { |
1583 if (TraceThreadEvents) { |
1577 if (TraceThreadEvents) { |
1584 tty->print_cr("terminate thread %p", this); |
1578 tty->print_cr("terminate thread %p", this); |
1585 } |
1579 } |
1586 |
|
1587 // By now, this thread should already be invisible to safepoint, |
|
1588 // and its per-thread recorder also collected. |
|
1589 assert(!is_safepoint_visible(), "wrong state"); |
|
1590 #if INCLUDE_NMT |
|
1591 assert(get_recorder() == NULL, "Already collected"); |
|
1592 #endif // INCLUDE_NMT |
|
1593 |
1580 |
1594 // JSR166 -- return the parker to the free list |
1581 // JSR166 -- return the parker to the free list |
1595 Parker::Release(_parker); |
1582 Parker::Release(_parker); |
1596 _parker = NULL; |
1583 _parker = NULL; |
1597 |
1584 |
3357 if (adjust_after_os_result != JNI_OK) return adjust_after_os_result; |
3344 if (adjust_after_os_result != JNI_OK) return adjust_after_os_result; |
3358 |
3345 |
3359 // initialize TLS |
3346 // initialize TLS |
3360 ThreadLocalStorage::init(); |
3347 ThreadLocalStorage::init(); |
3361 |
3348 |
3362 // Bootstrap native memory tracking, so it can start recording memory |
|
3363 // activities before worker thread is started. This is the first phase |
|
3364 // of bootstrapping, VM is currently running in single-thread mode. |
|
3365 MemTracker::bootstrap_single_thread(); |
|
3366 |
|
3367 // Initialize output stream logging |
3349 // Initialize output stream logging |
3368 ostream_init_log(); |
3350 ostream_init_log(); |
3369 |
3351 |
3370 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad |
3352 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad |
3371 // Must be before create_vm_init_agents() |
3353 // Must be before create_vm_init_agents() |
3412 main_thread->create_stack_guard_pages(); |
3394 main_thread->create_stack_guard_pages(); |
3413 |
3395 |
3414 // Initialize Java-Level synchronization subsystem |
3396 // Initialize Java-Level synchronization subsystem |
3415 ObjectMonitor::Initialize(); |
3397 ObjectMonitor::Initialize(); |
3416 |
3398 |
3417 // Second phase of bootstrapping, VM is about entering multi-thread mode |
|
3418 MemTracker::bootstrap_multi_thread(); |
|
3419 |
|
3420 // Initialize global modules |
3399 // Initialize global modules |
3421 jint status = init_globals(); |
3400 jint status = init_globals(); |
3422 if (status != JNI_OK) { |
3401 if (status != JNI_OK) { |
3423 delete main_thread; |
3402 delete main_thread; |
3424 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again |
3403 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again |
3435 } |
3414 } |
3436 |
3415 |
3437 // Any JVMTI raw monitors entered in onload will transition into |
3416 // Any JVMTI raw monitors entered in onload will transition into |
3438 // real raw monitor. VM is setup enough here for raw monitor enter. |
3417 // real raw monitor. VM is setup enough here for raw monitor enter. |
3439 JvmtiExport::transition_pending_onload_raw_monitors(); |
3418 JvmtiExport::transition_pending_onload_raw_monitors(); |
3440 |
|
3441 // Fully start NMT |
|
3442 MemTracker::start(); |
|
3443 |
3419 |
3444 // Create the VMThread |
3420 // Create the VMThread |
3445 { TraceTime timer("Start VMThread", TraceStartupTime); |
3421 { TraceTime timer("Start VMThread", TraceStartupTime); |
3446 VMThread::create(); |
3422 VMThread::create(); |
3447 Thread* vmthread = VMThread::vm_thread(); |
3423 Thread* vmthread = VMThread::vm_thread(); |
3993 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) { |
3969 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) { |
3994 _number_of_non_daemon_threads++; |
3970 _number_of_non_daemon_threads++; |
3995 daemon = false; |
3971 daemon = false; |
3996 } |
3972 } |
3997 |
3973 |
3998 p->set_safepoint_visible(true); |
|
3999 |
|
4000 ThreadService::add_thread(p, daemon); |
3974 ThreadService::add_thread(p, daemon); |
4001 |
3975 |
4002 // Possible GC point. |
3976 // Possible GC point. |
4003 Events::log(p, "Thread added: " INTPTR_FORMAT, p); |
3977 Events::log(p, "Thread added: " INTPTR_FORMAT, p); |
4004 } |
3978 } |
4040 // Make sure that safepoint code disregard this thread. This is needed since |
4014 // Make sure that safepoint code disregard this thread. This is needed since |
4041 // the thread might mess around with locks after this point. This can cause it |
4015 // the thread might mess around with locks after this point. This can cause it |
4042 // to do callbacks into the safepoint code. However, the safepoint code is not aware |
4016 // to do callbacks into the safepoint code. However, the safepoint code is not aware |
4043 // of this thread since it is removed from the queue. |
4017 // of this thread since it is removed from the queue. |
4044 p->set_terminated_value(); |
4018 p->set_terminated_value(); |
4045 |
|
4046 // Now, this thread is not visible to safepoint |
|
4047 p->set_safepoint_visible(false); |
|
4048 // once the thread becomes safepoint invisible, we can not use its per-thread |
|
4049 // recorder. And Threads::do_threads() no longer walks this thread, so we have |
|
4050 // to release its per-thread recorder here. |
|
4051 MemTracker::thread_exiting(p); |
|
4052 } // unlock Threads_lock |
4019 } // unlock Threads_lock |
4053 |
4020 |
4054 // Since Events::log uses a lock, we grab it outside the Threads_lock |
4021 // Since Events::log uses a lock, we grab it outside the Threads_lock |
4055 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); |
4022 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); |
4056 } |
4023 } |