Don't corrupt the thread list in static executables.

Several previous changes conspired to make a mess of the thread list
in static binaries. This was most obvious when trying to call
pthread_key_delete(3) on the main thread.

Bug: http://code.google.com/p/android/issues/detail?id=36893
Change-Id: I2a2f553114d8fb40533c481252b410c10656da2e
diff --git a/libc/bionic/pthread.c b/libc/bionic/pthread.c
index 40a09ba..14d2fec 100644
--- a/libc/bionic/pthread.c
+++ b/libc/bionic/pthread.c
@@ -117,8 +117,8 @@
 static void
 _pthread_internal_remove_locked( pthread_internal_t*  thread )
 {
-    thread->next->pref = thread->pref;
-    thread->pref[0]    = thread->next;
+    thread->next->prev = thread->prev;
+    thread->prev[0]    = thread->next;
 }
 
 static void
@@ -130,14 +130,17 @@
 }
 
 __LIBC_ABI_PRIVATE__ void
-_pthread_internal_add( pthread_internal_t*  thread )
+_pthread_internal_add(pthread_internal_t* thread)
 {
     pthread_mutex_lock(&gThreadListLock);
-    thread->pref = &gThreadList;
-    thread->next = thread->pref[0];
-    if (thread->next)
-        thread->next->pref = &thread->next;
-    thread->pref[0] = thread;
+
+    thread->prev = &gThreadList;
+    thread->next = *(thread->prev);
+    if (thread->next != NULL) {
+        thread->next->prev = &thread->next;
+    }
+    *(thread->prev) = thread;
+
     pthread_mutex_unlock(&gThreadListLock);
 }
 
@@ -203,7 +206,8 @@
 }
 
 __LIBC_ABI_PRIVATE__
-int _init_thread(pthread_internal_t * thread, pid_t kernel_id, pthread_attr_t * attr, void * stack_base)
+int _init_thread(pthread_internal_t* thread, pid_t kernel_id, pthread_attr_t* attr,
+                 void* stack_base, bool add_to_thread_list)
 {
     int error = 0;
 
@@ -231,10 +235,12 @@
 
     pthread_cond_init(&thread->join_cond, NULL);
     thread->join_count = 0;
-
     thread->cleanup_stack = NULL;
 
-    _pthread_internal_add(thread);
+    if (add_to_thread_list) {
+        _pthread_internal_add(thread);
+    }
+
     return error;
 }
 
@@ -348,7 +354,7 @@
         return clone_errno;
     }
 
-    int init_errno = _init_thread(thread, tid, (pthread_attr_t*) attr, stack);
+    int init_errno = _init_thread(thread, tid, (pthread_attr_t*) attr, stack, true);
     if (init_errno != 0) {
         // Mark the thread detached and let its __thread_entry run to
         // completion. (It'll just exit immediately, cleaning up its resources.)
@@ -1919,8 +1925,8 @@
 
 /* This deletes a pthread_key_t. note that the standard mandates that this does
  * not call the destructor of non-NULL key values. Instead, it is the
- * responsability of the caller to properly dispose of the corresponding data
- * and resources, using any mean it finds suitable.
+ * responsibility of the caller to properly dispose of the corresponding data
+ * and resources, using any means it finds suitable.
  *
  * On the other hand, this function will clear the corresponding key data
  * values in all known threads. this prevents later (invalid) calls to
@@ -2184,7 +2190,7 @@
     for (;;) {
         /* Try to atomically set the INITIALIZING flag.
          * This requires a cmpxchg loop, and we may need
-         * to exit prematurely if we detect that 
+         * to exit prematurely if we detect that
          * COMPLETED is now set.
          */
         int32_t  oldval, newval;