comparison src/share/vm/runtime/thread.cpp @ 20804:7848fc12602b

Merge with jdk8u40-b25
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Tue, 07 Apr 2015 14:58:49 +0200
parents 8f2fb6bec986 d3f3f7677537
children cecb4e39521c be896a1983c0
comparison
equal deleted inserted replaced
20184:84105dcdb05b 20804:7848fc12602b
60 #include "runtime/javaCalls.hpp" 60 #include "runtime/javaCalls.hpp"
61 #include "runtime/jniPeriodicChecker.hpp" 61 #include "runtime/jniPeriodicChecker.hpp"
62 #include "runtime/memprofiler.hpp" 62 #include "runtime/memprofiler.hpp"
63 #include "runtime/mutexLocker.hpp" 63 #include "runtime/mutexLocker.hpp"
64 #include "runtime/objectMonitor.hpp" 64 #include "runtime/objectMonitor.hpp"
65 #include "runtime/orderAccess.inline.hpp"
65 #include "runtime/osThread.hpp" 66 #include "runtime/osThread.hpp"
66 #include "runtime/safepoint.hpp" 67 #include "runtime/safepoint.hpp"
67 #include "runtime/sharedRuntime.hpp" 68 #include "runtime/sharedRuntime.hpp"
68 #include "runtime/statSampler.hpp" 69 #include "runtime/statSampler.hpp"
69 #include "runtime/stubRoutines.hpp" 70 #include "runtime/stubRoutines.hpp"
235 set_last_handle_mark(NULL); 236 set_last_handle_mark(NULL);
236 237
237 // This initial value ==> never claimed. 238 // This initial value ==> never claimed.
238 _oops_do_parity = 0; 239 _oops_do_parity = 0;
239 240
241 _metadata_on_stack_buffer = NULL;
242
240 // the handle mark links itself to last_handle_mark 243 // the handle mark links itself to last_handle_mark
241 new HandleMark(this); 244 new HandleMark(this);
242 245
243 // plain initialization 246 // plain initialization
244 debug_only(_owned_locks = NULL;) 247 debug_only(_owned_locks = NULL;)
332 os::initialize_thread(this); 335 os::initialize_thread(this);
333 336
334 #if INCLUDE_NMT 337 #if INCLUDE_NMT
335 // record thread's native stack, stack grows downward 338 // record thread's native stack, stack grows downward
336 address stack_low_addr = stack_base() - stack_size(); 339 address stack_low_addr = stack_base() - stack_size();
337 MemTracker::record_thread_stack(stack_low_addr, stack_size(), this, 340 MemTracker::record_thread_stack(stack_low_addr, stack_size());
338 CURRENT_PC);
339 #endif // INCLUDE_NMT 341 #endif // INCLUDE_NMT
340 } 342 }
341 343
342 344
343 Thread::~Thread() { 345 Thread::~Thread() {
351 // that all started threads do call record_stack_base_and_size(), there is 353 // that all started threads do call record_stack_base_and_size(), there is
352 // not proper way to enforce that. 354 // not proper way to enforce that.
353 #if INCLUDE_NMT 355 #if INCLUDE_NMT
354 if (_stack_base != NULL) { 356 if (_stack_base != NULL) {
355 address low_stack_addr = stack_base() - stack_size(); 357 address low_stack_addr = stack_base() - stack_size();
356 MemTracker::release_thread_stack(low_stack_addr, stack_size(), this); 358 MemTracker::release_thread_stack(low_stack_addr, stack_size());
357 #ifdef ASSERT 359 #ifdef ASSERT
358 set_stack_base(NULL); 360 set_stack_base(NULL);
359 #endif 361 #endif
360 } 362 }
361 #endif // INCLUDE_NMT 363 #endif // INCLUDE_NMT
835 assert(SharedHeap::heap()->workers()->active_workers() > 0, 837 assert(SharedHeap::heap()->workers()->active_workers() > 0,
836 "Should only fail when parallel."); 838 "Should only fail when parallel.");
837 return false; 839 return false;
838 } 840 }
839 841
840 void Thread::oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) { 842 void Thread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
841 active_handles()->oops_do(f); 843 active_handles()->oops_do(f);
842 // Do oop for ThreadShadow 844 // Do oop for ThreadShadow
843 f->do_oop((oop*)&_pending_exception); 845 f->do_oop((oop*)&_pending_exception);
844 #ifdef GRAAL 846 #ifdef GRAAL
845 f->do_oop((oop*)&_pending_failed_speculation); 847 f->do_oop((oop*)&_pending_failed_speculation);
865 int os_prio; 867 int os_prio;
866 if (os::get_native_priority(this, &os_prio) == OS_OK) { 868 if (os::get_native_priority(this, &os_prio) == OS_OK) {
867 st->print("os_prio=%d ", os_prio); 869 st->print("os_prio=%d ", os_prio);
868 } 870 }
869 st->print("tid=" INTPTR_FORMAT " ", this); 871 st->print("tid=" INTPTR_FORMAT " ", this);
872 ext().print_on(st);
870 osthread()->print_on(st); 873 osthread()->print_on(st);
871 } 874 }
872 debug_only(if (WizardMode) print_owned_locks_on(st);) 875 debug_only(if (WizardMode) print_owned_locks_on(st);)
873 } 876 }
874 877
944 cur != Threads_lock && 947 cur != Threads_lock &&
945 cur != Compile_lock && // Temporary: should not be necessary when we get spearate compilation 948 cur != Compile_lock && // Temporary: should not be necessary when we get spearate compilation
946 cur != VMOperationRequest_lock && 949 cur != VMOperationRequest_lock &&
947 cur != VMOperationQueue_lock) || 950 cur != VMOperationQueue_lock) ||
948 cur->rank() == Mutex::special) { 951 cur->rank() == Mutex::special) {
949 warning("Thread holding lock at safepoint that vm can block on: %s", cur->name()); 952 fatal(err_msg("Thread holding lock at safepoint that vm can block on: %s", cur->name()));
950 } 953 }
951 } 954 }
952 } 955 }
953 956
954 if (GCALotAtAllSafepoints) { 957 if (GCALotAtAllSafepoints) {
1474 set_deopt_nmethod(NULL); 1477 set_deopt_nmethod(NULL);
1475 clear_must_deopt_id(); 1478 clear_must_deopt_id();
1476 set_monitor_chunks(NULL); 1479 set_monitor_chunks(NULL);
1477 set_next(NULL); 1480 set_next(NULL);
1478 set_thread_state(_thread_new); 1481 set_thread_state(_thread_new);
1479 #if INCLUDE_NMT
1480 set_recorder(NULL);
1481 #endif
1482 _terminated = _not_terminated; 1482 _terminated = _not_terminated;
1483 _privileged_stack_top = NULL; 1483 _privileged_stack_top = NULL;
1484 _array_for_gc = NULL; 1484 _array_for_gc = NULL;
1485 _suspend_equivalent = false; 1485 _suspend_equivalent = false;
1486 _in_deopt_handler = 0; 1486 _in_deopt_handler = 0;
1539 1539
1540 // JVMTI PopFrame support 1540 // JVMTI PopFrame support
1541 _popframe_condition = popframe_inactive; 1541 _popframe_condition = popframe_inactive;
1542 _popframe_preserved_args = NULL; 1542 _popframe_preserved_args = NULL;
1543 _popframe_preserved_args_size = 0; 1543 _popframe_preserved_args_size = 0;
1544 _frames_to_pop_failed_realloc = 0;
1544 1545
1545 pd_initialize(); 1546 pd_initialize();
1546 } 1547 }
1547 1548
1548 #if INCLUDE_ALL_GCS 1549 #if INCLUDE_ALL_GCS
1562 _jni_attach_state = _attaching_via_jni; 1563 _jni_attach_state = _attaching_via_jni;
1563 } else { 1564 } else {
1564 _jni_attach_state = _not_attaching_via_jni; 1565 _jni_attach_state = _not_attaching_via_jni;
1565 } 1566 }
1566 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor"); 1567 assert(deferred_card_mark().is_empty(), "Default MemRegion ctor");
1567 _safepoint_visible = false;
1568 } 1568 }
1569 1569
1570 bool JavaThread::reguard_stack(address cur_sp) { 1570 bool JavaThread::reguard_stack(address cur_sp) {
1571 if (_stack_guard_state != stack_guard_yellow_disabled) { 1571 if (_stack_guard_state != stack_guard_yellow_disabled) {
1572 return true; // Stack already guarded or guard pages not needed. 1572 return true; // Stack already guarded or guard pages not needed.
1625 // %note runtime_23 1625 // %note runtime_23
1626 os::ThreadType thr_type = os::java_thread; 1626 os::ThreadType thr_type = os::java_thread;
1627 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread : 1627 thr_type = entry_point == &compiler_thread_entry ? os::compiler_thread :
1628 os::java_thread; 1628 os::java_thread;
1629 os::create_thread(this, thr_type, stack_sz); 1629 os::create_thread(this, thr_type, stack_sz);
1630 _safepoint_visible = false;
1631 // The _osthread may be NULL here because we ran out of memory (too many threads active). 1630 // The _osthread may be NULL here because we ran out of memory (too many threads active).
1632 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller 1631 // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1633 // may hold a lock and all locks must be unlocked before throwing the exception (throwing 1632 // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1634 // the exception consists of creating the exception object & initializing it, initialization 1633 // the exception consists of creating the exception object & initializing it, initialization
1635 // will leave the VM via a JavaCall and then all locks must be unlocked). 1634 // will leave the VM via a JavaCall and then all locks must be unlocked).
1642 1641
1643 JavaThread::~JavaThread() { 1642 JavaThread::~JavaThread() {
1644 if (TraceThreadEvents) { 1643 if (TraceThreadEvents) {
1645 tty->print_cr("terminate thread %p", this); 1644 tty->print_cr("terminate thread %p", this);
1646 } 1645 }
1647
1648 // By now, this thread should already be invisible to safepoint,
1649 // and its per-thread recorder also collected.
1650 assert(!is_safepoint_visible(), "wrong state");
1651 #if INCLUDE_NMT
1652 assert(get_recorder() == NULL, "Already collected");
1653 #endif // INCLUDE_NMT
1654 1646
1655 // JSR166 -- return the parker to the free list 1647 // JSR166 -- return the parker to the free list
1656 Parker::Release(_parker); 1648 Parker::Release(_parker);
1657 _parker = NULL ; 1649 _parker = NULL ;
1658 1650
2778 _cur_thr->set_processed_thread(NULL); 2770 _cur_thr->set_processed_thread(NULL);
2779 } 2771 }
2780 } 2772 }
2781 }; 2773 };
2782 2774
2783 void JavaThread::oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) { 2775 void JavaThread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
2784 // Verify that the deferred card marks have been flushed. 2776 // Verify that the deferred card marks have been flushed.
2785 assert(deferred_card_mark().is_empty(), "Should be empty during GC"); 2777 assert(deferred_card_mark().is_empty(), "Should be empty during GC");
2786 2778
2787 // The ThreadProfiler oops_do is done from FlatProfiler::oops_do 2779 // The ThreadProfiler oops_do is done from FlatProfiler::oops_do
2788 // since there may be more than one thread using each ThreadProfiler. 2780 // since there may be more than one thread using each ThreadProfiler.
3089 } 3081 }
3090 3082
3091 // Push the Java priority down to the native thread; needs Threads_lock 3083 // Push the Java priority down to the native thread; needs Threads_lock
3092 Thread::set_priority(this, prio); 3084 Thread::set_priority(this, prio);
3093 3085
3086 prepare_ext();
3087
3094 // Add the new thread to the Threads list and set it in motion. 3088 // Add the new thread to the Threads list and set it in motion.
3095 // We must have threads lock in order to call Threads::add. 3089 // We must have threads lock in order to call Threads::add.
3096 // It is crucial that we do not block before the thread is 3090 // It is crucial that we do not block before the thread is
3097 // added to the Threads list for if a GC happens, then the java_thread oop 3091 // added to the Threads list for if a GC happens, then the java_thread oop
3098 // will not be visited by GC. 3092 // will not be visited by GC.
3314 bool CompilerThread::can_call_java() const { 3308 bool CompilerThread::can_call_java() const {
3315 return _compiler != NULL && _compiler->is_graal(); 3309 return _compiler != NULL && _compiler->is_graal();
3316 } 3310 }
3317 #endif 3311 #endif
3318 3312
3319 void CompilerThread::oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) { 3313 void CompilerThread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
3320 JavaThread::oops_do(f, cld_f, cf); 3314 JavaThread::oops_do(f, cld_f, cf);
3321 if (_scanned_nmethod != NULL && cf != NULL) { 3315 if (_scanned_nmethod != NULL && cf != NULL) {
3322 // Safepoints can occur when the sweeper is scanning an nmethod so 3316 // Safepoints can occur when the sweeper is scanning an nmethod so
3323 // process it here to make sure it isn't unloaded in the middle of 3317 // process it here to make sure it isn't unloaded in the middle of
3324 // a scan. 3318 // a scan.
3431 if (adjust_after_os_result != JNI_OK) return adjust_after_os_result; 3425 if (adjust_after_os_result != JNI_OK) return adjust_after_os_result;
3432 3426
3433 // intialize TLS 3427 // intialize TLS
3434 ThreadLocalStorage::init(); 3428 ThreadLocalStorage::init();
3435 3429
3436 // Bootstrap native memory tracking, so it can start recording memory
3437 // activities before worker thread is started. This is the first phase
3438 // of bootstrapping, VM is currently running in single-thread mode.
3439 MemTracker::bootstrap_single_thread();
3440
3441 // Initialize output stream logging 3430 // Initialize output stream logging
3442 ostream_init_log(); 3431 ostream_init_log();
3443 3432
3444 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad 3433 // Convert -Xrun to -agentlib: if there is no JVM_OnLoad
3445 // Must be before create_vm_init_agents() 3434 // Must be before create_vm_init_agents()
3495 main_thread->create_stack_guard_pages(); 3484 main_thread->create_stack_guard_pages();
3496 3485
3497 // Initialize Java-Level synchronization subsystem 3486 // Initialize Java-Level synchronization subsystem
3498 ObjectMonitor::Initialize() ; 3487 ObjectMonitor::Initialize() ;
3499 3488
3500 // Second phase of bootstrapping, VM is about entering multi-thread mode
3501 MemTracker::bootstrap_multi_thread();
3502
3503 // Initialize global modules 3489 // Initialize global modules
3504 jint status = init_globals(); 3490 jint status = init_globals();
3505 if (status != JNI_OK) { 3491 if (status != JNI_OK) {
3506 delete main_thread; 3492 delete main_thread;
3507 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again 3493 *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
3518 } 3504 }
3519 3505
3520 // Any JVMTI raw monitors entered in onload will transition into 3506 // Any JVMTI raw monitors entered in onload will transition into
3521 // real raw monitor. VM is setup enough here for raw monitor enter. 3507 // real raw monitor. VM is setup enough here for raw monitor enter.
3522 JvmtiExport::transition_pending_onload_raw_monitors(); 3508 JvmtiExport::transition_pending_onload_raw_monitors();
3523
3524 // Fully start NMT
3525 MemTracker::start();
3526 3509
3527 // Create the VMThread 3510 // Create the VMThread
3528 { TraceTime timer("Start VMThread", TraceStartupTime); 3511 { TraceTime timer("Start VMThread", TraceStartupTime);
3529 VMThread::create(); 3512 VMThread::create();
3530 Thread* vmthread = VMThread::vm_thread(); 3513 Thread* vmthread = VMThread::vm_thread();
3977 vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name()); 3960 vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
3978 } 3961 }
3979 } 3962 }
3980 } 3963 }
3981 3964
3965 JavaThread* Threads::find_java_thread_from_java_tid(jlong java_tid) {
3966 assert(Threads_lock->owned_by_self(), "Must hold Threads_lock");
3967
3968 JavaThread* java_thread = NULL;
3969 // Sequential search for now. Need to do better optimization later.
3970 for (JavaThread* thread = Threads::first(); thread != NULL; thread = thread->next()) {
3971 oop tobj = thread->threadObj();
3972 if (!thread->is_exiting() &&
3973 tobj != NULL &&
3974 java_tid == java_lang_Thread::thread_id(tobj)) {
3975 java_thread = thread;
3976 break;
3977 }
3978 }
3979 return java_thread;
3980 }
3981
3982
3982 // Last thread running calls java.lang.Shutdown.shutdown() 3983 // Last thread running calls java.lang.Shutdown.shutdown()
3983 void JavaThread::invoke_shutdown_hooks() { 3984 void JavaThread::invoke_shutdown_hooks() {
3984 HandleMark hm(this); 3985 HandleMark hm(this);
3985 3986
3986 // We could get here with a pending exception, if so clear it now. 3987 // We could get here with a pending exception, if so clear it now.
4173 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) { 4174 if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4174 _number_of_non_daemon_threads++; 4175 _number_of_non_daemon_threads++;
4175 daemon = false; 4176 daemon = false;
4176 } 4177 }
4177 4178
4178 p->set_safepoint_visible(true);
4179
4180 ThreadService::add_thread(p, daemon); 4179 ThreadService::add_thread(p, daemon);
4181 4180
4182 // Possible GC point. 4181 // Possible GC point.
4183 Events::log(p, "Thread added: " INTPTR_FORMAT, p); 4182 Events::log(p, "Thread added: " INTPTR_FORMAT, p);
4184 } 4183 }
4220 // Make sure that safepoint code disregard this thread. This is needed since 4219 // Make sure that safepoint code disregard this thread. This is needed since
4221 // the thread might mess around with locks after this point. This can cause it 4220 // the thread might mess around with locks after this point. This can cause it
4222 // to do callbacks into the safepoint code. However, the safepoint code is not aware 4221 // to do callbacks into the safepoint code. However, the safepoint code is not aware
4223 // of this thread since it is removed from the queue. 4222 // of this thread since it is removed from the queue.
4224 p->set_terminated_value(); 4223 p->set_terminated_value();
4225
4226 // Now, this thread is not visible to safepoint
4227 p->set_safepoint_visible(false);
4228 // once the thread becomes safepoint invisible, we can not use its per-thread
4229 // recorder. And Threads::do_threads() no longer walks this thread, so we have
4230 // to release its per-thread recorder here.
4231 MemTracker::thread_exiting(p);
4232 } // unlock Threads_lock 4224 } // unlock Threads_lock
4233 4225
4234 // Since Events::log uses a lock, we grab it outside the Threads_lock 4226 // Since Events::log uses a lock, we grab it outside the Threads_lock
4235 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); 4227 Events::log(p, "Thread exited: " INTPTR_FORMAT, p);
4236 } 4228 }
4251 // In particular, these things should never be called when the Threads_lock 4243 // In particular, these things should never be called when the Threads_lock
4252 // is held by some other thread. (Note: the Safepoint abstraction also 4244 // is held by some other thread. (Note: the Safepoint abstraction also
4253 // uses the Threads_lock to gurantee this property. It also makes sure that 4245 // uses the Threads_lock to gurantee this property. It also makes sure that
4254 // all threads gets blocked when exiting or starting). 4246 // all threads gets blocked when exiting or starting).
4255 4247
4256 void Threads::oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) { 4248 void Threads::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
4257 ALL_JAVA_THREADS(p) { 4249 ALL_JAVA_THREADS(p) {
4258 p->oops_do(f, cld_f, cf); 4250 p->oops_do(f, cld_f, cf);
4259 } 4251 }
4260 VMThread::vm_thread()->oops_do(f, cld_f, cf); 4252 VMThread::vm_thread()->oops_do(f, cld_f, cf);
4261 } 4253 }
4262 4254
4263 void Threads::possibly_parallel_oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) { 4255 void Threads::possibly_parallel_oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
4264 // Introduce a mechanism allowing parallel threads to claim threads as 4256 // Introduce a mechanism allowing parallel threads to claim threads as
4265 // root groups. Overhead should be small enough to use all the time, 4257 // root groups. Overhead should be small enough to use all the time,
4266 // even in sequential code. 4258 // even in sequential code.
4267 SharedHeap* sh = SharedHeap::heap(); 4259 SharedHeap* sh = SharedHeap::heap();
4268 // Cannot yet substitute active_workers for n_par_threads 4260 // Cannot yet substitute active_workers for n_par_threads
4269 // because of G1CollectedHeap::verify() use of 4261 // because of G1CollectedHeap::verify() use of
4270 // SharedHeap::process_strong_roots(). n_par_threads == 0 will 4262 // SharedHeap::process_roots(). n_par_threads == 0 will
4271 // turn off parallelism in process_strong_roots while active_workers 4263 // turn off parallelism in process_roots while active_workers
4272 // is being used for parallelism elsewhere. 4264 // is being used for parallelism elsewhere.
4273 bool is_par = sh->n_par_threads() > 0; 4265 bool is_par = sh->n_par_threads() > 0;
4274 assert(!is_par || 4266 assert(!is_par ||
4275 (SharedHeap::heap()->n_par_threads() == 4267 (SharedHeap::heap()->n_par_threads() ==
4276 SharedHeap::heap()->workers()->active_workers()), "Mismatch"); 4268 SharedHeap::heap()->workers()->active_workers()), "Mismatch");