< prev index next >

src/share/vm/runtime/thread.cpp

Print this page




 933 // should be revisited, and they should be removed if possible.
 934 
 935 bool Thread::is_lock_owned(address adr) const {
 936   return on_local_stack(adr);
 937 }
 938 
 939 bool Thread::set_as_starting_thread() {
 940   // NOTE: this must be called inside the main thread.
 941   return os::create_main_thread((JavaThread*)this);
 942 }
 943 
 944 static void initialize_class(Symbol* class_name, TRAPS) {
 945   Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK);
 946   InstanceKlass::cast(klass)->initialize(CHECK);
 947 }
 948 
 949 
 950 // Creates the initial ThreadGroup
 951 static Handle create_initial_thread_group(TRAPS) {
 952   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH);
 953   instanceKlassHandle klass (THREAD, k);
 954 
 955   Handle system_instance = klass->allocate_instance_handle(CHECK_NH);
 956   {
 957     JavaValue result(T_VOID);
 958     JavaCalls::call_special(&result,
 959                             system_instance,
 960                             klass,
 961                             vmSymbols::object_initializer_name(),
 962                             vmSymbols::void_method_signature(),
 963                             CHECK_NH);
 964   }
 965   Universe::set_system_thread_group(system_instance());
 966 
 967   Handle main_instance = klass->allocate_instance_handle(CHECK_NH);
 968   {
 969     JavaValue result(T_VOID);
 970     Handle string = java_lang_String::create_from_str("main", CHECK_NH);
 971     JavaCalls::call_special(&result,
 972                             main_instance,
 973                             klass,
 974                             vmSymbols::object_initializer_name(),
 975                             vmSymbols::threadgroup_string_void_signature(),
 976                             system_instance,
 977                             string,
 978                             CHECK_NH);
 979   }
 980   return main_instance;
 981 }
 982 
 983 // Creates the initial Thread
 984 static oop create_initial_thread(Handle thread_group, JavaThread* thread,
 985                                  TRAPS) {
 986   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_NULL);
 987   instanceKlassHandle klass (THREAD, k);
 988   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);
 989 
 990   java_lang_Thread::set_thread(thread_oop(), thread);
 991   java_lang_Thread::set_priority(thread_oop(), NormPriority);
 992   thread->set_threadObj(thread_oop());
 993 
 994   Handle string = java_lang_String::create_from_str("main", CHECK_NULL);
 995 
 996   JavaValue result(T_VOID);
 997   JavaCalls::call_special(&result, thread_oop,
 998                           klass,
 999                           vmSymbols::object_initializer_name(),
1000                           vmSymbols::threadgroup_string_void_signature(),
1001                           thread_group,
1002                           string,
1003                           CHECK_NULL);
1004   return thread_oop();
1005 }
1006 
1007 char java_runtime_name[128] = "";
1008 char java_runtime_version[128] = "";
1009 
1010 // extract the JRE name from java.lang.VersionProps.java_runtime_name
1011 static const char* get_java_runtime_name(TRAPS) {
1012   Klass* k = SystemDictionary::find(vmSymbols::java_lang_VersionProps(),
1013                                     Handle(), Handle(), CHECK_AND_CLEAR_NULL);
1014   fieldDescriptor fd;
1015   bool found = k != NULL &&
1016                InstanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_name_name(),
1017                                                         vmSymbols::string_signature(), &fd);
1018   if (found) {


1037   bool found = k != NULL &&
1038                InstanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_version_name(),
1039                                                         vmSymbols::string_signature(), &fd);
1040   if (found) {
1041     oop name_oop = k->java_mirror()->obj_field(fd.offset());
1042     if (name_oop == NULL) {
1043       return NULL;
1044     }
1045     const char* name = java_lang_String::as_utf8_string(name_oop,
1046                                                         java_runtime_version,
1047                                                         sizeof(java_runtime_version));
1048     return name;
1049   } else {
1050     return NULL;
1051   }
1052 }
1053 
1054 // General purpose hook into Java code, run once when the VM is initialized.
1055 // The Java library method itself may be changed independently from the VM.
1056 static void call_postVMInitHook(TRAPS) {
1057   Klass* k = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_vm_PostVMInitHook(), THREAD);
1058   instanceKlassHandle klass (THREAD, k);
1059   if (klass.not_null()) {
1060     JavaValue result(T_VOID);
1061     JavaCalls::call_static(&result, klass, vmSymbols::run_method_name(),
1062                            vmSymbols::void_method_signature(),
1063                            CHECK);
1064   }
1065 }
1066 
1067 static void reset_vm_info_property(TRAPS) {
1068   // the vm info string
1069   ResourceMark rm(THREAD);
1070   const char *vm_info = VM_Version::vm_info_string();
1071 
1072   // java.lang.System class
1073   Klass* k =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
1074   instanceKlassHandle klass (THREAD, k);
1075 
1076   // setProperty arguments
1077   Handle key_str    = java_lang_String::create_from_str("java.vm.info", CHECK);
1078   Handle value_str  = java_lang_String::create_from_str(vm_info, CHECK);
1079 
1080   // return value
1081   JavaValue r(T_OBJECT);
1082 
1083   // public static String setProperty(String key, String value);
1084   JavaCalls::call_static(&r,
1085                          klass,
1086                          vmSymbols::setProperty_name(),
1087                          vmSymbols::string_string_string_signature(),
1088                          key_str,
1089                          value_str,
1090                          CHECK);
1091 }
1092 
1093 
1094 void JavaThread::allocate_threadObj(Handle thread_group, const char* thread_name,
1095                                     bool daemon, TRAPS) {
1096   assert(thread_group.not_null(), "thread group should be specified");
1097   assert(threadObj() == NULL, "should only create Java thread object once");
1098 
1099   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
1100   instanceKlassHandle klass (THREAD, k);
1101   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
1102 
1103   java_lang_Thread::set_thread(thread_oop(), this);
1104   java_lang_Thread::set_priority(thread_oop(), NormPriority);
1105   set_threadObj(thread_oop());
1106 
1107   JavaValue result(T_VOID);
1108   if (thread_name != NULL) {
1109     Handle name = java_lang_String::create_from_str(thread_name, CHECK);
1110     // Thread gets assigned specified name and null target
1111     JavaCalls::call_special(&result,
1112                             thread_oop,
1113                             klass,
1114                             vmSymbols::object_initializer_name(),
1115                             vmSymbols::threadgroup_string_void_signature(),
1116                             thread_group, // Argument 1
1117                             name,         // Argument 2
1118                             THREAD);
1119   } else {
1120     // Thread gets assigned name "Thread-nnn" and null target
1121     // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
1122     JavaCalls::call_special(&result,
1123                             thread_oop,
1124                             klass,
1125                             vmSymbols::object_initializer_name(),
1126                             vmSymbols::threadgroup_runnable_void_signature(),
1127                             thread_group, // Argument 1
1128                             Handle(),     // Argument 2
1129                             THREAD);
1130   }
1131 
1132 
1133   if (daemon) {
1134     java_lang_Thread::set_daemon(thread_oop());
1135   }
1136 
1137   if (HAS_PENDING_EXCEPTION) {
1138     return;
1139   }
1140 
1141   KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass());
1142   Handle threadObj(THREAD, this->threadObj());
1143 
1144   JavaCalls::call_special(&result,
1145                           thread_group,
1146                           group,
1147                           vmSymbols::add_method_name(),
1148                           vmSymbols::thread_void_signature(),
1149                           threadObj,          // Arg 1
1150                           THREAD);
1151 }
1152 
1153 // NamedThread --  non-JavaThread subclasses with multiple
1154 // uniquely named instances should derive from this.
1155 NamedThread::NamedThread() : Thread() {
1156   _name = NULL;
1157   _processed_thread = NULL;
1158   _gc_id = GCId::undefined();
1159 }
1160 
1161 NamedThread::~NamedThread() {


1762   Handle threadObj(this, this->threadObj());
1763   assert(threadObj.not_null(), "Java thread object should be created");
1764 
1765   if (get_thread_profiler() != NULL) {
1766     get_thread_profiler()->disengage();
1767     ResourceMark rm;
1768     get_thread_profiler()->print(get_thread_name());
1769   }
1770 
1771 
1772   // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1773   {
1774     EXCEPTION_MARK;
1775 
1776     CLEAR_PENDING_EXCEPTION;
1777   }
1778   if (!destroy_vm) {
1779     if (uncaught_exception.not_null()) {
1780       EXCEPTION_MARK;
1781       // Call method Thread.dispatchUncaughtException().
1782       KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1783       JavaValue result(T_VOID);
1784       JavaCalls::call_virtual(&result,
1785                               threadObj, thread_klass,
1786                               vmSymbols::dispatchUncaughtException_name(),
1787                               vmSymbols::throwable_void_signature(),
1788                               uncaught_exception,
1789                               THREAD);
1790       if (HAS_PENDING_EXCEPTION) {
1791         ResourceMark rm(this);
1792         jio_fprintf(defaultStream::error_stream(),
1793                     "\nException: %s thrown from the UncaughtExceptionHandler"
1794                     " in thread \"%s\"\n",
1795                     pending_exception()->klass()->external_name(),
1796                     get_thread_name());
1797         CLEAR_PENDING_EXCEPTION;
1798       }
1799     }
1800 
1801     // Called before the java thread exit since we want to read info
1802     // from java_lang_Thread object
1803     EventThreadEnd event;
1804     if (event.should_commit()) {
1805       event.set_thread(THREAD_TRACE_ID(this));
1806       event.commit();
1807     }
1808 
1809     // Call after last event on thread
1810     EVENT_THREAD_EXIT(this);
1811 
1812     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1813     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1814     // is deprecated anyhow.
1815     if (!is_Compiler_thread()) {
1816       int count = 3;
1817       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1818         EXCEPTION_MARK;
1819         JavaValue result(T_VOID);
1820         KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1821         JavaCalls::call_virtual(&result,
1822                                 threadObj, thread_klass,
1823                                 vmSymbols::exit_method_name(),
1824                                 vmSymbols::void_method_signature(),
1825                                 THREAD);
1826         CLEAR_PENDING_EXCEPTION;
1827       }
1828     }
1829     // notify JVMTI
1830     if (JvmtiExport::should_post_thread_life()) {
1831       JvmtiExport::post_thread_end(this);
1832     }
1833 
1834     // We have notified the agents that we are exiting, before we go on,
1835     // we must check for a pending external suspend request and honor it
1836     // in order to not surprise the thread that made the suspend request.
1837     while (true) {
1838       {
1839         MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1840         if (!is_external_suspend()) {


3371   // checking in some cases.
3372   if (wt != NULL) {
3373     tc->do_thread(wt);
3374   }
3375 
3376   // If CompilerThreads ever become non-JavaThreads, add them here
3377 }
3378 
3379 // The system initialization in the library has three phases.
3380 //
3381 // Phase 1: java.lang.System class initialization
3382 //     java.lang.System is a primordial class loaded and initialized
3383 //     by the VM early during startup.  java.lang.System.<clinit>
3384 //     only does registerNatives and keeps the rest of the class
3385 //     initialization work later until thread initialization completes.
3386 //
3387 //     System.initPhase1 initializes the system properties, the static
3388 //     fields in, out, and err. Set up java signal handlers, OS-specific
3389 //     system settings, and thread group of the main thread.
3390 static void call_initPhase1(TRAPS) {
3391   Klass* k =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
3392   instanceKlassHandle klass (THREAD, k);
3393 
3394   JavaValue result(T_VOID);
3395   JavaCalls::call_static(&result, klass, vmSymbols::initPhase1_name(),
3396                                          vmSymbols::void_method_signature(), CHECK);
3397 }
3398 
3399 // Phase 2. Module system initialization
3400 //     This will initialize the module system.  Only java.base classes
3401 //     can be loaded until phase 2 completes.
3402 //
3403 //     Call System.initPhase2 after the compiler initialization and jsr292
3404 //     classes get initialized because module initialization runs a lot of java
3405 //     code, that for performance reasons, should be compiled.  Also, this will
3406 //     enable the startup code to use lambda and other language features in this
3407 //     phase and onward.
3408 //
3409 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
3410 static void call_initPhase2(TRAPS) {
3411   TraceTime timer("Phase2 initialization", TRACETIME_LOG(Info, modules, startuptime));
3412 
3413   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
3414   instanceKlassHandle klass (THREAD, k);
3415 
3416   JavaValue result(T_VOID);
3417   JavaCalls::call_static(&result, klass, vmSymbols::initPhase2_name(),
3418                                          vmSymbols::void_method_signature(), CHECK);
3419   universe_post_module_init();
3420 }
3421 
3422 // Phase 3. final setup - set security manager, system class loader and TCCL
3423 //
3424 //     This will instantiate and set the security manager, set the system class
3425 //     loader as well as the thread context class loader.  The security manager
3426 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
3427 //     other modules or the application's classpath.
3428 static void call_initPhase3(TRAPS) {
3429   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
3430   instanceKlassHandle klass (THREAD, k);
3431 
3432   JavaValue result(T_VOID);
3433   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
3434                                          vmSymbols::void_method_signature(), CHECK);
3435 }
3436 
3437 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3438   TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
3439 
3440   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3441     create_vm_init_libraries();
3442   }
3443 
3444   initialize_class(vmSymbols::java_lang_String(), CHECK);
3445 
3446   // Inject CompactStrings value after the static initializers for String ran.
3447   java_lang_String::set_compact_strings(CompactStrings);
3448 
3449   // Initialize java_lang.System (needed before creating the thread)
3450   initialize_class(vmSymbols::java_lang_System(), CHECK);
3451   // The VM creates & returns objects of this class. Make sure it's initialized.


