Stop checking the global thread list in several trivial cases.

Since removing the global thread is hard, let's take the different
groups of functions individually.

The existing code was racy anyway, because the thread might still be
on the list but have exited (leaving tid == 0).

Bug: http://b/19636317
Test: ran tests
Change-Id: Icc0986ff124d5f9b8a653edf718c549d1563973b
diff --git a/libc/bionic/pthread_getcpuclockid.cpp b/libc/bionic/pthread_getcpuclockid.cpp
index 2bf2004..dbfb7d4 100644
--- a/libc/bionic/pthread_getcpuclockid.cpp
+++ b/libc/bionic/pthread_getcpuclockid.cpp
@@ -31,13 +31,11 @@
 #include "pthread_internal.h"
 
 int pthread_getcpuclockid(pthread_t t, clockid_t* clockid) {
-  pthread_internal_t* thread = __pthread_internal_find(t);
-  if (thread == NULL) {
-    return ESRCH;
-  }
+  pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
+  if (tid == 0) return ESRCH;
 
   // The tid is stored in the top bits, but negated.
-  clockid_t result = ~static_cast<clockid_t>(thread->tid) << 3;
+  clockid_t result = ~static_cast<clockid_t>(tid) << 3;
   // Bits 0 and 1: clock type (0 = CPUCLOCK_PROF, 1 = CPUCLOCK_VIRT, 2 = CPUCLOCK_SCHED).
   result |= 2;
   // Bit 2: thread (set) or process (clear)?
diff --git a/libc/bionic/pthread_getschedparam.cpp b/libc/bionic/pthread_getschedparam.cpp
index 052fb05..4267a0e 100644
--- a/libc/bionic/pthread_getschedparam.cpp
+++ b/libc/bionic/pthread_getschedparam.cpp
@@ -34,15 +34,11 @@
 int pthread_getschedparam(pthread_t t, int* policy, sched_param* param) {
   ErrnoRestorer errno_restorer;
 
-  pthread_internal_t* thread = __pthread_internal_find(t);
-  if (thread == NULL) {
-    return ESRCH;
-  }
+  pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
+  if (tid == 0) return ESRCH;
 
-  int rc = sched_getparam(thread->tid, param);
-  if (rc == -1) {
-    return errno;
-  }
-  *policy = sched_getscheduler(thread->tid);
+  if (sched_getparam(tid, param) == -1) return errno;
+
+  *policy = sched_getscheduler(tid);
   return 0;
 }
diff --git a/libc/bionic/pthread_setname_np.cpp b/libc/bionic/pthread_setname_np.cpp
index 6d2880e..0778a3c 100644
--- a/libc/bionic/pthread_setname_np.cpp
+++ b/libc/bionic/pthread_setname_np.cpp
@@ -43,14 +43,9 @@
 #define MAX_TASK_COMM_LEN 16
 
 static int __open_task_comm_fd(pthread_t t, int flags) {
-  pthread_internal_t* thread = __pthread_internal_find(t);
-  if (thread == nullptr) {
-    errno = ENOENT;
-    return -1;
-  }
-
   char comm_name[64];
-  snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm", thread->tid);
+  snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm",
+           reinterpret_cast<pthread_internal_t*>(t)->tid);
   return open(comm_name, O_CLOEXEC | flags);
 }
 
diff --git a/libc/bionic/pthread_setschedparam.cpp b/libc/bionic/pthread_setschedparam.cpp
index 0ad68bb..a0520fb 100644
--- a/libc/bionic/pthread_setschedparam.cpp
+++ b/libc/bionic/pthread_setschedparam.cpp
@@ -34,14 +34,8 @@
 int pthread_setschedparam(pthread_t t, int policy, const sched_param* param) {
   ErrnoRestorer errno_restorer;
 
-  pthread_internal_t* thread = __pthread_internal_find(t);
-  if (thread == NULL) {
-    return ESRCH;
-  }
+  pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
+  if (tid == 0) return ESRCH;
 
-  int rc = sched_setscheduler(thread->tid, policy, param);
-  if (rc == -1) {
-    return errno;
-  }
-  return 0;
+  return (sched_setscheduler(tid, policy, param) == -1) ? errno : 0;
 }
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index b0c95fe..8345a47 100755
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -443,16 +443,6 @@
   ASSERT_EQ(0, pthread_join(t, nullptr));
 }
 
-TEST(pthread, pthread_setname_np__pthread_getname_np__no_such_thread) {
-  pthread_t dead_thread;
-  MakeDeadThread(dead_thread);
-
-  // Call pthread_getname_np and pthread_setname_np after the thread has already exited.
-  ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
-  char name[64];
-  ASSERT_EQ(ENOENT, pthread_getname_np(dead_thread, name, sizeof(name)));
-}
-
 TEST(pthread, pthread_kill__0) {
   // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
   ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
@@ -497,32 +487,6 @@
   ASSERT_EQ(0, pthread_join(t, nullptr));
 }
 
-TEST(pthread, pthread_getcpuclockid__no_such_thread) {
-  pthread_t dead_thread;
-  MakeDeadThread(dead_thread);
-
-  clockid_t c;
-  ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
-}
-
-TEST(pthread, pthread_getschedparam__no_such_thread) {
-  pthread_t dead_thread;
-  MakeDeadThread(dead_thread);
-
-  int policy;
-  sched_param param;
-  ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
-}
-
-TEST(pthread, pthread_setschedparam__no_such_thread) {
-  pthread_t dead_thread;
-  MakeDeadThread(dead_thread);
-
-  int policy = 0;
-  sched_param param;
-  ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
-}
-
 TEST(pthread, pthread_join__no_such_thread) {
   pthread_t dead_thread;
   MakeDeadThread(dead_thread);