2262 "must have handled the async condition, if no exception"); |
2262 "must have handled the async condition, if no exception"); |
2263 } |
2263 } |
2264 |
2264 |
2265 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) { |
2265 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) { |
2266 // |
2266 // |
2267 // Check for pending external suspend. |
2267 // Check for pending external suspend. Internal suspend requests do |
|
2268 // not use handle_special_runtime_exit_condition(). |
2268 // If JNIEnv proxies are allowed, don't self-suspend if the target |
2269 // If JNIEnv proxies are allowed, don't self-suspend if the target |
2269 // thread is not the current thread. In older versions of jdbx, jdbx |
2270 // thread is not the current thread. In older versions of jdbx, jdbx |
2270 // threads could call into the VM with another thread's JNIEnv so we |
2271 // threads could call into the VM with another thread's JNIEnv so we |
2271 // can be here operating on behalf of a suspended thread (4432884). |
2272 // can be here operating on behalf of a suspended thread (4432884). |
2272 bool do_self_suspend = is_external_suspend_with_lock(); |
2273 bool do_self_suspend = is_external_suspend_with_lock(); |
2273 if (do_self_suspend && (!AllowJNIEnvProxy || this == JavaThread::current())) { |
2274 if (do_self_suspend && (!AllowJNIEnvProxy || this == JavaThread::current())) { |
|
2275 // |
|
2276 // Because thread is external suspended the safepoint code will count |
|
2277 // thread as at a safepoint. This can be odd because we can be here |
|
2278 // as _thread_in_Java which would normally transition to _thread_blocked |
|
2279 // at a safepoint. We would like to mark the thread as _thread_blocked |
|
2280 // before calling java_suspend_self like all other callers of it but |
|
2281 // we must then observe proper safepoint protocol. (We can't leave |
|
2282 // _thread_blocked with a safepoint in progress). However we can be |
|
2283 // here as _thread_in_native_trans so we can't use a normal transition |
|
2284 // constructor/destructor pair because they assert on that type of |
|
2285 // transition. We could do something like: |
|
2286 // |
|
2287 // JavaThreadState state = thread_state(); |
|
2288 // set_thread_state(_thread_in_vm); |
|
2289 // { |
|
2290 // ThreadBlockInVM tbivm(this); |
|
2291 // java_suspend_self() |
|
2292 // } |
|
2293 // set_thread_state(_thread_in_vm_trans); |
|
2294 // if (safepoint) block; |
|
2295 // set_thread_state(state); |
|
2296 // |
|
2297 // but that is pretty messy. Instead we just go with the way the |
|
2298 // code has worked before and note that this is the only path to |
|
2299 // java_suspend_self that doesn't put the thread in _thread_blocked |
|
2300 // mode. |
|
2301 |
2274 frame_anchor()->make_walkable(this); |
2302 frame_anchor()->make_walkable(this); |
2275 java_suspend_self_with_safepoint_check(); |
2303 java_suspend_self(); |
2276 } |
2304 |
2277 |
2305 // We might be here for reasons in addition to the self-suspend request |
2278 // We might be here for reasons in addition to the self-suspend request |
2306 // so check for other async requests. |
2279 // so check for other async requests. |
2307 } |
|
2308 |
2280 if (check_asyncs) { |
2309 if (check_asyncs) { |
2281 check_and_handle_async_exceptions(); |
2310 check_and_handle_async_exceptions(); |
2282 } |
2311 } |
2283 |
2312 |
2284 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);) |
2313 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);) |
2393 // thread. java_suspend_self() is the second stage of cooperative |
2422 // thread. java_suspend_self() is the second stage of cooperative |
2394 // suspension for external suspend requests and should only be used |
2423 // suspension for external suspend requests and should only be used |
2395 // to complete an external suspend request. |
2424 // to complete an external suspend request. |
2396 // |
2425 // |
2397 int JavaThread::java_suspend_self() { |
2426 int JavaThread::java_suspend_self() { |
2398 assert(thread_state() == _thread_blocked, "wrong state for java_suspend_self()"); |
|
2399 int ret = 0; |
2427 int ret = 0; |
2400 |
2428 |
2401 // we are in the process of exiting so don't suspend |
2429 // we are in the process of exiting so don't suspend |
2402 if (is_exiting()) { |
2430 if (is_exiting()) { |
2403 clear_external_suspend(); |
2431 clear_external_suspend(); |
2441 } |
2469 } |
2442 |
2470 |
2443 return ret; |
2471 return ret; |
2444 } |
2472 } |
2445 |
2473 |
2446 // Helper routine to set up the correct thread state before calling java_suspend_self. |
|
2447 // This is called when regular thread-state transition helpers can't be used because |
|
2448 // we can be in various states, in particular _thread_in_native_trans. |
|
2449 // Because this thread is external suspended the safepoint code will count it as at |
|
2450 // a safepoint, regardless of what its actual current thread-state is. But |
|
2451 // is_ext_suspend_completed() may be waiting to see a thread transition from |
|
2452 // _thread_in_native_trans to _thread_blocked. So we set the thread state directly |
|
2453 // to _thread_blocked. The problem with setting thread state directly is that a |
|
2454 // safepoint could happen just after java_suspend_self() returns after being resumed, |
|
2455 // and the VM thread will see the _thread_blocked state. So we must check for a safepoint |
|
2456 // after restoring the state to make sure we won't leave while a safepoint is in progress. |
|
2457 void JavaThread::java_suspend_self_with_safepoint_check() { |
|
2458 assert(this == Thread::current(), "invariant"); |
|
2459 JavaThreadState state = thread_state(); |
|
2460 set_thread_state(_thread_blocked); |
|
2461 java_suspend_self(); |
|
2462 set_thread_state(state); |
|
2463 InterfaceSupport::serialize_thread_state_with_handler(this); |
|
2464 SafepointMechanism::block_if_requested(this); |
|
2465 } |
|
2466 |
|
2467 #ifdef ASSERT |
2474 #ifdef ASSERT |
2468 // Verify the JavaThread has not yet been published in the Threads::list, and |
2475 // Verify the JavaThread has not yet been published in the Threads::list, and |
2469 // hence doesn't need protection from concurrent access at this stage. |
2476 // hence doesn't need protection from concurrent access at this stage. |
2470 void JavaThread::verify_not_published() { |
2477 void JavaThread::verify_not_published() { |
2471 // Cannot create a ThreadsListHandle here and check !tlh.includes(this) |
2478 // Cannot create a ThreadsListHandle here and check !tlh.includes(this) |
2493 // If JNIEnv proxies are allowed, don't self-suspend if the target |
2500 // If JNIEnv proxies are allowed, don't self-suspend if the target |
2494 // thread is not the current thread. In older versions of jdbx, jdbx |
2501 // thread is not the current thread. In older versions of jdbx, jdbx |
2495 // threads could call into the VM with another thread's JNIEnv so we |
2502 // threads could call into the VM with another thread's JNIEnv so we |
2496 // can be here operating on behalf of a suspended thread (4432884). |
2503 // can be here operating on behalf of a suspended thread (4432884). |
2497 if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) { |
2504 if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) { |
2498 thread->java_suspend_self_with_safepoint_check(); |
2505 JavaThreadState state = thread->thread_state(); |
2499 } else { |
2506 |
2500 SafepointMechanism::block_if_requested(curJT); |
2507 // We mark this thread_blocked state as a suspend-equivalent so |
2501 } |
2508 // that a caller to is_ext_suspend_completed() won't be confused. |
|
2509 // The suspend-equivalent state is cleared by java_suspend_self(). |
|
2510 thread->set_suspend_equivalent(); |
|
2511 |
|
2512 // If the safepoint code sees the _thread_in_native_trans state, it will |
|
2513 // wait until the thread changes to other thread state. There is no |
|
2514 // guarantee on how soon we can obtain the SR_lock and complete the |
|
2515 // self-suspend request. It would be a bad idea to let safepoint wait for |
|
2516 // too long. Temporarily change the state to _thread_blocked to |
|
2517 // let the VM thread know that this thread is ready for GC. The problem |
|
2518 // of changing thread state is that safepoint could happen just after |
|
2519 // java_suspend_self() returns after being resumed, and VM thread will |
|
2520 // see the _thread_blocked state. We must check for safepoint |
|
2521 // after restoring the state and make sure we won't leave while a safepoint |
|
2522 // is in progress. |
|
2523 thread->set_thread_state(_thread_blocked); |
|
2524 thread->java_suspend_self(); |
|
2525 thread->set_thread_state(state); |
|
2526 |
|
2527 InterfaceSupport::serialize_thread_state_with_handler(thread); |
|
2528 } |
|
2529 |
|
2530 SafepointMechanism::block_if_requested(curJT); |
2502 |
2531 |
2503 if (thread->is_deopt_suspend()) { |
2532 if (thread->is_deopt_suspend()) { |
2504 thread->clear_deopt_suspend(); |
2533 thread->clear_deopt_suspend(); |
2505 RegisterMap map(thread, false); |
2534 RegisterMap map(thread, false); |
2506 frame f = thread->last_frame(); |
2535 frame f = thread->last_frame(); |