< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page

        

@@ -1689,10 +1689,11 @@
   _cached_monitor_info = NULL;
   _parker = Parker::Allocate(this);
   _SleepEvent = ParkEvent::Allocate(this);
   // Setup safepoint state info for this thread
   ThreadSafepointState::create(this);
+  _handshake.set_thread(this);
 
   debug_only(_java_call_counter = 0);
 
   // JVMTI PopFrame support
   _popframe_condition = popframe_inactive;

@@ -4464,16 +4465,25 @@
   notify_vm_shutdown();
 
   // exit_globals() will delete tty
   exit_globals();
 
-  // We are after VM_Exit::set_vm_exited() so we can't call
+  // 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.
-  // Deleting the shutdown thread here is safe because another
-  // JavaThread cannot have an active ThreadsListHandle for
-  // this JavaThread.
+  // We must check 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((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 >