4047         tobj != NULL &&
4048         java_tid == java_lang_Thread::thread_id(tobj)) {
4049       java_thread = thread;
4050       break;
4051     }
4052   }
4053   return java_thread;
4054 }
4055 
4056 
4057 // Last thread running calls java.lang.Shutdown.shutdown()
4058 void JavaThread::invoke_shutdown_hooks() {
4059   HandleMark hm(this);
4060 
4061   // We could get here with a pending exception, if so clear it now.
4062   if (this->has_pending_exception()) {
4063     this->clear_pending_exception();
4064   }
4065 
4066   EXCEPTION_MARK;
4067   Klass* k =
4068     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4069                                       THREAD);
4070   if (k != NULL) {
4071     // SystemDictionary::resolve_or_null will return null if there was
4072     // an exception.  If we cannot load the Shutdown class, just don't
4073     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
4074     // and finalizers (if runFinalizersOnExit is set) won't be run.
4075     // Note that if a shutdown hook was registered or runFinalizersOnExit
4076     // was called, the Shutdown class would have already been loaded
4077     // (Runtime.addShutdownHook and runFinalizersOnExit will load it).
4078     instanceKlassHandle shutdown_klass (THREAD, k);
4079     JavaValue result(T_VOID);
4080     JavaCalls::call_static(&result,
4081                            shutdown_klass,
4082                            vmSymbols::shutdown_method_name(),
4083                            vmSymbols::void_method_signature(),
4084                            THREAD);
4085   }
4086   CLEAR_PENDING_EXCEPTION;
4087 }
4088 
4089 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
4090 // the program falls off the end of main(). Another VM exit path is through
4091 // vm_exit() when the program calls System.exit() to return a value or when
4092 // there is a serious error in VM. The two shutdown paths are not exactly
4093 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
4094 // and VM_Exit op at VM level.
4095 //
4096 // Shutdown sequence:
4097 //   + Shutdown native memory tracking if it is on
4098 //   + Wait until we are the last non-daemon thread to execute




 933 // should be revisited, and they should be removed if possible.
 934 
 935 bool Thread::is_lock_owned(address adr) const {
 936   return on_local_stack(adr);
 937 }
 938 
 939 bool Thread::set_as_starting_thread() {
 940   // NOTE: this must be called inside the main thread.
 941   return os::create_main_thread((JavaThread*)this);
 942 }
 943 
 944 static void initialize_class(Symbol* class_name, TRAPS) {
 945   Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK);
 946   InstanceKlass::cast(klass)->initialize(CHECK);
 947 }
 948 
 949 
 950 // Creates the initial ThreadGroup
 951 static Handle create_initial_thread_group(TRAPS) {
 952   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH);
 953   InstanceKlass* ik = InstanceKlass::cast(k);
 954 
 955   Handle system_instance = ik->allocate_instance_handle(CHECK_NH);
 956   {
 957     JavaValue result(T_VOID);
 958     JavaCalls::call_special(&result,
 959                             system_instance,
 960                             ik,
 961                             vmSymbols::object_initializer_name(),
 962                             vmSymbols::void_method_signature(),
 963                             CHECK_NH);
 964   }
 965   Universe::set_system_thread_group(system_instance());
 966 
 967   Handle main_instance = ik->allocate_instance_handle(CHECK_NH);
 968   {
 969     JavaValue result(T_VOID);
 970     Handle string = java_lang_String::create_from_str("main", CHECK_NH);
 971     JavaCalls::call_special(&result,
 972                             main_instance,
 973                             ik,
 974                             vmSymbols::object_initializer_name(),
 975                             vmSymbols::threadgroup_string_void_signature(),
 976                             system_instance,
 977                             string,
 978                             CHECK_NH);
 979   }
 980   return main_instance;
 981 }
 982 
 983 // Creates the initial Thread
 984 static oop create_initial_thread(Handle thread_group, JavaThread* thread,
 985                                  TRAPS) {
 986   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_NULL);
 987   InstanceKlass* ik = InstanceKlass::cast(k);
 988   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK_NULL);
 989 
 990   java_lang_Thread::set_thread(thread_oop(), thread);
 991   java_lang_Thread::set_priority(thread_oop(), NormPriority);
 992   thread->set_threadObj(thread_oop());
 993 
 994   Handle string = java_lang_String::create_from_str("main", CHECK_NULL);
 995 
 996   JavaValue result(T_VOID);
 997   JavaCalls::call_special(&result, thread_oop,
 998                           ik,
 999                           vmSymbols::object_initializer_name(),
1000                           vmSymbols::threadgroup_string_void_signature(),
1001                           thread_group,
1002                           string,
1003                           CHECK_NULL);
1004   return thread_oop();
1005 }
1006 
1007 char java_runtime_name[128] = "";
1008 char java_runtime_version[128] = "";
1009 
1010 // extract the JRE name from java.lang.VersionProps.java_runtime_name
1011 static const char* get_java_runtime_name(TRAPS) {
1012   Klass* k = SystemDictionary::find(vmSymbols::java_lang_VersionProps(),
1013                                     Handle(), Handle(), CHECK_AND_CLEAR_NULL);
1014   fieldDescriptor fd;
1015   bool found = k != NULL &&
1016                InstanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_name_name(),
1017                                                         vmSymbols::string_signature(), &fd);
1018   if (found) {


1037   bool found = k != NULL &&
1038                InstanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_version_name(),
1039                                                         vmSymbols::string_signature(), &fd);
1040   if (found) {
1041     oop name_oop = k->java_mirror()->obj_field(fd.offset());
1042     if (name_oop == NULL) {
1043       return NULL;
1044     }
1045     const char* name = java_lang_String::as_utf8_string(name_oop,
1046                                                         java_runtime_version,
1047                                                         sizeof(java_runtime_version));
1048     return name;
1049   } else {
1050     return NULL;
1051   }
1052 }
1053 
1054 // General purpose hook into Java code, run once when the VM is initialized.
1055 // The Java library method itself may be changed independently from the VM.
1056 static void call_postVMInitHook(TRAPS) {
1057   Klass* klass = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_vm_PostVMInitHook(), THREAD);
1058   if (klass != NULL) {

1059     JavaValue result(T_VOID);
1060     JavaCalls::call_static(&result, klass, vmSymbols::run_method_name(),
1061                            vmSymbols::void_method_signature(),
1062                            CHECK);
1063   }
1064 }
1065 
1066 static void reset_vm_info_property(TRAPS) {
1067   // the vm info string
1068   ResourceMark rm(THREAD);
1069   const char *vm_info = VM_Version::vm_info_string();
1070 
1071   // java.lang.System class
1072   Klass* klass =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);

