1134 if (mdo == NULL) return; |
1134 if (mdo == NULL) return; |
1135 // There is a benign race here. See comments in methodData.hpp. |
1135 // There is a benign race here. See comments in methodData.hpp. |
1136 mdo->inc_decompile_count(); |
1136 mdo->inc_decompile_count(); |
1137 } |
1137 } |
1138 |
1138 |
|
1139 bool nmethod::try_transition(int new_state_int) { |
|
1140 signed char new_state = new_state_int; |
|
1141 for (;;) { |
|
1142 signed char old_state = Atomic::load(&_state); |
|
1143 if (old_state >= new_state) { |
|
1144 // Ensure monotonicity of transitions. |
|
1145 return false; |
|
1146 } |
|
1147 if (Atomic::cmpxchg(new_state, &_state, old_state) == old_state) { |
|
1148 return true; |
|
1149 } |
|
1150 } |
|
1151 } |
|
1152 |
1139 void nmethod::make_unloaded() { |
1153 void nmethod::make_unloaded() { |
1140 post_compiled_method_unload(); |
1154 post_compiled_method_unload(); |
1141 |
1155 |
1142 // This nmethod is being unloaded, make sure that dependencies |
1156 // This nmethod is being unloaded, make sure that dependencies |
1143 // recorded in instanceKlasses get flushed. |
1157 // recorded in instanceKlasses get flushed. |
1157 p2i(this), p2i(_method)); |
1171 p2i(this), p2i(_method)); |
1158 ls.cr(); |
1172 ls.cr(); |
1159 } |
1173 } |
1160 // Unlink the osr method, so we do not look this up again |
1174 // Unlink the osr method, so we do not look this up again |
1161 if (is_osr_method()) { |
1175 if (is_osr_method()) { |
1162 // Invalidate the osr nmethod only once |
1176 // Invalidate the osr nmethod only once. Note that with concurrent |
|
1177 // code cache unloading, OSR nmethods are invalidated before they |
|
1178 // are made unloaded. Therefore, this becomes a no-op then. |
1163 if (is_in_use()) { |
1179 if (is_in_use()) { |
1164 invalidate_osr_method(); |
1180 invalidate_osr_method(); |
1165 } |
1181 } |
1166 #ifdef ASSERT |
1182 #ifdef ASSERT |
1167 if (method() != NULL) { |
1183 if (method() != NULL) { |
1211 assert(_method == NULL, "Tautology"); |
1227 assert(_method == NULL, "Tautology"); |
1212 |
1228 |
1213 set_osr_link(NULL); |
1229 set_osr_link(NULL); |
1214 NMethodSweeper::report_state_change(this); |
1230 NMethodSweeper::report_state_change(this); |
1215 |
1231 |
1216 // The release is only needed for compile-time ordering, as accesses |
1232 bool transition_success = try_transition(unloaded); |
1217 // into the nmethod after the store are not safe due to the sweeper |
1233 |
1218 // being allowed to free it when the store is observed, during |
1234 // It is an important invariant that there exists no race between |
1219 // concurrent nmethod unloading. Therefore, there is no need for |
1235 // the sweeper and GC thread competing for making the same nmethod |
1220 // acquire on the loader side. |
1236 // zombie and unloaded respectively. This is ensured by |
1221 OrderAccess::release_store(&_state, (signed char)unloaded); |
1237 // can_convert_to_zombie() returning false for any is_unloading() |
|
1238 // nmethod, informing the sweeper not to step on any GC toes. |
|
1239 assert(transition_success, "Invalid nmethod transition to unloaded"); |
1222 |
1240 |
1223 #if INCLUDE_JVMCI |
1241 #if INCLUDE_JVMCI |
1224 // Clear the link between this nmethod and a HotSpotNmethod mirror |
1242 // Clear the link between this nmethod and a HotSpotNmethod mirror |
1225 JVMCINMethodData* nmethod_data = jvmci_nmethod_data(); |
1243 JVMCINMethodData* nmethod_data = jvmci_nmethod_data(); |
1226 if (nmethod_data != NULL) { |
1244 if (nmethod_data != NULL) { |
1281 */ |
1299 */ |
1282 bool nmethod::make_not_entrant_or_zombie(int state) { |
1300 bool nmethod::make_not_entrant_or_zombie(int state) { |
1283 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant"); |
1301 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant"); |
1284 assert(!is_zombie(), "should not already be a zombie"); |
1302 assert(!is_zombie(), "should not already be a zombie"); |
1285 |
1303 |
1286 if (_state == state) { |
1304 if (Atomic::load(&_state) >= state) { |
1287 // Avoid taking the lock if already in required state. |
1305 // Avoid taking the lock if already in required state. |
1288 // This is safe from races because the state is an end-state, |
1306 // This is safe from races because the state is an end-state, |
1289 // which the nmethod cannot back out of once entered. |
1307 // which the nmethod cannot back out of once entered. |
1290 // No need for fencing either. |
1308 // No need for fencing either. |
1291 return false; |
1309 return false; |
1316 } |
1334 } |
1317 |
1335 |
1318 // Enter critical section. Does not block for safepoint. |
1336 // Enter critical section. Does not block for safepoint. |
1319 MutexLocker pl(Patching_lock, Mutex::_no_safepoint_check_flag); |
1337 MutexLocker pl(Patching_lock, Mutex::_no_safepoint_check_flag); |
1320 |
1338 |
1321 if (_state == state) { |
1339 if (Atomic::load(&_state) >= state) { |
1322 // another thread already performed this transition so nothing |
1340 // another thread already performed this transition so nothing |
1323 // to do, but return false to indicate this. |
1341 // to do, but return false to indicate this. |
1324 return false; |
1342 return false; |
1325 } |
1343 } |
1326 |
1344 |
1352 mark_as_seen_on_stack(); |
1370 mark_as_seen_on_stack(); |
1353 OrderAccess::storestore(); // _stack_traversal_mark and _state |
1371 OrderAccess::storestore(); // _stack_traversal_mark and _state |
1354 } |
1372 } |
1355 |
1373 |
1356 // Change state |
1374 // Change state |
1357 _state = state; |
1375 if (!try_transition(state)) { |
|
1376 // If the transition fails, it is due to another thread making the nmethod more |
|
1377 // dead. In particular, one thread might be making the nmethod unloaded concurrently. |
|
1378 // If so, having patched in the jump in the verified entry unnecessarily is fine. |
|
1379 // The nmethod is no longer possible to call by Java threads. |
|
1380 // Incrementing the decompile count is also fine as the caller of make_not_entrant() |
|
1381 // had a valid reason to deoptimize the nmethod. |
|
1382 // Marking the nmethod as seen on stack also has no effect, as the nmethod is now |
|
1383 // !is_alive(), and the seen on stack value is only used to convert not_entrant |
|
1384 // nmethods to zombie in can_convert_to_zombie(). |
|
1385 return false; |
|
1386 } |
1358 |
1387 |
1359 // Log the transition once |
1388 // Log the transition once |
1360 log_state_change(); |
1389 log_state_change(); |
1361 |
1390 |
1362 // Remove nmethod from method. |
1391 // Remove nmethod from method. |