Add pthread_gettid_np and re-expose __get_thread for LP32.

A lot of third-party code calls the private __get_thread symbol,
often as part of a backport of bionic's pthread_rwlock implementation.
Hopefully this will go away for LP64 (since you're guaranteed the
real implementation there), but there are still APIs that take a tid
and no way to convert between a pthread_t and a tid. pthread_gettid_np
is a public API for that. To aid the transition, make __get_thread
available again for LP32.

(cherry-pick of 27efc48814b8153c55cbcd0af5d9add824816e69.)

Bug: 14079438
Change-Id: I43fabc7f1918250d31d4665ffa4ca352d0dbeac1
diff --git a/libc/Android.mk b/libc/Android.mk
index 80abce7..ce75e0d 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -158,6 +158,7 @@
     bionic/pthread_exit.cpp \
     bionic/pthread_getcpuclockid.cpp \
     bionic/pthread_getschedparam.cpp \
+    bionic/pthread_gettid_np.cpp \
     bionic/pthread_internals.cpp \
     bionic/pthread_join.cpp \
     bionic/pthread_key.cpp \
diff --git a/libc/bionic/posix_timers.cpp b/libc/bionic/posix_timers.cpp
index 312bf9e..7ad0ef1 100644
--- a/libc/bionic/posix_timers.cpp
+++ b/libc/bionic/posix_timers.cpp
@@ -28,7 +28,6 @@
 
 #include "pthread_internal.h"
 #include "private/bionic_futex.h"
-#include "private/bionic_pthread.h"
 #include "private/kernel_sigset_t.h"
 
 #include <errno.h>
@@ -159,7 +158,7 @@
   sigevent se = *evp;
   se.sigev_signo = TIMER_SIGNAL;
   se.sigev_notify = SIGEV_THREAD_ID;
-  se.sigev_notify_thread_id = __pthread_gettid(timer->callback_thread);
+  se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread);
   if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) {
     __timer_thread_stop(timer);
     return -1;
diff --git a/libc/bionic/pthread_gettid_np.cpp b/libc/bionic/pthread_gettid_np.cpp
new file mode 100644
index 0000000..f4663a7
--- /dev/null
+++ b/libc/bionic/pthread_gettid_np.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "pthread_internal.h"
+#include "private/bionic_pthread.h"
+
+pid_t pthread_gettid_np(pthread_t t) {
+  return reinterpret_cast<pthread_internal_t*>(t)->tid;
+}
+
+// TODO: move callers over to pthread_gettid_np and remove this.
+pid_t __pthread_gettid(pthread_t t) {
+  return pthread_gettid_np(t);
+}
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index 7bcd758..a5b3002 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -98,7 +98,9 @@
 __LIBC_HIDDEN__ void __init_tls(pthread_internal_t* thread);
 __LIBC_HIDDEN__ void __init_alternate_signal_stack(pthread_internal_t*);
 __LIBC_HIDDEN__ void _pthread_internal_add(pthread_internal_t* thread);
-__LIBC_HIDDEN__ pthread_internal_t* __get_thread(void);
+
+/* Various third-party apps contain a backport of our pthread_rwlock implementation that uses this. */
+extern "C" __LIBC64_HIDDEN__ pthread_internal_t* __get_thread(void);
 
 __LIBC_HIDDEN__ void pthread_key_clean_all(void);
 __LIBC_HIDDEN__ void _pthread_internal_remove_locked(pthread_internal_t* thread);
diff --git a/libc/bionic/pthread_internals.cpp b/libc/bionic/pthread_internals.cpp
index 8c5d9c7..4c08ba8 100644
--- a/libc/bionic/pthread_internals.cpp
+++ b/libc/bionic/pthread_internals.cpp
@@ -29,7 +29,6 @@
 #include "pthread_internal.h"
 
 #include "private/bionic_futex.h"
-#include "private/bionic_pthread.h"
 #include "private/bionic_tls.h"
 #include "private/ScopedPthreadMutexLocker.h"
 
@@ -69,10 +68,6 @@
   return reinterpret_cast<pthread_internal_t*>(__get_tls()[TLS_SLOT_THREAD_ID]);
 }
 
-pid_t __pthread_gettid(pthread_t t) {
-  return reinterpret_cast<pthread_internal_t*>(t)->tid;
-}
-
 // Initialize 'ts' with the difference between 'abstime' and the current time
 // according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.
 int __timespec_from_absolute(timespec* ts, const timespec* abstime, clockid_t clock) {
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 29caafc..86a1005 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -188,6 +188,8 @@
 
 void* pthread_getspecific(pthread_key_t);
 
+pid_t pthread_gettid_np(pthread_t);
+
 int pthread_join(pthread_t, void**);
 
 int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));