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/dirent.cpp b/libc/bionic/dirent.cpp
index 3a7b5b4..74297d8 100644
--- a/libc/bionic/dirent.cpp
+++ b/libc/bionic/dirent.cpp
@@ -36,7 +36,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <private/ScopedPthreadMutexLocker.h>
+#include "private/ErrnoRestorer.h"
+#include "private/ScopedPthreadMutexLocker.h"
 
 struct DIR {
   int fd_;
@@ -108,7 +109,7 @@
 }
 
 int readdir_r(DIR* d, dirent* entry, dirent** result) {
-  int saved_errno = errno;
+  ErrnoRestorer errno_restorer;
 
   *result = NULL;
   errno = 0;
@@ -124,7 +125,6 @@
     memcpy(entry, next, next->d_reclen);
     *result = entry;
   }
-  errno = saved_errno;
   return 0;
 }