merge in jb-mr1-release history after reset to jb-mr1-dev
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 26673e6..d79e6f3 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -23,7 +23,7 @@
 #        a relevant C stub
 #
 #      - additionally, if the syscall number is different amoung ARM, and x86, MIPS use:
-#        return_type funcname[:syscall_name](parameters) arm_number,x86_number, mips_number
+#        return_type funcname[:syscall_name](parameters) arm_number,x86_number,mips_number
 #
 # the file is processed by a python script named gensyscalls.py
 #
@@ -123,6 +123,8 @@
 int         madvise(const void *, size_t, int)  220,219,218
 int         mlock(const void *addr, size_t len)    150,150,154
 int         munlock(const void *addr, size_t len)   151,151,155
+int         mlockall(int flags)   152,152,156
+int         munlockall()   153,153,157
 int         mincore(void*  start, size_t  length, unsigned char*  vec)   219,218,217
 int         __ioctl:ioctl(int, int, void *)  54
 int         readv(int, const struct iovec *, int)   145
diff --git a/libc/arch-arm/bionic/crtbegin_so.c b/libc/arch-arm/bionic/crtbegin_so.c
index f382f63..cd0257a 100644
--- a/libc/arch-arm/bionic/crtbegin_so.c
+++ b/libc/arch-arm/bionic/crtbegin_so.c
@@ -53,6 +53,6 @@
 #ifdef CRT_LEGACY_WORKAROUND
 #include "__dso_handle.h"
 #else
-#include "__dso_handle_so.c"
+#include "__dso_handle_so.h"
 #include "atexit.h"
 #endif
diff --git a/libc/arch-arm/syscalls.mk b/libc/arch-arm/syscalls.mk
index 565c2bd..62eda87 100644
--- a/libc/arch-arm/syscalls.mk
+++ b/libc/arch-arm/syscalls.mk
@@ -59,6 +59,8 @@
 syscall_src += arch-arm/syscalls/madvise.S
 syscall_src += arch-arm/syscalls/mlock.S
 syscall_src += arch-arm/syscalls/munlock.S
+syscall_src += arch-arm/syscalls/mlockall.S
+syscall_src += arch-arm/syscalls/munlockall.S
 syscall_src += arch-arm/syscalls/mincore.S
 syscall_src += arch-arm/syscalls/__ioctl.S
 syscall_src += arch-arm/syscalls/readv.S
diff --git a/libc/arch-arm/syscalls/mlockall.S b/libc/arch-arm/syscalls/mlockall.S
new file mode 100644
index 0000000..45c782e
--- /dev/null
+++ b/libc/arch-arm/syscalls/mlockall.S
@@ -0,0 +1,14 @@
+/* autogenerated by gensyscalls.py */
+#include <machine/asm.h>
+#include <sys/linux-syscalls.h>
+
+ENTRY(mlockall)
+    .save   {r4, r7}
+    stmfd   sp!, {r4, r7}
+    ldr     r7, =__NR_mlockall
+    swi     #0
+    ldmfd   sp!, {r4, r7}
+    movs    r0, r0
+    bxpl    lr
+    b       __set_syscall_errno
+END(mlockall)
diff --git a/libc/arch-arm/syscalls/munlockall.S b/libc/arch-arm/syscalls/munlockall.S
new file mode 100644
index 0000000..8106965
--- /dev/null
+++ b/libc/arch-arm/syscalls/munlockall.S
@@ -0,0 +1,14 @@
+/* autogenerated by gensyscalls.py */
+#include <machine/asm.h>
+#include <sys/linux-syscalls.h>
+
+ENTRY(munlockall)
+    .save   {r4, r7}
+    stmfd   sp!, {r4, r7}
+    ldr     r7, =__NR_munlockall
+    swi     #0
+    ldmfd   sp!, {r4, r7}
+    movs    r0, r0
+    bxpl    lr
+    b       __set_syscall_errno
+END(munlockall)
diff --git a/libc/arch-mips/syscalls.mk b/libc/arch-mips/syscalls.mk
index 51fd32d..263f307 100644
--- a/libc/arch-mips/syscalls.mk
+++ b/libc/arch-mips/syscalls.mk
@@ -63,6 +63,8 @@
 syscall_src += arch-mips/syscalls/madvise.S
 syscall_src += arch-mips/syscalls/mlock.S
 syscall_src += arch-mips/syscalls/munlock.S