1073 
1074   // setProperty arguments
1075   Handle key_str    = java_lang_String::create_from_str("java.vm.info", CHECK);
1076   Handle value_str  = java_lang_String::create_from_str(vm_info, CHECK);
1077 
1078   // return value
1079   JavaValue r(T_OBJECT);
1080 
1081   // public static String setProperty(String key, String value);
1082   JavaCalls::call_static(&r,
1083                          klass,
1084                          vmSymbols::setProperty_name(),
1085                          vmSymbols::string_string_string_signature(),
1086                          key_str,
1087                          value_str,
1088                          CHECK);
1089 }
1090 
1091 
1092 void JavaThread::allocate_threadObj(Handle thread_group, const char* thread_name,
1093                                     bool daemon, TRAPS) {
1094   assert(thread_group.not_null(), "thread group should be specified");
1095   assert(threadObj() == NULL, "should only create Java thread object once");
1096 
1097   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
1098   InstanceKlass* ik = InstanceKlass::cast(k);
1099   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
1100 
1101   java_lang_Thread::set_thread(thread_oop(), this);
1102   java_lang_Thread::set_priority(thread_oop(), NormPriority);
1103   set_threadObj(thread_oop());
1104 
1105   JavaValue result(T_VOID);
1106   if (thread_name != NULL) {
1107     Handle name = java_lang_String::create_from_str(thread_name, CHECK);
1108     // Thread gets assigned specified name and null target
1109     JavaCalls::call_special(&result,
1110                             thread_oop,
1111                             ik,
1112                             vmSymbols::object_initializer_name(),
1113                             vmSymbols::threadgroup_string_void_signature(),
1114                             thread_group, // Argument 1
1115                             name,         // Argument 2
1116                             THREAD);
1117   } else {
1118     // Thread gets assigned name "Thread-nnn" and null target
1119     // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
1120     JavaCalls::call_special(&result,
1121                             thread_oop,
1122                             ik,
1123                             vmSymbols::object_initializer_name(),
1124                             vmSymbols::threadgroup_runnable_void_signature(),
1125                             thread_group, // Argument 1
1126                             Handle(),     // Argument 2
1127                             THREAD);
1128   }
1129 
1130 
1131   if (daemon) {
1132     java_lang_Thread::set_daemon(thread_oop());
1133   }
1134 
1135   if (HAS_PENDING_EXCEPTION) {
1136     return;
1137   }
1138 
1139   Klass* group =  SystemDictionary::ThreadGroup_klass();
1140   Handle threadObj(THREAD, this->threadObj());
1141 
1142   JavaCalls::call_special(&result,
1143                           thread_group,
1144                           group,
1145                           vmSymbols::add_method_name(),
1146                           vmSymbols::thread_void_signature(),
1147                           threadObj,          // Arg 1
1148                           THREAD);
1149 }
1150 
1151 // NamedThread --  non-JavaThread subclasses with multiple
1152 // uniquely named instances should derive from this.
1153 NamedThread::NamedThread() : Thread() {
1154   _name = NULL;
1155   _processed_thread = NULL;
1156   _gc_id = GCId::undefined();
1157 }
1158 
1159 NamedThread::~NamedThread() {


1760   Handle threadObj(this, this->threadObj());
1761   assert(threadObj.not_null(), "Java thread object should be created");
1762 
1763   if (get_thread_profiler() != NULL) {
1764     get_thread_profiler()->disengage();
1765     ResourceMark rm;
1766     get_thread_profiler()->print(get_thread_name());
1767   }
1768 
1769 
1770   // FIXIT: This code should be moved into else part, when reliable 1.2/1.3 check is in place
1771   {
1772     EXCEPTION_MARK;
1773 
1774     CLEAR_PENDING_EXCEPTION;
1775   }
1776   if (!destroy_vm) {
1777     if (uncaught_exception.not_null()) {
1778       EXCEPTION_MARK;
1779       // Call method Thread.dispatchUncaughtException().
1780       Klass* thread_klass = SystemDictionary::Thread_klass();
1781       JavaValue result(T_VOID);
1782       JavaCalls::call_virtual(&result,
1783                               threadObj, thread_klass,
1784                               vmSymbols::dispatchUncaughtException_name(),
1785                               vmSymbols::throwable_void_signature(),
1786                               uncaught_exception,
1787                               THREAD);
1788       if (HAS_PENDING_EXCEPTION) {
1789         ResourceMark rm(this);
1790         jio_fprintf(defaultStream::error_stream(),
1791                     "\nException: %s thrown from the UncaughtExceptionHandler"
1792                     " in thread \"%s\"\n",
1793                     pending_exception()->klass()->external_name(),
1794                     get_thread_name());
1795         CLEAR_PENDING_EXCEPTION;
1796       }
1797     }
1798 
1799     // Called before the java thread exit since we want to read info
1800     // from java_lang_Thread object
1801     EventThreadEnd event;
1802     if (event.should_commit()) {
1803       event.set_thread(THREAD_TRACE_ID(this));
1804       event.commit();
1805     }
1806 
1807     // Call after last event on thread
1808     EVENT_THREAD_EXIT(this);
1809 
1810     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1811     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1812     // is deprecated anyhow.
1813     if (!is_Compiler_thread()) {
1814       int count = 3;
1815       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1816         EXCEPTION_MARK;
1817         JavaValue result(T_VOID);
1818         Klass* thread_klass = SystemDictionary::Thread_klass();
1819         JavaCalls::call_virtual(&result,
1820                                 threadObj, thread_klass,
1821                                 vmSymbols::exit_method_name(),
1822                                 vmSymbols::void_method_signature(),
1823                                 THREAD);
1824         CLEAR_PENDING_EXCEPTION;
1825       }
1826     }
1827     // notify JVMTI
1828     if (JvmtiExport::should_post_thread_life()) {
1829       JvmtiExport::post_thread_end(this);
1830     }
1831 
1832     // We have notified the agents that we are exiting, before we go on,
1833     // we must check for a pending external suspend request and honor it
1834     // in order to not surprise the thread that made the suspend request.
1835     while (true) {
1836       {
1837         MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1838         if (!is_external_suspend()) {


3369   // checking in some cases.
3370   if (wt != NULL) {
3371     tc->do_thread(wt);
3372   }
3373 
3374   // If CompilerThreads ever become non-JavaThreads, add them here
3375 }
3376 
3377 // The system initialization in the library has three phases.
3378 //
3379 // Phase 1: java.lang.System class initialization
3380 //     java.lang.System is a primordial class loaded and initialized
3381 //     by the VM early during startup.  java.lang.System.<clinit>
3382 //     only does registerNatives and keeps the rest of the class
3383 //     initialization work later until thread initialization completes.
3384 //
3385 //     System.initPhase1 initializes the system properties, the static
3386 //     fields in, out, and err. Set up java signal handlers, OS-specific
3387 //     system settings, and thread group of the main thread.
3388 static void call_initPhase1(TRAPS) {
3389   Klass* klass =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);


3390   JavaValue result(T_VOID);
3391   JavaCalls::call_static(&result, klass, vmSymbols::initPhase1_name(),
3392                                          vmSymbols::void_method_signature(), CHECK);
3393 }
3394 
3395 // Phase 2. Module system initialization
3396 //     This will initialize the module system.  Only java.base classes
3397 //     can be loaded until phase 2 completes.
3398 //
3399 //     Call System.initPhase2 after the compiler initialization and jsr292
3400 //     classes get initialized because module initialization runs a lot of java
3401 //     code, that for performance reasons, should be compiled.  Also, this will
3402 //     enable the startup code to use lambda and other language features in this
3403 //     phase and onward.
3404 //
3405 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
3406 static void call_initPhase2(TRAPS) {
3407   TraceTime timer("Phase2 initialization", TRACETIME_LOG(Info, modules, startuptime));
3408 
3409   Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);

3410 
3411   JavaValue result(T_VOID);
3412   JavaCalls::call_static(&result, klass, vmSymbols::initPhase2_name(),
3413                                          vmSymbols::void_method_signature(), CHECK);
3414   universe_post_module_init();
3415 }
3416 
3417 // Phase 3. final setup - set security manager, system class loader and TCCL
3418 //
3419 //     This will instantiate and set the security manager, set the system class
3420 //     loader as well as the thread context class loader.  The security manager
3421 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
3422 //     other modules or the application's classpath.
3423 static void call_initPhase3(TRAPS) {
3424   Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);


3425   JavaValue result(T_VOID);
3426   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
3427                                          vmSymbols::void_method_signature(), CHECK);
3428 }
3429 
3430 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
3431   TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
3432 
3433   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
3434     create_vm_init_libraries();
3435   }
3436 
3437   initialize_class(vmSymbols::java_lang_String(), CHECK);
3438 
3439   // Inject CompactStrings value after the static initializers for String ran.
3440   java_lang_String::set_compact_strings(CompactStrings);
3441 
3442   // Initialize java_lang.System (needed before creating the thread)
3443   initialize_class(vmSymbols::java_lang_System(), CHECK);
3444   // The VM creates & returns objects of this class. Make sure it's initialized.


