88 } |
88 } |
89 |
89 |
90 #endif |
90 #endif |
91 |
91 |
92 // ---------------------------------------------------------------------------- |
92 // ---------------------------------------------------------------------------- |
|
93 // Parallel class loading check |
|
94 |
|
95 bool SystemDictionary::is_parallelCapable(Handle class_loader) { |
|
96 if (UnsyncloadClass || class_loader.is_null()) return true; |
|
97 if (AlwaysLockClassLoader) return false; |
|
98 return java_lang_Class::parallelCapable(class_loader()); |
|
99 } |
|
100 // ---------------------------------------------------------------------------- |
93 // Resolving of classes |
101 // Resolving of classes |
94 |
102 |
95 // Forwards to resolve_or_null |
103 // Forwards to resolve_or_null |
96 |
104 |
97 klassOop SystemDictionary::resolve_or_fail(symbolHandle class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) { |
105 klassOop SystemDictionary::resolve_or_fail(symbolHandle class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) { |
194 // super-interface callers: |
202 // super-interface callers: |
195 // parse_interfaces - for defineClass & jvmtiRedefineClasses |
203 // parse_interfaces - for defineClass & jvmtiRedefineClasses |
196 // super-class callers: |
204 // super-class callers: |
197 // ClassFileParser - for defineClass & jvmtiRedefineClasses |
205 // ClassFileParser - for defineClass & jvmtiRedefineClasses |
198 // load_shared_class - while loading a class from shared archive |
206 // load_shared_class - while loading a class from shared archive |
199 // resolve_instance_class_or_fail: |
207 // resolve_instance_class_or_null: |
|
208 // via: handle_parallel_super_load |
200 // when resolving a class that has an existing placeholder with |
209 // when resolving a class that has an existing placeholder with |
201 // a saved superclass [i.e. a defineClass is currently in progress] |
210 // a saved superclass [i.e. a defineClass is currently in progress] |
202 // if another thread is trying to resolve the class, it must do |
211 // if another thread is trying to resolve the class, it must do |
203 // super-class checks on its own thread to catch class circularity |
212 // super-class checks on its own thread to catch class circularity |
204 // This last call is critical in class circularity checking for cases |
213 // This last call is critical in class circularity checking for cases |
281 } else { |
290 } else { |
282 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader); |
291 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader); |
283 if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) { |
292 if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) { |
284 throw_circularity_error = true; |
293 throw_circularity_error = true; |
285 } |
294 } |
286 |
295 } |
287 // add placeholder entry even if error - callers will remove on error |
296 if (!throw_circularity_error) { |
288 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, class_loader, PlaceholderTable::LOAD_SUPER, class_name, THREAD); |
297 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, class_loader, PlaceholderTable::LOAD_SUPER, class_name, THREAD); |
289 if (throw_circularity_error) { |
|
290 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER); |
|
291 } |
|
292 } |
298 } |
293 } |
299 } |
294 if (throw_circularity_error) { |
300 if (throw_circularity_error) { |
295 ResourceMark rm(THREAD); |
301 ResourceMark rm(THREAD); |
296 THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string()); |
302 THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string()); |
322 superk_h = KlassHandle(THREAD, handle_resolution_exception(class_name, class_loader, protection_domain, true, superk_h, THREAD)); |
328 superk_h = KlassHandle(THREAD, handle_resolution_exception(class_name, class_loader, protection_domain, true, superk_h, THREAD)); |
323 } |
329 } |
324 |
330 |
325 return superk_h(); |
331 return superk_h(); |
326 } |
332 } |
327 |
|
328 |
333 |
329 void SystemDictionary::validate_protection_domain(instanceKlassHandle klass, |
334 void SystemDictionary::validate_protection_domain(instanceKlassHandle klass, |
330 Handle class_loader, |
335 Handle class_loader, |
331 Handle protection_domain, |
336 Handle protection_domain, |
332 TRAPS) { |
337 TRAPS) { |
419 assert_lock_strong(SystemDictionary_lock); |
424 assert_lock_strong(SystemDictionary_lock); |
420 |
425 |
421 bool calledholdinglock |
426 bool calledholdinglock |
422 = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject); |
427 = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject); |
423 assert(calledholdinglock,"must hold lock for notify"); |
428 assert(calledholdinglock,"must hold lock for notify"); |
424 assert(!UnsyncloadClass, "unexpected double_lock_wait"); |
429 assert((!(lockObject() == _system_loader_lock_obj) && !is_parallelCapable(lockObject)), "unexpected double_lock_wait"); |
425 ObjectSynchronizer::notifyall(lockObject, THREAD); |
430 ObjectSynchronizer::notifyall(lockObject, THREAD); |
426 intptr_t recursions = ObjectSynchronizer::complete_exit(lockObject, THREAD); |
431 intptr_t recursions = ObjectSynchronizer::complete_exit(lockObject, THREAD); |
427 SystemDictionary_lock->wait(); |
432 SystemDictionary_lock->wait(); |
428 SystemDictionary_lock->unlock(); |
433 SystemDictionary_lock->unlock(); |
429 ObjectSynchronizer::reenter(lockObject, recursions, THREAD); |
434 ObjectSynchronizer::reenter(lockObject, recursions, THREAD); |
437 // super class loading here. |
442 // super class loading here. |
438 // This also is critical in cases where the original thread gets stalled |
443 // This also is critical in cases where the original thread gets stalled |
439 // even in non-circularity situations. |
444 // even in non-circularity situations. |
440 // Note: only one thread can define the class, but multiple can resolve |
445 // Note: only one thread can define the class, but multiple can resolve |
441 // Note: must call resolve_super_or_fail even if null super - |
446 // Note: must call resolve_super_or_fail even if null super - |
442 // to force placeholder entry creation for this class |
447 // to force placeholder entry creation for this class for circularity detection |
443 // Caller must check for pending exception |
448 // Caller must check for pending exception |
444 // Returns non-null klassOop if other thread has completed load |
449 // Returns non-null klassOop if other thread has completed load |
445 // and we are done, |
450 // and we are done, |
446 // If return null klassOop and no pending exception, the caller must load the class |
451 // If return null klassOop and no pending exception, the caller must load the class |
447 instanceKlassHandle SystemDictionary::handle_parallel_super_load( |
452 instanceKlassHandle SystemDictionary::handle_parallel_super_load( |
475 MutexLocker mu(SystemDictionary_lock, THREAD); |
480 MutexLocker mu(SystemDictionary_lock, THREAD); |
476 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD); |
481 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD); |
477 SystemDictionary_lock->notify_all(); |
482 SystemDictionary_lock->notify_all(); |
478 } |
483 } |
479 |
484 |
480 // UnsyncloadClass does NOT wait for parallel superclass loads to complete |
485 // parallelCapable class loaders do NOT wait for parallel superclass loads to complete |
481 // Bootstrap classloader does wait for parallel superclass loads |
486 // Serial class loaders and bootstrap classloader do wait for superclass loads |
482 if (UnsyncloadClass) { |
487 if (!class_loader.is_null() && is_parallelCapable(class_loader)) { |
483 MutexLocker mu(SystemDictionary_lock, THREAD); |
488 MutexLocker mu(SystemDictionary_lock, THREAD); |
484 // Check if classloading completed while we were loading superclass or waiting |
489 // Check if classloading completed while we were loading superclass or waiting |
485 klassOop check = find_class(d_index, d_hash, name, class_loader); |
490 klassOop check = find_class(d_index, d_hash, name, class_loader); |
486 if (check != NULL) { |
491 if (check != NULL) { |
487 // Klass is already loaded, so just return it |
492 // Klass is already loaded, so just return it |
564 // class loader object lock to protect against multiple threads |
569 // class loader object lock to protect against multiple threads |
565 // defining the class in parallel by accident. |
570 // defining the class in parallel by accident. |
566 // This lock must be acquired here so the waiter will find |
571 // This lock must be acquired here so the waiter will find |
567 // any successful result in the SystemDictionary and not attempt |
572 // any successful result in the SystemDictionary and not attempt |
568 // the define |
573 // the define |
569 // Classloaders that support parallelism, e.g. bootstrap classloader, |
574 // ParallelCapable Classloaders and the bootstrap classloader, |
570 // or all classloaders with UnsyncloadClass do not acquire lock here |
575 // or all classloaders with UnsyncloadClass do not acquire lock here |
571 bool DoObjectLock = true; |
576 bool DoObjectLock = true; |
572 if (UnsyncloadClass || (class_loader.is_null())) { |
577 if (is_parallelCapable(class_loader)) { |
573 DoObjectLock = false; |
578 DoObjectLock = false; |
574 } |
579 } |
575 |
580 |
576 unsigned int p_hash = placeholders()->compute_hash(name, class_loader); |
581 unsigned int p_hash = placeholders()->compute_hash(name, class_loader); |
577 int p_index = placeholders()->hash_to_index(p_hash); |
582 int p_index = placeholders()->hash_to_index(p_hash); |
625 |
630 |
626 // add placeholder entry to record loading instance class |
631 // add placeholder entry to record loading instance class |
627 // Five cases: |
632 // Five cases: |
628 // All cases need to prevent modifying bootclasssearchpath |
633 // All cases need to prevent modifying bootclasssearchpath |
629 // in parallel with a classload of same classname |
634 // in parallel with a classload of same classname |
|
635 // Redefineclasses uses existence of the placeholder for the duration |
|
636 // of the class load to prevent concurrent redefinition of not completely |
|
637 // defined classes. |
630 // case 1. traditional classloaders that rely on the classloader object lock |
638 // case 1. traditional classloaders that rely on the classloader object lock |
631 // - no other need for LOAD_INSTANCE |
639 // - no other need for LOAD_INSTANCE |
632 // case 2. traditional classloaders that break the classloader object lock |
640 // case 2. traditional classloaders that break the classloader object lock |
633 // as a deadlock workaround. Detection of this case requires that |
641 // as a deadlock workaround. Detection of this case requires that |
634 // this check is done while holding the classloader object lock, |
642 // this check is done while holding the classloader object lock, |
640 // class/classloader pair |
648 // class/classloader pair |
641 // case4. Bootstrap classloader - don't own objectLocker |
649 // case4. Bootstrap classloader - don't own objectLocker |
642 // This classloader supports parallelism at the classloader level, |
650 // This classloader supports parallelism at the classloader level, |
643 // but only allows a single load of a class/classloader pair. |
651 // but only allows a single load of a class/classloader pair. |
644 // No performance benefit and no deadlock issues. |
652 // No performance benefit and no deadlock issues. |
645 // case 5. Future: parallel user level classloaders - without objectLocker |
653 // case 5. parallelCapable user level classloaders - without objectLocker |
|
654 // Allow parallel classloading of a class/classloader pair |
646 symbolHandle nullsymbolHandle; |
655 symbolHandle nullsymbolHandle; |
647 bool throw_circularity_error = false; |
656 bool throw_circularity_error = false; |
648 { |
657 { |
649 MutexLocker mu(SystemDictionary_lock, THREAD); |
658 MutexLocker mu(SystemDictionary_lock, THREAD); |
650 if (!UnsyncloadClass) { |
659 if (class_loader.is_null() || !is_parallelCapable(class_loader)) { |
651 PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader); |
660 PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader); |
652 if (oldprobe) { |
661 if (oldprobe) { |
653 // only need check_seen_thread once, not on each loop |
662 // only need check_seen_thread once, not on each loop |
654 // 6341374 java/lang/Instrument with -Xcomp |
663 // 6341374 java/lang/Instrument with -Xcomp |
655 if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) { |
664 if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) { |
679 } |
688 } |
680 } |
689 } |
681 } |
690 } |
682 } |
691 } |
683 // All cases: add LOAD_INSTANCE |
692 // All cases: add LOAD_INSTANCE |
684 // case 3: UnsyncloadClass: allow competing threads to try |
693 // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try |
685 // LOAD_INSTANCE in parallel |
694 // LOAD_INSTANCE in parallel |
686 // add placeholder entry even if error - callers will remove on error |
695 // add placeholder entry even if error - callers will remove on error |
687 if (!class_has_been_loaded) { |
696 if (!throw_circularity_error && !class_has_been_loaded) { |
688 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, class_loader, PlaceholderTable::LOAD_INSTANCE, nullsymbolHandle, THREAD); |
697 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, class_loader, PlaceholderTable::LOAD_INSTANCE, nullsymbolHandle, THREAD); |
689 if (throw_circularity_error) { |
|
690 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE); |
|
691 } |
|
692 // For class loaders that do not acquire the classloader object lock, |
698 // For class loaders that do not acquire the classloader object lock, |
693 // if they did not catch another thread holding LOAD_INSTANCE, |
699 // if they did not catch another thread holding LOAD_INSTANCE, |
694 // need a check analogous to the acquire ObjectLocker/find_class |
700 // need a check analogous to the acquire ObjectLocker/find_class |
695 // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL |
701 // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL |
696 // one final check if the load has already completed |
702 // one final check if the load has already completed |
|
703 // class loaders holding the ObjectLock shouldn't find the class here |
697 klassOop check = find_class(d_index, d_hash, name, class_loader); |
704 klassOop check = find_class(d_index, d_hash, name, class_loader); |
698 if (check != NULL) { |
705 if (check != NULL) { |
699 // Klass is already loaded, so just return it |
706 // Klass is already loaded, so just return it |
700 k = instanceKlassHandle(THREAD, check); |
707 k = instanceKlassHandle(THREAD, check); |
701 class_has_been_loaded = true; |
708 class_has_been_loaded = true; |
702 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE); |
709 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE); |
|
710 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD); |
|
711 SystemDictionary_lock->notify_all(); |
703 } |
712 } |
704 } |
713 } |
705 } |
714 } |
706 // must throw error outside of owning lock |
715 // must throw error outside of owning lock |
707 if (throw_circularity_error) { |
716 if (throw_circularity_error) { |
712 if (!class_has_been_loaded) { |
721 if (!class_has_been_loaded) { |
713 |
722 |
714 // Do actual loading |
723 // Do actual loading |
715 k = load_instance_class(name, class_loader, THREAD); |
724 k = load_instance_class(name, class_loader, THREAD); |
716 |
725 |
717 // In custom class loaders, the usual findClass calls |
726 // For UnsyncloadClass and AllowParallelDefineClass only: |
718 // findLoadedClass, which directly searches the SystemDictionary, then |
|
719 // defineClass. If these are not atomic with respect to other threads, |
|
720 // the findLoadedClass can fail, but the defineClass can get a |
|
721 // LinkageError:: duplicate class definition. |
|
722 // If they got a linkageError, check if a parallel class load succeeded. |
727 // If they got a linkageError, check if a parallel class load succeeded. |
723 // If it did, then for bytecode resolution the specification requires |
728 // If it did, then for bytecode resolution the specification requires |
724 // that we return the same result we did for the other thread, i.e. the |
729 // that we return the same result we did for the other thread, i.e. the |
725 // successfully loaded instanceKlass |
730 // successfully loaded instanceKlass |
726 // Note: Class can not be unloaded as long as any classloader refs exist |
|
727 // Should not get here for classloaders that support parallelism |
731 // Should not get here for classloaders that support parallelism |
728 // with the new cleaner mechanism, e.g. bootstrap classloader |
732 // with the new cleaner mechanism |
|
733 // Bootstrap goes through here to allow for an extra guarantee check |
729 if (UnsyncloadClass || (class_loader.is_null())) { |
734 if (UnsyncloadClass || (class_loader.is_null())) { |
730 if (k.is_null() && HAS_PENDING_EXCEPTION |
735 if (k.is_null() && HAS_PENDING_EXCEPTION |
731 && PENDING_EXCEPTION->is_a(SystemDictionary::linkageError_klass())) { |
736 && PENDING_EXCEPTION->is_a(SystemDictionary::linkageError_klass())) { |
732 MutexLocker mu(SystemDictionary_lock, THREAD); |
737 MutexLocker mu(SystemDictionary_lock, THREAD); |
733 klassOop check = find_class(d_index, d_hash, name, class_loader); |
738 klassOop check = find_class(d_index, d_hash, name, class_loader); |
953 // java.lang.Object through resolve_or_fail, not this path. |
958 // java.lang.Object through resolve_or_fail, not this path. |
954 |
959 |
955 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name, |
960 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name, |
956 class_loader, |
961 class_loader, |
957 protection_domain, |
962 protection_domain, |
958 cp_patches, |
|
959 parsed_name, |
963 parsed_name, |
960 THREAD); |
964 THREAD); |
|
965 |
961 |
966 |
962 // We don't redefine the class, so we just need to clean up whether there |
967 // We don't redefine the class, so we just need to clean up whether there |
963 // was an error or not (don't want to modify any system dictionary |
968 // was an error or not (don't want to modify any system dictionary |
964 // data structures). |
969 // data structures). |
965 // Parsed name could be null if we threw an error before we got far |
970 // Parsed name could be null if we threw an error before we got far |
1011 Handle class_loader, |
1016 Handle class_loader, |
1012 Handle protection_domain, |
1017 Handle protection_domain, |
1013 ClassFileStream* st, |
1018 ClassFileStream* st, |
1014 TRAPS) { |
1019 TRAPS) { |
1015 |
1020 |
1016 // Make sure we are synchronized on the class loader before we initiate |
1021 // Classloaders that support parallelism, e.g. bootstrap classloader, |
1017 // loading. |
1022 // or all classloaders with UnsyncloadClass do not acquire lock here |
|
1023 bool DoObjectLock = true; |
|
1024 if (is_parallelCapable(class_loader)) { |
|
1025 DoObjectLock = false; |
|
1026 } |
|
1027 |
|
1028 // Make sure we are synchronized on the class loader before we proceed |
1018 Handle lockObject = compute_loader_lock_object(class_loader, THREAD); |
1029 Handle lockObject = compute_loader_lock_object(class_loader, THREAD); |
1019 check_loader_lock_contention(lockObject, THREAD); |
1030 check_loader_lock_contention(lockObject, THREAD); |
1020 ObjectLocker ol(lockObject, THREAD); |
1031 ObjectLocker ol(lockObject, THREAD, DoObjectLock); |
1021 |
1032 |
1022 symbolHandle parsed_name; |
1033 symbolHandle parsed_name; |
1023 |
1034 |
1024 // Parse the stream. Note that we do this even though this klass might |
1035 // Parse the stream. Note that we do this even though this klass might |
1025 // already be present in the SystemDictionary, otherwise we would not |
1036 // already be present in the SystemDictionary, otherwise we would not |
1067 // asserts that that's the case. |
1078 // asserts that that's the case. |
1068 assert(is_internal_format(parsed_name), |
1079 assert(is_internal_format(parsed_name), |
1069 "external class name format used internally"); |
1080 "external class name format used internally"); |
1070 |
1081 |
1071 // Add class just loaded |
1082 // Add class just loaded |
1072 define_instance_class(k, THREAD); |
1083 // If a class loader supports parallel classloading handle parallel define requests |
|
1084 // find_or_define_instance_class may return a different instanceKlass |
|
1085 if (is_parallelCapable(class_loader)) { |
|
1086 k = find_or_define_instance_class(class_name, class_loader, k, THREAD); |
|
1087 } else { |
|
1088 define_instance_class(k, THREAD); |
|
1089 } |
1073 } |
1090 } |
1074 |
1091 |
1075 // If parsing the class file or define_instance_class failed, we |
1092 // If parsing the class file or define_instance_class failed, we |
1076 // need to remove the placeholder added on our behalf. But we |
1093 // need to remove the placeholder added on our behalf. But we |
1077 // must make sure parsed_name is valid first (it won't be if we had |
1094 // must make sure parsed_name is valid first (it won't be if we had |
1314 |
1331 |
1315 JavaValue result(T_OBJECT); |
1332 JavaValue result(T_OBJECT); |
1316 |
1333 |
1317 KlassHandle spec_klass (THREAD, SystemDictionary::classloader_klass()); |
1334 KlassHandle spec_klass (THREAD, SystemDictionary::classloader_klass()); |
1318 |
1335 |
1319 // UnsyncloadClass option means don't synchronize loadClass() calls. |
1336 // Call public unsynchronized loadClass(String) directly for all class loaders |
1320 // loadClassInternal() is synchronized and public loadClass(String) is not. |
1337 // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will |
1321 // This flag is for diagnostic purposes only. It is risky to call |
1338 // acquire a class-name based lock rather than the class loader object lock. |
|
1339 // JDK < 7 already acquire the class loader lock in loadClass(String, boolean), |
|
1340 // so the call to loadClassInternal() was not required. |
|
1341 // |
|
1342 // UnsyncloadClass flag means both call loadClass(String) and do |
|
1343 // not acquire the class loader lock even for class loaders that are |
|
1344 // not parallelCapable. This was a risky transitional |
|
1345 // flag for diagnostic purposes only. It is risky to call |
1322 // custom class loaders without synchronization. |
1346 // custom class loaders without synchronization. |
1323 // WARNING If a custom class loader does NOT synchronizer findClass, or callers of |
1347 // WARNING If a custom class loader does NOT synchronizer findClass, or callers of |
1324 // findClass, this flag risks unexpected timing bugs in the field. |
1348 // findClass, the UnsyncloadClass flag risks unexpected timing bugs in the field. |
1325 // Do NOT assume this will be supported in future releases. |
1349 // Do NOT assume this will be supported in future releases. |
1326 if (!UnsyncloadClass && has_loadClassInternal()) { |
1350 // |
|
1351 // Added MustCallLoadClassInternal in case we discover in the field |
|
1352 // a customer that counts on this call |
|
1353 if (MustCallLoadClassInternal && has_loadClassInternal()) { |
1327 JavaCalls::call_special(&result, |
1354 JavaCalls::call_special(&result, |
1328 class_loader, |
1355 class_loader, |
1329 spec_klass, |
1356 spec_klass, |
1330 vmSymbolHandles::loadClassInternal_name(), |
1357 vmSymbolHandles::loadClassInternal_name(), |
1331 vmSymbolHandles::string_class_signature(), |
1358 vmSymbolHandles::string_class_signature(), |
1363 |
1390 |
1364 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) { |
1391 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) { |
1365 |
1392 |
1366 Handle class_loader_h(THREAD, k->class_loader()); |
1393 Handle class_loader_h(THREAD, k->class_loader()); |
1367 |
1394 |
1368 // for bootstrap classloader don't acquire lock |
1395 // for bootstrap and other parallel classloaders don't acquire lock, |
1369 if (!class_loader_h.is_null()) { |
1396 // use placeholder token |
|
1397 // If a parallelCapable class loader calls define_instance_class instead of |
|
1398 // find_or_define_instance_class to get here, we have a timing |
|
1399 // hole with systemDictionary updates and check_constraints |
|
1400 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) { |
1370 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, |
1401 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, |
1371 compute_loader_lock_object(class_loader_h, THREAD)), |
1402 compute_loader_lock_object(class_loader_h, THREAD)), |
1372 "define called without lock"); |
1403 "define called without lock"); |
1373 } |
1404 } |
1374 |
|
1375 |
1405 |
1376 // Check class-loading constraints. Throw exception if violation is detected. |
1406 // Check class-loading constraints. Throw exception if violation is detected. |
1377 // Grabs and releases SystemDictionary_lock |
1407 // Grabs and releases SystemDictionary_lock |
1378 // The check_constraints/find_class call and update_dictionary sequence |
1408 // The check_constraints/find_class call and update_dictionary sequence |
1379 // must be "atomic" for a specific class/classloader pair so we never |
1409 // must be "atomic" for a specific class/classloader pair so we never |
1425 } |
1455 } |
1426 } |
1456 } |
1427 |
1457 |
1428 // Support parallel classloading |
1458 // Support parallel classloading |
1429 // Initial implementation for bootstrap classloader |
1459 // Initial implementation for bootstrap classloader |
1430 // For future: |
|
1431 // For custom class loaders that support parallel classloading, |
1460 // For custom class loaders that support parallel classloading, |
1432 // in case they do not synchronize around |
1461 // With AllowParallelDefine flag==true, in case they do not synchronize around |
1433 // FindLoadedClass/DefineClass calls, we check for parallel |
1462 // FindLoadedClass/DefineClass, calls, we check for parallel |
1434 // loading for them, wait if a defineClass is in progress |
1463 // loading for them, wait if a defineClass is in progress |
1435 // and return the initial requestor's results |
1464 // and return the initial requestor's results |
|
1465 // With AllowParallelDefine flag==false, call through to define_instance_class |
|
1466 // which will throw LinkageError: duplicate class definition. |
1436 // For better performance, the class loaders should synchronize |
1467 // For better performance, the class loaders should synchronize |
1437 // findClass(), i.e. FindLoadedClass/DefineClass or they |
1468 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they |
1438 // potentially waste time reading and parsing the bytestream. |
1469 // potentially waste time reading and parsing the bytestream. |
1439 // Note: VM callers should ensure consistency of k/class_name,class_loader |
1470 // Note: VM callers should ensure consistency of k/class_name,class_loader |
1440 instanceKlassHandle SystemDictionary::find_or_define_instance_class(symbolHandle class_name, Handle class_loader, instanceKlassHandle k, TRAPS) { |
1471 instanceKlassHandle SystemDictionary::find_or_define_instance_class(symbolHandle class_name, Handle class_loader, instanceKlassHandle k, TRAPS) { |
1441 |
1472 |
1442 instanceKlassHandle nh = instanceKlassHandle(); // null Handle |
1473 instanceKlassHandle nh = instanceKlassHandle(); // null Handle |
1458 } |
1489 } |
1459 |
1490 |
1460 // Acquire define token for this class/classloader |
1491 // Acquire define token for this class/classloader |
1461 symbolHandle nullsymbolHandle; |
1492 symbolHandle nullsymbolHandle; |
1462 probe = placeholders()->find_and_add(p_index, p_hash, class_name, class_loader, PlaceholderTable::DEFINE_CLASS, nullsymbolHandle, THREAD); |
1493 probe = placeholders()->find_and_add(p_index, p_hash, class_name, class_loader, PlaceholderTable::DEFINE_CLASS, nullsymbolHandle, THREAD); |
1463 // Check if another thread defining in parallel |
1494 // Wait if another thread defining in parallel |
1464 if (probe->definer() == NULL) { |
1495 // All threads wait - even those that will throw duplicate class: otherwise |
1465 // Thread will define the class |
1496 // caller is surprised by LinkageError: duplicate, but findLoadedClass fails |
1466 probe->set_definer(THREAD); |
1497 // if other thread has not finished updating dictionary |
1467 } else { |
1498 while (probe->definer() != NULL) { |
1468 // Wait for defining thread to finish and return results |
1499 SystemDictionary_lock->wait(); |
1469 while (probe->definer() != NULL) { |
1500 } |
1470 SystemDictionary_lock->wait(); |
1501 // Only special cases allow parallel defines and can use other thread's results |
1471 } |
1502 // Other cases fall through, and may run into duplicate defines |
1472 if (probe->instanceKlass() != NULL) { |
1503 // caught by finding an entry in the SystemDictionary |
|
1504 if ((UnsyncloadClass || AllowParallelDefineClass) && (probe->instanceKlass() != NULL)) { |
1473 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS); |
1505 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS); |
1474 return(instanceKlassHandle(THREAD, probe->instanceKlass())); |
1506 placeholders()->find_and_remove(p_index, p_hash, class_name, class_loader, THREAD); |
1475 } else { |
1507 SystemDictionary_lock->notify_all(); |
1476 // If definer had an error, try again as any new thread would |
|
1477 probe->set_definer(THREAD); |
|
1478 #ifdef ASSERT |
1508 #ifdef ASSERT |
1479 klassOop check = find_class(d_index, d_hash, class_name, class_loader); |
1509 klassOop check = find_class(d_index, d_hash, class_name, class_loader); |
1480 assert(check == NULL, "definer missed recording success"); |
1510 assert(check != NULL, "definer missed recording success"); |
1481 #endif |
1511 #endif |
1482 } |
1512 return(instanceKlassHandle(THREAD, probe->instanceKlass())); |
|
1513 } else { |
|
1514 // This thread will define the class (even if earlier thread tried and had an error) |
|
1515 probe->set_definer(THREAD); |
1483 } |
1516 } |
1484 } |
1517 } |
1485 |
1518 |
1486 define_instance_class(k, THREAD); |
1519 define_instance_class(k, THREAD); |
1487 |
1520 |
1499 } else { |
1532 } else { |
1500 probe->set_instanceKlass(k()); |
1533 probe->set_instanceKlass(k()); |
1501 } |
1534 } |
1502 probe->set_definer(NULL); |
1535 probe->set_definer(NULL); |
1503 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS); |
1536 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS); |
|
1537 placeholders()->find_and_remove(p_index, p_hash, class_name, class_loader, THREAD); |
1504 SystemDictionary_lock->notify_all(); |
1538 SystemDictionary_lock->notify_all(); |
1505 } |
1539 } |
1506 } |
1540 } |
1507 |
1541 |
1508 // Can't throw exception while holding lock due to rank ordering |
1542 // Can't throw exception while holding lock due to rank ordering |
1510 THROW_OOP_(linkage_exception(), nh); // throws exception and returns |
1544 THROW_OOP_(linkage_exception(), nh); // throws exception and returns |
1511 } |
1545 } |
1512 |
1546 |
1513 return k; |
1547 return k; |
1514 } |
1548 } |
1515 |
|
1516 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) { |
1549 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) { |
1517 // If class_loader is NULL we synchronize on _system_loader_lock_obj |
1550 // If class_loader is NULL we synchronize on _system_loader_lock_obj |
1518 if (class_loader.is_null()) { |
1551 if (class_loader.is_null()) { |
1519 return Handle(THREAD, _system_loader_lock_obj); |
1552 return Handle(THREAD, _system_loader_lock_obj); |
1520 } else { |
1553 } else { |
1900 #ifdef KERNEL |
1933 #ifdef KERNEL |
1901 if (sun_jkernel_DownloadManager_klass() == NULL) { |
1934 if (sun_jkernel_DownloadManager_klass() == NULL) { |
1902 warning("Cannot find sun/jkernel/DownloadManager"); |
1935 warning("Cannot find sun/jkernel/DownloadManager"); |
1903 } |
1936 } |
1904 #endif // KERNEL |
1937 #endif // KERNEL |
|
1938 |
1905 { // Compute whether we should use loadClass or loadClassInternal when loading classes. |
1939 { // Compute whether we should use loadClass or loadClassInternal when loading classes. |
1906 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature()); |
1940 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature()); |
1907 _has_loadClassInternal = (method != NULL); |
1941 _has_loadClassInternal = (method != NULL); |
1908 } |
1942 } |
1909 |
|
1910 { // Compute whether we should use checkPackageAccess or NOT |
1943 { // Compute whether we should use checkPackageAccess or NOT |
1911 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature()); |
1944 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature()); |
1912 _has_checkPackageAccess = (method != NULL); |
1945 _has_checkPackageAccess = (method != NULL); |
1913 } |
1946 } |
1914 } |
1947 } |