src/hotspot/share/code/nmethod.cpp
changeset 55005 9b70ebd131b4
parent 54960 e46fe26d7f77
child 55068 f6260463dbe4
equal deleted inserted replaced
55004:4645b6d57f54 55005:9b70ebd131b4
    48 #include "oops/method.inline.hpp"
    48 #include "oops/method.inline.hpp"
    49 #include "oops/methodData.hpp"
    49 #include "oops/methodData.hpp"
    50 #include "oops/oop.inline.hpp"
    50 #include "oops/oop.inline.hpp"
    51 #include "prims/jvmtiImpl.hpp"
    51 #include "prims/jvmtiImpl.hpp"
    52 #include "runtime/atomic.hpp"
    52 #include "runtime/atomic.hpp"
       
    53 #include "runtime/deoptimization.hpp"
    53 #include "runtime/flags/flagSetting.hpp"
    54 #include "runtime/flags/flagSetting.hpp"
    54 #include "runtime/frame.inline.hpp"
    55 #include "runtime/frame.inline.hpp"
    55 #include "runtime/handles.inline.hpp"
    56 #include "runtime/handles.inline.hpp"
    56 #include "runtime/jniHandles.inline.hpp"
    57 #include "runtime/jniHandles.inline.hpp"
    57 #include "runtime/orderAccess.hpp"
    58 #include "runtime/orderAccess.hpp"
  1174   // If _method is already NULL the Method* is about to be unloaded,
  1175   // If _method is already NULL the Method* is about to be unloaded,
  1175   // so we don't have to break the cycle. Note that it is possible to
  1176   // so we don't have to break the cycle. Note that it is possible to
  1176   // have the Method* live here, in case we unload the nmethod because
  1177   // have the Method* live here, in case we unload the nmethod because
  1177   // it is pointing to some oop (other than the Method*) being unloaded.
  1178   // it is pointing to some oop (other than the Method*) being unloaded.
  1178   if (_method != NULL) {
  1179   if (_method != NULL) {
  1179     // OSR methods point to the Method*, but the Method* does not
  1180     _method->unlink_code(this);
  1180     // point back!
       
  1181     if (_method->code() == this) {
       
  1182       _method->clear_code(); // Break a cycle
       
  1183     }
       
  1184   }
  1181   }
  1185 
  1182 
  1186   // Make the class unloaded - i.e., change state and notify sweeper
  1183   // Make the class unloaded - i.e., change state and notify sweeper
  1187   assert(SafepointSynchronize::is_at_safepoint() || Thread::current()->is_ConcurrentGC_thread(),
  1184   assert(SafepointSynchronize::is_at_safepoint() || Thread::current()->is_ConcurrentGC_thread(),
  1188          "must be at safepoint");
  1185          "must be at safepoint");
  1260   if (PrintCompilation && _state != unloaded) {
  1257   if (PrintCompilation && _state != unloaded) {
  1261     print_on(tty, state_msg);
  1258     print_on(tty, state_msg);
  1262   }
  1259   }
  1263 }
  1260 }
  1264 
  1261 
  1265 void nmethod::unlink_from_method(bool acquire_lock) {
  1262 void nmethod::unlink_from_method() {
  1266   // We need to check if both the _code and _from_compiled_code_entry_point
  1263   if (method() != NULL) {
  1267   // refer to this nmethod because there is a race in setting these two fields
  1264     method()->unlink_code();
  1268   // in Method* as seen in bugid 4947125.
       
  1269   // If the vep() points to the zombie nmethod, the memory for the nmethod
       
  1270   // could be flushed and the compiler and vtable stubs could still call
       
  1271   // through it.
       
  1272   if (method() != NULL && (method()->code() == this ||
       
  1273                            method()->from_compiled_entry() == verified_entry_point())) {
       
  1274     method()->clear_code(acquire_lock);
       
  1275   }
  1265   }
  1276 }
  1266 }
  1277 
  1267 
  1278 /**
  1268 /**
  1279  * Common functionality for both make_not_entrant and make_zombie
  1269  * Common functionality for both make_not_entrant and make_zombie
  1296   // This can be called while the system is already at a safepoint which is ok
  1286   // This can be called while the system is already at a safepoint which is ok
  1297   NoSafepointVerifier nsv(true, !SafepointSynchronize::is_at_safepoint());
  1287   NoSafepointVerifier nsv(true, !SafepointSynchronize::is_at_safepoint());
  1298 
  1288 
  1299   // during patching, depending on the nmethod state we must notify the GC that
  1289   // during patching, depending on the nmethod state we must notify the GC that
  1300   // code has been unloaded, unregistering it. We cannot do this right while
  1290   // code has been unloaded, unregistering it. We cannot do this right while
  1301   // holding the Patching_lock because we need to use the CodeCache_lock. This
  1291   // holding the CompiledMethod_lock because we need to use the CodeCache_lock. This
  1302   // would be prone to deadlocks.
  1292   // would be prone to deadlocks.
  1303   // This flag is used to remember whether we need to later lock and unregister.
  1293   // This flag is used to remember whether we need to later lock and unregister.
  1304   bool nmethod_needs_unregister = false;
  1294   bool nmethod_needs_unregister = false;
  1305 
  1295 
       
  1296   // invalidate osr nmethod before acquiring the patching lock since
       
  1297   // they both acquire leaf locks and we don't want a deadlock.
       
  1298   // This logic is equivalent to the logic below for patching the
       
  1299   // verified entry point of regular methods. We check that the
       
  1300   // nmethod is in use to ensure that it is invalidated only once.
       
  1301   if (is_osr_method() && is_in_use()) {
       
  1302     // this effectively makes the osr nmethod not entrant
       
  1303     invalidate_osr_method();
       
  1304   }
       
  1305 
  1306   {
  1306   {
  1307     // invalidate osr nmethod before acquiring the patching lock since
       
  1308     // they both acquire leaf locks and we don't want a deadlock.
       
  1309     // This logic is equivalent to the logic below for patching the
       
  1310     // verified entry point of regular methods. We check that the
       
  1311     // nmethod is in use to ensure that it is invalidated only once.
       
  1312     if (is_osr_method() && is_in_use()) {
       
  1313       // this effectively makes the osr nmethod not entrant
       
  1314       invalidate_osr_method();
       
  1315     }
       
  1316 
       
  1317     // Enter critical section.  Does not block for safepoint.
  1307     // Enter critical section.  Does not block for safepoint.
  1318     MutexLocker pl(Patching_lock, Mutex::_no_safepoint_check_flag);
  1308     MutexLocker pl(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
  1319 
  1309 
  1320     if (_state == state) {
  1310     if (_state == state) {
  1321       // another thread already performed this transition so nothing
  1311       // another thread already performed this transition so nothing
  1322       // to do, but return false to indicate this.
  1312       // to do, but return false to indicate this.
  1323       return false;
  1313       return false;
  1357 
  1347 
  1358     // Log the transition once
  1348     // Log the transition once
  1359     log_state_change();
  1349     log_state_change();
  1360 
  1350 
  1361     // Remove nmethod from method.
  1351     // Remove nmethod from method.
  1362     unlink_from_method(false /* already owns Patching_lock */);
  1352     unlink_from_method();
  1363   } // leave critical region under Patching_lock
  1353 
       
  1354   } // leave critical region under CompiledMethod_lock
  1364 
  1355 
  1365 #if INCLUDE_JVMCI
  1356 #if INCLUDE_JVMCI
  1366   // Invalidate can't occur while holding the Patching lock
  1357   // Invalidate can't occur while holding the Patching lock
  1367   JVMCINMethodData* nmethod_data = jvmci_nmethod_data();
  1358   JVMCINMethodData* nmethod_data = jvmci_nmethod_data();
  1368   if (nmethod_data != NULL) {
  1359   if (nmethod_data != NULL) {