hotspot/src/share/vm/runtime/thread.cpp
changeset 46369 3bf4544bec14
parent 44326 6c59cca7ff07
parent 46329 53ccc37bda19
child 46388 d7a164ad6b7f
equal deleted inserted replaced
44470:d10b1eca0b45 46369:3bf4544bec14
   948 
   948 
   949 
   949 
   950 // Creates the initial ThreadGroup
   950 // Creates the initial ThreadGroup
   951 static Handle create_initial_thread_group(TRAPS) {
   951 static Handle create_initial_thread_group(TRAPS) {
   952   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH);
   952   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH);
   953   instanceKlassHandle klass (THREAD, k);
   953   InstanceKlass* ik = InstanceKlass::cast(k);
   954 
   954 
   955   Handle system_instance = klass->allocate_instance_handle(CHECK_NH);
   955   Handle system_instance = ik->allocate_instance_handle(CHECK_NH);
   956   {
   956   {
   957     JavaValue result(T_VOID);
   957     JavaValue result(T_VOID);
   958     JavaCalls::call_special(&result,
   958     JavaCalls::call_special(&result,
   959                             system_instance,
   959                             system_instance,
   960                             klass,
   960                             ik,
   961                             vmSymbols::object_initializer_name(),
   961                             vmSymbols::object_initializer_name(),
   962                             vmSymbols::void_method_signature(),
   962                             vmSymbols::void_method_signature(),
   963                             CHECK_NH);
   963                             CHECK_NH);
   964   }
   964   }
   965   Universe::set_system_thread_group(system_instance());
   965   Universe::set_system_thread_group(system_instance());
   966 
   966 
   967   Handle main_instance = klass->allocate_instance_handle(CHECK_NH);
   967   Handle main_instance = ik->allocate_instance_handle(CHECK_NH);
   968   {
   968   {
   969     JavaValue result(T_VOID);
   969     JavaValue result(T_VOID);
   970     Handle string = java_lang_String::create_from_str("main", CHECK_NH);
   970     Handle string = java_lang_String::create_from_str("main", CHECK_NH);
   971     JavaCalls::call_special(&result,
   971     JavaCalls::call_special(&result,
   972                             main_instance,
   972                             main_instance,
   973                             klass,
   973                             ik,
   974                             vmSymbols::object_initializer_name(),
   974                             vmSymbols::object_initializer_name(),
   975                             vmSymbols::threadgroup_string_void_signature(),
   975                             vmSymbols::threadgroup_string_void_signature(),
   976                             system_instance,
   976                             system_instance,
   977                             string,
   977                             string,
   978                             CHECK_NH);
   978                             CHECK_NH);
   982 
   982 
   983 // Creates the initial Thread
   983 // Creates the initial Thread
   984 static oop create_initial_thread(Handle thread_group, JavaThread* thread,
   984 static oop create_initial_thread(Handle thread_group, JavaThread* thread,
   985                                  TRAPS) {
   985                                  TRAPS) {
   986   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_NULL);
   986   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_NULL);
   987   instanceKlassHandle klass (THREAD, k);
   987   InstanceKlass* ik = InstanceKlass::cast(k);
   988   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);
   988   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK_NULL);
   989 
   989 
   990   java_lang_Thread::set_thread(thread_oop(), thread);
   990   java_lang_Thread::set_thread(thread_oop(), thread);
   991   java_lang_Thread::set_priority(thread_oop(), NormPriority);
   991   java_lang_Thread::set_priority(thread_oop(), NormPriority);
   992   thread->set_threadObj(thread_oop());
   992   thread->set_threadObj(thread_oop());
   993 
   993 
   994   Handle string = java_lang_String::create_from_str("main", CHECK_NULL);
   994   Handle string = java_lang_String::create_from_str("main", CHECK_NULL);
   995 
   995 
   996   JavaValue result(T_VOID);
   996   JavaValue result(T_VOID);
   997   JavaCalls::call_special(&result, thread_oop,
   997   JavaCalls::call_special(&result, thread_oop,
   998                           klass,
   998                           ik,
   999                           vmSymbols::object_initializer_name(),
   999                           vmSymbols::object_initializer_name(),
  1000                           vmSymbols::threadgroup_string_void_signature(),
  1000                           vmSymbols::threadgroup_string_void_signature(),
  1001                           thread_group,
  1001                           thread_group,
  1002                           string,
  1002                           string,
  1003                           CHECK_NULL);
  1003                           CHECK_NULL);
  1052 }
  1052 }
  1053 
  1053 
  1054 // General purpose hook into Java code, run once when the VM is initialized.
  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.
  1055 // The Java library method itself may be changed independently from the VM.
  1056 static void call_postVMInitHook(TRAPS) {
  1056 static void call_postVMInitHook(TRAPS) {
  1057   Klass* k = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_vm_PostVMInitHook(), THREAD);
  1057   Klass* klass = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_vm_PostVMInitHook(), THREAD);
  1058   instanceKlassHandle klass (THREAD, k);
  1058   if (klass != NULL) {
  1059   if (klass.not_null()) {
       
  1060     JavaValue result(T_VOID);
  1059     JavaValue result(T_VOID);
  1061     JavaCalls::call_static(&result, klass, vmSymbols::run_method_name(),
  1060     JavaCalls::call_static(&result, klass, vmSymbols::run_method_name(),
  1062                            vmSymbols::void_method_signature(),
  1061                            vmSymbols::void_method_signature(),
  1063                            CHECK);
  1062                            CHECK);
  1064   }
  1063   }
  1068   // the vm info string
  1067   // the vm info string
  1069   ResourceMark rm(THREAD);
  1068   ResourceMark rm(THREAD);
  1070   const char *vm_info = VM_Version::vm_info_string();
  1069   const char *vm_info = VM_Version::vm_info_string();
  1071 
  1070 
  1072   // java.lang.System class
  1071   // java.lang.System class
  1073   Klass* k =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  1072   Klass* klass =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  1074   instanceKlassHandle klass (THREAD, k);
       
  1075 
  1073 
  1076   // setProperty arguments
  1074   // setProperty arguments
  1077   Handle key_str    = java_lang_String::create_from_str("java.vm.info", CHECK);
  1075   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);
  1076   Handle value_str  = java_lang_String::create_from_str(vm_info, CHECK);
  1079 
  1077 
  1095                                     bool daemon, TRAPS) {
  1093                                     bool daemon, TRAPS) {
  1096   assert(thread_group.not_null(), "thread group should be specified");
  1094   assert(thread_group.not_null(), "thread group should be specified");
  1097   assert(threadObj() == NULL, "should only create Java thread object once");
  1095   assert(threadObj() == NULL, "should only create Java thread object once");
  1098 
  1096 
  1099   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
  1097   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
  1100   instanceKlassHandle klass (THREAD, k);
  1098   InstanceKlass* ik = InstanceKlass::cast(k);
  1101   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
  1099   instanceHandle thread_oop = ik->allocate_instance_handle(CHECK);
  1102 
  1100 
  1103   java_lang_Thread::set_thread(thread_oop(), this);
  1101   java_lang_Thread::set_thread(thread_oop(), this);
  1104   java_lang_Thread::set_priority(thread_oop(), NormPriority);
  1102   java_lang_Thread::set_priority(thread_oop(), NormPriority);
  1105   set_threadObj(thread_oop());
  1103   set_threadObj(thread_oop());
  1106 
  1104 
  1108   if (thread_name != NULL) {
  1106   if (thread_name != NULL) {
  1109     Handle name = java_lang_String::create_from_str(thread_name, CHECK);
  1107     Handle name = java_lang_String::create_from_str(thread_name, CHECK);
  1110     // Thread gets assigned specified name and null target
  1108     // Thread gets assigned specified name and null target
  1111     JavaCalls::call_special(&result,
  1109     JavaCalls::call_special(&result,
  1112                             thread_oop,
  1110                             thread_oop,
  1113                             klass,
  1111                             ik,
  1114                             vmSymbols::object_initializer_name(),
  1112                             vmSymbols::object_initializer_name(),
  1115                             vmSymbols::threadgroup_string_void_signature(),
  1113                             vmSymbols::threadgroup_string_void_signature(),
  1116                             thread_group, // Argument 1
  1114                             thread_group, // Argument 1
  1117                             name,         // Argument 2
  1115                             name,         // Argument 2
  1118                             THREAD);
  1116                             THREAD);
  1119   } else {
  1117   } else {
  1120     // Thread gets assigned name "Thread-nnn" and null target
  1118     // Thread gets assigned name "Thread-nnn" and null target
  1121     // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
  1119     // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
  1122     JavaCalls::call_special(&result,
  1120     JavaCalls::call_special(&result,
  1123                             thread_oop,
  1121                             thread_oop,
  1124                             klass,
  1122                             ik,
  1125                             vmSymbols::object_initializer_name(),
  1123                             vmSymbols::object_initializer_name(),
  1126                             vmSymbols::threadgroup_runnable_void_signature(),
  1124                             vmSymbols::threadgroup_runnable_void_signature(),
  1127                             thread_group, // Argument 1
  1125                             thread_group, // Argument 1
  1128                             Handle(),     // Argument 2
  1126                             Handle(),     // Argument 2
  1129                             THREAD);
  1127                             THREAD);
  1136 
  1134 
  1137   if (HAS_PENDING_EXCEPTION) {
  1135   if (HAS_PENDING_EXCEPTION) {
  1138     return;
  1136     return;
  1139   }
  1137   }
  1140 
  1138 
  1141   KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass());
  1139   Klass* group =  SystemDictionary::ThreadGroup_klass();
  1142   Handle threadObj(THREAD, this->threadObj());
  1140   Handle threadObj(THREAD, this->threadObj());
  1143 
  1141 
  1144   JavaCalls::call_special(&result,
  1142   JavaCalls::call_special(&result,
  1145                           thread_group,
  1143                           thread_group,
  1146                           group,
  1144                           group,
  1287     int time_waited = sleep();
  1285     int time_waited = sleep();
  1288 
  1286 
  1289     if (is_error_reported()) {
  1287     if (is_error_reported()) {
  1290       // A fatal error has happened, the error handler(VMError::report_and_die)
  1288       // A fatal error has happened, the error handler(VMError::report_and_die)
  1291       // should abort JVM after creating an error log file. However in some
  1289       // should abort JVM after creating an error log file. However in some
  1292       // rare cases, the error handler itself might deadlock. Here we try to
  1290       // rare cases, the error handler itself might deadlock. Here periodically
  1293       // kill JVM if the fatal error handler fails to abort in 2 minutes.
  1291       // check for error reporting timeouts, and if it happens, just proceed to
  1294       //
  1292       // abort the VM.
       
  1293 
  1295       // This code is in WatcherThread because WatcherThread wakes up
  1294       // This code is in WatcherThread because WatcherThread wakes up
  1296       // periodically so the fatal error handler doesn't need to do anything;
  1295       // periodically so the fatal error handler doesn't need to do anything;
  1297       // also because the WatcherThread is less likely to crash than other
  1296       // also because the WatcherThread is less likely to crash than other
  1298       // threads.
  1297       // threads.
  1299 
  1298 
  1300       for (;;) {
  1299       for (;;) {
  1301         if (!ShowMessageBoxOnError
  1300         // Note: we use naked sleep in this loop because we want to avoid using
  1302             && (OnError == NULL || OnError[0] == '\0')
  1301         // any kind of VM infrastructure which may be broken at this point.
  1303             && Arguments::abort_hook() == NULL) {
  1302         if (VMError::check_timeout()) {
  1304           os::sleep(this, (jlong)ErrorLogTimeout * 1000, false); // in seconds
  1303           // We hit error reporting timeout. Error reporting was interrupted and
       
  1304           // will be wrapping things up now (closing files etc). Give it some more
       
  1305           // time, then quit the VM.
       
  1306           os::naked_short_sleep(200);
       
  1307           // Print a message to stderr.
  1305           fdStream err(defaultStream::output_fd());
  1308           fdStream err(defaultStream::output_fd());
  1306           err.print_raw_cr("# [ timer expired, abort... ]");
  1309           err.print_raw_cr("# [ timer expired, abort... ]");
  1307           // skip atexit/vm_exit/vm_abort hooks
  1310           // skip atexit/vm_exit/vm_abort hooks
  1308           os::die();
  1311           os::die();
  1309         }
  1312         }
  1310 
  1313 
  1311         // Wake up 5 seconds later, the fatal handler may reset OnError or
  1314         // Wait a second, then recheck for timeout.
  1312         // ShowMessageBoxOnError when it is ready to abort.
  1315         os::naked_short_sleep(999);
  1313         os::sleep(this, 5 * 1000, false);
       
  1314       }
  1316       }
  1315     }
  1317     }
  1316 
  1318 
  1317     if (_should_terminate) {
  1319     if (_should_terminate) {
  1318       // check for termination before posting the next tick
  1320       // check for termination before posting the next tick
  1773   }
  1775   }
  1774   if (!destroy_vm) {
  1776   if (!destroy_vm) {
  1775     if (uncaught_exception.not_null()) {
  1777     if (uncaught_exception.not_null()) {
  1776       EXCEPTION_MARK;
  1778       EXCEPTION_MARK;
  1777       // Call method Thread.dispatchUncaughtException().
  1779       // Call method Thread.dispatchUncaughtException().
  1778       KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
  1780       Klass* thread_klass = SystemDictionary::Thread_klass();
  1779       JavaValue result(T_VOID);
  1781       JavaValue result(T_VOID);
  1780       JavaCalls::call_virtual(&result,
  1782       JavaCalls::call_virtual(&result,
  1781                               threadObj, thread_klass,
  1783                               threadObj, thread_klass,
  1782                               vmSymbols::dispatchUncaughtException_name(),
  1784                               vmSymbols::dispatchUncaughtException_name(),
  1783                               vmSymbols::throwable_void_signature(),
  1785                               vmSymbols::throwable_void_signature(),
  1811     if (!is_Compiler_thread()) {
  1813     if (!is_Compiler_thread()) {
  1812       int count = 3;
  1814       int count = 3;
  1813       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
  1815       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
  1814         EXCEPTION_MARK;
  1816         EXCEPTION_MARK;
  1815         JavaValue result(T_VOID);
  1817         JavaValue result(T_VOID);
  1816         KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
  1818         Klass* thread_klass = SystemDictionary::Thread_klass();
  1817         JavaCalls::call_virtual(&result,
  1819         JavaCalls::call_virtual(&result,
  1818                                 threadObj, thread_klass,
  1820                                 threadObj, thread_klass,
  1819                                 vmSymbols::exit_method_name(),
  1821                                 vmSymbols::exit_method_name(),
  1820                                 vmSymbols::void_method_signature(),
  1822                                 vmSymbols::void_method_signature(),
  1821                                 THREAD);
  1823                                 THREAD);
  3382 //
  3384 //
  3383 //     System.initPhase1 initializes the system properties, the static
  3385 //     System.initPhase1 initializes the system properties, the static
  3384 //     fields in, out, and err. Set up java signal handlers, OS-specific
  3386 //     fields in, out, and err. Set up java signal handlers, OS-specific
  3385 //     system settings, and thread group of the main thread.
  3387 //     system settings, and thread group of the main thread.
  3386 static void call_initPhase1(TRAPS) {
  3388 static void call_initPhase1(TRAPS) {
  3387   Klass* k =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  3389   Klass* klass =  SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  3388   instanceKlassHandle klass (THREAD, k);
       
  3389 
       
  3390   JavaValue result(T_VOID);
  3390   JavaValue result(T_VOID);
  3391   JavaCalls::call_static(&result, klass, vmSymbols::initPhase1_name(),
  3391   JavaCalls::call_static(&result, klass, vmSymbols::initPhase1_name(),
  3392                                          vmSymbols::void_method_signature(), CHECK);
  3392                                          vmSymbols::void_method_signature(), CHECK);
  3393 }
  3393 }
  3394 
  3394 
  3404 //
  3404 //
  3405 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
  3405 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
  3406 static void call_initPhase2(TRAPS) {
  3406 static void call_initPhase2(TRAPS) {
  3407   TraceTime timer("Phase2 initialization", TRACETIME_LOG(Info, modules, startuptime));
  3407   TraceTime timer("Phase2 initialization", TRACETIME_LOG(Info, modules, startuptime));
  3408 
  3408 
  3409   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  3409   Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  3410   instanceKlassHandle klass (THREAD, k);
       
  3411 
  3410 
  3412   JavaValue result(T_INT);
  3411   JavaValue result(T_INT);
  3413   JavaCallArguments args;
  3412   JavaCallArguments args;
  3414   args.push_int(DisplayVMOutputToStderr);
  3413   args.push_int(DisplayVMOutputToStderr);
  3415   args.push_int(log_is_enabled(Debug, init)); // print stack trace if exception thrown
  3414   args.push_int(log_is_enabled(Debug, init)); // print stack trace if exception thrown
  3427 //     This will instantiate and set the security manager, set the system class
  3426 //     This will instantiate and set the security manager, set the system class
  3428 //     loader as well as the thread context class loader.  The security manager
  3427 //     loader as well as the thread context class loader.  The security manager
  3429 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
  3428 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
  3430 //     other modules or the application's classpath.
  3429 //     other modules or the application's classpath.
  3431 static void call_initPhase3(TRAPS) {
  3430 static void call_initPhase3(TRAPS) {
  3432   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  3431   Klass* klass = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
  3433   instanceKlassHandle klass (THREAD, k);
       
  3434 
       
  3435   JavaValue result(T_VOID);
  3432   JavaValue result(T_VOID);
  3436   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
  3433   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
  3437                                          vmSymbols::void_method_signature(), CHECK);
  3434                                          vmSymbols::void_method_signature(), CHECK);
  3438 }
  3435 }
  3439 
  3436 
  3722 #if INCLUDE_MANAGEMENT
  3719 #if INCLUDE_MANAGEMENT
  3723   Management::record_vm_init_completed();
  3720   Management::record_vm_init_completed();
  3724 #endif // INCLUDE_MANAGEMENT
  3721 #endif // INCLUDE_MANAGEMENT
  3725 
  3722 
  3726   // Signal Dispatcher needs to be started before VMInit event is posted
  3723   // Signal Dispatcher needs to be started before VMInit event is posted
  3727   os::signal_init();
  3724   os::signal_init(CHECK_JNI_ERR);
  3728 
  3725 
  3729   // Start Attach Listener if +StartAttachListener or it can't be started lazily
  3726   // Start Attach Listener if +StartAttachListener or it can't be started lazily
  3730   if (!DisableAttachMechanism) {
  3727   if (!DisableAttachMechanism) {
  3731     AttachListener::vm_start();
  3728     AttachListener::vm_start();
  3732     if (StartAttachListener || AttachListener::init_at_startup()) {
  3729     if (StartAttachListener || AttachListener::init_at_startup()) {
  4065   if (this->has_pending_exception()) {
  4062   if (this->has_pending_exception()) {
  4066     this->clear_pending_exception();
  4063     this->clear_pending_exception();
  4067   }
  4064   }
  4068 
  4065 
  4069   EXCEPTION_MARK;
  4066   EXCEPTION_MARK;
  4070   Klass* k =
  4067   Klass* shutdown_klass =
  4071     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
  4068     SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
  4072                                       THREAD);
  4069                                       THREAD);
  4073   if (k != NULL) {
  4070   if (shutdown_klass != NULL) {
  4074     // SystemDictionary::resolve_or_null will return null if there was
  4071     // SystemDictionary::resolve_or_null will return null if there was
  4075     // an exception.  If we cannot load the Shutdown class, just don't
  4072     // an exception.  If we cannot load the Shutdown class, just don't
  4076     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
  4073     // call Shutdown.shutdown() at all.  This will mean the shutdown hooks
  4077     // and finalizers (if runFinalizersOnExit is set) won't be run.
  4074     // and finalizers (if runFinalizersOnExit is set) won't be run.
  4078     // Note that if a shutdown hook was registered or runFinalizersOnExit
  4075     // Note that if a shutdown hook was registered or runFinalizersOnExit
  4079     // was called, the Shutdown class would have already been loaded
  4076     // was called, the Shutdown class would have already been loaded
  4080     // (Runtime.addShutdownHook and runFinalizersOnExit will load it).
  4077     // (Runtime.addShutdownHook and runFinalizersOnExit will load it).
  4081     instanceKlassHandle shutdown_klass (THREAD, k);
       
  4082     JavaValue result(T_VOID);
  4078     JavaValue result(T_VOID);
  4083     JavaCalls::call_static(&result,
  4079     JavaCalls::call_static(&result,
  4084                            shutdown_klass,
  4080                            shutdown_klass,
  4085                            vmSymbols::shutdown_method_name(),
  4081                            vmSymbols::shutdown_method_name(),
  4086                            vmSymbols::void_method_signature(),
  4082                            vmSymbols::void_method_signature(),