< prev index next >

src/share/vm/oops/instanceKlass.cpp

Print this page




 454   OrderAccess::loadload();
 455   assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state
 456          "only fully initialized state can have a null lock");
 457   return lock;
 458 }
 459 
 460 // Set the initialization lock to null so the object can be GC'ed.  Any racing
 461 // threads to get this lock will see a null lock and will not lock.
 462 // That's okay because they all check for initialized state after getting
 463 // the lock and return.
 464 void InstanceKlass::fence_and_clear_init_lock() {
 465   // make sure previous stores are all done, notably the init_state.
 466   OrderAccess::storestore();
 467   java_lang_Class::set_init_lock(java_mirror(), NULL);
 468   assert(!is_not_initialized(), "class must be initialized now");
 469 }
 470 
 471 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_k) {
 472   EXCEPTION_MARK;
 473   oop init_lock = this_k->init_lock();

 474   ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 475 
 476   // abort if someone beat us to the initialization
 477   if (!this_k->is_not_initialized()) return;  // note: not equivalent to is_initialized()
 478 
 479   ClassState old_state = this_k->init_state();
 480   link_class_impl(this_k, true, THREAD);
 481   if (HAS_PENDING_EXCEPTION) {
 482     CLEAR_PENDING_EXCEPTION;
 483     // Abort if linking the class throws an exception.
 484 
 485     // Use a test to avoid redundantly resetting the state if there's
 486     // no change.  Set_init_state() asserts that state changes make
 487     // progress, whereas here we might just be spinning in place.
 488     if( old_state != this_k->_init_state )
 489       this_k->set_init_state (old_state);
 490   } else {
 491     // linking successfull, mark class as initialized
 492     this_k->set_init_state (fully_initialized);
 493     this_k->fence_and_clear_init_lock();


 600     link_class_impl(ih, throw_verifyerror, CHECK_false);
 601   }
 602 
 603   // in case the class is linked in the process of linking its superclasses
 604   if (this_k->is_linked()) {
 605     return true;
 606   }
 607 
 608   // trace only the link time for this klass that includes
 609   // the verification time
 610   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 611                              ClassLoader::perf_class_link_selftime(),
 612                              ClassLoader::perf_classes_linked(),
 613                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 614                              jt->get_thread_stat()->perf_timers_addr(),
 615                              PerfClassTraceTime::CLASS_LINK);
 616 
 617   // verification & rewriting
 618   {
 619     oop init_lock = this_k->init_lock();

 620     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 621     // rewritten will have been set if loader constraint error found
 622     // on an earlier link attempt
 623     // don't verify or rewrite if already rewritten
 624 
 625     if (!this_k->is_linked()) {
 626       if (!this_k->is_rewritten()) {
 627         {
 628           bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD);
 629           if (!verify_ok) {
 630             return false;
 631           }
 632         }
 633 
 634         // Just in case a side-effect of verify linked this class already
 635         // (which can sometimes happen since the verifier loads classes
 636         // using custom class loaders, which are free to initialize things)
 637         if (this_k->is_linked()) {
 638           return true;
 639         }


 733           THROW_OOP(e());
 734         }
 735       }
 736     }
 737   }
 738 }
 739 
 740 void InstanceKlass::initialize_impl(instanceKlassHandle this_k, TRAPS) {
 741   // Make sure klass is linked (verified) before initialization
 742   // A class could already be verified, since it has been reflected upon.
 743   this_k->link_class(CHECK);
 744 
 745   DTRACE_CLASSINIT_PROBE(required, InstanceKlass::cast(this_k()), -1);
 746 
 747   bool wait = false;
 748 
 749   // refer to the JVM book page 47 for description of steps
 750   // Step 1
 751   {
 752     oop init_lock = this_k->init_lock();

 753     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 754 
 755     Thread *self = THREAD; // it's passed the current thread
 756 
 757     // Step 2
 758     // If we were to use wait() instead of waitInterruptibly() then
 759     // we might end up throwing IE from link/symbol resolution sites
 760     // that aren't expected to throw.  This would wreak havoc.  See 6320309.
 761     while(this_k->is_being_initialized() && !this_k->is_reentrant_initialization(self)) {
 762         wait = true;
 763       ol.waitUninterruptibly(CHECK);
 764     }
 765 
 766     // Step 3
 767     if (this_k->is_being_initialized() && this_k->is_reentrant_initialization(self)) {
 768       DTRACE_CLASSINIT_PROBE_WAIT(recursive, InstanceKlass::cast(this_k()), -1,wait);
 769       return;
 770     }
 771 
 772     // Step 4


 865       THROW_OOP(e());
 866     } else {
 867       JavaCallArguments args(e);
 868       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
 869                 vmSymbols::throwable_void_signature(),
 870                 &args);
 871     }
 872   }
 873   DTRACE_CLASSINIT_PROBE_WAIT(end, InstanceKlass::cast(this_k()), -1,wait);
 874 }
 875 
 876 
 877 // Note: implementation moved to static method to expose the this pointer.
 878 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
 879   instanceKlassHandle kh(THREAD, this);
 880   set_initialization_state_and_notify_impl(kh, state, CHECK);
 881 }
 882 
 883 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_k, ClassState state, TRAPS) {
 884   oop init_lock = this_k->init_lock();

 885   ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 886   this_k->set_init_state(state);
 887   this_k->fence_and_clear_init_lock();
 888   ol.notify_all(CHECK);
 889 }
 890 
 891 // The embedded _implementor field can only record one implementor.
 892 // When there are more than one implementors, the _implementor field
 893 // is set to the interface Klass* itself. Following are the possible
 894 // values for the _implementor field:
 895 //   NULL                  - no implementor
 896 //   implementor Klass*    - one implementor
 897 //   self                  - more than one implementor
 898 //
 899 // The _implementor field only exists for interfaces.
 900 void InstanceKlass::add_implementor(Klass* k) {
 901   assert(Compile_lock->owned_by_self(), "");
 902   assert(is_interface(), "not interface");
 903   // Filter out my subinterfaces.
 904   // (Note: Interfaces are never on the subklass list.)


2237 
2238 void InstanceKlass::set_source_debug_extension(char* array, int length) {
2239   if (array == NULL) {
2240     _source_debug_extension = NULL;
2241   } else {
2242     // Adding one to the attribute length in order to store a null terminator
2243     // character could cause an overflow because the attribute length is
2244     // already coded with an u4 in the classfile, but in practice, it's
2245     // unlikely to happen.
2246     assert((length+1) > length, "Overflow checking");
2247     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2248     for (int i = 0; i < length; i++) {
2249       sde[i] = array[i];
2250     }
2251     sde[length] = '\0';
2252     _source_debug_extension = sde;
2253   }
2254 }
2255 
2256 address InstanceKlass::static_field_addr(int offset) {
2257   return (address)(offset + InstanceMirrorKlass::offset_of_static_fields() + cast_from_oop<intptr_t>(java_mirror()));
2258 }
2259 
2260 
2261 const char* InstanceKlass::signature_name() const {
2262   int hash_len = 0;
2263   char hash_buf[40];
2264 
2265   // If this is an anonymous class, append a hash to make the name unique
2266   if (is_anonymous()) {
2267     intptr_t hash = (java_mirror() != NULL) ? java_mirror()->identity_hash() : 0;
2268     jio_snprintf(hash_buf, sizeof(hash_buf), "/" UINTX_FORMAT, (uintx)hash);
2269     hash_len = (int)strlen(hash_buf);
2270   }
2271 
2272   // Get the internal name as a c string
2273   const char* src = (const char*) (name()->as_C_string());
2274   const int src_length = (int)strlen(src);
2275 
2276   char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
2277 




 454   OrderAccess::loadload();
 455   assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state
 456          "only fully initialized state can have a null lock");
 457   return lock;
 458 }
 459 
 460 // Set the initialization lock to null so the object can be GC'ed.  Any racing
 461 // threads to get this lock will see a null lock and will not lock.
 462 // That's okay because they all check for initialized state after getting
 463 // the lock and return.
 464 void InstanceKlass::fence_and_clear_init_lock() {
 465   // make sure previous stores are all done, notably the init_state.
 466   OrderAccess::storestore();
 467   java_lang_Class::set_init_lock(java_mirror(), NULL);
 468   assert(!is_not_initialized(), "class must be initialized now");
 469 }
 470 
 471 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_k) {
 472   EXCEPTION_MARK;
 473   oop init_lock = this_k->init_lock();
 474   init_lock = oopDesc::bs()->write_barrier(init_lock);
 475   ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 476 
 477   // abort if someone beat us to the initialization
 478   if (!this_k->is_not_initialized()) return;  // note: not equivalent to is_initialized()
 479 
 480   ClassState old_state = this_k->init_state();
 481   link_class_impl(this_k, true, THREAD);
 482   if (HAS_PENDING_EXCEPTION) {
 483     CLEAR_PENDING_EXCEPTION;
 484     // Abort if linking the class throws an exception.
 485 
 486     // Use a test to avoid redundantly resetting the state if there's
 487     // no change.  Set_init_state() asserts that state changes make
 488     // progress, whereas here we might just be spinning in place.
 489     if( old_state != this_k->_init_state )
 490       this_k->set_init_state (old_state);
 491   } else {
 492     // linking successfull, mark class as initialized
 493     this_k->set_init_state (fully_initialized);
 494     this_k->fence_and_clear_init_lock();


 601     link_class_impl(ih, throw_verifyerror, CHECK_false);
 602   }
 603 
 604   // in case the class is linked in the process of linking its superclasses
 605   if (this_k->is_linked()) {
 606     return true;
 607   }
 608 
 609   // trace only the link time for this klass that includes
 610   // the verification time
 611   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 612                              ClassLoader::perf_class_link_selftime(),
 613                              ClassLoader::perf_classes_linked(),
 614                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 615                              jt->get_thread_stat()->perf_timers_addr(),
 616                              PerfClassTraceTime::CLASS_LINK);
 617 
 618   // verification & rewriting
 619   {
 620     oop init_lock = this_k->init_lock();
 621     init_lock = oopDesc::bs()->write_barrier(init_lock);
 622     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 623     // rewritten will have been set if loader constraint error found
 624     // on an earlier link attempt
 625     // don't verify or rewrite if already rewritten
 626 
 627     if (!this_k->is_linked()) {
 628       if (!this_k->is_rewritten()) {
 629         {
 630           bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD);
 631           if (!verify_ok) {
 632             return false;
 633           }
 634         }
 635 
 636         // Just in case a side-effect of verify linked this class already
 637         // (which can sometimes happen since the verifier loads classes
 638         // using custom class loaders, which are free to initialize things)
 639         if (this_k->is_linked()) {
 640           return true;
 641         }


 735           THROW_OOP(e());
 736         }
 737       }
 738     }
 739   }
 740 }
 741 
 742 void InstanceKlass::initialize_impl(instanceKlassHandle this_k, TRAPS) {
 743   // Make sure klass is linked (verified) before initialization
 744   // A class could already be verified, since it has been reflected upon.
 745   this_k->link_class(CHECK);
 746 
 747   DTRACE_CLASSINIT_PROBE(required, InstanceKlass::cast(this_k()), -1);
 748 
 749   bool wait = false;
 750 
 751   // refer to the JVM book page 47 for description of steps
 752   // Step 1
 753   {
 754     oop init_lock = this_k->init_lock();
 755     init_lock = oopDesc::bs()->write_barrier(init_lock);
 756     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 757 
 758     Thread *self = THREAD; // it's passed the current thread
 759 
 760     // Step 2
 761     // If we were to use wait() instead of waitInterruptibly() then
 762     // we might end up throwing IE from link/symbol resolution sites
 763     // that aren't expected to throw.  This would wreak havoc.  See 6320309.
 764     while(this_k->is_being_initialized() && !this_k->is_reentrant_initialization(self)) {
 765         wait = true;
 766       ol.waitUninterruptibly(CHECK);
 767     }
 768 
 769     // Step 3
 770     if (this_k->is_being_initialized() && this_k->is_reentrant_initialization(self)) {
 771       DTRACE_CLASSINIT_PROBE_WAIT(recursive, InstanceKlass::cast(this_k()), -1,wait);
 772       return;
 773     }
 774 
 775     // Step 4


 868       THROW_OOP(e());
 869     } else {
 870       JavaCallArguments args(e);
 871       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
 872                 vmSymbols::throwable_void_signature(),
 873                 &args);
 874     }
 875   }
 876   DTRACE_CLASSINIT_PROBE_WAIT(end, InstanceKlass::cast(this_k()), -1,wait);
 877 }
 878 
 879 
 880 // Note: implementation moved to static method to expose the this pointer.
 881 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
 882   instanceKlassHandle kh(THREAD, this);
 883   set_initialization_state_and_notify_impl(kh, state, CHECK);
 884 }
 885 
 886 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_k, ClassState state, TRAPS) {
 887   oop init_lock = this_k->init_lock();
 888   init_lock = oopDesc::bs()->write_barrier(init_lock);
 889   ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
 890   this_k->set_init_state(state);
 891   this_k->fence_and_clear_init_lock();
 892   ol.notify_all(CHECK);
 893 }
 894 
 895 // The embedded _implementor field can only record one implementor.
 896 // When there are more than one implementors, the _implementor field
 897 // is set to the interface Klass* itself. Following are the possible
 898 // values for the _implementor field:
 899 //   NULL                  - no implementor
 900 //   implementor Klass*    - one implementor
 901 //   self                  - more than one implementor
 902 //
 903 // The _implementor field only exists for interfaces.
 904 void InstanceKlass::add_implementor(Klass* k) {
 905   assert(Compile_lock->owned_by_self(), "");
 906   assert(is_interface(), "not interface");
 907   // Filter out my subinterfaces.
 908   // (Note: Interfaces are never on the subklass list.)


2241 
2242 void InstanceKlass::set_source_debug_extension(char* array, int length) {
2243   if (array == NULL) {
2244     _source_debug_extension = NULL;
2245   } else {
2246     // Adding one to the attribute length in order to store a null terminator
2247     // character could cause an overflow because the attribute length is
2248     // already coded with an u4 in the classfile, but in practice, it's
2249     // unlikely to happen.
2250     assert((length+1) > length, "Overflow checking");
2251     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2252     for (int i = 0; i < length; i++) {
2253       sde[i] = array[i];
2254     }
2255     sde[length] = '\0';
2256     _source_debug_extension = sde;
2257   }
2258 }
2259 
2260 address InstanceKlass::static_field_addr(int offset) {
2261   return (address)(offset + InstanceMirrorKlass::offset_of_static_fields() + cast_from_oop<intptr_t>(oopDesc::bs()->write_barrier(java_mirror())));
2262 }
2263 
2264 
2265 const char* InstanceKlass::signature_name() const {
2266   int hash_len = 0;
2267   char hash_buf[40];
2268 
2269   // If this is an anonymous class, append a hash to make the name unique
2270   if (is_anonymous()) {
2271     intptr_t hash = (java_mirror() != NULL) ? java_mirror()->identity_hash() : 0;
2272     jio_snprintf(hash_buf, sizeof(hash_buf), "/" UINTX_FORMAT, (uintx)hash);
2273     hash_len = (int)strlen(hash_buf);
2274   }
2275 
2276   // Get the internal name as a c string
2277   const char* src = (const char*) (name()->as_C_string());
2278   const int src_length = (int)strlen(src);
2279 
2280   char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
2281 


< prev index next >