src/hotspot/share/oops/instanceKlass.cpp
changeset 52311 274ba8fbd96d
parent 52232 c9459e2f7bc8
child 52356 19c4a3eec4d7
--- a/src/hotspot/share/oops/instanceKlass.cpp	Mon Oct 29 08:38:59 2018 -0400
+++ b/src/hotspot/share/oops/instanceKlass.cpp	Mon Oct 29 10:21:34 2018 -0400
@@ -1054,16 +1054,28 @@
   }
 }
 
-Klass* InstanceKlass::implementor() const {
+Klass* InstanceKlass::implementor(bool log) const {
   assert_locked_or_safepoint(Compile_lock);
   Klass** k = adr_implementor();
   if (k == NULL) {
     return NULL;
   } else {
-    return *k;
+    Klass* kls = *k;
+    if (kls != NULL && !kls->is_loader_alive()) {
+      if (log) {
+        if (log_is_enabled(Trace, class, unload)) {
+          ResourceMark rm;
+          log_trace(class, unload)("unlinking class (implementor): %s", kls->external_name());
+        }
+      }
+      return NULL;  // don't return unloaded class
+    } else {
+      return kls;
+    }
   }
 }
 
+
 void InstanceKlass::set_implementor(Klass* k) {
   assert_lock_strong(Compile_lock);
   assert(is_interface(), "not interface");
@@ -2139,20 +2151,15 @@
 }
 
 void InstanceKlass::clean_implementors_list() {
+  assert_locked_or_safepoint(Compile_lock);
   assert(is_loader_alive(), "this klass should be live");
   if (is_interface()) {
-    if (ClassUnloading) {
-      Klass* impl = implementor();
-      if (impl != NULL) {
-        if (!impl->is_loader_alive()) {
-          // remove this guy
-          Klass** klass = adr_implementor();
-          assert(klass != NULL, "null klass");
-          if (klass != NULL) {
-            *klass = NULL;
-          }
-        }
-      }
+    assert (ClassUnloading, "only called for ClassUnloading");
+    Klass* impl = implementor(true);
+    if (impl == NULL) {
+      // NULL this field, might be an unloaded klass or NULL
+      Klass** klass = adr_implementor();
+      *klass = NULL;
     }
   }
 }
@@ -3080,28 +3087,29 @@
   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
   st->print(BULLET"sub:               ");
-  Klass* sub = subklass();
-  int n;
-  for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
-    if (n < MaxSubklassPrintSize) {
-      sub->print_value_on(st);
-      st->print("   ");
+  {
+    MutexLocker ml(Compile_lock);
+    Klass* sub = subklass();
+    int n;
+    for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
+      if (n < MaxSubklassPrintSize) {
+        sub->print_value_on(st);
+        st->print("   ");
+      }
+    }
+    if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
+    st->cr();
+
+    if (is_interface()) {
+      st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
+      if (nof_implementors() == 1) {
+        st->print_cr(BULLET"implementor:    ");
+        st->print("   ");
+        implementor()->print_value_on(st);
+        st->cr();
+      }
     }
   }
-  if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
-  st->cr();
-
-  if (is_interface()) {
-    MutexLocker ml(Compile_lock);
-    st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
-    if (nof_implementors() == 1) {
-      st->print_cr(BULLET"implementor:    ");
-      st->print("   ");
-      implementor()->print_value_on(st);
-      st->cr();
-    }
-  }
-
   st->print(BULLET"arrays:            "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
   st->print(BULLET"methods:           "); methods()->print_value_on(st);                  st->cr();
   if (Verbose || WizardMode) {
@@ -3483,26 +3491,30 @@
     vtable().verify(st);
   }
 
-  // Verify first subklass
-  if (subklass() != NULL) {
-    guarantee(subklass()->is_klass(), "should be klass");
+  // This is called from add_to_hierarchy when the Compile_lock is owned.
+  {
+    MutexLockerEx ml(Compile_lock->owned_by_self() ? NULL : Compile_lock);
+    // Verify first subklass
+    if (subklass() != NULL) {
+      guarantee(subklass()->is_klass(), "should be klass");
+    }
+
+    // Verify siblings
+    Klass* super = this->super();
+    Klass* sib = next_sibling();
+    if (sib != NULL) {
+      if (sib == this) {
+        fatal("subclass points to itself " PTR_FORMAT, p2i(sib));
+      }
+
+      guarantee(sib->is_klass(), "should be klass");
+      guarantee(sib->super() == super, "siblings should have same superklass");
+    }
+
+    // Verify implementor fields requires the Compile_lock,
+    // but this is sometimes called inside a safepoint, so don't verify.
   }
 
-  // Verify siblings
-  Klass* super = this->super();
-  Klass* sib = next_sibling();
-  if (sib != NULL) {
-    if (sib == this) {
-      fatal("subclass points to itself " PTR_FORMAT, p2i(sib));
-    }
-
-    guarantee(sib->is_klass(), "should be klass");
-    guarantee(sib->super() == super, "siblings should have same superklass");
-  }
-
-  // Verify implementor fields requires the Compile_lock, but this is sometimes
-  // called inside a safepoint, so don't verify.
-
   // Verify local interfaces
   if (local_interfaces()) {
     Array<InstanceKlass*>* local_interfaces = this->local_interfaces();