src/hotspot/share/classfile/classLoaderData.cpp
changeset 51698 74dde8b66b7f
parent 51682 a30461a359f5
child 51747 9bf5205655ee
equal deleted inserted replaced
51697:49e1b21d9878 51698:74dde8b66b7f
   155   _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL) {
   155   _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL) {
   156 
   156 
   157   if (!h_class_loader.is_null()) {
   157   if (!h_class_loader.is_null()) {
   158     _class_loader = _handles.add(h_class_loader());
   158     _class_loader = _handles.add(h_class_loader());
   159     _class_loader_klass = h_class_loader->klass();
   159     _class_loader_klass = h_class_loader->klass();
       
   160     initialize_name(h_class_loader);
   160   }
   161   }
   161 
   162 
   162   if (!is_unsafe_anonymous) {
   163   if (!is_unsafe_anonymous) {
   163     // The holder is initialized later for unsafe anonymous classes, and before calling anything
   164     // The holder is initialized later for unsafe anonymous classes, and before calling anything
   164     // that call class_loader().
   165     // that call class_loader().
  1075 
  1076 
  1076 // Add a new class loader data node to the list.  Assign the newly created
  1077 // Add a new class loader data node to the list.  Assign the newly created
  1077 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
  1078 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
  1078 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
  1079 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
  1079 
  1080 
       
  1081   assert_lock_strong(ClassLoaderDataGraph_lock);
  1080 
  1082 
  1081   ClassLoaderData* cld;
  1083   ClassLoaderData* cld;
  1082   {
  1084 
  1083     NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
  1085   // First check if another thread beat us to creating the CLD and installing
  1084                                        // ClassLoaderData in the loader since the CLD
  1086   // it into the loader while we were waiting for the lock.
  1085                                        // contains oops in _handles that must be walked.
  1087   if (!is_unsafe_anonymous && loader.not_null()) {
  1086                                        // GC will find the CLD through the loader after this.
  1088     cld = java_lang_ClassLoader::loader_data_acquire(loader());
  1087 
  1089     if (cld != NULL) {
  1088     cld = new ClassLoaderData(loader, is_unsafe_anonymous);
  1090       return cld;
  1089 
  1091     }
  1090     if (!is_unsafe_anonymous) {
  1092   }
  1091       // First, Atomically set it
  1093 
  1092       ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
  1094   // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD
  1093       if (old != NULL) {
  1095   // contains oops in _handles that must be walked.  GC doesn't walk CLD from the
  1094         delete cld;
  1096   // loader oop in all collections, particularly young collections.
  1095         // Returns the data.
  1097   NoSafepointVerifier no_safepoints;
  1096         return old;
  1098 
  1097       }
  1099   cld = new ClassLoaderData(loader, is_unsafe_anonymous);
  1098     }
  1100 
  1099   }
  1101   // First install the new CLD to the Graph.
  1100 
       
  1101   MutexLocker ml(ClassLoaderDataGraph_lock);
       
  1102 
       
  1103   // We won the race, and therefore the task of adding the data to the list of
       
  1104   // class loader data
       
  1105   cld->set_next(_head);
  1102   cld->set_next(_head);
  1106   _head = cld;
  1103   _head = cld;
       
  1104 
       
  1105   // Next associate with the class_loader.
       
  1106   if (!is_unsafe_anonymous) {
       
  1107     // Use OrderAccess, since readers need to get the loader_data only after
       
  1108     // it's added to the Graph
       
  1109     java_lang_ClassLoader::release_set_loader_data(loader(), cld);
       
  1110   }
       
  1111 
       
  1112   // Lastly log, if requested
  1107   LogTarget(Trace, class, loader, data) lt;
  1113   LogTarget(Trace, class, loader, data) lt;
  1108   if (lt.is_enabled()) {
  1114   if (lt.is_enabled()) {
  1109     ResourceMark rm;
  1115     ResourceMark rm;
  1110     LogStream ls(lt);
  1116     LogStream ls(lt);
  1111     ls.print("create ");
  1117     ls.print("create ");
  1114   }
  1120   }
  1115   return cld;
  1121   return cld;
  1116 }
  1122 }
  1117 
  1123 
  1118 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) {
  1124 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) {
       
  1125   MutexLocker ml(ClassLoaderDataGraph_lock);
  1119   ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous);
  1126   ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous);
  1120   // Initialize _name and _name_and_id after the loader data is added to the
       
  1121   // CLDG because adding the Symbol for _name and _name_and_id might safepoint.
       
  1122   if (loader.not_null()) {
       
  1123     loader_data->initialize_name(loader);
       
  1124   }
       
  1125   return loader_data;
  1127   return loader_data;
  1126 }
  1128 }
  1127 
  1129 
  1128 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
  1130 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
  1129   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
  1131   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);