302 // non-blocking operations. Otherwise, it might not work
303 // with the thread-startup/safepoint interaction.
304
305 // During Java thread startup, safepoint code should allow this
306 // method to complete because it may need to allocate memory to
307 // store information for the new thread.
308
309 // initialize structure dependent on thread local storage
310 ThreadLocalStorage::set_thread(this);
311
312 // set up any platform-specific state.
313 os::initialize_thread();
314 }
315
316 void Thread::record_stack_base_and_size() {
317 set_stack_base(os::current_stack_base());
318 set_stack_size(os::current_stack_size());
319
320 // record thread's native stack, stack grows downward
321 address low_stack_addr = stack_base() - stack_size();
322 MemTracker::record_thread_stack(low_stack_addr, stack_size(), this,
323 CURRENT_PC);
324 }
325
326
327 Thread::~Thread() {
328 // Reclaim the objectmonitors from the omFreeList of the moribund thread.
329 ObjectSynchronizer::omFlush (this) ;
330
331 // stack_base can be NULL if the thread is never started or exited before
332 // record_stack_base_and_size called. Although, we would like to ensure
333 // that all started threads do call record_stack_base_and_size(), there is
334 // not proper way to enforce that.
335 if (_stack_base != NULL) {
336 address low_stack_addr = stack_base() - stack_size();
337 MemTracker::release_thread_stack(low_stack_addr, stack_size(), this);
338 }
339
340 // deallocate data structures
341 delete resource_area();
342 // since the handle marks are using the handle area, we have to deallocated the root
343 // handle mark before deallocating the thread's handle area,
344 assert(last_handle_mark() != NULL, "check we have an element");
345 delete last_handle_mark();
346 assert(last_handle_mark() == NULL, "check we have reached the end");
347
348 // It's possible we can encounter a null _ParkEvent, etc., in stillborn threads.
349 // We NULL out the fields for good hygiene.
350 ParkEvent::Release (_ParkEvent) ; _ParkEvent = NULL ;
351 ParkEvent::Release (_SleepEvent) ; _SleepEvent = NULL ;
352 ParkEvent::Release (_MutexEvent) ; _MutexEvent = NULL ;
353 ParkEvent::Release (_MuxEvent) ; _MuxEvent = NULL ;
354
355 delete handle_area();
356 delete metadata_handles();
357
358 // osthread() can be NULL, if creation of thread failed.
1330 // Set the claimed par_id to -1 (ie not claiming any par_ids)
1331 set_claimed_par_id(-1);
1332
1333 set_saved_exception_pc(NULL);
1334 set_threadObj(NULL);
1335 _anchor.clear();
1336 set_entry_point(NULL);
1337 set_jni_functions(jni_functions());
1338 set_callee_target(NULL);
1339 set_vm_result(NULL);
1340 set_vm_result_2(NULL);
1341 set_vframe_array_head(NULL);
1342 set_vframe_array_last(NULL);
1343 set_deferred_locals(NULL);
1344 set_deopt_mark(NULL);
1345 set_deopt_nmethod(NULL);
1346 clear_must_deopt_id();
1347 set_monitor_chunks(NULL);
1348 set_next(NULL);
1349 set_thread_state(_thread_new);
1350 set_recorder(NULL);
1351 _terminated = _not_terminated;
1352 _privileged_stack_top = NULL;
1353 _array_for_gc = NULL;
1354 _suspend_equivalent = false;
1355 _in_deopt_handler = 0;
1356 _doing_unsafe_access = false;
1357 _stack_guard_state = stack_guard_unused;
1358 _exception_oop = NULL;
1359 _exception_pc = 0;
1360 _exception_handler_pc = 0;
1361 _is_method_handle_return = 0;
1362 _jvmti_thread_state= NULL;
1363 _should_post_on_exceptions_flag = JNI_FALSE;
1364 _jvmti_get_loaded_classes_closure = NULL;
1365 _interp_only_mode = 0;
1366 _special_runtime_exit_condition = _no_async_condition;
1367 _pending_async_exception = NULL;
1368 _is_compiling = false;
1369 _thread_stat = NULL;
1370 _thread_stat = new ThreadStatistics();
3492 reset_vm_info_property(CHECK_0);
3493
3494 quicken_jni_functions();
3495
3496 // Must be run after init_ft which initializes ft_enabled
3497 if (TRACE_INITIALIZE() != JNI_OK) {
3498 vm_exit_during_initialization("Failed to initialize tracing backend");
3499 }
3500
3501 // Set flag that basic initialization has completed. Used by exceptions and various
3502 // debug stuff, that does not work until all basic classes have been initialized.
3503 set_init_completed();
3504
3505 #ifndef USDT2
3506 HS_DTRACE_PROBE(hotspot, vm__init__end);
3507 #else /* USDT2 */
3508 HOTSPOT_VM_INIT_END();
3509 #endif /* USDT2 */
3510
3511 // record VM initialization completion time
3512 Management::record_vm_init_completed();
3513
3514 // Compute system loader. Note that this has to occur after set_init_completed, since
3515 // valid exceptions may be thrown in the process.
3516 // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
3517 // set_init_completed has just been called, causing exceptions not to be shortcut
3518 // anymore. We call vm_exit_during_initialization directly instead.
3519 SystemDictionary::compute_java_system_loader(THREAD);
3520 if (HAS_PENDING_EXCEPTION) {
3521 vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3522 }
3523
3524 #ifndef SERIALGC
3525 // Support for ConcurrentMarkSweep. This should be cleaned up
3526 // and better encapsulated. The ugly nested if test would go away
3527 // once things are properly refactored. XXX YSR
3528 if (UseConcMarkSweepGC || UseG1GC) {
3529 if (UseConcMarkSweepGC) {
3530 ConcurrentMarkSweepThread::makeSurrogateLockerThread(THREAD);
3531 } else {
3532 ConcurrentMarkThread::makeSurrogateLockerThread(THREAD);
3553
3554 // Launch -Xrun agents
3555 // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3556 // back-end can launch with -Xdebug -Xrunjdwp.
3557 if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3558 create_vm_init_libraries();
3559 }
3560
3561 // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3562 JvmtiExport::post_vm_initialized();
3563
3564 if (!TRACE_START()) {
3565 vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3566 }
3567
3568 if (CleanChunkPoolAsync) {
3569 Chunk::start_chunk_pool_cleaner_task();
3570 }
3571
3572 // initialize compiler(s)
3573 CompileBroker::compilation_init();
3574
3575 Management::initialize(THREAD);
3576 if (HAS_PENDING_EXCEPTION) {
3577 // management agent fails to start possibly due to
3578 // configuration problem and is responsible for printing
3579 // stack trace if appropriate. Simply exit VM.
3580 vm_exit(1);
3581 }
3582
3583 if (Arguments::has_profile()) FlatProfiler::engage(main_thread, true);
3584 if (Arguments::has_alloc_profile()) AllocationProfiler::engage();
3585 if (MemProfiling) MemProfiler::engage();
3586 StatSampler::engage();
3587 if (CheckJNICalls) JniPeriodicChecker::engage();
3588
3589 BiasedLocking::init();
3590
3591 if (JDK_Version::current().post_vm_init_hook_enabled()) {
3592 call_postVMInitHook(THREAD);
3593 // The Java side of PostVMInitHook.run must deal with all
3594 // exceptions and provide means of diagnosis.
3595 if (HAS_PENDING_EXCEPTION) {
3725 on_load_entry = lookup_agent_on_load(agent);
3726 if (on_load_entry != NULL) {
3727 // switch it to the agent list -- so that Agent_OnLoad will be called,
3728 // JVM_OnLoad won't be attempted and Agent_OnUnload will
3729 Arguments::convert_library_to_agent(agent);
3730 } else {
3731 vm_exit_during_initialization("Could not find JVM_OnLoad or Agent_OnLoad function in the library", agent->name());
3732 }
3733 }
3734 }
3735 }
3736
3737 // Create agents for -agentlib: -agentpath: and converted -Xrun
3738 // Invokes Agent_OnLoad
3739 // Called very early -- before JavaThreads exist
3740 void Threads::create_vm_init_agents() {
3741 extern struct JavaVM_ main_vm;
3742 AgentLibrary* agent;
3743
3744 JvmtiExport::enter_onload_phase();
3745 for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
3746 OnLoadEntry_t on_load_entry = lookup_agent_on_load(agent);
3747
3748 if (on_load_entry != NULL) {
3749 // Invoke the Agent_OnLoad function
3750 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
3751 if (err != JNI_OK) {
3752 vm_exit_during_initialization("agent library failed to init", agent->name());
3753 }
3754 } else {
3755 vm_exit_during_initialization("Could not find Agent_OnLoad function in the agent library", agent->name());
3756 }
3757 }
3758 JvmtiExport::enter_primordial_phase();
3759 }
3760
3761 extern "C" {
3762 typedef void (JNICALL *Agent_OnUnload_t)(JavaVM *);
3763 }
3764
|
302 // non-blocking operations. Otherwise, it might not work
303 // with the thread-startup/safepoint interaction.
304
305 // During Java thread startup, safepoint code should allow this
306 // method to complete because it may need to allocate memory to
307 // store information for the new thread.
308
309 // initialize structure dependent on thread local storage
310 ThreadLocalStorage::set_thread(this);
311
312 // set up any platform-specific state.
313 os::initialize_thread();
314 }
315
316 void Thread::record_stack_base_and_size() {
317 set_stack_base(os::current_stack_base());
318 set_stack_size(os::current_stack_size());
319
320 // record thread's native stack, stack grows downward
321 address low_stack_addr = stack_base() - stack_size();
322 #if INCLUDE_NMT
323 MemTracker::record_thread_stack(low_stack_addr, stack_size(), this,
324 CURRENT_PC);
325 #endif // INCLUDE_NMT
326 }
327
328
329 Thread::~Thread() {
330 // Reclaim the objectmonitors from the omFreeList of the moribund thread.
331 ObjectSynchronizer::omFlush (this) ;
332
333 // stack_base can be NULL if the thread is never started or exited before
334 // record_stack_base_and_size called. Although, we would like to ensure
335 // that all started threads do call record_stack_base_and_size(), there is
336 // not proper way to enforce that.
337 #if INCLUDE_NMT
338 if (_stack_base != NULL) {
339 address low_stack_addr = stack_base() - stack_size();
340 MemTracker::release_thread_stack(low_stack_addr, stack_size(), this);
341 }
342 #endif // INCLUDE_NMT
343
344 // deallocate data structures
345 delete resource_area();
346 // since the handle marks are using the handle area, we have to deallocated the root
347 // handle mark before deallocating the thread's handle area,
348 assert(last_handle_mark() != NULL, "check we have an element");
349 delete last_handle_mark();
350 assert(last_handle_mark() == NULL, "check we have reached the end");
351
352 // It's possible we can encounter a null _ParkEvent, etc., in stillborn threads.
353 // We NULL out the fields for good hygiene.
354 ParkEvent::Release (_ParkEvent) ; _ParkEvent = NULL ;
355 ParkEvent::Release (_SleepEvent) ; _SleepEvent = NULL ;
356 ParkEvent::Release (_MutexEvent) ; _MutexEvent = NULL ;
357 ParkEvent::Release (_MuxEvent) ; _MuxEvent = NULL ;
358
359 delete handle_area();
360 delete metadata_handles();
361
362 // osthread() can be NULL, if creation of thread failed.
1334 // Set the claimed par_id to -1 (ie not claiming any par_ids)
1335 set_claimed_par_id(-1);
1336
1337 set_saved_exception_pc(NULL);
1338 set_threadObj(NULL);
1339 _anchor.clear();
1340 set_entry_point(NULL);
1341 set_jni_functions(jni_functions());
1342 set_callee_target(NULL);
1343 set_vm_result(NULL);
1344 set_vm_result_2(NULL);
1345 set_vframe_array_head(NULL);
1346 set_vframe_array_last(NULL);
1347 set_deferred_locals(NULL);
1348 set_deopt_mark(NULL);
1349 set_deopt_nmethod(NULL);
1350 clear_must_deopt_id();
1351 set_monitor_chunks(NULL);
1352 set_next(NULL);
1353 set_thread_state(_thread_new);
1354 #if INCLUDE_NMT
1355 set_recorder(NULL);
1356 #endif
1357 _terminated = _not_terminated;
1358 _privileged_stack_top = NULL;
1359 _array_for_gc = NULL;
1360 _suspend_equivalent = false;
1361 _in_deopt_handler = 0;
1362 _doing_unsafe_access = false;
1363 _stack_guard_state = stack_guard_unused;
1364 _exception_oop = NULL;
1365 _exception_pc = 0;
1366 _exception_handler_pc = 0;
1367 _is_method_handle_return = 0;
1368 _jvmti_thread_state= NULL;
1369 _should_post_on_exceptions_flag = JNI_FALSE;
1370 _jvmti_get_loaded_classes_closure = NULL;
1371 _interp_only_mode = 0;
1372 _special_runtime_exit_condition = _no_async_condition;
1373 _pending_async_exception = NULL;
1374 _is_compiling = false;
1375 _thread_stat = NULL;
1376 _thread_stat = new ThreadStatistics();
3498 reset_vm_info_property(CHECK_0);
3499
3500 quicken_jni_functions();
3501
3502 // Must be run after init_ft which initializes ft_enabled
3503 if (TRACE_INITIALIZE() != JNI_OK) {
3504 vm_exit_during_initialization("Failed to initialize tracing backend");
3505 }
3506
3507 // Set flag that basic initialization has completed. Used by exceptions and various
3508 // debug stuff, that does not work until all basic classes have been initialized.
3509 set_init_completed();
3510
3511 #ifndef USDT2
3512 HS_DTRACE_PROBE(hotspot, vm__init__end);
3513 #else /* USDT2 */
3514 HOTSPOT_VM_INIT_END();
3515 #endif /* USDT2 */
3516
3517 // record VM initialization completion time
3518 #if INCLUDE_MANAGEMENT
3519 Management::record_vm_init_completed();
3520 #endif // INCLUDE_MANAGEMENT
3521
3522 // Compute system loader. Note that this has to occur after set_init_completed, since
3523 // valid exceptions may be thrown in the process.
3524 // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
3525 // set_init_completed has just been called, causing exceptions not to be shortcut
3526 // anymore. We call vm_exit_during_initialization directly instead.
3527 SystemDictionary::compute_java_system_loader(THREAD);
3528 if (HAS_PENDING_EXCEPTION) {
3529 vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3530 }
3531
3532 #ifndef SERIALGC
3533 // Support for ConcurrentMarkSweep. This should be cleaned up
3534 // and better encapsulated. The ugly nested if test would go away
3535 // once things are properly refactored. XXX YSR
3536 if (UseConcMarkSweepGC || UseG1GC) {
3537 if (UseConcMarkSweepGC) {
3538 ConcurrentMarkSweepThread::makeSurrogateLockerThread(THREAD);
3539 } else {
3540 ConcurrentMarkThread::makeSurrogateLockerThread(THREAD);
3561
3562 // Launch -Xrun agents
3563 // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3564 // back-end can launch with -Xdebug -Xrunjdwp.
3565 if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3566 create_vm_init_libraries();
3567 }
3568
3569 // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3570 JvmtiExport::post_vm_initialized();
3571
3572 if (!TRACE_START()) {
3573 vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3574 }
3575
3576 if (CleanChunkPoolAsync) {
3577 Chunk::start_chunk_pool_cleaner_task();
3578 }
3579
3580 // initialize compiler(s)
3581 #if defined(COMPILER1) || defined(COMPILER2)
3582 CompileBroker::compilation_init();
3583 #endif
3584
3585 #if INCLUDE_MANAGEMENT
3586 Management::initialize(THREAD);
3587 #endif // INCLUDE_MANAGEMENT
3588
3589 if (HAS_PENDING_EXCEPTION) {
3590 // management agent fails to start possibly due to
3591 // configuration problem and is responsible for printing
3592 // stack trace if appropriate. Simply exit VM.
3593 vm_exit(1);
3594 }
3595
3596 if (Arguments::has_profile()) FlatProfiler::engage(main_thread, true);
3597 if (Arguments::has_alloc_profile()) AllocationProfiler::engage();
3598 if (MemProfiling) MemProfiler::engage();
3599 StatSampler::engage();
3600 if (CheckJNICalls) JniPeriodicChecker::engage();
3601
3602 BiasedLocking::init();
3603
3604 if (JDK_Version::current().post_vm_init_hook_enabled()) {
3605 call_postVMInitHook(THREAD);
3606 // The Java side of PostVMInitHook.run must deal with all
3607 // exceptions and provide means of diagnosis.
3608 if (HAS_PENDING_EXCEPTION) {
3738 on_load_entry = lookup_agent_on_load(agent);
3739 if (on_load_entry != NULL) {
3740 // switch it to the agent list -- so that Agent_OnLoad will be called,
3741 // JVM_OnLoad won't be attempted and Agent_OnUnload will
3742 Arguments::convert_library_to_agent(agent);
3743 } else {
3744 vm_exit_during_initialization("Could not find JVM_OnLoad or Agent_OnLoad function in the library", agent->name());
3745 }
3746 }
3747 }
3748 }
3749
3750 // Create agents for -agentlib: -agentpath: and converted -Xrun
3751 // Invokes Agent_OnLoad
3752 // Called very early -- before JavaThreads exist
3753 void Threads::create_vm_init_agents() {
3754 extern struct JavaVM_ main_vm;
3755 AgentLibrary* agent;
3756
3757 JvmtiExport::enter_onload_phase();
3758
3759 for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
3760 OnLoadEntry_t on_load_entry = lookup_agent_on_load(agent);
3761
3762 if (on_load_entry != NULL) {
3763 // Invoke the Agent_OnLoad function
3764 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
3765 if (err != JNI_OK) {
3766 vm_exit_during_initialization("agent library failed to init", agent->name());
3767 }
3768 } else {
3769 vm_exit_during_initialization("Could not find Agent_OnLoad function in the agent library", agent->name());
3770 }
3771 }
3772 JvmtiExport::enter_primordial_phase();
3773 }
3774
3775 extern "C" {
3776 typedef void (JNICALL *Agent_OnUnload_t)(JavaVM *);
3777 }
3778
|