843 void Thread::nmethods_do(CodeBlobClosure* cf) {
844 // no nmethods in a generic thread...
845 }
846
847 void Thread::metadata_do(void f(Metadata*)) {
848 if (metadata_handles() != NULL) {
849 for (int i = 0; i< metadata_handles()->length(); i++) {
850 f(metadata_handles()->at(i));
851 }
852 }
853 }
854
855 void Thread::print_on(outputStream* st) const {
856 // get_priority assumes osthread initialized
857 if (osthread() != NULL) {
858 int os_prio;
859 if (os::get_native_priority(this, &os_prio) == OS_OK) {
860 st->print("os_prio=%d ", os_prio);
861 }
862 st->print("tid=" INTPTR_FORMAT " ", this);
863 osthread()->print_on(st);
864 }
865 debug_only(if (WizardMode) print_owned_locks_on(st);)
866 }
867
868 // Thread::print_on_error() is called by fatal error handler. Don't use
869 // any lock or allocate memory.
870 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
871 if (is_VM_thread()) st->print("VMThread");
872 else if (is_Compiler_thread()) st->print("CompilerThread");
873 else if (is_Java_thread()) st->print("JavaThread");
874 else if (is_GC_task_thread()) st->print("GCTaskThread");
875 else if (is_Watcher_thread()) st->print("WatcherThread");
876 else if (is_ConcurrentGC_thread()) st->print("ConcurrentGCThread");
877 else st->print("Thread");
878
879 st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
880 _stack_base - _stack_size, _stack_base);
881
882 if (osthread()) {
3004 // "thread_oop" handle.
3005
3006 // Set the thread field (a JavaThread *) of the
3007 // oop representing the java_lang_Thread to the new thread (a JavaThread *).
3008
3009 Handle thread_oop(Thread::current(),
3010 JNIHandles::resolve_non_null(jni_thread));
3011 assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
3012 "must be initialized");
3013 set_threadObj(thread_oop());
3014 java_lang_Thread::set_thread(thread_oop(), this);
3015
3016 if (prio == NoPriority) {
3017 prio = java_lang_Thread::priority(thread_oop());
3018 assert(prio != NoPriority, "A valid priority should be present");
3019 }
3020
3021 // Push the Java priority down to the native thread; needs Threads_lock
3022 Thread::set_priority(this, prio);
3023
3024 // Add the new thread to the Threads list and set it in motion.
3025 // We must have threads lock in order to call Threads::add.
3026 // It is crucial that we do not block before the thread is
3027 // added to the Threads list for if a GC happens, then the java_thread oop
3028 // will not be visited by GC.
3029 Threads::add(this);
3030 }
3031
3032 oop JavaThread::current_park_blocker() {
3033 // Support for JSR-166 locks
3034 oop thread_oop = threadObj();
3035 if (thread_oop != NULL &&
3036 JDK_Version::current().supports_thread_park_blocker()) {
3037 return java_lang_Thread::park_blocker(thread_oop);
3038 }
3039 return NULL;
3040 }
3041
3042
3043 void JavaThread::print_stack_on(outputStream* st) {
3857 extern struct JavaVM_ main_vm;
3858 AgentLibrary* agent;
3859
3860 for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) {
3861 OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
3862
3863 if (on_load_entry != NULL) {
3864 // Invoke the JVM_OnLoad function
3865 JavaThread* thread = JavaThread::current();
3866 ThreadToNativeFromVM ttn(thread);
3867 HandleMark hm(thread);
3868 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
3869 if (err != JNI_OK) {
3870 vm_exit_during_initialization("-Xrun library failed to init", agent->name());
3871 }
3872 } else {
3873 vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
3874 }
3875 }
3876 }
3877
3878 // Last thread running calls java.lang.Shutdown.shutdown()
3879 void JavaThread::invoke_shutdown_hooks() {
3880 HandleMark hm(this);
3881
3882 // We could get here with a pending exception, if so clear it now.
3883 if (this->has_pending_exception()) {
3884 this->clear_pending_exception();
3885 }
3886
3887 EXCEPTION_MARK;
3888 Klass* k =
3889 SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
3890 THREAD);
3891 if (k != NULL) {
3892 // SystemDictionary::resolve_or_null will return null if there was
3893 // an exception. If we cannot load the Shutdown class, just don't
3894 // call Shutdown.shutdown() at all. This will mean the shutdown hooks
3895 // and finalizers (if runFinalizersOnExit is set) won't be run.
3896 // Note that if a shutdown hook was registered or runFinalizersOnExit
|
843 void Thread::nmethods_do(CodeBlobClosure* cf) {
844 // no nmethods in a generic thread...
845 }
846
847 void Thread::metadata_do(void f(Metadata*)) {
848 if (metadata_handles() != NULL) {
849 for (int i = 0; i< metadata_handles()->length(); i++) {
850 f(metadata_handles()->at(i));
851 }
852 }
853 }
854
855 void Thread::print_on(outputStream* st) const {
856 // get_priority assumes osthread initialized
857 if (osthread() != NULL) {
858 int os_prio;
859 if (os::get_native_priority(this, &os_prio) == OS_OK) {
860 st->print("os_prio=%d ", os_prio);
861 }
862 st->print("tid=" INTPTR_FORMAT " ", this);
863 ext().print_on(st);
864 osthread()->print_on(st);
865 }
866 debug_only(if (WizardMode) print_owned_locks_on(st);)
867 }
868
869 // Thread::print_on_error() is called by fatal error handler. Don't use
870 // any lock or allocate memory.
871 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
872 if (is_VM_thread()) st->print("VMThread");
873 else if (is_Compiler_thread()) st->print("CompilerThread");
874 else if (is_Java_thread()) st->print("JavaThread");
875 else if (is_GC_task_thread()) st->print("GCTaskThread");
876 else if (is_Watcher_thread()) st->print("WatcherThread");
877 else if (is_ConcurrentGC_thread()) st->print("ConcurrentGCThread");
878 else st->print("Thread");
879
880 st->print(" [stack: " PTR_FORMAT "," PTR_FORMAT "]",
881 _stack_base - _stack_size, _stack_base);
882
883 if (osthread()) {
3005 // "thread_oop" handle.
3006
3007 // Set the thread field (a JavaThread *) of the
3008 // oop representing the java_lang_Thread to the new thread (a JavaThread *).
3009
3010 Handle thread_oop(Thread::current(),
3011 JNIHandles::resolve_non_null(jni_thread));
3012 assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
3013 "must be initialized");
3014 set_threadObj(thread_oop());
3015 java_lang_Thread::set_thread(thread_oop(), this);
3016
3017 if (prio == NoPriority) {
3018 prio = java_lang_Thread::priority(thread_oop());
3019 assert(prio != NoPriority, "A valid priority should be present");
3020 }
3021
3022 // Push the Java priority down to the native thread; needs Threads_lock
3023 Thread::set_priority(this, prio);
3024
3025 prepare_ext();
3026
3027 // Add the new thread to the Threads list and set it in motion.
3028 // We must have threads lock in order to call Threads::add.
3029 // It is crucial that we do not block before the thread is
3030 // added to the Threads list for if a GC happens, then the java_thread oop
3031 // will not be visited by GC.
3032 Threads::add(this);
3033 }
3034
3035 oop JavaThread::current_park_blocker() {
3036 // Support for JSR-166 locks
3037 oop thread_oop = threadObj();
3038 if (thread_oop != NULL &&
3039 JDK_Version::current().supports_thread_park_blocker()) {
3040 return java_lang_Thread::park_blocker(thread_oop);
3041 }
3042 return NULL;
3043 }
3044
3045
3046 void JavaThread::print_stack_on(outputStream* st) {
3860 extern struct JavaVM_ main_vm;
3861 AgentLibrary* agent;
3862
3863 for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) {
3864 OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent);
3865
3866 if (on_load_entry != NULL) {
3867 // Invoke the JVM_OnLoad function
3868 JavaThread* thread = JavaThread::current();
3869 ThreadToNativeFromVM ttn(thread);
3870 HandleMark hm(thread);
3871 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL);
3872 if (err != JNI_OK) {
3873 vm_exit_during_initialization("-Xrun library failed to init", agent->name());
3874 }
3875 } else {
3876 vm_exit_during_initialization("Could not find JVM_OnLoad function in -Xrun library", agent->name());
3877 }
3878 }
3879 }
3880
3881 JavaThread* Threads::find_java_thread_from_java_tid(jlong java_tid) {
3882 assert(Threads_lock->owned_by_self(), "Must hold Threads_lock");
3883
3884 JavaThread* java_thread = NULL;
3885 // Sequential search for now. Need to do better optimization later.
3886 for (JavaThread* thread = Threads::first(); thread != NULL; thread = thread->next()) {
3887 oop tobj = thread->threadObj();
3888 if (!thread->is_exiting() &&
3889 tobj != NULL &&
3890 java_tid == java_lang_Thread::thread_id(tobj)) {
3891 java_thread = thread;
3892 break;
3893 }
3894 }
3895 return java_thread;
3896 }
3897
3898
3899 // Last thread running calls java.lang.Shutdown.shutdown()
3900 void JavaThread::invoke_shutdown_hooks() {
3901 HandleMark hm(this);
3902
3903 // We could get here with a pending exception, if so clear it now.
3904 if (this->has_pending_exception()) {
3905 this->clear_pending_exception();
3906 }
3907
3908 EXCEPTION_MARK;
3909 Klass* k =
3910 SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
3911 THREAD);
3912 if (k != NULL) {
3913 // SystemDictionary::resolve_or_null will return null if there was
3914 // an exception. If we cannot load the Shutdown class, just don't
3915 // call Shutdown.shutdown() at all. This will mean the shutdown hooks
3916 // and finalizers (if runFinalizersOnExit is set) won't be run.
3917 // Note that if a shutdown hook was registered or runFinalizersOnExit
|