Fix the pthread_setname_np test.

Fix the pthread_setname_np test to take into account that emulator kernels are
so old that they don't support setting the name of other threads.

The CLONE_DETACHED thread is obsolete since 2.5 kernels.

Rename kernel_id to tid.

Fix the signature of __pthread_clone.

Clean up the clone and pthread_setname_np implementations slightly.

Change-Id: I16c2ff8845b67530544bbda9aa6618058603066d
diff --git a/libc/bionic/pthread-rwlocks.c b/libc/bionic/pthread-rwlocks.c
index ca3e95c..deee577 100644
--- a/libc/bionic/pthread-rwlocks.c
+++ b/libc/bionic/pthread-rwlocks.c
@@ -42,7 +42,7 @@
  *  - trying to get the read-lock while there is a writer blocks
  *  - a single thread can acquire the lock multiple times in the same mode
  *
- *  - Posix states that behaviour is undefined it a thread tries to acquire
+ *  - Posix states that behavior is undefined it a thread tries to acquire
  *    the lock in two distinct modes (e.g. write after read, or read after write).
  *
  *  - This implementation tries to avoid writer starvation by making the readers
@@ -61,12 +61,6 @@
 
 extern pthread_internal_t* __get_thread(void);
 
-/* Return a global kernel ID for the current thread */
-static int __get_thread_id(void)
-{
-    return __get_thread()->kernel_id;
-}
-
 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
 {
     if (!attr)
@@ -150,8 +144,6 @@
 
 int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
 {
-    int  ret;
-
     if (rwlock == NULL)
         return EINVAL;
 
@@ -164,7 +156,7 @@
 }
 
 /* Returns TRUE iff we can acquire a read lock. */
-static __inline__ int read_precondition(pthread_rwlock_t *rwlock, int  thread_id)
+static __inline__ int read_precondition(pthread_rwlock_t* rwlock, int tid)
 {
     /* We can't have the lock if any writer is waiting for it (writer bias).
      * This tries to avoid starvation when there are multiple readers racing.
@@ -174,7 +166,7 @@
 
     /* We can have the lock if there is no writer, or if we write-own it */
     /* The second test avoids a self-dead lock in case of buggy code. */
-    if (rwlock->writerThreadId == 0 || rwlock->writerThreadId == thread_id)
+    if (rwlock->writerThreadId == 0 || rwlock->writerThreadId == tid)
         return 1;
 
     /* Otherwise, we can't have it */
@@ -182,14 +174,14 @@
 }
 
 /* returns TRUE iff we can acquire a write lock. */
-static __inline__ int write_precondition(pthread_rwlock_t *rwlock, int  thread_id)
+static __inline__ int write_precondition(pthread_rwlock_t* rwlock, int tid)
 {
     /* We can get the lock if nobody has it */
     if (rwlock->numLocks == 0)
         return 1;
 
     /* Or if we already own it */
-    if (rwlock->writerThreadId == thread_id)
+    if (rwlock->writerThreadId == tid)
         return 1;
 
     /* Otherwise, not */
@@ -220,7 +212,7 @@
         return EINVAL;
 
     pthread_mutex_lock(&rwlock->lock);
-    if (__unlikely(!read_precondition(rwlock, __get_thread_id())))
+    if (__unlikely(!read_precondition(rwlock, __get_thread()->tid)))
         ret = EBUSY;
     else
         rwlock->numLocks ++;
@@ -231,18 +223,18 @@
 
 int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout)
 {
-    int thread_id, ret = 0;
+    int ret = 0;
 
     if (rwlock == NULL)
         return EINVAL;
 
     pthread_mutex_lock(&rwlock->lock);
-    thread_id = __get_thread_id();
-    if (__unlikely(!read_precondition(rwlock, thread_id))) {
+    int tid = __get_thread()->tid;
+    if (__unlikely(!read_precondition(rwlock, tid))) {
         rwlock->pendingReaders += 1;
         do {
             ret = pthread_cond_timedwait(&rwlock->cond, &rwlock->lock, abs_timeout);
-        } while (ret == 0 && !read_precondition(rwlock, thread_id));
+        } while (ret == 0 && !read_precondition(rwlock, tid));
         rwlock->pendingReaders -= 1;
         if (ret != 0)
             goto EXIT;
@@ -261,18 +253,18 @@
 
 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
 {
-    int thread_id, ret = 0;
+    int ret = 0;
 
     if (rwlock == NULL)
         return EINVAL;
 
     pthread_mutex_lock(&rwlock->lock);
-    thread_id = __get_thread_id();
-    if (__unlikely(!write_precondition(rwlock, thread_id))) {
+    int tid = __get_thread()->tid;
+    if (__unlikely(!write_precondition(rwlock, tid))) {
         ret = EBUSY;
     } else {
         rwlock->numLocks ++;
-        rwlock->writerThreadId = thread_id;
+        rwlock->writerThreadId = tid;
     }
     pthread_mutex_unlock(&rwlock->lock);
     return ret;
@@ -280,14 +272,14 @@
 
 int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout)
 {
-    int thread_id, ret = 0;
+    int ret = 0;
 
     if (rwlock == NULL)
         return EINVAL;
 
     pthread_mutex_lock(&rwlock->lock);
-    thread_id = __get_thread_id();
-    if (__unlikely(!write_precondition(rwlock, thread_id))) {
+    int tid = __get_thread()->tid;
+    if (__unlikely(!write_precondition(rwlock, tid))) {
         /* If we can't read yet, wait until the rwlock is unlocked
          * and try again. Increment pendingReaders to get the
          * cond broadcast when that happens.
@@ -295,13 +287,13 @@
         rwlock->pendingWriters += 1;
         do {
             ret = pthread_cond_timedwait(&rwlock->cond, &rwlock->lock, abs_timeout);
-        } while (ret == 0 && !write_precondition(rwlock, thread_id));
+        } while (ret == 0 && !write_precondition(rwlock, tid));
         rwlock->pendingWriters -= 1;
         if (ret != 0)
             goto EXIT;
     }
     rwlock->numLocks ++;
-    rwlock->writerThreadId = thread_id;
+    rwlock->writerThreadId = tid;
 EXIT:
     pthread_mutex_unlock(&rwlock->lock);
     return ret;
@@ -332,7 +324,7 @@
      * must be ourselves.
      */
     else {
-        if (rwlock->writerThreadId != __get_thread_id()) {
+        if (rwlock->writerThreadId != __get_thread()->tid) {
             ret = EPERM;
             goto EXIT;
         }