2367
2368 assert(condition == _no_async_condition || has_pending_exception() ||
2369 (!check_unsafe_error && condition == _async_unsafe_access_error),
2370 "must have handled the async condition, if no exception");
2371 }
2372
2373 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2374
2375 // Check for pending external suspend.
2376 if (is_external_suspend_with_lock()) {
2377 frame_anchor()->make_walkable(this);
2378 java_suspend_self_with_safepoint_check();
2379 }
2380
2381 // We might be here for reasons in addition to the self-suspend request
2382 // so check for other async requests.
2383 if (check_asyncs) {
2384 check_and_handle_async_exceptions();
2385 }
2386
2387 if (is_ea_obj_deopt_suspend()) {
2388 frame_anchor()->make_walkable(this);
2389 wait_for_object_deoptimization();
2390 }
2391
2392 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2393 }
2394
2395 void JavaThread::send_thread_stop(oop java_throwable) {
2396 ResourceMark rm;
2397 assert(Thread::current()->is_VM_thread() || Thread::current() == this, "should be in the vm thread");
2398
2399 // Do not throw asynchronous exceptions against the compiler thread
2400 // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2401 if (!can_call_java()) return;
2402
2403 {
2404 // Actually throw the Throwable against the target Thread - however
2405 // only if there is no thread death exception installed already.
2406 if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2407 // If the topmost frame is a runtime stub, then we are calling into
2569
2570 void JavaThread::java_suspend_self_with_safepoint_check() {
2571 assert(this == Thread::current(), "invariant");
2572 JavaThreadState state = thread_state();
2573 set_thread_state(_thread_blocked);
2574 java_suspend_self();
2575 set_thread_state_fence(state);
2576 // Since we are not using a regular thread-state transition helper here,
2577 // we must manually emit the instruction barrier after leaving a safe state.
2578 OrderAccess::cross_modify_fence();
2579 if (state != _thread_in_native) {
2580 SafepointMechanism::block_if_requested(this);
2581 }
2582 }
2583
2584 void JavaThread::wait_for_object_deoptimization() {
2585 assert(!has_last_Java_frame() || frame_anchor()->walkable(), "should have walkable stack");
2586 assert(this == Thread::current(), "invariant");
2587 JavaThreadState state = thread_state();
2588
2589 do {
2590 set_thread_state(_thread_blocked);
2591 set_suspend_equivalent();
2592 MonitorLocker ml(this, EscapeBarrier_lock, Monitor::_no_safepoint_check_flag);
2593 if (is_ea_obj_deopt_suspend()) {
2594 ml.wait();
2595 }
2596 if (handle_special_suspend_equivalent_condition()) {
2597 MutexUnlocker mu(EscapeBarrier_lock, Monitor::_no_safepoint_check_flag);
2598 java_suspend_self();
2599 }
2600 set_thread_state_fence(state);
2601 } while (is_ea_obj_deopt_suspend());
2602
2603 // Since we are not using a regular thread-state transition helper here,
2604 // we must manually emit the instruction barrier after leaving a safe state.
2605 OrderAccess::cross_modify_fence();
2606 if (state != _thread_in_native) {
2607 SafepointMechanism::block_if_requested(this);
2608 }
2609 }
2610
2611 #ifdef ASSERT
2612 // Verify the JavaThread has not yet been published in the Threads::list, and
2613 // hence doesn't need protection from concurrent access at this stage.
2614 void JavaThread::verify_not_published() {
2615 // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2616 // since an unpublished JavaThread doesn't participate in the
2617 // Thread-SMR protocol for keeping a ThreadsList alive.
2618 assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2619 }
2620 #endif
2621
2622 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2623 // progress or when _suspend_flags is non-zero.
2624 // Current thread needs to self-suspend if there is a suspend request and/or
2625 // block if a safepoint is in progress.
2626 // Async exception ISN'T checked.
2627 // Note only the ThreadInVMfromNative transition can call this function
2628 // directly and when thread state is _thread_in_native_trans
2629 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2630 assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2631
2632 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2633
2634 if (thread->is_external_suspend()) {
2635 thread->java_suspend_self_with_safepoint_check();
2636 } else {
2637 SafepointMechanism::block_if_requested(thread);
2638 }
2639
2640 if (thread->is_ea_obj_deopt_suspend()) {
2641 thread->wait_for_object_deoptimization();
2642 }
2643
2644 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2645 }
2646
2647 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2648 // progress or when _suspend_flags is non-zero.
2649 // Current thread needs to self-suspend if there is a suspend request and/or
2650 // block if a safepoint is in progress.
2651 // Also check for pending async exception (not including unsafe access error).
2652 // Note only the native==>VM/Java barriers can call this function and when
2653 // thread state is _thread_in_native_trans.
2654 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2655 check_safepoint_and_suspend_for_native_trans(thread);
2656
2657 if (thread->has_async_exception()) {
2658 // We are in _thread_in_native_trans state, don't handle unsafe
2659 // access error since that may block.
2660 thread->check_and_handle_async_exceptions(false);
4599 // Maintain fast thread list
4600 ThreadsSMRSupport::remove_thread(p);
4601
4602 _number_of_threads--;
4603 if (!is_daemon) {
4604 _number_of_non_daemon_threads--;
4605
4606 // Only one thread left, do a notify on the Threads_lock so a thread waiting
4607 // on destroy_vm will wake up.
4608 if (number_of_non_daemon_threads() == 1) {
4609 ml.notify_all();
4610 }
4611 }
4612 ThreadService::remove_thread(p, is_daemon);
4613
4614 // Make sure that safepoint code disregard this thread. This is needed since
4615 // the thread might mess around with locks after this point. This can cause it
4616 // to do callbacks into the safepoint code. However, the safepoint code is not aware
4617 // of this thread since it is removed from the queue.
4618 p->set_terminated_value();
4619 } // unlock Threads_lock
4620
4621 // Since Events::log uses a lock, we grab it outside the Threads_lock
4622 Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
4623 }
4624
4625 // Operations on the Threads list for GC. These are not explicitly locked,
4626 // but the garbage collector must provide a safe context for them to run.
4627 // In particular, these things should never be called when the Threads_lock
4628 // is held by some other thread. (Note: the Safepoint abstraction also
4629 // uses the Threads_lock to guarantee this property. It also makes sure that
4630 // all threads gets blocked when exiting or starting).
4631
4632 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) {
4633 ALL_JAVA_THREADS(p) {
4634 p->oops_do(f, cf);
4635 }
4636 VMThread::vm_thread()->oops_do(f, cf);
4637 }
4638
|
2367
2368 assert(condition == _no_async_condition || has_pending_exception() ||
2369 (!check_unsafe_error && condition == _async_unsafe_access_error),
2370 "must have handled the async condition, if no exception");
2371 }
2372
2373 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2374
2375 // Check for pending external suspend.
2376 if (is_external_suspend_with_lock()) {
2377 frame_anchor()->make_walkable(this);
2378 java_suspend_self_with_safepoint_check();
2379 }
2380
2381 // We might be here for reasons in addition to the self-suspend request
2382 // so check for other async requests.
2383 if (check_asyncs) {
2384 check_and_handle_async_exceptions();
2385 }
2386
2387 if (is_obj_deopt_suspend()) {
2388 frame_anchor()->make_walkable(this);
2389 wait_for_object_deoptimization();
2390 }
2391
2392 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2393 }
2394
2395 void JavaThread::send_thread_stop(oop java_throwable) {
2396 ResourceMark rm;
2397 assert(Thread::current()->is_VM_thread() || Thread::current() == this, "should be in the vm thread");
2398
2399 // Do not throw asynchronous exceptions against the compiler thread
2400 // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2401 if (!can_call_java()) return;
2402
2403 {
2404 // Actually throw the Throwable against the target Thread - however
2405 // only if there is no thread death exception installed already.
2406 if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2407 // If the topmost frame is a runtime stub, then we are calling into
2569
2570 void JavaThread::java_suspend_self_with_safepoint_check() {
2571 assert(this == Thread::current(), "invariant");
2572 JavaThreadState state = thread_state();
2573 set_thread_state(_thread_blocked);
2574 java_suspend_self();
2575 set_thread_state_fence(state);
2576 // Since we are not using a regular thread-state transition helper here,
2577 // we must manually emit the instruction barrier after leaving a safe state.
2578 OrderAccess::cross_modify_fence();
2579 if (state != _thread_in_native) {
2580 SafepointMechanism::block_if_requested(this);
2581 }
2582 }
2583
2584 void JavaThread::wait_for_object_deoptimization() {
2585 assert(!has_last_Java_frame() || frame_anchor()->walkable(), "should have walkable stack");
2586 assert(this == Thread::current(), "invariant");
2587 JavaThreadState state = thread_state();
2588
2589 bool should_spin_wait = true;
2590 do {
2591 set_thread_state(_thread_blocked);
2592 set_suspend_equivalent();
2593 {
2594 MonitorLocker ml(this, EscapeBarrier_lock, Monitor::_no_safepoint_check_flag);
2595 if (EscapeBarrier::deoptimizing_objects_for_all_threads() ||
2596 (is_obj_deopt_suspend() && !should_spin_wait)) {
2597 ml.wait();
2598 }
2599 should_spin_wait = should_spin_wait && is_obj_deopt_suspend();
2600 }
2601 // Single deoptimization is typically very short. Microbenchmarks
2602 // showed 5% better performance when spinning
2603 if (should_spin_wait) {
2604 // Inspired by HandshakeSpinYield
2605 const jlong max_spin_time_ns = 100 /* us */ * (NANOUNITS / MICROUNITS);
2606 const int free_cpus = os::active_processor_count() - 1;
2607 jlong spin_time_ns = (5 /* us */ * (NANOUNITS / MICROUNITS)) * free_cpus; // zero on UP
2608 spin_time_ns = spin_time_ns > max_spin_time_ns ? max_spin_time_ns : spin_time_ns;
2609 jlong spin_start = os::javaTimeNanos();
2610 while (is_obj_deopt_suspend()) {
2611 os::naked_yield();
2612 if ((os::javaTimeNanos() - spin_start) > spin_time_ns) {
2613 should_spin_wait = false;
2614 break;
2615 }
2616 }
2617 }
2618 set_thread_state_fence(state);
2619
2620 if (handle_special_suspend_equivalent_condition()) {
2621 java_suspend_self_with_safepoint_check();
2622 }
2623
2624 // Since we are not using a regular thread-state transition helper here,
2625 // we must manually emit the instruction barrier after leaving a safe state.
2626 OrderAccess::cross_modify_fence();
2627 if (state != _thread_in_native) {
2628 SafepointMechanism::block_if_requested(this);
2629 }
2630
2631 // Check for another deopt suspend _after_ checking for safepont/handshake,
2632 // or otherwise a stale value can be seen if the flag was changed with a
2633 // handshake while the current thread was _thread_blocked above.
2634 } while (is_obj_deopt_suspend());
2635 }
2636
2637 #ifdef ASSERT
2638 // Verify the JavaThread has not yet been published in the Threads::list, and
2639 // hence doesn't need protection from concurrent access at this stage.
2640 void JavaThread::verify_not_published() {
2641 // Cannot create a ThreadsListHandle here and check !tlh.includes(this)
2642 // since an unpublished JavaThread doesn't participate in the
2643 // Thread-SMR protocol for keeping a ThreadsList alive.
2644 assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
2645 }
2646 #endif
2647
2648 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2649 // progress or when _suspend_flags is non-zero.
2650 // Current thread needs to self-suspend if there is a suspend request and/or
2651 // block if a safepoint is in progress.
2652 // Async exception ISN'T checked.
2653 // Note only the ThreadInVMfromNative transition can call this function
2654 // directly and when thread state is _thread_in_native_trans
2655 void JavaThread::check_safepoint_and_suspend_for_native_trans(JavaThread *thread) {
2656 assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
2657
2658 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "Unwalkable stack in native->vm transition");
2659
2660 if (thread->is_external_suspend()) {
2661 thread->java_suspend_self_with_safepoint_check();
2662 } else {
2663 SafepointMechanism::block_if_requested(thread);
2664 }
2665
2666 if (thread->is_obj_deopt_suspend()) {
2667 thread->wait_for_object_deoptimization();
2668 }
2669
2670 JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(thread);)
2671 }
2672
2673 // Slow path when the native==>VM/Java barriers detect a safepoint is in
2674 // progress or when _suspend_flags is non-zero.
2675 // Current thread needs to self-suspend if there is a suspend request and/or
2676 // block if a safepoint is in progress.
2677 // Also check for pending async exception (not including unsafe access error).
2678 // Note only the native==>VM/Java barriers can call this function and when
2679 // thread state is _thread_in_native_trans.
2680 void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
2681 check_safepoint_and_suspend_for_native_trans(thread);
2682
2683 if (thread->has_async_exception()) {
2684 // We are in _thread_in_native_trans state, don't handle unsafe
2685 // access error since that may block.
2686 thread->check_and_handle_async_exceptions(false);
4625 // Maintain fast thread list
4626 ThreadsSMRSupport::remove_thread(p);
4627
4628 _number_of_threads--;
4629 if (!is_daemon) {
4630 _number_of_non_daemon_threads--;
4631
4632 // Only one thread left, do a notify on the Threads_lock so a thread waiting
4633 // on destroy_vm will wake up.
4634 if (number_of_non_daemon_threads() == 1) {
4635 ml.notify_all();
4636 }
4637 }
4638 ThreadService::remove_thread(p, is_daemon);
4639
4640 // Make sure that safepoint code disregard this thread. This is needed since
4641 // the thread might mess around with locks after this point. This can cause it
4642 // to do callbacks into the safepoint code. However, the safepoint code is not aware
4643 // of this thread since it is removed from the queue.
4644 p->set_terminated_value();
4645
4646 // Notify threads waiting in EscapeBarriers
4647 EscapeBarrier::thread_removed(p);
4648 } // unlock Threads_lock
4649
4650 // Since Events::log uses a lock, we grab it outside the Threads_lock
4651 Events::log(p, "Thread exited: " INTPTR_FORMAT, p2i(p));
4652 }
4653
4654 // Operations on the Threads list for GC. These are not explicitly locked,
4655 // but the garbage collector must provide a safe context for them to run.
4656 // In particular, these things should never be called when the Threads_lock
4657 // is held by some other thread. (Note: the Safepoint abstraction also
4658 // uses the Threads_lock to guarantee this property. It also makes sure that
4659 // all threads gets blocked when exiting or starting).
4660
4661 void Threads::oops_do(OopClosure* f, CodeBlobClosure* cf) {
4662 ALL_JAVA_THREADS(p) {
4663 p->oops_do(f, cf);
4664 }
4665 VMThread::vm_thread()->oops_do(f, cf);
4666 }
4667
|