More pthreads cleanup.

POSIX says pthread_create returns EAGAIN, not ENOMEM.

Also pull pthread_attr_t functions into their own file.

Also pull pthread_setname_np into its own file.

Also remove unnecessary #includes from pthread_key.cpp.

Also account for those pthread keys used internally by bionic,
so they don't count against the number of keys available to user
code. (They do with glibc, but glibc's limit is the much more
generous 1024.)

Also factor out the common errno-restoring idiom to reduce gotos.

Bug: 6702535
Change-Id: I555e66efffcf2c1b5a2873569e91489156efca42
diff --git a/libc/private/ErrnoRestorer.h b/libc/private/ErrnoRestorer.h
new file mode 100644
index 0000000..ed6ab62
--- /dev/null
+++ b/libc/private/ErrnoRestorer.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#ifndef ERRNO_RESTORER_H
+#define ERRNO_RESTORER_H
+
+#include <errno.h>
+
+class ErrnoRestorer {
+ public:
+  explicit ErrnoRestorer() : saved_errno_(errno) {
+  }
+
+  ~ErrnoRestorer() {
+    errno = saved_errno_;
+  }
+
+  void override(int new_errno) {
+    saved_errno_ = new_errno;
+  }
+
+ private:
+  int saved_errno_;
+
+  // Disallow copy and assignment.
+  ErrnoRestorer(const ErrnoRestorer&);
+  void operator=(const ErrnoRestorer&);
+};
+
+#endif // ERRNO_RESTORER_H
diff --git a/libc/private/ThreadLocalBuffer.h b/libc/private/ThreadLocalBuffer.h
index 1c5e3f4..703acd6 100644
--- a/libc/private/ThreadLocalBuffer.h
+++ b/libc/private/ThreadLocalBuffer.h
@@ -37,18 +37,17 @@
 // TODO: move __cxa_guard_acquire and __cxa_guard_release into libc.
 
 #define GLOBAL_INIT_THREAD_LOCAL_BUFFER(name) \
-  static pthread_once_t __bionic_tls_ ## name ## _once; \
   static pthread_key_t __bionic_tls_ ## name ## _key; \
   static void __bionic_tls_ ## name ## _key_destroy(void* buffer) { \
     free(buffer); \
   } \
-  static void __bionic_tls_ ## name ## _key_init() { \
+  /* Run automatically when libc.so is opened by the dynamic linker. */ \
+  __attribute__((constructor)) static void __bionic_tls_ ## name ## _key_init() { \
     pthread_key_create(&__bionic_tls_ ## name ## _key, __bionic_tls_ ## name ## _key_destroy); \
   }
 
 // Leaves "name_tls_buffer" and "name_tls_buffer_size" defined and initialized.
 #define LOCAL_INIT_THREAD_LOCAL_BUFFER(type, name, byte_count) \
-  pthread_once(&__bionic_tls_ ## name ## _once, __bionic_tls_ ## name ## _key_init); \
   type name ## _tls_buffer = \
       reinterpret_cast<type>(pthread_getspecific(__bionic_tls_ ## name ## _key)); \
   if (name ## _tls_buffer == NULL) { \
diff --git a/libc/private/bionic_futex.h b/libc/private/bionic_futex.h
index eb49fb7..6c7fdbe 100644
--- a/libc/private/bionic_futex.h
+++ b/libc/private/bionic_futex.h
@@ -30,6 +30,8 @@
 
 #include <linux/futex.h>
 
+__BEGIN_DECLS
+
 extern int __futex_wait(volatile void *ftx, int val, const struct timespec *timeout);
 extern int __futex_wake(volatile void *ftx, int count);
 
@@ -54,4 +56,6 @@
 extern int  __futex_wake_ex(volatile void *ftx, int pshared, int val);
 extern int  __futex_wait_ex(volatile void *ftx, int pshared, int val, const struct timespec *timeout);
 
+__END_DECLS
+
 #endif /* _BIONIC_FUTEX_H */
diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h
index edf878f..846eb84 100644
--- a/libc/private/bionic_tls.h
+++ b/libc/private/bionic_tls.h
@@ -43,28 +43,40 @@
  ** pre-allocated slot directly for performance reason).
  **/
 
-/* Maximum number of elements in the TLS array. */
-#define BIONIC_TLS_SLOTS            64
-
 /* Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted. */
 enum {
   TLS_SLOT_SELF = 0, /* The kernel requires this specific slot for x86. */
   TLS_SLOT_THREAD_ID,
   TLS_SLOT_ERRNO,
+
+  /* These two aren't used by bionic itself, but allow the graphics code to
+   * access TLS directly rather than using the pthread API. */
   TLS_SLOT_OPENGL_API = 3,
   TLS_SLOT_OPENGL = 4,
+
+  /* This slot is only used to pass information from the dynamic linker to
+   * libc.so when the C library is loaded in to memory. The C runtime init
+   * function will then clear it. Since its use is extremely temporary,
+   * we reuse an existing location that isn't needed during libc startup. */
+  TLS_SLOT_BIONIC_PREINIT = TLS_SLOT_OPENGL_API,
+
   TLS_SLOT_STACK_GUARD = 5, /* GCC requires this specific slot for x86. */
   TLS_SLOT_DLERROR,
 
   TLS_SLOT_FIRST_USER_SLOT /* Must come last! */
 };
 
-/* This slot is only used to pass information from the dynamic linker to
- * libc.so when the C library is loaded in to memory. The C runtime init
- * function will then clear it. Since its use is extremely temporary,
- * we reuse an existing location that isn't needed during libc startup.
+/*
+ * Maximum number of elements in the TLS array.
+ * POSIX says this must be at least 128, but Android has traditionally had only 64, minus those
+ * ones used internally by bionic itself.
+ * There are two kinds of slot used internally by bionic --- there are the well-known slots
+ * enumerated above, and then there are those that are allocated during startup by calls to
+ * pthread_key_create; grep for GLOBAL_INIT_THREAD_LOCAL_BUFFER to find those. We need to manually
+ * maintain that second number, but pthread_test will fail if we forget.
  */
-#define  TLS_SLOT_BIONIC_PREINIT    TLS_SLOT_OPENGL_API
+#define GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT 4
+#define BIONIC_TLS_SLOTS (64 + TLS_SLOT_FIRST_USER_SLOT + GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT)
 
 /* set the Thread Local Storage, must contain at least BIONIC_TLS_SLOTS pointers */
 extern void __init_tls(void**  tls, void*  thread_info);