< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page
rev 52112 : [mq]: 8021335


1802 }
1803 
1804 
1805 static void ensure_join(JavaThread* thread) {
1806   // We do not need to grab the Threads_lock, since we are operating on ourself.
1807   Handle threadObj(thread, thread->threadObj());
1808   assert(threadObj.not_null(), "java thread object must exist");
1809   ObjectLocker lock(threadObj, thread);
1810   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1811   thread->clear_pending_exception();
1812   // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
1813   java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1814   // Clear the native thread instance - this makes isAlive return false and allows the join()
1815   // to complete once we've done the notify_all below
1816   java_lang_Thread::set_thread(threadObj(), NULL);
1817   lock.notify_all(thread);
1818   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1819   thread->clear_pending_exception();
1820 }
1821 



1822 
1823 // For any new cleanup additions, please check to see if they need to be applied to
1824 // cleanup_failed_attach_current_thread as well.
1825 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1826   assert(this == JavaThread::current(), "thread consistency check");
1827 
1828   elapsedTimer _timer_exit_phase1;
1829   elapsedTimer _timer_exit_phase2;
1830   elapsedTimer _timer_exit_phase3;
1831   elapsedTimer _timer_exit_phase4;
1832 
1833   if (log_is_enabled(Debug, os, thread, timer)) {
1834     _timer_exit_phase1.start();
1835   }
1836 
1837   HandleMark hm(this);
1838   Handle uncaught_exception(this, this->pending_exception());
1839   this->clear_pending_exception();
1840   Handle threadObj(this, this->threadObj());
1841   assert(threadObj.not_null(), "Java thread object should be created");


1893                                 threadObj, thread_klass,
1894                                 vmSymbols::exit_method_name(),
1895                                 vmSymbols::void_method_signature(),
1896                                 THREAD);
1897         CLEAR_PENDING_EXCEPTION;
1898       }
1899     }
1900     // notify JVMTI
1901     if (JvmtiExport::should_post_thread_life()) {
1902       JvmtiExport::post_thread_end(this);
1903     }
1904 
1905     // We have notified the agents that we are exiting, before we go on,
1906     // we must check for a pending external suspend request and honor it
1907     // in order to not surprise the thread that made the suspend request.
1908     while (true) {
1909       {
1910         MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1911         if (!is_external_suspend()) {
1912           set_terminated(_thread_exiting);
1913           ThreadService::current_thread_exiting(this);
1914           break;
1915         }
1916         // Implied else:
1917         // Things get a little tricky here. We have a pending external
1918         // suspend request, but we are holding the SR_lock so we
1919         // can't just self-suspend. So we temporarily drop the lock
1920         // and then self-suspend.
1921       }
1922 
1923       ThreadBlockInVM tbivm(this);
1924       java_suspend_self();
1925 
1926       // We're done with this suspend request, but we have to loop around
1927       // and check again. Eventually we will get SR_lock without a pending
1928       // external suspend request and will be able to mark ourselves as
1929       // exiting.
1930     }
1931     // no more external suspends are allowed at this point



1932   } else {

1933     // before_exit() has already posted JVMTI THREAD_END events
1934   }
1935 
1936   if (log_is_enabled(Debug, os, thread, timer)) {
1937     _timer_exit_phase1.stop();
1938     _timer_exit_phase2.start();
1939   }
1940   // Notify waiters on thread object. This has to be done after exit() is called
1941   // on the thread (if the thread is the last thread in a daemon ThreadGroup the
1942   // group should have the destroyed bit set before waiters are notified).
1943   ensure_join(this);
1944   assert(!this->has_pending_exception(), "ensure_join should have cleared");
1945 
1946   if (log_is_enabled(Debug, os, thread, timer)) {
1947     _timer_exit_phase2.stop();
1948     _timer_exit_phase3.start();
1949   }
1950   // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1951   // held by this thread must be released. The spec does not distinguish
1952   // between JNI-acquired and regular Java monitors. We can only see


