2006 } 2007 2008 ThreadBlockInVM tbivm(this); 2009 java_suspend_self(); 2010 2011 // We're done with this suspend request, but we have to loop around 2012 // and check again. Eventually we will get SR_lock without a pending 2013 // external suspend request and will be able to mark ourselves as 2014 // exiting. 2015 } 2016 // no more external suspends are allowed at this point 2017 } else { 2018 assert(!is_terminated() && !is_exiting(), "must not be exiting"); 2019 // before_exit() has already posted JVMTI THREAD_END events 2020 } 2021 2022 if (log_is_enabled(Debug, os, thread, timer)) { 2023 _timer_exit_phase1.stop(); 2024 _timer_exit_phase2.start(); 2025 } 2026 // Notify waiters on thread object. This has to be done after exit() is called 2027 // on the thread (if the thread is the last thread in a daemon ThreadGroup the 2028 // group should have the destroyed bit set before waiters are notified). 2029 ensure_join(this); 2030 assert(!this->has_pending_exception(), "ensure_join should have cleared"); 2031 2032 if (log_is_enabled(Debug, os, thread, timer)) { 2033 _timer_exit_phase2.stop(); 2034 _timer_exit_phase3.start(); 2035 } 2036 // 6282335 JNI DetachCurrentThread spec states that all Java monitors 2037 // held by this thread must be released. The spec does not distinguish 2038 // between JNI-acquired and regular Java monitors. We can only see 2039 // regular Java monitors here if monitor enter-exit matching is broken. 2040 // 2041 // ensure_join() ignores IllegalThreadStateExceptions, and so does 2042 // ObjectSynchronizer::release_monitors_owned_by_thread(). 2043 if (exit_type == jni_detach) { 2044 // Sanity check even though JNI DetachCurrentThread() would have 2045 // returned JNI_ERR if there was a Java frame. JavaThread exit 2074 } 2075 2076 if (JvmtiEnv::environments_might_exist()) { 2077 JvmtiExport::cleanup_thread(this); 2078 } 2079 2080 // We must flush any deferred card marks and other various GC barrier 2081 // related buffers (e.g. G1 SATB buffer and G1 dirty card queue buffer) 2082 // before removing a thread from the list of active threads. 2083 BarrierSet::barrier_set()->on_thread_detach(this); 2084 2085 log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").", 2086 exit_type == JavaThread::normal_exit ? "exiting" : "detaching", 2087 os::current_thread_id()); 2088 2089 if (log_is_enabled(Debug, os, thread, timer)) { 2090 _timer_exit_phase3.stop(); 2091 _timer_exit_phase4.start(); 2092 } 2093 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread 2094 Threads::remove(this); 2095 2096 if (log_is_enabled(Debug, os, thread, timer)) { 2097 _timer_exit_phase4.stop(); 2098 ResourceMark rm(this); 2099 log_debug(os, thread, timer)("name='%s'" 2100 ", exit-phase1=" JLONG_FORMAT 2101 ", exit-phase2=" JLONG_FORMAT 2102 ", exit-phase3=" JLONG_FORMAT 2103 ", exit-phase4=" JLONG_FORMAT, 2104 get_thread_name(), 2105 _timer_exit_phase1.milliseconds(), 2106 _timer_exit_phase2.milliseconds(), 2107 _timer_exit_phase3.milliseconds(), 2108 _timer_exit_phase4.milliseconds()); 2109 } 2110 } 2111 2112 void JavaThread::cleanup_failed_attach_current_thread() { 2113 if (active_handles() != NULL) { 2114 JNIHandleBlock* block = active_handles(); 2115 set_active_handles(NULL); 2116 JNIHandleBlock::release_block(block); 2117 } 2118 2119 if (free_handle_block() != NULL) { 2120 JNIHandleBlock* block = free_handle_block(); 2121 set_free_handle_block(NULL); 2122 JNIHandleBlock::release_block(block); 2123 } 2124 2125 // These have to be removed while this is still a valid thread. 2126 remove_stack_guard_pages(); 2127 2128 if (UseTLAB) { 2129 tlab().retire(); 2130 } 2131 2132 BarrierSet::barrier_set()->on_thread_detach(this); 2133 2134 Threads::remove(this); 2135 this->smr_delete(); 2136 } 2137 2138 JavaThread* JavaThread::active() { 2139 Thread* thread = Thread::current(); 2140 if (thread->is_Java_thread()) { 2141 return (JavaThread*) thread; 2142 } else { 2143 assert(thread->is_VM_thread(), "this must be a vm thread"); 2144 VM_Operation* op = ((VMThread*) thread)->vm_operation(); 2145 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread(); 2146 assert(ret->is_Java_thread(), "must be a Java thread"); 2147 return ret; 2148 } 2149 } 2150 2151 bool JavaThread::is_lock_owned(address adr) const { 2152 if (Thread::is_lock_owned(adr)) return true; 2153 2154 for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) { 4440 4441 _number_of_threads++; 4442 oop threadObj = p->threadObj(); 4443 bool daemon = true; 4444 // Bootstrapping problem: threadObj can be null for initial 4445 // JavaThread (or for threads attached via JNI) 4446 if ((!force_daemon) && !is_daemon((threadObj))) { 4447 _number_of_non_daemon_threads++; 4448 daemon = false; 4449 } 4450 4451 ThreadService::add_thread(p, daemon); 4452 4453 // Maintain fast thread list 4454 ThreadsSMRSupport::add_thread(p); 4455 4456 // Possible GC point. 4457 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p)); 4458 } 4459 4460 void Threads::remove(JavaThread* p) { 4461 4462 // Reclaim the ObjectMonitors from the omInUseList and omFreeList of the moribund thread. 4463 ObjectSynchronizer::omFlush(p); 4464 4465 // Extra scope needed for Thread_lock, so we can check 4466 // that we do not remove thread without safepoint code notice 4467 { MutexLocker ml(Threads_lock); 4468 4469 assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present"); 4470 4471 // Maintain fast thread list 4472 ThreadsSMRSupport::remove_thread(p); 4473 4474 JavaThread* current = _thread_list; 4475 JavaThread* prev = NULL; 4476 4477 while (current != p) { 4478 prev = current; 4479 current = current->next(); 4480 } 4481 4482 if (prev) { 4483 prev->set_next(current->next()); 4484 } else { 4485 _thread_list = p->next(); 4486 } 4487 4488 _number_of_threads--; 4489 oop threadObj = p->threadObj(); 4490 bool daemon = true; 4491 if (!is_daemon(threadObj)) { 4492 _number_of_non_daemon_threads--; 4493 daemon = false; 4494 4495 // Only one thread left, do a notify on the Threads_lock so a thread waiting 4496 // on destroy_vm will wake up. 4497 if (number_of_non_daemon_threads() == 1) { 4498 Threads_lock->notify_all(); 4499 } 4500 } 4501 ThreadService::remove_thread(p, daemon); 4502 4503 // Make sure that safepoint code disregard this thread. This is needed since 4504 // the thread might mess around with locks after this point. This can cause it 4505 // to do callbacks into the safepoint code. However, the safepoint code is not aware 4506 // of this thread since it is removed from the queue. 4507 p->set_terminated_value(); 4508 } // unlock Threads_lock 4509 4510 // Since Events::log uses a lock, we grab it outside the Threads_lock 4511 Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p)); 4512 } 4513 4514 // Operations on the Threads list for GC. These are not explicitly locked, 4515 // but the garbage collector must provide a safe context for them to run. 4516 // In particular, these things should never be called when the Threads_lock 4517 // is held by some other thread. (Note: the Safepoint abstraction also 4518 // uses the Threads_lock to guarantee this property. It also makes sure that 4519 // all threads gets blocked when exiting or starting). 4520 4521 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) { | 2006 } 2007 2008 ThreadBlockInVM tbivm(this); 2009 java_suspend_self(); 2010 2011 // We're done with this suspend request, but we have to loop around 2012 // and check again. Eventually we will get SR_lock without a pending 2013 // external suspend request and will be able to mark ourselves as 2014 // exiting. 2015 } 2016 // no more external suspends are allowed at this point 2017 } else { 2018 assert(!is_terminated() && !is_exiting(), "must not be exiting"); 2019 // before_exit() has already posted JVMTI THREAD_END events 2020 } 2021 2022 if (log_is_enabled(Debug, os, thread, timer)) { 2023 _timer_exit_phase1.stop(); 2024 _timer_exit_phase2.start(); 2025 } 2026 2027 // Capture daemon status before the thread is marked as terminated. 2028 bool daemon = is_daemon(threadObj()); 2029 2030 // Notify waiters on thread object. This has to be done after exit() is called 2031 // on the thread (if the thread is the last thread in a daemon ThreadGroup the 2032 // group should have the destroyed bit set before waiters are notified). 2033 ensure_join(this); 2034 assert(!this->has_pending_exception(), "ensure_join should have cleared"); 2035 2036 if (log_is_enabled(Debug, os, thread, timer)) { 2037 _timer_exit_phase2.stop(); 2038 _timer_exit_phase3.start(); 2039 } 2040 // 6282335 JNI DetachCurrentThread spec states that all Java monitors 2041 // held by this thread must be released. The spec does not distinguish 2042 // between JNI-acquired and regular Java monitors. We can only see 2043 // regular Java monitors here if monitor enter-exit matching is broken. 2044 // 2045 // ensure_join() ignores IllegalThreadStateExceptions, and so does 2046 // ObjectSynchronizer::release_monitors_owned_by_thread(). 2047 if (exit_type == jni_detach) { 2048 // Sanity check even though JNI DetachCurrentThread() would have 2049 // returned JNI_ERR if there was a Java frame. JavaThread exit 2078 } 2079 2080 if (JvmtiEnv::environments_might_exist()) { 2081 JvmtiExport::cleanup_thread(this); 2082 } 2083 2084 // We must flush any deferred card marks and other various GC barrier 2085 // related buffers (e.g. G1 SATB buffer and G1 dirty card queue buffer) 2086 // before removing a thread from the list of active threads. 2087 BarrierSet::barrier_set()->on_thread_detach(this); 2088 2089 log_info(os, thread)("JavaThread %s (tid: " UINTX_FORMAT ").", 2090 exit_type == JavaThread::normal_exit ? "exiting" : "detaching", 2091 os::current_thread_id()); 2092 2093 if (log_is_enabled(Debug, os, thread, timer)) { 2094 _timer_exit_phase3.stop(); 2095 _timer_exit_phase4.start(); 2096 } 2097 // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread 2098 Threads::remove(this, daemon); 2099 2100 if (log_is_enabled(Debug, os, thread, timer)) { 2101 _timer_exit_phase4.stop(); 2102 ResourceMark rm(this); 2103 log_debug(os, thread, timer)("name='%s'" 2104 ", exit-phase1=" JLONG_FORMAT 2105 ", exit-phase2=" JLONG_FORMAT 2106 ", exit-phase3=" JLONG_FORMAT 2107 ", exit-phase4=" JLONG_FORMAT, 2108 get_thread_name(), 2109 _timer_exit_phase1.milliseconds(), 2110 _timer_exit_phase2.milliseconds(), 2111 _timer_exit_phase3.milliseconds(), 2112 _timer_exit_phase4.milliseconds()); 2113 } 2114 } 2115 2116 void JavaThread::cleanup_failed_attach_current_thread(bool is_daemon) { 2117 if (active_handles() != NULL) { 2118 JNIHandleBlock* block = active_handles(); 2119 set_active_handles(NULL); 2120 JNIHandleBlock::release_block(block); 2121 } 2122 2123 if (free_handle_block() != NULL) { 2124 JNIHandleBlock* block = free_handle_block(); 2125 set_free_handle_block(NULL); 2126 JNIHandleBlock::release_block(block); 2127 } 2128 2129 // These have to be removed while this is still a valid thread. 2130 remove_stack_guard_pages(); 2131 2132 if (UseTLAB) { 2133 tlab().retire(); 2134 } 2135 2136 BarrierSet::barrier_set()->on_thread_detach(this); 2137 2138 Threads::remove(this, is_daemon); 2139 this->smr_delete(); 2140 } 2141 2142 JavaThread* JavaThread::active() { 2143 Thread* thread = Thread::current(); 2144 if (thread->is_Java_thread()) { 2145 return (JavaThread*) thread; 2146 } else { 2147 assert(thread->is_VM_thread(), "this must be a vm thread"); 2148 VM_Operation* op = ((VMThread*) thread)->vm_operation(); 2149 JavaThread *ret=op == NULL ? NULL : (JavaThread *)op->calling_thread(); 2150 assert(ret->is_Java_thread(), "must be a Java thread"); 2151 return ret; 2152 } 2153 } 2154 2155 bool JavaThread::is_lock_owned(address adr) const { 2156 if (Thread::is_lock_owned(adr)) return true; 2157 2158 for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) { 4444 4445 _number_of_threads++; 4446 oop threadObj = p->threadObj(); 4447 bool daemon = true; 4448 // Bootstrapping problem: threadObj can be null for initial 4449 // JavaThread (or for threads attached via JNI) 4450 if ((!force_daemon) && !is_daemon((threadObj))) { 4451 _number_of_non_daemon_threads++; 4452 daemon = false; 4453 } 4454 4455 ThreadService::add_thread(p, daemon); 4456 4457 // Maintain fast thread list 4458 ThreadsSMRSupport::add_thread(p); 4459 4460 // Possible GC point. 4461 Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p)); 4462 } 4463 4464 void Threads::remove(JavaThread* p, bool is_daemon) { 4465 4466 // Reclaim the ObjectMonitors from the omInUseList and omFreeList of the moribund thread. 4467 ObjectSynchronizer::omFlush(p); 4468 4469 // Extra scope needed for Thread_lock, so we can check 4470 // that we do not remove thread without safepoint code notice 4471 { MutexLocker ml(Threads_lock); 4472 4473 assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present"); 4474 4475 // Maintain fast thread list 4476 ThreadsSMRSupport::remove_thread(p); 4477 4478 JavaThread* current = _thread_list; 4479 JavaThread* prev = NULL; 4480 4481 while (current != p) { 4482 prev = current; 4483 current = current->next(); 4484 } 4485 4486 if (prev) { 4487 prev->set_next(current->next()); 4488 } else { 4489 _thread_list = p->next(); 4490 } 4491 4492 _number_of_threads--; 4493 if (!is_daemon) { 4494 _number_of_non_daemon_threads--; 4495 4496 // Only one thread left, do a notify on the Threads_lock so a thread waiting 4497 // on destroy_vm will wake up. 4498 if (number_of_non_daemon_threads() == 1) { 4499 Threads_lock->notify_all(); 4500 } 4501 } 4502 ThreadService::remove_thread(p, is_daemon); 4503 4504 // Make sure that safepoint code disregard this thread. This is needed since 4505 // the thread might mess around with locks after this point. This can cause it 4506 // to do callbacks into the safepoint code. However, the safepoint code is not aware 4507 // of this thread since it is removed from the queue. 4508 p->set_terminated_value(); 4509 } // unlock Threads_lock 4510 4511 // Since Events::log uses a lock, we grab it outside the Threads_lock 4512 Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p)); 4513 } 4514 4515 // Operations on the Threads list for GC. These are not explicitly locked, 4516 // but the garbage collector must provide a safe context for them to run. 4517 // In particular, these things should never be called when the Threads_lock 4518 // is held by some other thread. (Note: the Safepoint abstraction also 4519 // uses the Threads_lock to guarantee this property. It also makes sure that 4520 // all threads gets blocked when exiting or starting). 4521 4522 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) { |