< prev index next >

src/hotspot/share/classfile/packageEntry.cpp

Concurrent class unloading

107 
108 // Set the package as exported to all unnamed modules unless the package is                                                          
109 // already unqualifiedly exported.                                                                                                   
110 void PackageEntry::set_is_exported_allUnnamed() {                                                                                    
111   if (module()->is_open()) {                                                                                                         
112     // No-op for open modules since all packages are unqualifiedly exported                                                          
113     return;                                                                                                                          
114   }                                                                                                                                  
115 
116   MutexLocker m1(Module_lock);                                                                                                       
117   if (!is_unqual_exported()) {                                                                                                       
118    _export_flags = PKG_EXP_ALLUNNAMED;                                                                                               
119   }                                                                                                                                  
120 }                                                                                                                                    
121 
122 // Remove dead module entries within the package's exported list.  Note that                                                         
123 // if all of the modules on the _qualified_exports get purged the list does not                                                      
124 // get deleted.  This prevents the package from illegally transitioning from                                                         
125 // exported to non-exported.                                                                                                         
126 void PackageEntry::purge_qualified_exports() {                                                                                       
127   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");                                                           
128   if (_must_walk_exports &&                                                                                                          
129       _qualified_exports != NULL &&                                                                                                  
130       !_qualified_exports->is_empty()) {                                                                                             
131     ModuleEntry* pkg_module = module();                                                                                              
132 
133     // This package's _must_walk_exports flag will be reset based                                                                    
134     // on the remaining live modules on the exports list.                                                                            
135     _must_walk_exports = false;                                                                                                      
136 
137     if (log_is_enabled(Trace, module)) {                                                                                             
138       ResourceMark rm;                                                                                                               
139       assert(name() != NULL, "PackageEntry without a valid name");                                                                   
140       ModuleEntry* pkg_mod = module();                                                                                               
141       log_trace(module)("PackageEntry::purge_qualified_exports(): package %s defined in module %s, exports list being walked",       
142                         name()->as_C_string(),                                                                                       
143                         (pkg_mod == NULL || pkg_mod->name() == NULL) ? UNNAMED_MODULE : pkg_mod->name()->as_C_string());             
144     }                                                                                                                                
145 
146     // Go backwards because this removes entries that are dead.                                                                      
147     int len = _qualified_exports->length();                                                                                          
148     for (int idx = len - 1; idx >= 0; idx--) {                                                                                       
149       ModuleEntry* module_idx = _qualified_exports->at(idx);                                                                         
150       ClassLoaderData* cld_idx = module_idx->loader_data();                                                                          
151       if (cld_idx->is_unloading()) {                                                                                                 
152         _qualified_exports->delete_at(idx);                                                                                          
153       } else {                                                                                                                       
154         // Update the need to walk this package's exports based on live modules                                                      
155         set_export_walk_required(cld_idx);                                                                                           
156       }                                                                                                                              
157     }                                                                                                                                
158   }                                                                                                                                  
159 }                                                                                                                                    
160 
161 void PackageEntry::delete_qualified_exports() {                                                                                      
162   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");                                                           
163   if (_qualified_exports != NULL) {                                                                                                  
164     delete _qualified_exports;                                                                                                       
165   }                                                                                                                                  
166   _qualified_exports = NULL;                                                                                                         
167 }                                                                                                                                    
168 
169 PackageEntryTable::PackageEntryTable(int table_size)                                                                                 
170   : Hashtable<Symbol*, mtModule>(table_size, sizeof(PackageEntry))                                                                   
171 {                                                                                                                                    
172 }                                                                                                                                    
173 
174 PackageEntryTable::~PackageEntryTable() {                                                                                            
175   // Walk through all buckets and all entries in each bucket,                                                                        
176   // freeing each entry.                                                                                                             
177   for (int i = 0; i < table_size(); ++i) {                                                                                           
178     for (PackageEntry* p = bucket(i); p != NULL;) {                                                                                  
179       PackageEntry* to_remove = p;                                                                                                   
180       // read next before freeing.                                                                                                   
181       p = p->next();                                                                                                                 

107 
108 // Set the package as exported to all unnamed modules unless the package is
109 // already unqualifiedly exported.
110 void PackageEntry::set_is_exported_allUnnamed() {
111   if (module()->is_open()) {
112     // No-op for open modules since all packages are unqualifiedly exported
113     return;
114   }
115 
116   MutexLocker m1(Module_lock);
117   if (!is_unqual_exported()) {
118    _export_flags = PKG_EXP_ALLUNNAMED;
119   }
120 }
121 
122 // Remove dead module entries within the package's exported list.  Note that
123 // if all of the modules on the _qualified_exports get purged the list does not
124 // get deleted.  This prevents the package from illegally transitioning from
125 // exported to non-exported.
126 void PackageEntry::purge_qualified_exports() {
127   assert(UseZGC || SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
128   if (_must_walk_exports &&
129       _qualified_exports != NULL &&
130       !_qualified_exports->is_empty()) {
131     ModuleEntry* pkg_module = module();
132 
133     // This package's _must_walk_exports flag will be reset based
134     // on the remaining live modules on the exports list.
135     _must_walk_exports = false;
136 
137     if (log_is_enabled(Trace, module)) {
138       ResourceMark rm;
139       assert(name() != NULL, "PackageEntry without a valid name");
140       ModuleEntry* pkg_mod = module();
141       log_trace(module)("PackageEntry::purge_qualified_exports(): package %s defined in module %s, exports list being walked",
142                         name()->as_C_string(),
143                         (pkg_mod == NULL || pkg_mod->name() == NULL) ? UNNAMED_MODULE : pkg_mod->name()->as_C_string());
144     }
145 
146     // Go backwards because this removes entries that are dead.
147     int len = _qualified_exports->length();
148     for (int idx = len - 1; idx >= 0; idx--) {
149       ModuleEntry* module_idx = _qualified_exports->at(idx);
150       ClassLoaderData* cld_idx = module_idx->loader_data();
151       if (cld_idx->is_unloading()) {
152         _qualified_exports->delete_at(idx);
153       } else {
154         // Update the need to walk this package's exports based on live modules
155         set_export_walk_required(cld_idx);
156       }
157     }
158   }
159 }
160 
161 void PackageEntry::delete_qualified_exports() {
162   assert(UseZGC || SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
163   if (_qualified_exports != NULL) {
164     delete _qualified_exports;
165   }
166   _qualified_exports = NULL;
167 }
168 
169 PackageEntryTable::PackageEntryTable(int table_size)
170   : Hashtable<Symbol*, mtModule>(table_size, sizeof(PackageEntry))
171 {
172 }
173 
174 PackageEntryTable::~PackageEntryTable() {
175   // Walk through all buckets and all entries in each bucket,
176   // freeing each entry.
177   for (int i = 0; i < table_size(); ++i) {
178     for (PackageEntry* p = bucket(i); p != NULL;) {
179       PackageEntry* to_remove = p;
180       // read next before freeing.
181       p = p->next();

210 void PackageEntryTable::add_entry(int index, PackageEntry* new_entry) {                                                              
211   assert(Module_lock->owned_by_self(), "should have the Module_lock");                                                               
212   Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry);                                     
213 }                                                                                                                                    
214 
215 // Create package in loader's package entry table and return the entry.                                                              
216 // If entry already exists, return null.  Assume Module lock was taken by caller.                                                    
217 PackageEntry* PackageEntryTable::locked_create_entry_or_null(Symbol* name, ModuleEntry* module) {                                    
218   assert(Module_lock->owned_by_self(), "should have the Module_lock");                                                               
219   // Check if package already exists.  Return NULL if it does.                                                                       
220   if (lookup_only(name) != NULL) {                                                                                                   
221     return NULL;                                                                                                                     
222   } else {                                                                                                                           
223     PackageEntry* entry = new_entry(compute_hash(name), name, module);                                                               
224     add_entry(index_for(name), entry);                                                                                               
225     return entry;                                                                                                                    
226   }                                                                                                                                  
227 }                                                                                                                                    
228 
229 PackageEntry* PackageEntryTable::lookup(Symbol* name, ModuleEntry* module) {                                                         
                                                                                                                                     
                                                                                                                                     
230   PackageEntry* p = lookup_only(name);                                                                                               
231   if (p != NULL) {                                                                                                                   
232     return p;                                                                                                                        
233   } else {                                                                                                                           
234     // If not found, add to table. Grab the PackageEntryTable lock first.                                                            
235     MutexLocker ml(Module_lock);                                                                                                     
236 
237     // Since look-up was done lock-free, we need to check if another thread beat                                                     
238     // us in the race to insert the package.                                                                                         
239     PackageEntry* test = lookup_only(name);                                                                                          
240     if (test != NULL) {                                                                                                              
241       // A race occurred and another thread introduced the package.                                                                  
242       return test;                                                                                                                   
243     } else {                                                                                                                         
244       assert(module != NULL, "module should never be null");                                                                         
245       PackageEntry* entry = new_entry(compute_hash(name), name, module);                                                             
246       add_entry(index_for(name), entry);                                                                                             
247       return entry;                                                                                                                  
248     }                                                                                                                                
249   }                                                                                                                                  
250 }                                                                                                                                    
251 
252 PackageEntry* PackageEntryTable::lookup_only(Symbol* name) {                                                                         
253   int index = index_for(name);                                                                                                       
254   for (PackageEntry* p = bucket(index); p != NULL; p = p->next()) {                                                                  

210 void PackageEntryTable::add_entry(int index, PackageEntry* new_entry) {
211   assert(Module_lock->owned_by_self(), "should have the Module_lock");
212   Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry);
213 }
214 
215 // Create package in loader's package entry table and return the entry.
216 // If entry already exists, return null.  Assume Module lock was taken by caller.
217 PackageEntry* PackageEntryTable::locked_create_entry_or_null(Symbol* name, ModuleEntry* module) {
218   assert(Module_lock->owned_by_self(), "should have the Module_lock");
219   // Check if package already exists.  Return NULL if it does.
220   if (lookup_only(name) != NULL) {
221     return NULL;
222   } else {
223     PackageEntry* entry = new_entry(compute_hash(name), name, module);
224     add_entry(index_for(name), entry);
225     return entry;
226   }
227 }
228 
229 PackageEntry* PackageEntryTable::lookup(Symbol* name, ModuleEntry* module) {
230   // If not found, add to table. Grab the PackageEntryTable lock first.
231   MutexLocker ml(Module_lock);
232   PackageEntry* p = lookup_only(name);
233   if (p != NULL) {
234     return p;
235   } else {


236 
237     // Since look-up was done lock-free, we need to check if another thread beat
238     // us in the race to insert the package.
239     PackageEntry* test = lookup_only(name);
240     if (test != NULL) {
241       // A race occurred and another thread introduced the package.
242       return test;
243     } else {
244       assert(module != NULL, "module should never be null");
245       PackageEntry* entry = new_entry(compute_hash(name), name, module);
246       add_entry(index_for(name), entry);
247       return entry;
248     }
249   }
250 }
251 
252 PackageEntry* PackageEntryTable::lookup_only(Symbol* name) {
253   int index = index_for(name);
254   for (PackageEntry* p = bucket(index); p != NULL; p = p->next()) {

278     }                                                                                                                                
279   }                                                                                                                                  
280 
281 }                                                                                                                                    
282 
283 // iteration of qualified exports                                                                                                    
284 void PackageEntry::package_exports_do(ModuleClosure* f) {                                                                            
285   assert_locked_or_safepoint(Module_lock);                                                                                           
286   assert(f != NULL, "invariant");                                                                                                    
287 
288   if (has_qual_exports_list()) {                                                                                                     
289     int qe_len = _qualified_exports->length();                                                                                       
290 
291     for (int i = 0; i < qe_len; ++i) {                                                                                               
292       f->do_module(_qualified_exports->at(i));                                                                                       
293     }                                                                                                                                
294   }                                                                                                                                  
295 }                                                                                                                                    
296 
297 bool PackageEntry::exported_pending_delete() const {                                                                                 
298   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");                                                           
299   return (is_unqual_exported() && _qualified_exports != NULL);                                                                       
300 }                                                                                                                                    
301 
302 // Remove dead entries from all packages' exported list                                                                              
303 void PackageEntryTable::purge_all_package_exports() {                                                                                
304   assert_locked_or_safepoint(Module_lock);                                                                                           
305   for (int i = 0; i < table_size(); i++) {                                                                                           
306     for (PackageEntry* entry = bucket(i);                                                                                            
307                        entry != NULL;                                                                                                
308                        entry = entry->next()) {                                                                                      
309       if (entry->exported_pending_delete()) {                                                                                        
310         // exported list is pending deletion due to a transition                                                                     
311         // from qualified to unqualified                                                                                             
312         entry->delete_qualified_exports();                                                                                           
313       } else if (entry->is_qual_exported()) {                                                                                        
314         entry->purge_qualified_exports();                                                                                            
315       }                                                                                                                              
316     }                                                                                                                                
317   }                                                                                                                                  

278     }
279   }
280 
281 }
282 
283 // iteration of qualified exports
284 void PackageEntry::package_exports_do(ModuleClosure* f) {
285   assert_locked_or_safepoint(Module_lock);
286   assert(f != NULL, "invariant");
287 
288   if (has_qual_exports_list()) {
289     int qe_len = _qualified_exports->length();
290 
291     for (int i = 0; i < qe_len; ++i) {
292       f->do_module(_qualified_exports->at(i));
293     }
294   }
295 }
296 
297 bool PackageEntry::exported_pending_delete() const {
298   assert(UseZGC || SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
299   return (is_unqual_exported() && _qualified_exports != NULL);
300 }
301 
302 // Remove dead entries from all packages' exported list
303 void PackageEntryTable::purge_all_package_exports() {
304   assert_locked_or_safepoint(Module_lock);
305   for (int i = 0; i < table_size(); i++) {
306     for (PackageEntry* entry = bucket(i);
307                        entry != NULL;
308                        entry = entry->next()) {
309       if (entry->exported_pending_delete()) {
310         // exported list is pending deletion due to a transition
311         // from qualified to unqualified
312         entry->delete_qualified_exports();
313       } else if (entry->is_qual_exported()) {
314         entry->purge_qualified_exports();
315       }
316     }
317   }
< prev index next >