Revert "Revert "More pthreads cleanup.""

This reverts commit 6f94de3ca49e4ea147b1c59e5818fa175846518f

(Doesn't try to increase the number of TLS slots; that leads to
an inability to boot. Adds more tests.)

Change-Id: Ia7d25ba3995219ed6e686463dbba80c95cc831ca
diff --git a/libc/bionic/tmpfile.cpp b/libc/bionic/tmpfile.cpp
index b97cc6c..8419ff5 100644
--- a/libc/bionic/tmpfile.cpp
+++ b/libc/bionic/tmpfile.cpp
@@ -38,6 +38,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "private/ErrnoRestorer.h"
+
 class ScopedSignalBlocker {
  public:
   ScopedSignalBlocker() {
@@ -77,9 +79,8 @@
     struct stat sb;
     int rc = fstat(fd, &sb);
     if (rc == -1) {
-      int old_errno = errno;
+      ErrnoRestorer errno_restorer;
       close(fd);
-      errno = old_errno;
       return NULL;
     }
   }
@@ -91,9 +92,8 @@
   }
 
   // Failure. Clean up. We already unlinked, so we just need to close.
-  int old_errno = errno;
+  ErrnoRestorer errno_restorer;
   close(fd);
-  errno = old_errno;
   return NULL;
 }