Fix pthreads functions that should return ESRCH.

imgtec pointed out that pthread_kill(3) was broken, but most of the
other functions that ought to return ESRCH for invalid/exited threads
were equally broken.

Change-Id: I96347f6195549aee0c72dc39063e6c5d06d2e01f
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index aebf477..239092c 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -79,6 +79,12 @@
   ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
 }
 
+static void MakeDeadThread(pthread_t& t) {
+  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
+  void* result;
+  ASSERT_EQ(0, pthread_join(t, &result));
+}
+
 TEST(pthread, pthread_create) {
   void* expected_result = reinterpret_cast<void*>(123);
   // Can we create a thread?
@@ -229,12 +235,67 @@
 
 #if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
 TEST(pthread, pthread_setname_np__no_such_thread) {
-  pthread_t t1;
-  ASSERT_EQ(0, pthread_create(&t1, NULL, IdFn, NULL));
-  void* result;
-  ASSERT_EQ(0, pthread_join(t1, &result));
+  pthread_t dead_thread;
+  MakeDeadThread(dead_thread);
 
   // Call pthread_setname_np after thread has already exited.
-  ASSERT_EQ(ENOENT, pthread_setname_np(t1, "short 3"));
+  ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
 }
 #endif
+
+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));
+}
+
+TEST(pthread, pthread_kill__invalid_signal) {
+  ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
+}
+
+TEST(pthread, pthread_detach__no_such_thread) {
+  pthread_t dead_thread;
+  MakeDeadThread(dead_thread);
+
+  ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
+}
+
+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);
+
+  void* result;
+  ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
+}
+
+TEST(pthread, pthread_kill__no_such_thread) {
+  pthread_t dead_thread;
+  MakeDeadThread(dead_thread);
+
+  ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
+}