src/hotspot/share/classfile/systemDictionaryShared.cpp
changeset 49931 840e26123940
parent 49824 e242740a92b8
child 50039 9fec54fe663d
equal deleted inserted replaced
49930:3aaaa5370999 49931:840e26123940
   358 }
   358 }
   359 
   359 
   360 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) {
   360 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) {
   361   oop class_loader = loader_data->class_loader();
   361   oop class_loader = loader_data->class_loader();
   362   return (class_loader == NULL ||
   362   return (class_loader == NULL ||
   363           (UseAppCDS && (SystemDictionary::is_system_class_loader(class_loader) ||
   363           SystemDictionary::is_system_class_loader(class_loader) ||
   364                          SystemDictionary::is_platform_class_loader(class_loader)))
   364           SystemDictionary::is_platform_class_loader(class_loader));
   365           );
       
   366 }
   365 }
   367 
   366 
   368 // Currently AppCDS only archives classes from the run-time image, the
   367 // Currently AppCDS only archives classes from the run-time image, the
   369 // -Xbootclasspath/a path, the class path, and the module path.
   368 // -Xbootclasspath/a path, the class path, and the module path.
   370 //
   369 //
   506 //
   505 //
   507 // An alternative is to modify the Java code of AppClassLoader.loadClass().
   506 // An alternative is to modify the Java code of AppClassLoader.loadClass().
   508 //
   507 //
   509 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class(
   508 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class(
   510                  Symbol* name, Handle class_loader, TRAPS) {
   509                  Symbol* name, Handle class_loader, TRAPS) {
   511   if (DumpSharedSpaces) {
       
   512     return NULL;
       
   513   }
       
   514 
       
   515   InstanceKlass* k = NULL;
   510   InstanceKlass* k = NULL;
   516   if (shared_dictionary() != NULL &&
   511   if (UseSharedSpaces) {
   517       UseAppCDS && (SystemDictionary::is_system_class_loader(class_loader()) ||
   512     FileMapHeaderExt* header = (FileMapHeaderExt*)FileMapInfo::current_info()->header();
   518                     SystemDictionary::is_platform_class_loader(class_loader()))) {
   513     if (!header->has_platform_or_app_classes()) {
   519 
   514       return NULL;
   520     // Fix for 4474172; see evaluation for more details
   515     }
   521     class_loader = Handle(
   516 
   522       THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
   517     if (shared_dictionary() != NULL &&
   523     ClassLoaderData *loader_data = register_loader(class_loader);
   518         (SystemDictionary::is_system_class_loader(class_loader()) ||
   524     Dictionary* dictionary = loader_data->dictionary();
   519          SystemDictionary::is_platform_class_loader(class_loader()))) {
   525 
   520       // Fix for 4474172; see evaluation for more details
   526     unsigned int d_hash = dictionary->compute_hash(name);
   521       class_loader = Handle(
   527 
   522         THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
   528     bool DoObjectLock = true;
   523       ClassLoaderData *loader_data = register_loader(class_loader);
   529     if (is_parallelCapable(class_loader)) {
   524       Dictionary* dictionary = loader_data->dictionary();
   530       DoObjectLock = false;
   525 
   531     }
   526       unsigned int d_hash = dictionary->compute_hash(name);
   532 
   527 
   533     // Make sure we are synchronized on the class loader before we proceed
   528       bool DoObjectLock = true;
   534     //
   529       if (is_parallelCapable(class_loader)) {
   535     // Note: currently, find_or_load_shared_class is called only from
   530         DoObjectLock = false;
   536     // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader,
   531       }
   537     // which are parallel-capable loaders, so this lock is NOT taken.
   532 
   538     Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
   533       // Make sure we are synchronized on the class loader before we proceed
   539     check_loader_lock_contention(lockObject, THREAD);
   534       //
   540     ObjectLocker ol(lockObject, THREAD, DoObjectLock);
   535       // Note: currently, find_or_load_shared_class is called only from
   541 
   536       // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader,
   542     {
   537       // which are parallel-capable loaders, so this lock is NOT taken.
   543       MutexLocker mu(SystemDictionary_lock, THREAD);
   538       Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
   544       Klass* check = find_class(d_hash, name, dictionary);
   539       check_loader_lock_contention(lockObject, THREAD);
   545       if (check != NULL) {
   540       ObjectLocker ol(lockObject, THREAD, DoObjectLock);
   546         return InstanceKlass::cast(check);
   541 
   547       }
   542       {
   548     }
   543         MutexLocker mu(SystemDictionary_lock, THREAD);
   549 
   544         Klass* check = find_class(d_hash, name, dictionary);
   550     k = load_shared_class_for_builtin_loader(name, class_loader, THREAD);
   545         if (check != NULL) {
   551     if (k != NULL) {
   546           return InstanceKlass::cast(check);
   552       define_instance_class(k, CHECK_NULL);
   547         }
   553     }
   548       }
   554   }
   549 
   555 
   550       k = load_shared_class_for_builtin_loader(name, class_loader, THREAD);
       
   551       if (k != NULL) {
       
   552         define_instance_class(k, CHECK_NULL);
       
   553       }
       
   554     }
       
   555   }
   556   return k;
   556   return k;
   557 }
   557 }
   558 
   558 
   559 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
   559 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
   560                  Symbol* class_name, Handle class_loader, TRAPS) {
   560                  Symbol* class_name, Handle class_loader, TRAPS) {
   561   assert(UseAppCDS && shared_dictionary() != NULL, "already checked");
   561   assert(UseSharedSpaces, "must be");
       
   562   assert(shared_dictionary() != NULL, "already checked");
   562   Klass* k = shared_dictionary()->find_class_for_builtin_loader(class_name);
   563   Klass* k = shared_dictionary()->find_class_for_builtin_loader(class_name);
   563 
   564 
   564   if (k != NULL) {
   565   if (k != NULL) {
   565     InstanceKlass* ik = InstanceKlass::cast(k);
   566     InstanceKlass* ik = InstanceKlass::cast(k);
   566     if ((ik->is_shared_app_class() &&
   567     if ((ik->is_shared_app_class() &&
   607   allocate_shared_protection_domain_array(size, CHECK);
   608   allocate_shared_protection_domain_array(size, CHECK);
   608   allocate_shared_jar_url_array(size, CHECK);
   609   allocate_shared_jar_url_array(size, CHECK);
   609   allocate_shared_jar_manifest_array(size, CHECK);
   610   allocate_shared_jar_manifest_array(size, CHECK);
   610 }
   611 }
   611 
   612 
   612 
   613 // This function is called for loading only UNREGISTERED classes
   613 InstanceKlass* SystemDictionaryShared::lookup_from_stream(const Symbol* class_name,
   614 InstanceKlass* SystemDictionaryShared::lookup_from_stream(const Symbol* class_name,
   614                                                           Handle class_loader,
   615                                                           Handle class_loader,
   615                                                           Handle protection_domain,
   616                                                           Handle protection_domain,
   616                                                           const ClassFileStream* cfs,
   617                                                           const ClassFileStream* cfs,
   617                                                           TRAPS) {
   618                                                           TRAPS) {
   618   if (!UseAppCDS || shared_dictionary() == NULL) {
   619   if (shared_dictionary() == NULL) {
   619     return NULL;
   620     return NULL;
   620   }
   621   }
   621   if (class_name == NULL) {  // don't do this for anonymous classes
   622   if (class_name == NULL) {  // don't do this for anonymous classes
   622     return NULL;
   623     return NULL;
   623   }
   624   }
   624   if (class_loader.is_null() ||
   625   if (class_loader.is_null() ||
   625       SystemDictionary::is_system_class_loader(class_loader()) ||
   626       SystemDictionary::is_system_class_loader(class_loader()) ||
   626       SystemDictionary::is_platform_class_loader(class_loader())) {
   627       SystemDictionary::is_platform_class_loader(class_loader())) {
   627     // This function is called for loading only UNREGISTERED classes.
       
   628     // Do nothing for the BUILTIN loaders.
   628     // Do nothing for the BUILTIN loaders.
   629     return NULL;
   629     return NULL;
   630   }
   630   }
   631 
   631 
   632   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
   632   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
   684   }
   684   }
   685 
   685 
   686   return shared_klass;
   686   return shared_klass;
   687 }
   687 }
   688 
   688 
   689 bool SystemDictionaryShared::add_non_builtin_klass(Symbol* name, ClassLoaderData* loader_data,
   689 bool SystemDictionaryShared::add_non_builtin_klass(Symbol* name,
       
   690                                                    ClassLoaderData* loader_data,
   690                                                    InstanceKlass* k,
   691                                                    InstanceKlass* k,
   691                                                    TRAPS) {
   692                                                    TRAPS) {
   692   assert(DumpSharedSpaces, "only when dumping");
   693   assert(DumpSharedSpaces, "only when dumping");
   693   assert(UseAppCDS && boot_loader_dictionary() != NULL, "must be");
   694   assert(boot_loader_dictionary() != NULL, "must be");
   694 
   695 
   695   if (boot_loader_dictionary()->add_non_builtin_klass(name, loader_data, k)) {
   696   if (boot_loader_dictionary()->add_non_builtin_klass(name, loader_data, k)) {
   696     MutexLocker mu_r(Compile_lock, THREAD); // not really necessary, but add_to_hierarchy asserts this.
   697     MutexLocker mu_r(Compile_lock, THREAD); // not really necessary, but add_to_hierarchy asserts this.
   697     add_to_hierarchy(k, CHECK_0);
   698     add_to_hierarchy(k, CHECK_0);
   698     return true;
   699     return true;