hotspot/src/share/vm/runtime/thread.cpp
changeset 22539 a27fa687eca8
parent 22533 76088853a2eb
child 22556 a17351e8c2c2
equal deleted inserted replaced
22538:5261297676ef 22539:a27fa687eca8
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
  3299     tc->do_thread(wt);
  3299     tc->do_thread(wt);
  3300 
  3300 
  3301   // If CompilerThreads ever become non-JavaThreads, add them here
  3301   // If CompilerThreads ever become non-JavaThreads, add them here
  3302 }
  3302 }
  3303 
  3303 
       
  3304 
       
  3305 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
       
  3306   TraceTime timer("Initialize java.lang classes", TraceStartupTime);
       
  3307 
       
  3308   if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
       
  3309     create_vm_init_libraries();
       
  3310   }
       
  3311 
       
  3312   initialize_class(vmSymbols::java_lang_String(), CHECK);
       
  3313 
       
  3314   // Initialize java_lang.System (needed before creating the thread)
       
  3315   initialize_class(vmSymbols::java_lang_System(), CHECK);
       
  3316   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
       
  3317   Handle thread_group = create_initial_thread_group(CHECK);
       
  3318   Universe::set_main_thread_group(thread_group());
       
  3319   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
       
  3320   oop thread_object = create_initial_thread(thread_group, main_thread, CHECK);
       
  3321   main_thread->set_threadObj(thread_object);
       
  3322   // Set thread status to running since main thread has
       
  3323   // been started and running.
       
  3324   java_lang_Thread::set_thread_status(thread_object,
       
  3325                                       java_lang_Thread::RUNNABLE);
       
  3326 
       
  3327   // The VM creates & returns objects of this class. Make sure it's initialized.
       
  3328   initialize_class(vmSymbols::java_lang_Class(), CHECK);
       
  3329 
       
  3330   // The VM preresolves methods to these classes. Make sure that they get initialized
       
  3331   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
       
  3332   initialize_class(vmSymbols::java_lang_ref_Finalizer(),  CHECK);
       
  3333   call_initializeSystemClass(CHECK);
       
  3334 
       
  3335   // get the Java runtime name after java.lang.System is initialized
       
  3336   JDK_Version::set_runtime_name(get_java_runtime_name(THREAD));
       
  3337   JDK_Version::set_runtime_version(get_java_runtime_version(THREAD));
       
  3338 
       
  3339   // an instance of OutOfMemory exception has been allocated earlier
       
  3340   initialize_class(vmSymbols::java_lang_OutOfMemoryError(), CHECK);
       
  3341   initialize_class(vmSymbols::java_lang_NullPointerException(), CHECK);
       
  3342   initialize_class(vmSymbols::java_lang_ClassCastException(), CHECK);
       
  3343   initialize_class(vmSymbols::java_lang_ArrayStoreException(), CHECK);
       
  3344   initialize_class(vmSymbols::java_lang_ArithmeticException(), CHECK);
       
  3345   initialize_class(vmSymbols::java_lang_StackOverflowError(), CHECK);
       
  3346   initialize_class(vmSymbols::java_lang_IllegalMonitorStateException(), CHECK);
       
  3347   initialize_class(vmSymbols::java_lang_IllegalArgumentException(), CHECK);
       
  3348 }
       
  3349 
       
  3350 void Threads::initialize_jsr292_core_classes(TRAPS) {
       
  3351   initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK);
       
  3352   initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK);
       
  3353   initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK);
       
  3354 }
       
  3355 
  3304 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
  3356 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
  3305 
  3357 
  3306   extern void JDK_Version_init();
  3358   extern void JDK_Version_init();
  3307 
  3359 
  3308   // Check version
  3360   // Check version
  3468     // Make sure we're starting with a clean slate.
  3520     // Make sure we're starting with a clean slate.
  3469     VM_Verify verify_op;
  3521     VM_Verify verify_op;
  3470     VMThread::execute(&verify_op);
  3522     VMThread::execute(&verify_op);
  3471   }
  3523   }
  3472 
  3524 
  3473   EXCEPTION_MARK;
  3525   Thread* THREAD = Thread::current();
  3474 
  3526 
  3475   // At this point, the Universe is initialized, but we have not executed
  3527   // At this point, the Universe is initialized, but we have not executed
  3476   // any byte code.  Now is a good time (the only time) to dump out the
  3528   // any byte code.  Now is a good time (the only time) to dump out the
  3477   // internal state of the JVM for sharing.
  3529   // internal state of the JVM for sharing.
  3478   if (DumpSharedSpaces) {
  3530   if (DumpSharedSpaces) {
  3479     MetaspaceShared::preload_and_dump(CHECK_0);
  3531     MetaspaceShared::preload_and_dump(CHECK_JNI_ERR);
  3480     ShouldNotReachHere();
  3532     ShouldNotReachHere();
  3481   }
  3533   }
  3482 
  3534 
  3483   // Always call even when there are not JVMTI environments yet, since environments
  3535   // Always call even when there are not JVMTI environments yet, since environments
  3484   // may be attached late and JVMTI must track phases of VM execution
  3536   // may be attached late and JVMTI must track phases of VM execution
  3485   JvmtiExport::enter_start_phase();
  3537   JvmtiExport::enter_start_phase();
  3486 
  3538 
  3487   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
  3539   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
  3488   JvmtiExport::post_vm_start();
  3540   JvmtiExport::post_vm_start();
  3489 
  3541 
  3490   {
  3542   initialize_java_lang_classes(main_thread, CHECK_JNI_ERR);
  3491     TraceTime timer("Initialize java.lang classes", TraceStartupTime);
  3543 
  3492 
  3544   // We need this for ClassDataSharing - the initial vm.info property is set
  3493     if (EagerXrunInit && Arguments::init_libraries_at_startup()) {
  3545   // with the default value of CDS "sharing" which may be reset through
  3494       create_vm_init_libraries();
  3546   // command line options.
  3495     }
  3547   reset_vm_info_property(CHECK_JNI_ERR);
  3496 
       
  3497     initialize_class(vmSymbols::java_lang_String(), CHECK_0);
       
  3498 
       
  3499     // Initialize java_lang.System (needed before creating the thread)
       
  3500     initialize_class(vmSymbols::java_lang_System(), CHECK_0);
       
  3501     initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK_0);
       
  3502     Handle thread_group = create_initial_thread_group(CHECK_0);
       
  3503     Universe::set_main_thread_group(thread_group());
       
  3504     initialize_class(vmSymbols::java_lang_Thread(), CHECK_0);
       
  3505     oop thread_object = create_initial_thread(thread_group, main_thread, CHECK_0);
       
  3506     main_thread->set_threadObj(thread_object);
       
  3507     // Set thread status to running since main thread has
       
  3508     // been started and running.
       
  3509     java_lang_Thread::set_thread_status(thread_object,
       
  3510                                         java_lang_Thread::RUNNABLE);
       
  3511 
       
  3512     // The VM creates & returns objects of this class. Make sure it's initialized.
       
  3513     initialize_class(vmSymbols::java_lang_Class(), CHECK_0);
       
  3514 
       
  3515     // The VM preresolves methods to these classes. Make sure that they get initialized
       
  3516     initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK_0);
       
  3517     initialize_class(vmSymbols::java_lang_ref_Finalizer(),  CHECK_0);
       
  3518     call_initializeSystemClass(CHECK_0);
       
  3519 
       
  3520     // get the Java runtime name after java.lang.System is initialized
       
  3521     JDK_Version::set_runtime_name(get_java_runtime_name(THREAD));
       
  3522     JDK_Version::set_runtime_version(get_java_runtime_version(THREAD));
       
  3523 
       
  3524     // an instance of OutOfMemory exception has been allocated earlier
       
  3525     initialize_class(vmSymbols::java_lang_OutOfMemoryError(), CHECK_0);
       
  3526     initialize_class(vmSymbols::java_lang_NullPointerException(), CHECK_0);
       
  3527     initialize_class(vmSymbols::java_lang_ClassCastException(), CHECK_0);
       
  3528     initialize_class(vmSymbols::java_lang_ArrayStoreException(), CHECK_0);
       
  3529     initialize_class(vmSymbols::java_lang_ArithmeticException(), CHECK_0);
       
  3530     initialize_class(vmSymbols::java_lang_StackOverflowError(), CHECK_0);
       
  3531     initialize_class(vmSymbols::java_lang_IllegalMonitorStateException(), CHECK_0);
       
  3532     initialize_class(vmSymbols::java_lang_IllegalArgumentException(), CHECK_0);
       
  3533   }
       
  3534 
       
  3535   // See        : bugid 4211085.
       
  3536   // Background : the static initializer of java.lang.Compiler tries to read
       
  3537   //              property"java.compiler" and read & write property "java.vm.info".
       
  3538   //              When a security manager is installed through the command line
       
  3539   //              option "-Djava.security.manager", the above properties are not
       
  3540   //              readable and the static initializer for java.lang.Compiler fails
       
  3541   //              resulting in a NoClassDefFoundError.  This can happen in any
       
  3542   //              user code which calls methods in java.lang.Compiler.
       
  3543   // Hack :       the hack is to pre-load and initialize this class, so that only
       
  3544   //              system domains are on the stack when the properties are read.
       
  3545   //              Currently even the AWT code has calls to methods in java.lang.Compiler.
       
  3546   //              On the classic VM, java.lang.Compiler is loaded very early to load the JIT.
       
  3547   // Future Fix : the best fix is to grant everyone permissions to read "java.compiler" and
       
  3548   //              read and write"java.vm.info" in the default policy file. See bugid 4211383
       
  3549   //              Once that is done, we should remove this hack.
       
  3550   initialize_class(vmSymbols::java_lang_Compiler(), CHECK_0);
       
  3551 
       
  3552   // More hackery - the static initializer of java.lang.Compiler adds the string "nojit" to
       
  3553   // the java.vm.info property if no jit gets loaded through java.lang.Compiler (the hotspot
       
  3554   // compiler does not get loaded through java.lang.Compiler).  "java -version" with the
       
  3555   // hotspot vm says "nojit" all the time which is confusing.  So, we reset it here.
       
  3556   // This should also be taken out as soon as 4211383 gets fixed.
       
  3557   reset_vm_info_property(CHECK_0);
       
  3558 
  3548 
  3559   quicken_jni_functions();
  3549   quicken_jni_functions();
  3560 
  3550 
  3561   // Must be run after init_ft which initializes ft_enabled
  3551   // Must be run after init_ft which initializes ft_enabled
  3562   if (TRACE_INITIALIZE() != JNI_OK) {
  3552   if (TRACE_INITIALIZE() != JNI_OK) {
  3581   // Compute system loader. Note that this has to occur after set_init_completed, since
  3571   // Compute system loader. Note that this has to occur after set_init_completed, since
  3582   // valid exceptions may be thrown in the process.
  3572   // valid exceptions may be thrown in the process.
  3583   // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
  3573   // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
  3584   // set_init_completed has just been called, causing exceptions not to be shortcut
  3574   // set_init_completed has just been called, causing exceptions not to be shortcut
  3585   // anymore. We call vm_exit_during_initialization directly instead.
  3575   // anymore. We call vm_exit_during_initialization directly instead.
  3586   SystemDictionary::compute_java_system_loader(THREAD);
  3576   SystemDictionary::compute_java_system_loader(CHECK_JNI_ERR);
  3587   if (HAS_PENDING_EXCEPTION) {
       
  3588     vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
       
  3589   }
       
  3590 
  3577 
  3591 #if INCLUDE_ALL_GCS
  3578 #if INCLUDE_ALL_GCS
  3592   // Support for ConcurrentMarkSweep. This should be cleaned up
  3579   // Support for ConcurrentMarkSweep. This should be cleaned up
  3593   // and better encapsulated. The ugly nested if test would go away
  3580   // and better encapsulated. The ugly nested if test would go away
  3594   // once things are properly refactored. XXX YSR
  3581   // once things are properly refactored. XXX YSR
  3595   if (UseConcMarkSweepGC || UseG1GC) {
  3582   if (UseConcMarkSweepGC || UseG1GC) {
  3596     if (UseConcMarkSweepGC) {
  3583     if (UseConcMarkSweepGC) {
  3597       ConcurrentMarkSweepThread::makeSurrogateLockerThread(THREAD);
  3584       ConcurrentMarkSweepThread::makeSurrogateLockerThread(CHECK_JNI_ERR);
  3598     } else {
  3585     } else {
  3599       ConcurrentMarkThread::makeSurrogateLockerThread(THREAD);
  3586       ConcurrentMarkThread::makeSurrogateLockerThread(CHECK_JNI_ERR);
  3600     }
       
  3601     if (HAS_PENDING_EXCEPTION) {
       
  3602       vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
       
  3603     }
  3587     }
  3604   }
  3588   }
  3605 #endif // INCLUDE_ALL_GCS
  3589 #endif // INCLUDE_ALL_GCS
  3606 
  3590 
  3607   // Always call even when there are not JVMTI environments yet, since environments
  3591   // Always call even when there are not JVMTI environments yet, since environments
  3640   // initialize compiler(s)
  3624   // initialize compiler(s)
  3641 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
  3625 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
  3642   CompileBroker::compilation_init();
  3626   CompileBroker::compilation_init();
  3643 #endif
  3627 #endif
  3644 
  3628 
       
  3629   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
       
  3630   // It is done after compilers are initialized, because otherwise compilations of
       
  3631   // signature polymorphic MH intrinsics can be missed
       
  3632   // (see SystemDictionary::find_method_handle_intrinsic).
  3645   if (EnableInvokeDynamic) {
  3633   if (EnableInvokeDynamic) {
  3646     // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
  3634     initialize_jsr292_core_classes(CHECK_JNI_ERR);
  3647     // It is done after compilers are initialized, because otherwise compilations of
       
  3648     // signature polymorphic MH intrinsics can be missed
       
  3649     // (see SystemDictionary::find_method_handle_intrinsic).
       
  3650     initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK_0);
       
  3651     initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK_0);
       
  3652     initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK_0);
       
  3653   }
  3635   }
  3654 
  3636 
  3655 #if INCLUDE_MANAGEMENT
  3637 #if INCLUDE_MANAGEMENT
  3656   Management::initialize(THREAD);
  3638   Management::initialize(THREAD);
  3657 #endif // INCLUDE_MANAGEMENT
       
  3658 
  3639 
  3659   if (HAS_PENDING_EXCEPTION) {
  3640   if (HAS_PENDING_EXCEPTION) {
  3660     // management agent fails to start possibly due to
  3641     // management agent fails to start possibly due to
  3661     // configuration problem and is responsible for printing
  3642     // configuration problem and is responsible for printing
  3662     // stack trace if appropriate. Simply exit VM.
  3643     // stack trace if appropriate. Simply exit VM.
  3663     vm_exit(1);
  3644     vm_exit(1);
  3664   }
  3645   }
       
  3646 #endif // INCLUDE_MANAGEMENT
  3665 
  3647 
  3666   if (Arguments::has_profile())       FlatProfiler::engage(main_thread, true);
  3648   if (Arguments::has_profile())       FlatProfiler::engage(main_thread, true);
  3667   if (MemProfiling)                   MemProfiler::engage();
  3649   if (MemProfiling)                   MemProfiler::engage();
  3668   StatSampler::engage();
  3650   StatSampler::engage();
  3669   if (CheckJNICalls)                  JniPeriodicChecker::engage();
  3651   if (CheckJNICalls)                  JniPeriodicChecker::engage();