8224875: Shenandoah: ParallelCleaning code unloading should take lock to protect shared code roots array
authorzgu
Wed, 29 May 2019 10:57:19 -0400
changeset 55089 934d68e9c45d
parent 55088 69a35cd74f7d
child 55090 c1ad2862d0dd
8224875: Shenandoah: ParallelCleaning code unloading should take lock to protect shared code roots array Reviewed-by: shade
src/hotspot/share/gc/shenandoah/shenandoahCodeRoots.cpp
src/hotspot/share/gc/shenandoah/shenandoahCodeRoots.hpp
--- a/src/hotspot/share/gc/shenandoah/shenandoahCodeRoots.cpp	Wed May 29 23:36:36 2019 +0800
+++ b/src/hotspot/share/gc/shenandoah/shenandoahCodeRoots.cpp	Wed May 29 10:57:19 2019 -0400
@@ -26,6 +26,7 @@
 #include "code/nmethod.hpp"
 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
+#include "gc/shenandoah/shenandoahUtils.hpp"
 #include "memory/resourceArea.hpp"
 #include "memory/universe.hpp"
 
@@ -121,18 +122,21 @@
 };
 
 GrowableArray<ShenandoahNMethod*>* ShenandoahCodeRoots::_recorded_nms;
+ShenandoahLock                     ShenandoahCodeRoots::_recorded_nms_lock;
 
 void ShenandoahCodeRoots::initialize() {
   _recorded_nms = new (ResourceObj::C_HEAP, mtGC) GrowableArray<ShenandoahNMethod*>(100, true, mtGC);
 }
 
 void ShenandoahCodeRoots::add_nmethod(nmethod* nm) {
-  assert(CodeCache_lock->owned_by_self(), "Must own CodeCache_lock");
   switch (ShenandoahCodeRootsStyle) {
     case 0:
     case 1:
       break;
     case 2: {
+      assert_locked_or_safepoint(CodeCache_lock);
+      ShenandoahLocker locker(CodeCache_lock->owned_by_self() ? NULL : &_recorded_nms_lock);
+
       ShenandoahNMethodOopDetector detector;
       nm->oops_do(&detector);
 
@@ -156,13 +160,15 @@
 };
 
 void ShenandoahCodeRoots::remove_nmethod(nmethod* nm) {
-  assert(CodeCache_lock->owned_by_self(), "Must own CodeCache_lock");
   switch (ShenandoahCodeRootsStyle) {
     case 0:
     case 1: {
       break;
     }
     case 2: {
+      assert_locked_or_safepoint(CodeCache_lock);
+      ShenandoahLocker locker(CodeCache_lock->owned_by_self() ? NULL : &_recorded_nms_lock);
+
       ShenandoahNMethodOopDetector detector;
       nm->oops_do(&detector, /* allow_zombie = */ true);
 
--- a/src/hotspot/share/gc/shenandoah/shenandoahCodeRoots.hpp	Wed May 29 23:36:36 2019 +0800
+++ b/src/hotspot/share/gc/shenandoah/shenandoahCodeRoots.hpp	Wed May 29 10:57:19 2019 -0400
@@ -26,6 +26,7 @@
 
 #include "code/codeCache.hpp"
 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
+#include "gc/shenandoah/shenandoahLock.hpp"
 #include "memory/allocation.hpp"
 #include "memory/iterator.hpp"
 
@@ -132,6 +133,7 @@
 
 private:
   static GrowableArray<ShenandoahNMethod*>* _recorded_nms;
+  static ShenandoahLock                     _recorded_nms_lock;
 };
 
 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCODEROOTS_HPP