< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page

        

@@ -4425,10 +4425,16 @@
 
   before_exit(thread);
 
   thread->exit(true);
 
+  // We are no longer on the main thread list but could still be in a
+  // secondary list where another thread may try to interact with us.
+  // So wait until all such interactions are complete before we bring
+  // the VM to the termination safepoint.
+  ThreadsSMRSupport::wait_until_not_protected(thread);
+
   // Stop VM thread.
   {
     // 4945125 The vm thread comes to a safepoint during exit.
     // GC vm_operations can get caught at the safepoint, and the
     // heap is unparseable if they are caught. Grab the Heap_lock

@@ -4464,25 +4470,11 @@
   notify_vm_shutdown();
 
   // exit_globals() will delete tty
   exit_globals();
 
-  // We are here after VM_Exit::set_vm_exited() so we can't call
-  // thread->smr_delete() or we will block on the Threads_lock. We
-  // must check that there are no active references to this thread
-  // before attempting to delete it. A thread could be waiting on
-  // _handshake_turn_sem trying to execute a direct handshake with
-  // this thread.
-  if (!ThreadsSMRSupport::is_a_protected_JavaThread(thread)) {
     delete thread;
-  } else {
-    // Clear value for _thread_key in TLS to prevent, depending
-    // on pthreads implementation, possible execution of
-    // thread-specific destructor in infinite loop at thread
-    // exit.
-    Thread::clear_thread_current();
-  }
 
 #if INCLUDE_JVMCI
   if (JVMCICounterSize > 0) {
     FREE_C_HEAP_ARRAY(jlong, JavaThread::_jvmci_old_thread_counters);
   }
< prev index next >