Move gettid() into libcutils.

Change-Id: Ic8a15036833e6d129b7998d954b804be391de399
diff --git a/libcutils/threads.c b/libcutils/threads.c
index ca600b3..cafeff7 100644
--- a/libcutils/threads.c
+++ b/libcutils/threads.c
@@ -14,9 +14,23 @@
 ** limitations under the License.
 */
 
-#include <cutils/threads.h>
+#include "cutils/threads.h"
 
 #if !defined(_WIN32)
+
+// For gettid.
+#if defined(__APPLE__)
+#include "AvailabilityMacros.h"  // For MAC_OS_X_VERSION_MAX_ALLOWED
+#include <sys/syscall.h>
+#include <sys/time.h>
+#include "base/logging.h"
+#elif defined(__linux__) && !defined(__ANDROID__)
+#include <syscall.h>
+#include <unistd.h>
+#elif defined(_WIN32)
+#include <Windows.h>
+#endif
+
 void*  thread_store_get( thread_store_t*  store )
 {
     if (!store->has_tls)
@@ -42,6 +56,21 @@
     pthread_setspecific( store->tls, value );
 }
 
+// No definition needed for Android because we'll just pick up bionic's copy.
+#ifndef __ANDROID__
+pid_t gettid() {
+#if defined(__APPLE__)
+  uint64_t owner;
+  CHECK_PTHREAD_CALL(pthread_threadid_np, (NULL, &owner), __FUNCTION__);
+  return owner;
+#elif defined(__linux__)
+  return syscall(__NR_gettid);
+#elif defined(_WIN32)
+  return (pid_t)GetCurrentThreadId();
+#endif
+}
+#endif  // __ANDROID__
+
 #else /* !defined(_WIN32) */
 void*  thread_store_get( thread_store_t*  store )
 {