+syscall_src += arch-mips/syscalls/mlockall.S
+syscall_src += arch-mips/syscalls/munlockall.S
 syscall_src += arch-mips/syscalls/mincore.S
 syscall_src += arch-mips/syscalls/__ioctl.S
 syscall_src += arch-mips/syscalls/readv.S
diff --git a/libc/arch-mips/syscalls/mlockall.S b/libc/arch-mips/syscalls/mlockall.S
new file mode 100644
index 0000000..393c090
--- /dev/null
+++ b/libc/arch-mips/syscalls/mlockall.S
@@ -0,0 +1,22 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+    .text
+    .globl mlockall
+    .align 4
+    .ent mlockall
+
+mlockall:
+    .set noreorder
+    .cpload $t9
+    li $v0, __NR_mlockall
+    syscall
+    bnez $a3, 1f
+    move $a0, $v0
+    j $ra
+    nop
+1:
+    la $t9,__set_errno
+    j $t9
+    nop
+    .set reorder
+    .end mlockall
diff --git a/libc/arch-mips/syscalls/munlockall.S b/libc/arch-mips/syscalls/munlockall.S
new file mode 100644
index 0000000..20fa7b8
--- /dev/null
+++ b/libc/arch-mips/syscalls/munlockall.S
@@ -0,0 +1,22 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+    .text
+    .globl munlockall
+    .align 4
+    .ent munlockall
+
+munlockall:
+    .set noreorder
+    .cpload $t9
+    li $v0, __NR_munlockall
+    syscall
+    bnez $a3, 1f
+    move $a0, $v0
+    j $ra
+    nop
+1:
+    la $t9,__set_errno
+    j $t9
+    nop
+    .set reorder
+    .end munlockall
diff --git a/libc/arch-x86/syscalls.mk b/libc/arch-x86/syscalls.mk
index 623c193..2517ebf 100644
--- a/libc/arch-x86/syscalls.mk
+++ b/libc/arch-x86/syscalls.mk
@@ -63,6 +63,8 @@
 syscall_src += arch-x86/syscalls/madvise.S
 syscall_src += arch-x86/syscalls/mlock.S
 syscall_src += arch-x86/syscalls/munlock.S
+syscall_src += arch-x86/syscalls/mlockall.S
+syscall_src += arch-x86/syscalls/munlockall.S
 syscall_src += arch-x86/syscalls/mincore.S
 syscall_src += arch-x86/syscalls/__ioctl.S
 syscall_src += arch-x86/syscalls/readv.S
diff --git a/libc/arch-x86/syscalls/mlockall.S b/libc/arch-x86/syscalls/mlockall.S
new file mode 100644
index 0000000..ad24213
--- /dev/null
+++ b/libc/arch-x86/syscalls/mlockall.S
@@ -0,0 +1,23 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+
+    .text
+    .type mlockall, @function
+    .globl mlockall
+    .align 4
+
+mlockall:
+    pushl   %ebx
+    mov     8(%esp), %ebx
+    movl    $__NR_mlockall, %eax
+    int     $0x80
+    cmpl    $-129, %eax
+    jb      1f
+    negl    %eax
+    pushl   %eax
+    call    __set_errno
+    addl    $4, %esp
+    orl     $-1, %eax
+1:
+    popl    %ebx
+    ret
diff --git a/libc/arch-x86/syscalls/munlockall.S b/libc/arch-x86/syscalls/munlockall.S
new file mode 100644
index 0000000..c43e162
--- /dev/null
+++ b/libc/arch-x86/syscalls/munlockall.S
@@ -0,0 +1,20 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+
+    .text
+    .type munlockall, @function
+    .globl munlockall
+    .align 4
+
+munlockall:
+    movl    $__NR_munlockall, %eax
+    int     $0x80
+    cmpl    $-129, %eax
+    jb      1f
+    negl    %eax
+    pushl   %eax
+    call    __set_errno
+    addl    $4, %esp
+    orl     $-1, %eax
+1:
+    ret
diff --git a/libc/bionic/libc_init_common.c b/libc/bionic/libc_init_common.c
index 4ce4db6..6508c0b 100644
--- a/libc/bionic/libc_init_common.c
+++ b/libc/bionic/libc_init_common.c
@@ -76,7 +76,7 @@
 
     pthread_attr_init(&thread_attr);
     pthread_attr_setstack(&thread_attr, (void*)stackbottom, stacksize);