4315 
4316 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
4317   if (version == JNI_VERSION_1_1) return JNI_TRUE;
4318   return is_supported_jni_version(version);
4319 }
4320 
4321 
4322 jboolean Threads::is_supported_jni_version(jint version) {
4323   if (version == JNI_VERSION_1_2) return JNI_TRUE;
4324   if (version == JNI_VERSION_1_4) return JNI_TRUE;
4325   if (version == JNI_VERSION_1_6) return JNI_TRUE;
4326   if (version == JNI_VERSION_1_8) return JNI_TRUE;
4327   if (version == JNI_VERSION_9) return JNI_TRUE;
4328   if (version == JNI_VERSION_10) return JNI_TRUE;
4329   return JNI_FALSE;
4330 }
4331 
4332 
4333 void Threads::add(JavaThread* p, bool force_daemon) {
4334   // The threads lock must be owned at this point
4335   assert_locked_or_safepoint(Threads_lock);
4336 
4337   BarrierSet::barrier_set()->on_thread_attach(p);
4338 
4339   p->set_next(_thread_list);
4340   _thread_list = p;
4341 
4342   // Once a JavaThread is added to the Threads list, smr_delete() has
4343   // to be used to delete it. Otherwise we can just delete it directly.
4344   p->set_on_thread_list();
4345 
4346   _number_of_threads++;
4347   oop threadObj = p->threadObj();
4348   bool daemon = true;
4349   // Bootstrapping problem: threadObj can be null for initial
4350   // JavaThread (or for threads attached via JNI)
4351   if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {
4352     _number_of_non_daemon_threads++;
4353     daemon = false;
4354   }
4355 
4356   ThreadService::add_thread(p, daemon);
4357 
4358   // Maintain fast thread list
4359   ThreadsSMRSupport::add_thread(p);
4360 
4361   // Possible GC point.
4362   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4363 }
4364 
4365 void Threads::remove(JavaThread* p) {
4366 
4367   // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
4368   ObjectSynchronizer::omFlush(p);
4369 
4370   // Extra scope needed for Thread_lock, so we can check
4371   // that we do not remove thread without safepoint code notice


4376     // Maintain fast thread list
4377     ThreadsSMRSupport::remove_thread(p);
4378 
4379     JavaThread* current = _thread_list;
4380     JavaThread* prev    = NULL;
4381 
4382     while (current != p) {
4383       prev    = current;
4384       current = current->next();
4385     }
4386 
4387     if (prev) {
4388       prev->set_next(current->next());
4389     } else {
4390       _thread_list = p->next();
4391     }
4392 
4393     _number_of_threads--;
4394     oop threadObj = p->threadObj();
4395     bool daemon = true;
4396     if (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj)) {
4397       _number_of_non_daemon_threads--;
4398       daemon = false;
4399 
4400       // Only one thread left, do a notify on the Threads_lock so a thread waiting
4401       // on destroy_vm will wake up.
4402       if (number_of_non_daemon_threads() == 1) {
4403         Threads_lock->notify_all();
4404       }
4405     }
4406     ThreadService::remove_thread(p, daemon);
4407 
4408     // Make sure that safepoint code disregard this thread. This is needed since
4409     // the thread might mess around with locks after this point. This can cause it
4410     // to do callbacks into the safepoint code. However, the safepoint code is not aware
4411     // of this thread since it is removed from the queue.
4412     p->set_terminated_value();
4413   } // unlock Threads_lock
4414 
4415   // Since Events::log uses a lock, we grab it outside the Threads_lock
4416   Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));




