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