4040         tobj != NULL &&
4041         java_tid == java_lang_Thread::thread_id(tobj)) {
4042       java_thread = thread;
4043       break;
4044     }
4045   }
4046   return java_thread;
4047 }
4048 
4049 
4050 // Last thread running calls java.lang.Shutdown.shutdown()
4051 void JavaThread::invoke_shutdown_hooks() {
4052   HandleMark hm(this);
4053 
4054   // We could get here with a pending exception, if so clear it now.
4055   if (this->has_pending_exception()) {
4056     this->clear_pending_exception();
4057   }
4058 
4059   EXCEPTION_MARK;
4060   Klass* shutdown_klass =
4061     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
4062                                       THREAD);
4063   if (shutdown_klass != NULL) {
4064     // SystemDictionary::resolve_or_null will return null if there was
4065     // an exception.  If we cannot load the Shutdown class, just don't
4066     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
4067     // and finalizers (if runFinalizersOnExit is set) won't be run.
4068     // Note that if a shutdown hook was registered or runFinalizersOnExit
4069     // was called, the Shutdown class would have already been loaded
4070     // (Runtime.addShutdownHook and runFinalizersOnExit will load it).

4071     JavaValue result(T_VOID);
4072     JavaCalls::call_static(&result,
4073                            shutdown_klass,
4074                            vmSymbols::shutdown_method_name(),
4075                            vmSymbols::void_method_signature(),
4076                            THREAD);
4077   }
4078   CLEAR_PENDING_EXCEPTION;
4079 }
4080 
4081 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
4082 // the program falls off the end of main(). Another VM exit path is through
4083 // vm_exit() when the program calls System.exit() to return a value or when
4084 // there is a serious error in VM. The two shutdown paths are not exactly
4085 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
4086 // and VM_Exit op at VM level.
4087 //
4088 // Shutdown sequence:
4089 //   + Shutdown native memory tracking if it is on
4090 //   + Wait until we are the last non-daemon thread to execute


< prev index next >