src/hotspot/share/classfile/classLoaderDataGraph.cpp
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54961 30c8a21ce002
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
    36 #include "memory/metaspace.hpp"
    36 #include "memory/metaspace.hpp"
    37 #include "memory/resourceArea.hpp"
    37 #include "memory/resourceArea.hpp"
    38 #include "runtime/atomic.hpp"
    38 #include "runtime/atomic.hpp"
    39 #include "runtime/handles.inline.hpp"
    39 #include "runtime/handles.inline.hpp"
    40 #include "runtime/mutex.hpp"
    40 #include "runtime/mutex.hpp"
       
    41 #include "runtime/orderAccess.hpp"
    41 #include "runtime/safepoint.hpp"
    42 #include "runtime/safepoint.hpp"
    42 #include "runtime/safepointVerifiers.hpp"
    43 #include "runtime/safepointVerifiers.hpp"
    43 #include "utilities/growableArray.hpp"
    44 #include "utilities/growableArray.hpp"
    44 #include "utilities/macros.hpp"
    45 #include "utilities/macros.hpp"
    45 #include "utilities/ostream.hpp"
    46 #include "utilities/ostream.hpp"
    46 
    47 
    47 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0;
    48 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0;
    48 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0;
    49 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0;
    49 
    50 
    50 void ClassLoaderDataGraph::clear_claimed_marks() {
    51 void ClassLoaderDataGraph::clear_claimed_marks() {
    51   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
    52   // The claimed marks of the CLDs in the ClassLoaderDataGraph are cleared
    52   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
    53   // outside a safepoint and without locking the ClassLoaderDataGraph_lock.
       
    54   // This is required to avoid a deadlock between concurrent GC threads and safepointing.
       
    55   //
       
    56   // We need to make sure that the CLD contents are fully visible to the
       
    57   // reader thread. This is accomplished by acquire/release of the _head,
       
    58   // and is sufficient.
       
    59   //
       
    60   // Any ClassLoaderData added after or during walking the list are prepended to
       
    61   // _head. Their claim mark need not be handled here.
       
    62   for (ClassLoaderData* cld = OrderAccess::load_acquire(&_head); cld != NULL; cld = cld->next()) {
    53     cld->clear_claim();
    63     cld->clear_claim();
    54   }
    64   }
    55 }
    65 }
    56 
    66 
       
    67 void ClassLoaderDataGraph::clear_claimed_marks(int claim) {
       
    68  for (ClassLoaderData* cld = OrderAccess::load_acquire(&_head); cld != NULL; cld = cld->next()) {
       
    69     cld->clear_claim(claim);
       
    70   }
       
    71 }
    57 // Class iterator used by the compiler.  It gets some number of classes at
    72 // Class iterator used by the compiler.  It gets some number of classes at
    58 // a safepoint to decay invocation counters on the methods.
    73 // a safepoint to decay invocation counters on the methods.
    59 class ClassLoaderDataGraphKlassIteratorStatic {
    74 class ClassLoaderDataGraphKlassIteratorStatic {
    60   ClassLoaderData* _current_loader_data;
    75   ClassLoaderData* _current_loader_data;
    61   Klass*           _current_class_entry;
    76   Klass*           _current_class_entry;
   167   MetadataOnStackMark md_on_stack(walk_all_metadata, /*redefinition_walk*/false);
   182   MetadataOnStackMark md_on_stack(walk_all_metadata, /*redefinition_walk*/false);
   168   clean_deallocate_lists(walk_all_metadata);
   183   clean_deallocate_lists(walk_all_metadata);
   169 }
   184 }
   170 
   185 
   171 // GC root of class loader data created.
   186 // GC root of class loader data created.
   172 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
   187 ClassLoaderData* volatile ClassLoaderDataGraph::_head = NULL;
   173 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
   188 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
   174 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
   189 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
   175 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
   190 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
   176 
   191 
   177 bool ClassLoaderDataGraph::_should_purge = false;
   192 bool ClassLoaderDataGraph::_should_purge = false;
   203 
   218 
   204   cld = new ClassLoaderData(loader, is_unsafe_anonymous);
   219   cld = new ClassLoaderData(loader, is_unsafe_anonymous);
   205 
   220 
   206   // First install the new CLD to the Graph.
   221   // First install the new CLD to the Graph.
   207   cld->set_next(_head);
   222   cld->set_next(_head);
   208   _head = cld;
   223   OrderAccess::release_store(&_head, cld);
   209 
   224 
   210   // Next associate with the class_loader.
   225   // Next associate with the class_loader.
   211   if (!is_unsafe_anonymous) {
   226   if (!is_unsafe_anonymous) {
   212     // Use OrderAccess, since readers need to get the loader_data only after
   227     // Use OrderAccess, since readers need to get the loader_data only after
   213     // it's added to the Graph
   228     // it's added to the Graph
   268   } else {
   283   } else {
   269     cld_do(cl);
   284     cld_do(cl);
   270   }
   285   }
   271 }
   286 }
   272 
   287 
   273 // Closure for locking and iterating through classes.
   288 // Closure for locking and iterating through classes. Only lock outside of safepoint.
   274 LockedClassesDo::LockedClassesDo(classes_do_func_t f) : _function(f) {
   289 LockedClassesDo::LockedClassesDo(classes_do_func_t f) : _function(f),
   275   ClassLoaderDataGraph_lock->lock();
   290   _do_lock(!SafepointSynchronize::is_at_safepoint()) {
   276 }
   291   if (_do_lock) {
   277 
   292     ClassLoaderDataGraph_lock->lock();
   278 LockedClassesDo::LockedClassesDo() : _function(NULL) {
   293   }
       
   294 }
       
   295 
       
   296 LockedClassesDo::LockedClassesDo() : _function(NULL),
       
   297   _do_lock(!SafepointSynchronize::is_at_safepoint()) {
   279   // callers provide their own do_klass
   298   // callers provide their own do_klass
   280   ClassLoaderDataGraph_lock->lock();
   299   if (_do_lock) {
   281 }
   300     ClassLoaderDataGraph_lock->lock();
   282 
   301   }
   283 LockedClassesDo::~LockedClassesDo() { ClassLoaderDataGraph_lock->unlock(); }
   302 }
       
   303 
       
   304 LockedClassesDo::~LockedClassesDo() {
       
   305   if (_do_lock) {
       
   306     ClassLoaderDataGraph_lock->unlock();
       
   307   }
       
   308 }
   284 
   309 
   285 
   310 
   286 // Iterating over the CLDG needs to be locked because
   311 // Iterating over the CLDG needs to be locked because
   287 // unloading can remove entries concurrently soon.
   312 // unloading can remove entries concurrently soon.
   288 class ClassLoaderDataGraphIterator : public StackObj {
   313 class ClassLoaderDataGraphIterator : public StackObj {
   292   Thread*          _thread;
   317   Thread*          _thread;
   293   NoSafepointVerifier _nsv; // No safepoints allowed in this scope
   318   NoSafepointVerifier _nsv; // No safepoints allowed in this scope
   294                             // unless verifying at a safepoint.
   319                             // unless verifying at a safepoint.
   295 
   320 
   296 public:
   321 public:
   297   ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head),
   322   ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head) {
   298      _nsv(true, !SafepointSynchronize::is_at_safepoint()) {
       
   299     _thread = Thread::current();
   323     _thread = Thread::current();
   300     assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
   324     assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
   301   }
   325   }
   302 
   326 
   303   ClassLoaderData* get_next() {
   327   ClassLoaderData* get_next() {
   459   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
   483   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
   460 
   484 
   461   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
   485   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
   462   ClassLoaderData* curr = _head;
   486   ClassLoaderData* curr = _head;
   463   while (curr != _saved_head) {
   487   while (curr != _saved_head) {
   464     if (!curr->claimed()) {
   488     if (!curr->claimed(ClassLoaderData::_claim_strong)) {
   465       array->push(curr);
   489       array->push(curr);
   466       LogTarget(Debug, class, loader, data) lt;
   490       LogTarget(Debug, class, loader, data) lt;
   467       if (lt.is_enabled()) {
   491       if (lt.is_enabled()) {
   468         LogStream ls(lt);
   492         LogStream ls(lt);
   469         ls.print("found new CLD: ");
   493         ls.print("found new CLD: ");