ART: Use thread_local on the host for ART Thread*

Use thread_local instead of a pthread key for Thread::Current().
Retain the pthread key as we are still using it for some sanity
checks in ThreadExitCallback.

Bug: 138329277
Test: m test-art-host
Change-Id: Idfe43bc279459e307e2165d3a3762af09230bdfd
diff --git a/runtime/thread-current-inl.h b/runtime/thread-current-inl.h
index 787ec5b..36f767c 100644
--- a/runtime/thread-current-inl.h
+++ b/runtime/thread-current-inl.h
@@ -36,7 +36,7 @@
 #ifdef __BIONIC__
     void* thread = __get_tls()[TLS_SLOT_ART_THREAD_SELF];
 #else
-    void* thread = pthread_getspecific(Thread::pthread_key_self_);
+    Thread* thread = Thread::self_tls_;
 #endif
     return reinterpret_cast<Thread*>(thread);
   }
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 53450ba..84983b8 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -131,6 +131,9 @@
 const size_t Thread::kStackOverflowImplicitCheckSize = GetStackOverflowReservedBytes(kRuntimeISA);
 bool (*Thread::is_sensitive_thread_hook_)() = nullptr;
 Thread* Thread::jit_sensitive_thread_ = nullptr;
+#ifndef __BIONIC__
+thread_local Thread* Thread::self_tls_ = nullptr;
+#endif
 
 static constexpr bool kVerifyImageObjectsMarked = kIsDebugBuild;
 
@@ -937,6 +940,7 @@
   __get_tls()[TLS_SLOT_ART_THREAD_SELF] = this;
 #else
   CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self");
+  Thread::self_tls_ = this;
 #endif
   DCHECK_EQ(Thread::Current(), this);
 
@@ -2191,6 +2195,7 @@
     __get_tls()[TLS_SLOT_ART_THREAD_SELF] = self;
 #else
     CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self");
+    Thread::self_tls_ = self;
 #endif
     self->tls32_.thread_exit_check_count = 1;
   } else {
@@ -2221,6 +2226,9 @@
   if (pthread_getspecific(pthread_key_self_) != nullptr) {
     LOG(FATAL) << "Newly-created pthread TLS slot is not nullptr";
   }
+#ifndef __BIONIC__
+  CHECK(Thread::self_tls_ == nullptr);
+#endif
 }
 
 void Thread::FinishStartup() {
diff --git a/runtime/thread.h b/runtime/thread.h
index 9a230e2..733f14e 100644
--- a/runtime/thread.h
+++ b/runtime/thread.h
@@ -1789,6 +1789,10 @@
   // compiled code or entrypoints.
   SafeMap<std::string, std::unique_ptr<TLSData>> custom_tls_ GUARDED_BY(Locks::custom_tls_lock_);
 
+#ifndef __BIONIC__
+  static thread_local Thread* self_tls_;
+#endif
+
   // True if the thread is some form of runtime thread (ex, GC or JIT).
   bool is_runtime_thread_;
 
diff --git a/runtime/thread_list.cc b/runtime/thread_list.cc
index c971183..2fe239a 100644
--- a/runtime/thread_list.cc
+++ b/runtime/thread_list.cc
@@ -1485,6 +1485,7 @@
   __get_tls()[TLS_SLOT_ART_THREAD_SELF] = nullptr;
 #else
   CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, nullptr), "detach self");
+  Thread::self_tls_ = nullptr;
 #endif
 
   // Signal that a thread just detached.