< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page




2247       JavaThread* THREAD = this;
2248       THROW_MSG(vmSymbols::java_lang_InternalError(), "a fault occurred in an unsafe memory access operation");
2249     }
2250     case _thread_in_Java: {
2251       ThreadInVMfromJava tiv(this);
2252       JavaThread* THREAD = this;
2253       THROW_MSG(vmSymbols::java_lang_InternalError(), "a fault occurred in a recent unsafe memory access operation in compiled Java code");
2254     }
2255     default:
2256       ShouldNotReachHere();
2257     }
2258   }
2259 
2260   assert(condition == _no_async_condition || has_pending_exception() ||
2261          (!check_unsafe_error && condition == _async_unsafe_access_error),
2262          "must have handled the async condition, if no exception");
2263 }
2264 
2265 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2266   //
2267   // Check for pending external suspend.

2268   // 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   // 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   bool do_self_suspend = is_external_suspend_with_lock();
2273   if (do_self_suspend && (!AllowJNIEnvProxy || this == JavaThread::current())) {



























2274     frame_anchor()->make_walkable(this);
2275     java_suspend_self_with_safepoint_check();
2276   }
2277 
2278   // We might be here for reasons in addition to the self-suspend request
2279   // so check for other async requests.


2280   if (check_asyncs) {
2281     check_and_handle_async_exceptions();
2282   }
2283 
2284   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2285 }
2286 
2287 void JavaThread::send_thread_stop(oop java_throwable)  {
2288   assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2289   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2290   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2291 
2292   // Do not throw asynchronous exceptions against the compiler thread
2293   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2294   if (!can_call_java()) return;
2295 
2296   {
2297     // Actually throw the Throwable against the target Thread - however
2298     // only if there is no thread death exception installed already.
2299     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {


2378   } else {
2379     VM_ThreadSuspend vm_suspend;
2380     VMThread::execute(&vm_suspend);
2381   }
2382 }
2383 
2384 // Part II of external suspension.
2385 // A JavaThread self suspends when it detects a pending external suspend
2386 // request. This is usually on transitions. It is also done in places
2387 // where continuing to the next transition would surprise the caller,
2388 // e.g., monitor entry.
2389 //
2390 // Returns the number of times that the thread self-suspended.
2391 //
2392 // Note: DO NOT call java_suspend_self() when you just want to block current
2393 //       thread. java_suspend_self() is the second stage of cooperative
2394 //       suspension for external suspend requests and should only be used
2395 //       to complete an external suspend request.
2396 //
2397 int JavaThread::java_suspend_self() {
2398   assert(thread_state() == _thread_blocked, "wrong state for java_suspend_self()");
2399   int ret = 0;
2400 
2401   // we are in the process of exiting so don't suspend
2402   if (is_exiting()) {
2403     clear_external_suspend();
2404     return ret;
2405   }
2406 
2407   assert(_anchor.walkable() ||
2408          (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()),
2409          "must have walkable stack");
2410 
2411   MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2412 
2413   assert(!this->is_ext_suspended(),
2414          "a thread trying to self-suspend should not already be suspended");
2415 
2416   if (this->is_suspend_equivalent()) {
2417     // If we are self-suspending as a result of the lifting of a
2418     // suspend equivalent condition, then the suspend_equivalent


2426   // above. Or another external suspend request could be waiting for us
2427   // by the time we return from SR_lock()->wait(). The thread
2428   // that requested the suspension may already be trying to walk our
2429   // stack and if we return now, we can change the stack out from under
2430   // it. This would be a "bad thing (TM)" and cause the stack walker
2431   // to crash. We stay self-suspended until there are no more pending
2432   // external suspend requests.
2433   while (is_external_suspend()) {
2434     ret++;
2435     this->set_ext_suspended();
2436 
2437     // _ext_suspended flag is cleared by java_resume()
2438     while (is_ext_suspended()) {
2439       this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
2440     }
2441   }
2442 
2443   return ret;
2444 }
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 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
2468 // 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.
2470 void JavaThread::verify_not_published() {
2471   // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2472   // since an unpublished JavaThread doesn't participate in the
2473   // Thread-SMR protocol for keeping a ThreadsList alive.
2474   assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2475 }
2476 #endif
2477 
2478 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2479 // progress or when _suspend_flags is non-zero.
2480 // Current thread needs to self-suspend if there is a suspend request and/or
2481 // block if a safepoint is in progress.
2482 // Async exception ISN'T checked.
2483 // Note only the ThreadInVMfromNative transition can call this function
2484 // directly and when thread state is _thread_in_native_trans
2485 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2486   assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2487 
2488   JavaThread *curJT = JavaThread::current();
2489   bool do_self_suspend = thread->is_external_suspend();
2490 
2491   assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2492 
2493   // 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
2495   // 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).
2497   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
2498     thread->java_suspend_self_with_safepoint_check();
2499   } else {
2500     SafepointMechanism::block_if_requested(curJT);




















2501   }


2502 
2503   if (thread->is_deopt_suspend()) {
2504     thread->clear_deopt_suspend();
2505     RegisterMap map(thread, false);
2506     frame f = thread->last_frame();
2507     while (f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2508       f = f.sender(&map);
2509     }
2510     if (f.id() == thread->must_deopt_id()) {
2511       thread->clear_must_deopt_id();
2512       f.deoptimize(thread);
2513     } else {
2514       fatal("missed deoptimization!");
2515     }
2516   }
2517 
2518   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2519 }
2520 
2521 // Slow path when the native==>VM/Java barriers detect a safepoint is in