1802 }
1803 
1804 
1805 static void ensure_join(JavaThread* thread) {
1806   // We do not need to grab the Threads_lock, since we are operating on ourself.
1807   Handle threadObj(thread, thread->threadObj());
1808   assert(threadObj.not_null(), "java thread object must exist");
1809   ObjectLocker lock(threadObj, thread);
1810   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1811   thread->clear_pending_exception();
1812   // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
1813   java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
1814   // Clear the native thread instance - this makes isAlive return false and allows the join()
1815   // to complete once we've done the notify_all below
1816   java_lang_Thread::set_thread(threadObj(), NULL);
1817   lock.notify_all(thread);
1818   // Ignore pending exception (ThreadDeath), since we are exiting anyway
1819   thread->clear_pending_exception();
1820 }
1821 
1822 static bool is_daemon(oop threadObj) {
1823   return (threadObj != NULL && java_lang_Thread::is_daemon(threadObj));
1824 }
1825 
1826 // For any new cleanup additions, please check to see if they need to be applied to
1827 // cleanup_failed_attach_current_thread as well.
1828 void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
1829   assert(this == JavaThread::current(), "thread consistency check");
1830 
1831   elapsedTimer _timer_exit_phase1;
1832   elapsedTimer _timer_exit_phase2;
1833   elapsedTimer _timer_exit_phase3;
1834   elapsedTimer _timer_exit_phase4;
1835 
1836   if (log_is_enabled(Debug, os, thread, timer)) {
1837     _timer_exit_phase1.start();
1838   }
1839 
1840   HandleMark hm(this);
1841   Handle uncaught_exception(this, this->pending_exception());
1842   this->clear_pending_exception();
1843   Handle threadObj(this, this->threadObj());
1844   assert(threadObj.not_null(), "Java thread object should be created");


1896                                 threadObj, thread_klass,
1897                                 vmSymbols::exit_method_name(),
1898                                 vmSymbols::void_method_signature(),
1899                                 THREAD);
1900         CLEAR_PENDING_EXCEPTION;
1901       }
1902     }
1903     // notify JVMTI
1904     if (JvmtiExport::should_post_thread_life()) {
1905       JvmtiExport::post_thread_end(this);
1906     }
1907 
1908     // We have notified the agents that we are exiting, before we go on,
1909     // we must check for a pending external suspend request and honor it
1910     // in order to not surprise the thread that made the suspend request.
1911     while (true) {
1912       {
1913         MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1914         if (!is_external_suspend()) {
1915           set_terminated(_thread_exiting);

1916           break;
1917         }
1918         // Implied else:
1919         // Things get a little tricky here. We have a pending external
1920         // suspend request, but we are holding the SR_lock so we
1921         // can't just self-suspend. So we temporarily drop the lock
1922         // and then self-suspend.
1923       }
1924 
1925       ThreadBlockInVM tbivm(this);
1926       java_suspend_self();
1927 
1928       // We're done with this suspend request, but we have to loop around
1929       // and check again. Eventually we will get SR_lock without a pending
1930       // external suspend request and will be able to mark ourselves as
1931       // exiting.
1932     }
1933     // no more external suspends are allowed at this point
1934     { MutexLocker mu(Threads_lock);
1935       ThreadService::current_thread_exiting(this, is_daemon(threadObj()));
1936     }
1937   } else {
1938     assert(!is_terminated() && !is_exiting(), "must not be exiting");
1939     // before_exit() has already posted JVMTI THREAD_END events
1940   }
1941 
1942   if (log_is_enabled(Debug, os, thread, timer)) {
1943     _timer_exit_phase1.stop();
1944     _timer_exit_phase2.start();
1945   }
1946   // Notify waiters on thread object. This has to be done after exit() is called
1947   // on the thread (if the thread is the last thread in a daemon ThreadGroup the
1948   // group should have the destroyed bit set before waiters are notified).
1949   ensure_join(this);
1950   assert(!this->has_pending_exception(), "ensure_join should have cleared");
1951 
1952   if (log_is_enabled(Debug, os, thread, timer)) {
1953     _timer_exit_phase2.stop();
1954     _timer_exit_phase3.start();
1955   }
1956   // 6282335 JNI DetachCurrentThread spec states that all Java monitors
1957   // held by this thread must be released. The spec does not distinguish
1958   // between JNI-acquired and regular Java monitors. We can only see


