791 |
791 |
792 |
792 |
793 // ------------------------------------------------------------------------------------------------------- |
793 // ------------------------------------------------------------------------------------------------------- |
794 // Implementation of Safepoint callback point |
794 // Implementation of Safepoint callback point |
795 |
795 |
796 void SafepointSynchronize::block(JavaThread *thread) { |
796 void SafepointSynchronize::block(JavaThread *thread, bool block_in_safepoint_check) { |
797 assert(thread != NULL, "thread must be set"); |
797 assert(thread != NULL, "thread must be set"); |
798 assert(thread->is_Java_thread(), "not a Java thread"); |
798 assert(thread->is_Java_thread(), "not a Java thread"); |
799 |
799 |
800 // Threads shouldn't block if they are in the middle of printing, but... |
800 // Threads shouldn't block if they are in the middle of printing, but... |
801 ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id()); |
801 ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id()); |
846 if (_waiting_to_block == 0) { |
846 if (_waiting_to_block == 0) { |
847 Safepoint_lock->notify_all(); |
847 Safepoint_lock->notify_all(); |
848 } |
848 } |
849 } |
849 } |
850 |
850 |
851 // We transition the thread to state _thread_blocked here, but |
851 if (block_in_safepoint_check) { |
852 // we can't do our usual check for external suspension and then |
852 // We transition the thread to state _thread_blocked here, but |
853 // self-suspend after the lock_without_safepoint_check() call |
853 // we can't do our usual check for external suspension and then |
854 // below because we are often called during transitions while |
854 // self-suspend after the lock_without_safepoint_check() call |
855 // we hold different locks. That would leave us suspended while |
855 // below because we are often called during transitions while |
856 // holding a resource which results in deadlocks. |
856 // we hold different locks. That would leave us suspended while |
857 thread->set_thread_state(_thread_blocked); |
857 // holding a resource which results in deadlocks. |
858 Safepoint_lock->unlock(); |
858 thread->set_thread_state(_thread_blocked); |
859 |
859 Safepoint_lock->unlock(); |
860 // We now try to acquire the threads lock. Since this lock is hold by the VM thread during |
860 |
861 // the entire safepoint, the threads will all line up here during the safepoint. |
861 // We now try to acquire the threads lock. Since this lock is hold by the VM thread during |
862 Threads_lock->lock_without_safepoint_check(); |
862 // the entire safepoint, the threads will all line up here during the safepoint. |
863 // restore original state. This is important if the thread comes from compiled code, so it |
863 Threads_lock->lock_without_safepoint_check(); |
864 // will continue to execute with the _thread_in_Java state. |
864 // restore original state. This is important if the thread comes from compiled code, so it |
865 thread->set_thread_state(state); |
865 // will continue to execute with the _thread_in_Java state. |
866 Threads_lock->unlock(); |
866 thread->set_thread_state(state); |
|
867 Threads_lock->unlock(); |
|
868 } else { |
|
869 // We choose not to block in this call since we would be |
|
870 // caught when transitioning back anyways if the safepoint |
|
871 // is still going on. |
|
872 thread->set_thread_state(state); |
|
873 Safepoint_lock->unlock(); |
|
874 } |
867 break; |
875 break; |
868 |
876 |
869 case _thread_in_native_trans: |
877 case _thread_in_native_trans: |
870 case _thread_blocked_trans: |
878 case _thread_blocked_trans: |
871 case _thread_new_trans: |
879 case _thread_new_trans: |
872 if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back) { |
880 if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back && |
|
881 block_in_safepoint_check) { |
873 thread->print_thread_state(); |
882 thread->print_thread_state(); |
874 fatal("Deadlock in safepoint code. " |
883 fatal("Deadlock in safepoint code. " |
875 "Should have called back to the VM before blocking."); |
884 "Should have called back to the VM before blocking."); |
876 } |
885 } |
877 |
886 |