Revert "Stop checking the global thread list in several trivial cases."

This reverts commit f5a4992b7186c01b98cb9400b2b5b490805a87bb.

Breaks OMX_ImgEnc in cameraserver (http://b/35088254).

Change-Id: I6dcf12706a184b0b8b72451584567a42dfa1bb4f
(cherry picked from commit bcb152903a26269e2c8ada3b27c787ae875f8812)
diff --git a/libc/bionic/pthread_getcpuclockid.cpp b/libc/bionic/pthread_getcpuclockid.cpp
index dbfb7d4..2bf2004 100644
--- a/libc/bionic/pthread_getcpuclockid.cpp
+++ b/libc/bionic/pthread_getcpuclockid.cpp
@@ -31,11 +31,13 @@
 #include "pthread_internal.h"
 
 int pthread_getcpuclockid(pthread_t t, clockid_t* clockid) {
-  pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
-  if (tid == 0) return ESRCH;
+  pthread_internal_t* thread = __pthread_internal_find(t);
+  if (thread == NULL) {
+    return ESRCH;
+  }
 
   // The tid is stored in the top bits, but negated.
-  clockid_t result = ~static_cast<clockid_t>(tid) << 3;
+  clockid_t result = ~static_cast<clockid_t>(thread->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 4267a0e..052fb05 100644
--- a/libc/bionic/pthread_getschedparam.cpp
+++ b/libc/bionic/pthread_getschedparam.cpp
@@ -34,11 +34,15 @@
 int pthread_getschedparam(pthread_t t, int* policy, sched_param* param) {
   ErrnoRestorer errno_restorer;
 
-  pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
-  if (tid == 0) return ESRCH;
+  pthread_internal_t* thread = __pthread_internal_find(t);
+  if (thread == NULL) {
+    return ESRCH;
+  }
 
-  if (sched_getparam(tid, param) == -1) return errno;
-
-  *policy = sched_getscheduler(tid);
+  int rc = sched_getparam(thread->tid, param);
+  if (rc == -1) {
+    return errno;
+  }
+  *policy = sched_getscheduler(thread->tid);
   return 0;
 }
diff --git a/libc/bionic/pthread_setname_np.cpp b/libc/bionic/pthread_setname_np.cpp
index 0778a3c..6d2880e 100644
--- a/libc/bionic/pthread_setname_np.cpp
+++ b/libc/bionic/pthread_setname_np.cpp
@@ -43,9 +43,14 @@
 #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",
-           reinterpret_cast<pthread_internal_t*>(t)->tid);
+  snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm", thread->tid);
   return open(comm_name, O_CLOEXEC | flags);
 }
 
diff --git a/libc/bionic/pthread_setschedparam.cpp b/libc/bionic/pthread_setschedparam.cpp
index a0520fb..0ad68bb 100644
--- a/libc/bionic/pthread_setschedparam.cpp
+++ b/libc/bionic/pthread_setschedparam.cpp
@@ -34,8 +34,14 @@
 int pthread_setschedparam(pthread_t t, int policy, const sched_param* param) {
   ErrnoRestorer errno_restorer;
 
-  pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
-  if (tid == 0) return ESRCH;
+  pthread_internal_t* thread = __pthread_internal_find(t);
+  if (thread == NULL) {
+    return ESRCH;
+  }
 
-  return (sched_setscheduler(tid, policy, param) == -1) ? errno : 0;
+  int rc = sched_setscheduler(thread->tid, policy, param);
+  if (rc == -1) {
+    return errno;
+  }
+  return 0;
 }
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 8345a47..b0c95fe 100755
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -443,6 +443,16 @@
   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));
@@ -487,6 +497,32 @@
   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);