4321 
4322 jboolean Threads::is_supported_jni_version_including_1_1(jint version) {
4323   if (version == JNI_VERSION_1_1) return JNI_TRUE;
4324   return is_supported_jni_version(version);
4325 }
4326 
4327 
4328 jboolean Threads::is_supported_jni_version(jint version) {
4329   if (version == JNI_VERSION_1_2) return JNI_TRUE;
4330   if (version == JNI_VERSION_1_4) return JNI_TRUE;
4331   if (version == JNI_VERSION_1_6) return JNI_TRUE;
4332   if (version == JNI_VERSION_1_8) return JNI_TRUE;
4333   if (version == JNI_VERSION_9) return JNI_TRUE;
4334   if (version == JNI_VERSION_10) return JNI_TRUE;
4335   return JNI_FALSE;
4336 }
4337 
4338 
4339 void Threads::add(JavaThread* p, bool force_daemon) {
4340   // The threads lock must be owned at this point
4341   assert(Threads_lock->owned_by_self(), "must have threads lock");
4342 
4343   BarrierSet::barrier_set()->on_thread_attach(p);
4344 
4345   p->set_next(_thread_list);
4346   _thread_list = p;
4347 
4348   // Once a JavaThread is added to the Threads list, smr_delete() has
4349   // to be used to delete it. Otherwise we can just delete it directly.
4350   p->set_on_thread_list();
4351 
4352   _number_of_threads++;
4353   oop threadObj = p->threadObj();
4354   bool daemon = true;
4355   // Bootstrapping problem: threadObj can be null for initial
4356   // JavaThread (or for threads attached via JNI)
4357   if ((!force_daemon) && !is_daemon((threadObj))) {
4358     _number_of_non_daemon_threads++;
4359     daemon = false;
4360   }
4361 
4362   ThreadService::add_thread(p, daemon);
4363 
4364   // Maintain fast thread list
4365   ThreadsSMRSupport::add_thread(p);
4366 
4367   // Possible GC point.
4368   Events::log(p, "Thread added: " INTPTR_FORMAT, p2i(p));
4369 }
4370 
4371 void Threads::remove(JavaThread* p) {
4372 
4373   // Reclaim the objectmonitors from the omInUseList and omFreeList of the moribund thread.
4374   ObjectSynchronizer::omFlush(p);
4375 
4376   // Extra scope needed for Thread_lock, so we can check
4377   // that we do not remove thread without safepoint code notice


4382     // Maintain fast thread list
4383     ThreadsSMRSupport::remove_thread(p);
4384 
4385     JavaThread* current = _thread_list;
4386     JavaThread* prev    = NULL;
4387 
4388     while (current != p) {
4389       prev    = current;
4390       current = current->next();
4391     }
4392 
4393     if (prev) {
4394       prev->set_next(current->next());
4395     } else {
4396       _thread_list = p->next();
4397     }
4398 
4399     _number_of_threads--;
4400     oop threadObj = p->threadObj();
4401     bool daemon = true;
4402     if (!is_daemon(threadObj)) {
4403       _number_of_non_daemon_threads--;
4404       daemon = false;
4405 
4406       // Only one thread left, do a notify on the Threads_lock so a thread waiting
4407       // on destroy_vm will wake up.
4408       if (number_of_non_daemon_threads() == 1) {
4409         Threads_lock->notify_all();
4410       }
4411     }
4412     ThreadService::remove_thread(p, daemon);
4413 
4414     // Make sure that safepoint code disregard this thread. This is needed since
4415     // the thread might mess around with locks after this point. This can cause it
4416     // to do callbacks into the safepoint code. However, the safepoint code is not aware
4417     // of this thread since it is removed from the queue.
4418     p->set_terminated_value();
4419   } // unlock Threads_lock
4420 
4421   // Since Events::log uses a lock, we grab it outside the Threads_lock
4422   Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));


< prev index next >