-    _init_thread(&thread, gettid(), &thread_attr, (void*)stackbottom);
+    _init_thread(&thread, gettid(), &thread_attr, (void*)stackbottom, false);
     __init_tls(tls_area, &thread);
 
     tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
diff --git a/libc/bionic/libc_init_static.c b/libc/bionic/libc_init_static.c
index 97156e8..a73bb71 100644
--- a/libc/bionic/libc_init_static.c
+++ b/libc/bionic/libc_init_static.c
@@ -67,9 +67,6 @@
 
     __libc_init_tls(NULL);
 
-    /* get the initial thread from TLS and add it to gThreadList */
-    _pthread_internal_add(__get_thread());
-
     /* Initialize the C runtime environment */
     __libc_init_common(elfdata);
 
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;
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index 2bd110c..58a809a 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -29,13 +29,14 @@
 #define _PTHREAD_INTERNAL_H_
 
 #include <pthread.h>
+#include <stdbool.h>
 
 __BEGIN_DECLS
 
 typedef struct pthread_internal_t
 {
     struct pthread_internal_t*  next;
-    struct pthread_internal_t** pref;
+    struct pthread_internal_t** prev;
     pthread_attr_t              attr;
     pid_t                       kernel_id;
     pthread_cond_t              join_cond;
@@ -46,7 +47,8 @@
     void**                      tls;         /* thread-local storage area */
 } pthread_internal_t;
 
-extern 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);
 void _pthread_internal_add( pthread_internal_t*  thread );
 pthread_internal_t* __get_thread(void);
 
@@ -100,9 +102,9 @@
     return 0;
 }
 
-extern int  __pthread_cond_timedwait(pthread_cond_t*, 
+extern int  __pthread_cond_timedwait(pthread_cond_t*,
                                      pthread_mutex_t*,
-                                     const struct timespec*, 
+                                     const struct timespec*,
                                      clockid_t);
 
 extern int  __pthread_cond_timedwait_relative(pthread_cond_t*,
diff --git a/libc/include/sys/linux-syscalls.h b/libc/include/sys/linux-syscalls.h
index 0568293..89ab167 100644
--- a/libc/include/sys/linux-syscalls.h
+++ b/libc/include/sys/linux-syscalls.h
@@ -119,6 +119,8 @@
 #define __NR_madvise                      (__NR_SYSCALL_BASE + 220)
 #define __NR_mlock                        (__NR_SYSCALL_BASE + 150)
 #define __NR_munlock                      (__NR_SYSCALL_BASE + 151)
+#define __NR_mlockall                     (__NR_SYSCALL_BASE + 152)
+#define __NR_munlockall                   (__NR_SYSCALL_BASE + 153)
 #define __NR_mincore                      (__NR_SYSCALL_BASE + 219)
 #define __NR_pipe                         (__NR_SYSCALL_BASE + 42)
 #define __NR_pipe2                        (__NR_SYSCALL_BASE + 359)
@@ -222,6 +224,8 @@
 #define __NR_fdatasync                    (__NR_SYSCALL_BASE + 148)
 #define __NR_mlock                        (__NR_SYSCALL_BASE + 150)
 #define __NR_munlock                      (__NR_SYSCALL_BASE + 151)
+#define __NR_mlockall                     (__NR_SYSCALL_BASE + 152)
+#define __NR_munlockall                   (__NR_SYSCALL_BASE + 153)
 #define __NR_sched_setparam               (__NR_SYSCALL_BASE + 154)
 #define __NR_sched_getparam               (__NR_SYSCALL_BASE + 155)
 #define __NR_sched_setscheduler           (__NR_SYSCALL_BASE + 156)
@@ -350,6 +354,8 @@
 #define __NR_fdatasync                    (__NR_SYSCALL_BASE + 152)
 #define __NR_mlock                        (__NR_SYSCALL_BASE + 154)
 #define __NR_munlock                      (__NR_SYSCALL_BASE + 155)
+#define __NR_mlockall                     (__NR_SYSCALL_BASE + 156)
+#define __NR_munlockall                   (__NR_SYSCALL_BASE + 157)
 #define __NR_sched_setparam               (__NR_SYSCALL_BASE + 158)
 #define __NR_sched_getparam               (__NR_SYSCALL_BASE + 159)
 #define __NR_sched_setscheduler           (__NR_SYSCALL_BASE + 160)
diff --git a/libc/private/__dso_handle_so.c b/libc/private/__dso_handle_so.h
similarity index 100%
rename from libc/private/__dso_handle_so.c
rename to libc/private/__dso_handle_so.h
diff --git a/libc/upstream-dlmalloc/malloc.c b/libc/upstream-dlmalloc/malloc.c
index d951841..3ef9b61 100644
--- a/libc/upstream-dlmalloc/malloc.c
+++ b/libc/upstream-dlmalloc/malloc.c
@@ -3193,11 +3193,11 @@
 
   RELEASE_MALLOC_GLOBAL_LOCK();
   /* BEGIN android-added: move pthread_atfork outside of lock */
-#if LOCK_AT_FORK
   if (first_run != 0) {
+#if LOCK_AT_FORK
     pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child);
-  }
 #endif
+  }
   /* END android-added */
   return 1;
 }
