src/hotspot/share/runtime/safepoint.cpp
changeset 53646 043ae846819f
parent 53582 881c5fbeb849
child 53775 5d20b085d893
--- a/src/hotspot/share/runtime/safepoint.cpp	Tue Feb 05 13:21:59 2019 -0500
+++ b/src/hotspot/share/runtime/safepoint.cpp	Tue Feb 05 15:12:13 2019 -0500
@@ -793,7 +793,7 @@
 // -------------------------------------------------------------------------------------------------------
 // Implementation of Safepoint callback point
 
-void SafepointSynchronize::block(JavaThread *thread) {
+void SafepointSynchronize::block(JavaThread *thread, bool block_in_safepoint_check) {
   assert(thread != NULL, "thread must be set");
   assert(thread->is_Java_thread(), "not a Java thread");
 
@@ -848,28 +848,37 @@
         }
       }
 
-      // We transition the thread to state _thread_blocked here, but
-      // we can't do our usual check for external suspension and then
-      // self-suspend after the lock_without_safepoint_check() call
-      // below because we are often called during transitions while
-      // we hold different locks. That would leave us suspended while
-      // holding a resource which results in deadlocks.
-      thread->set_thread_state(_thread_blocked);
-      Safepoint_lock->unlock();
+      if (block_in_safepoint_check) {
+        // We transition the thread to state _thread_blocked here, but
+        // we can't do our usual check for external suspension and then
+        // self-suspend after the lock_without_safepoint_check() call
+        // below because we are often called during transitions while
+        // we hold different locks. That would leave us suspended while
+        // holding a resource which results in deadlocks.
+        thread->set_thread_state(_thread_blocked);
+        Safepoint_lock->unlock();
 
-      // We now try to acquire the threads lock. Since this lock is hold by the VM thread during
-      // the entire safepoint, the threads will all line up here during the safepoint.
-      Threads_lock->lock_without_safepoint_check();
-      // restore original state. This is important if the thread comes from compiled code, so it
-      // will continue to execute with the _thread_in_Java state.
-      thread->set_thread_state(state);
-      Threads_lock->unlock();
+        // We now try to acquire the threads lock. Since this lock is hold by the VM thread during
+        // the entire safepoint, the threads will all line up here during the safepoint.
+        Threads_lock->lock_without_safepoint_check();
+        // restore original state. This is important if the thread comes from compiled code, so it
+        // will continue to execute with the _thread_in_Java state.
+        thread->set_thread_state(state);
+        Threads_lock->unlock();
+      } else {
+        // We choose not to block in this call since we would be
+        // caught when transitioning back anyways if the safepoint
+        // is still going on.
+        thread->set_thread_state(state);
+        Safepoint_lock->unlock();
+      }
       break;
 
     case _thread_in_native_trans:
     case _thread_blocked_trans:
     case _thread_new_trans:
-      if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back) {
+      if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back &&
+          block_in_safepoint_check) {
         thread->print_thread_state();
         fatal("Deadlock in safepoint code.  "
               "Should have called back to the VM before blocking.");