2247       JavaThread* THREAD = this;
2248       THROW_MSG(vmSymbols::java_lang_InternalError(), "a fault occurred in an unsafe memory access operation");
2249     }
2250     case _thread_in_Java: {
2251       ThreadInVMfromJava tiv(this);
2252       JavaThread* THREAD = this;
2253       THROW_MSG(vmSymbols::java_lang_InternalError(), "a fault occurred in a recent unsafe memory access operation in compiled Java code");
2254     }
2255     default:
2256       ShouldNotReachHere();
2257     }
2258   }
2259 
2260   assert(condition == _no_async_condition || has_pending_exception() ||
2261          (!check_unsafe_error && condition == _async_unsafe_access_error),
2262          "must have handled the async condition, if no exception");
2263 }
2264 
2265 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2266   //
2267   // Check for pending external suspend. Internal suspend requests do
2268   // not use handle_special_runtime_exit_condition().
2269   // 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
2271   // 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).
2273   bool do_self_suspend = is_external_suspend_with_lock();
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 
2302     frame_anchor()->make_walkable(this);
2303     java_suspend_self();

2304 
2305     // We might be here for reasons in addition to the self-suspend request
2306     // so check for other async requests.
2307   }
2308 
2309   if (check_asyncs) {
2310     check_and_handle_async_exceptions();
2311   }
2312 
2313   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2314 }
2315 
2316 void JavaThread::send_thread_stop(oop java_throwable)  {
2317   assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2318   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2319   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2320 
2321   // Do not throw asynchronous exceptions against the compiler thread
2322   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2323   if (!can_call_java()) return;
2324 
2325   {
2326     // Actually throw the Throwable against the target Thread - however
2327     // only if there is no thread death exception installed already.
2328     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {


2407   } else {
2408     VM_ThreadSuspend vm_suspend;
2409     VMThread::execute(&vm_suspend);
2410   }
2411 }
2412 
2413 // Part II of external suspension.
2414 // A JavaThread self suspends when it detects a pending external suspend
2415 // request. This is usually on transitions. It is also done in places
2416 // where continuing to the next transition would surprise the caller,
2417 // e.g., monitor entry.
2418 //
2419 // Returns the number of times that the thread self-suspended.
2420 //
2421 // Note: DO NOT call java_suspend_self() when you just want to block current
2422 //       thread. java_suspend_self() is the second stage of cooperative
2423 //       suspension for external suspend requests and should only be used
2424 //       to complete an external suspend request.
2425 //
2426 int JavaThread::java_suspend_self() {

2427   int ret = 0;
2428 
2429   // we are in the process of exiting so don't suspend
2430   if (is_exiting()) {
2431     clear_external_suspend();
2432     return ret;
2433   }
2434 
2435   assert(_anchor.walkable() ||
2436          (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()),
2437          "must have walkable stack");
2438 
2439   MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
2440 
2441   assert(!this->is_ext_suspended(),
2442          "a thread trying to self-suspend should not already be suspended");
2443 
2444   if (this->is_suspend_equivalent()) {
2445     // If we are self-suspending as a result of the lifting of a
2446     // suspend equivalent condition, then the suspend_equivalent


2454   // above. Or another external suspend request could be waiting for us
2455   // by the time we return from SR_lock()->wait(). The thread
2456   // that requested the suspension may already be trying to walk our
2457   // stack and if we return now, we can change the stack out from under
2458   // it. This would be a "bad thing (TM)" and cause the stack walker
2459   // to crash. We stay self-suspended until there are no more pending
2460   // external suspend requests.
2461   while (is_external_suspend()) {
2462     ret++;
2463     this->set_ext_suspended();
2464 
2465     // _ext_suspended flag is cleared by java_resume()
2466     while (is_ext_suspended()) {
2467       this->SR_lock()->wait(Mutex::_no_safepoint_check_flag);
2468     }
2469   }
2470 
2471   return ret;
2472 }
2473 





















2474 #ifdef ASSERT
2475 // 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.
2477 void JavaThread::verify_not_published() {
2478   // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2479   // since an unpublished JavaThread doesn't participate in the
2480   // Thread-SMR protocol for keeping a ThreadsList alive.
2481   assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2482 }
2483 #endif
2484 
2485 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2486 // progress or when _suspend_flags is non-zero.
2487 // Current thread needs to self-suspend if there is a suspend request and/or
2488 // block if a safepoint is in progress.
2489 // Async exception ISN'T checked.
2490 // Note only the ThreadInVMfromNative transition can call this function
2491 // directly and when thread state is _thread_in_native_trans
2492 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2493   assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2494 
2495   JavaThread *curJT = JavaThread::current();
2496   bool do_self_suspend = thread->is_external_suspend();
2497 
2498   assert(!curJT->has_last_Java_frame() || curJT->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2499 
2500   // 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
2502   // 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).
2504   if (do_self_suspend && (!AllowJNIEnvProxy || curJT == thread)) {
2505     JavaThreadState state = thread->thread_state();
2506 
2507     // We mark this thread_blocked state as a suspend-equivalent so
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);
2531 
2532   if (thread->is_deopt_suspend()) {
2533     thread->clear_deopt_suspend();
2534     RegisterMap map(thread, false);
2535     frame f = thread->last_frame();
2536     while (f.id() != thread->must_deopt_id() && ! f.is_first_frame()) {
2537       f = f.sender(&map);
2538     }
2539     if (f.id() == thread->must_deopt_id()) {
2540       thread->clear_must_deopt_id();
2541       f.deoptimize(thread);
2542     } else {
2543       fatal("missed deoptimization!");
2544     }
2545   }
2546 
2547   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2548 }
2549 
2550 // Slow path when the native==>VM/Java barriers detect a safepoint is in


< prev index next >