changeset 5519:a40c5b54f6b3

8033696: "assert(thread != NULL) failed: just checking" due to Thread::current() and JNI pthread interaction Reviewed-by: dholmes, dsamersoff Contributed-by: andreas.eriksson@oracle.com
author kevinw
date Wed, 02 Apr 2014 18:40:52 +0200
parents 042420b675e8
children 546d6ac32342
files src/os/bsd/vm/os_bsd.cpp src/os/linux/vm/os_linux.cpp src/share/vm/runtime/vmThread.cpp
diffstat 3 files changed, 27 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/os/bsd/vm/os_bsd.cpp	Wed Apr 09 09:14:16 2014 -0700
+++ b/src/os/bsd/vm/os_bsd.cpp	Wed Apr 02 18:40:52 2014 +0200
@@ -1297,9 +1297,20 @@
 //////////////////////////////////////////////////////////////////////////////
 // thread local storage
 
+// Restore the thread pointer if the destructor is called. This is in case
+// someone from JNI code sets up a destructor with pthread_key_create to run
+// detachCurrentThread on thread death. Unless we restore the thread pointer we
+// will hang or crash. When detachCurrentThread is called the key will be set
+// to null and we will not be called again. If detachCurrentThread is never
+// called we could loop forever depending on the pthread implementation.
+static void restore_thread_pointer(void* p) {
+  Thread* thread = (Thread*) p;
+  os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);
+}
+
 int os::allocate_thread_local_storage() {
   pthread_key_t key;
-  int rslt = pthread_key_create(&key, NULL);
+  int rslt = pthread_key_create(&key, restore_thread_pointer);
   assert(rslt == 0, "cannot allocate thread local storage");
   return (int)key;
 }
--- a/src/os/linux/vm/os_linux.cpp	Wed Apr 09 09:14:16 2014 -0700
+++ b/src/os/linux/vm/os_linux.cpp	Wed Apr 02 18:40:52 2014 +0200
@@ -1103,9 +1103,20 @@
 //////////////////////////////////////////////////////////////////////////////
 // thread local storage
 
+// Restore the thread pointer if the destructor is called. This is in case
+// someone from JNI code sets up a destructor with pthread_key_create to run
+// detachCurrentThread on thread death. Unless we restore the thread pointer we
+// will hang or crash. When detachCurrentThread is called the key will be set
+// to null and we will not be called again. If detachCurrentThread is never
+// called we could loop forever depending on the pthread implementation.
+static void restore_thread_pointer(void* p) {
+  Thread* thread = (Thread*) p;
+  os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);
+}
+
 int os::allocate_thread_local_storage() {
   pthread_key_t key;
-  int rslt = pthread_key_create(&key, NULL);
+  int rslt = pthread_key_create(&key, restore_thread_pointer);
   assert(rslt == 0, "cannot allocate thread local storage");
   return (int)key;
 }
--- a/src/share/vm/runtime/vmThread.cpp	Wed Apr 09 09:14:16 2014 -0700
+++ b/src/share/vm/runtime/vmThread.cpp	Wed Apr 02 18:40:52 2014 +0200
@@ -327,6 +327,9 @@
     _terminate_lock->notify();
   }
 
+  // Thread destructor usually does this.
+  ThreadLocalStorage::set_thread(NULL);
+
   // Deletion must be done synchronously by the JNI DestroyJavaVM thread
   // so that the VMThread deletion completes before the main thread frees
   // up the CodeHeap.