src/hotspot/share/runtime/thread.cpp
changeset 54259 d25b24c70126
parent 54209 a8e7194c2b0d
child 54278 16999bd91ba6
equal deleted inserted replaced
54258:0db90e99f13b 54259:d25b24c70126
  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. Internal suspend requests do
  2267   // Check for pending external suspend.
  2268   // not use handle_special_runtime_exit_condition().
       
  2269   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2268   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2270   // thread is not the current thread. In older versions of jdbx, jdbx
  2269   // thread is not the current thread. In older versions of jdbx, jdbx
  2271   // threads could call into the VM with another thread's JNIEnv so we
  2270   // threads could call into the VM with another thread's JNIEnv so we
  2272   // can be here operating on behalf of a suspended thread (4432884).
  2271   // can be here operating on behalf of a suspended thread (4432884).
  2273   bool do_self_suspend = is_external_suspend_with_lock();
  2272   bool do_self_suspend = is_external_suspend_with_lock();
  2274   if (do_self_suspend && (!AllowJNIEnvProxy || this == JavaThread::current())) {
  2273   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 
       
  2302     frame_anchor()->make_walkable(this);
  2274     frame_anchor()->make_walkable(this);
  2303     java_suspend_self();
  2275     java_suspend_self_with_safepoint_check();
  2304 
  2276   }
  2305     // We might be here for reasons in addition to the self-suspend request
  2277 
  2306     // so check for other async requests.
  2278   // We might be here for reasons in addition to the self-suspend request
  2307   }
  2279   // so check for other async requests.
  2308 
       
  2309   if (check_asyncs) {
  2280   if (check_asyncs) {
  2310     check_and_handle_async_exceptions();
  2281     check_and_handle_async_exceptions();
  2311   }
  2282   }
  2312 
  2283 
  2313   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
  2284   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
  2422 //       thread. java_suspend_self() is the second stage of cooperative
  2393 //       thread. java_suspend_self() is the second stage of cooperative
  2423 //       suspension for external suspend requests and should only be used
  2394 //       suspension for external suspend requests and should only be used
  2424 //       to complete an external suspend request.
  2395 //       to complete an external suspend request.
  2425 //
  2396 //
  2426 int JavaThread::java_suspend_self() {
  2397 int JavaThread::java_suspend_self() {
       
  2398   assert(thread_state() == _thread_blocked, "wrong state for java_suspend_self()");
  2427   int ret = 0;
  2399   int ret = 0;
  2428 
  2400 
  2429   // we are in the process of exiting so don't suspend
  2401   // we are in the process of exiting so don't suspend
  2430   if (is_exiting()) {
  2402   if (is_exiting()) {
  2431     clear_external_suspend();
  2403     clear_external_suspend();
  2469   }
  2441   }
  2470 
  2442 
  2471   return ret;
  2443   return ret;
  2472 }
  2444 }
  2473 
  2445 
       
  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 // However, not all initial-states are allowed when performing a safepoint check, as we
       
  2458 // should never be blocking at a safepoint whilst in those states. Of these 'bad' states
       
  2459 // only _thread_in_native is possible when executing this code (based on our two callers).
       
  2460 // A thread that is _thread_in_native is already safepoint-safe and so it doesn't matter
       
  2461 // whether the VMThread sees the _thread_blocked state, or the _thread_in_native state,
       
  2462 // and so we don't need the explicit safepoint check.
       
  2463 
       
  2464 void JavaThread::java_suspend_self_with_safepoint_check() {
       
  2465   assert(this == Thread::current(), "invariant");
       
  2466   JavaThreadState state = thread_state();
       
  2467   set_thread_state(_thread_blocked);
       
  2468   java_suspend_self();
       
  2469   set_thread_state(state);
       
  2470   InterfaceSupport::serialize_thread_state_with_handler(this);
       
  2471   if (state != _thread_in_native) {
       
  2472     SafepointMechanism::block_if_requested(this);
       
  2473   }
       
  2474 }
       
  2475 
  2474 #ifdef ASSERT
  2476 #ifdef ASSERT
  2475 // Verify the JavaThread has not yet been published in the Threads::list, and
  2477 // Verify the JavaThread has not yet been published in the Threads::list, and
  2476 // hence doesn't need protection from concurrent access at this stage.
  2478 // hence doesn't need protection from concurrent access at this stage.
  2477 void JavaThread::verify_not_published() {
  2479 void JavaThread::verify_not_published() {
  2478   // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
  2480   // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
  2500   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2502   // If JNIEnv proxies are allowed, don't self-suspend if the target
  2501   // thread is not the current thread. In older versions of jdbx, jdbx
  2503   // thread is not the current thread. In older versions of jdbx, jdbx
  2502   // threads could call into the VM with another thread's JNIEnv so we
  2504   // threads could call into the VM with another thread's JNIEnv so we
  2503   // can be here operating on behalf of a suspended thread (4432884).
  2505   // can be here operating on behalf of a suspended thread (4432884).
  2504   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
  2506   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
  2505     JavaThreadState state = thread->thread_state();
  2507     thread->java_suspend_self_with_safepoint_check();
  2506 
  2508   } else {
  2507     // We mark this thread_blocked state as a suspend-equivalent so
  2509     SafepointMechanism::block_if_requested(curJT);
  2508     // that a caller to is_ext_suspend_completed() won't be confused.
  2510   }
  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);
       
  2531 
  2511 
  2532   if (thread->is_deopt_suspend()) {
  2512   if (thread->is_deopt_suspend()) {
  2533     thread->clear_deopt_suspend();
  2513     thread->clear_deopt_suspend();
  2534     RegisterMap map(thread, false);
  2514     RegisterMap map(thread, false);
  2535     frame f = thread->last_frame();
  2515     frame f = thread->last_frame();