diff --git a/tests/Android.mk b/tests/Android.mk
index 1219afd..ae57d9b 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -20,9 +20,10 @@
 
 test_src_files = \
     getcwd_test.cpp \
+    pthread_test.cpp \
     regex_test.cpp \
 
-# Build for the device (with bionic). Run with:
+# Build for the device (with bionic's .so). Run with:
 #   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
 include $(CLEAR_VARS)
 LOCAL_MODULE := bionic-unit-tests
@@ -30,6 +31,16 @@
 LOCAL_SRC_FILES := $(test_src_files)
 include $(BUILD_NATIVE_TEST)
 
+# Build for the device (with bionic's .a). Run with:
+#   adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
+include $(CLEAR_VARS)
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_STATIC_LIBRARIES += libstlport_static libstdc++ libm libc
+LOCAL_MODULE := bionic-unit-tests-static
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_SRC_FILES := $(test_src_files)
+include $(BUILD_NATIVE_TEST)
+
 # Build for the host (with glibc).
 # Note that this will build against glibc, so it's not useful for testing
 # bionic's implementation, but it does let you use glibc as a reference
@@ -37,6 +48,7 @@
 include $(CLEAR_VARS)
 LOCAL_MODULE := bionic-unit-tests-glibc
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_LDFLAGS += -lpthread
 LOCAL_SRC_FILES := $(test_src_files)
 include $(BUILD_HOST_NATIVE_TEST)
 
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
new file mode 100644
index 0000000..9a474c0
--- /dev/null
+++ b/tests/pthread_test.cpp
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <unistd.h>
+
+TEST(pthread, pthread_key_create) {
+  pthread_key_t key;
+  ASSERT_EQ(0, pthread_key_create(&key, NULL));
+  ASSERT_EQ(0, pthread_key_delete(key));
+  // Can't delete a key that's already been deleted.
+  ASSERT_EQ(EINVAL, pthread_key_delete(key));
+}
+
+static void* IdFn(void* arg) {
+  return arg;
+}
+
+static void* SleepFn(void* arg) {
+  sleep(reinterpret_cast<unsigned int>(arg));
+  return NULL;
+}
+
+static void* JoinFn(void* arg) {
+  return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
+}
+
+TEST(pthread, pthread_create) {
+  void* expected_result = reinterpret_cast<void*>(123);
+  // Can we create a thread?
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
+  // If we join, do we get the expected value back?
+  void* result;
+  ASSERT_EQ(0, pthread_join(t, &result));
+  ASSERT_EQ(expected_result, result);
+}
+
+TEST(pthread, pthread_no_join_after_detach) {
+  pthread_t t1;
+  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
+
+  // After a pthread_detach...
+  ASSERT_EQ(0, pthread_detach(t1));
+
+  // ...pthread_join should fail.
+  void* result;
+  ASSERT_EQ(EINVAL, pthread_join(t1, &result));
+}
+
+TEST(pthread, pthread_no_op_detach_after_join) {
+  pthread_t t1;
+  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(1)));
+
+  // If thread 2 is already waiting to join thread 1...
+  pthread_t t2;
+  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
+
+  // ...a call to pthread_detach on thread 1 will "succeed"...
+  ASSERT_EQ(0, pthread_detach(t1));
+
+  // ...but the join still goes ahead.
+  void* join_result;
+  ASSERT_EQ(0, pthread_join(t2, &join_result));
+  ASSERT_EQ(EINVAL, reinterpret_cast<int>(join_result));
+}