c-index-test: Run inside a separate thread iff we have pthread support, to
ensure we at least get some minimal testing of running in a multithreaded
environment (for example, having a reduced stack size).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@115200 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index acc868e..4d4e117 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -1478,7 +1478,9 @@
     "   scan-function - scan function bodies (non-PCH)\n\n");
 }
 
-int main(int argc, const char **argv) {
+/***/
+
+int cindextest_main(int argc, const char **argv) {
   clang_enableStackTraces();
   if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
     return perform_code_completion(argc, argv, 0);
@@ -1538,3 +1540,55 @@
   print_usage();
   return 1;
 }
+
+/***/
+
+/* We intentionally run in a separate thread to ensure we at least minimal
+ * testing of a multithreaded environment (for example, having a reduced stack
+ * size). */
+
+#include "llvm/Config/config.h"
+#ifdef HAVE_PTHREAD_H
+
+#include <pthread.h>
+
+typedef struct thread_info {
+  int argc;
+  const char **argv;
+  int result;
+} thread_info;
+void *thread_runner(void *client_data_v) {
+  thread_info *client_data = client_data_v;
+  client_data->result = cindextest_main(client_data->argc, client_data->argv);
+  return 0;
+}
+
+int main(int argc, const char **argv) {
+  thread_info client_data;
+  pthread_t thread;
+  int res;
+
+  client_data.argc = argc;
+  client_data.argv = argv;
+  res = pthread_create(&thread, 0, thread_runner, &client_data);
+  if (res != 0) {
+    perror("thread creation failed");
+    return 1;
+  }
+
+  res = pthread_join(thread, 0);
+  if (res != 0) {
+    perror("thread join failed");
+    return 1;
+  }
+
+  return client_data.result;
+}
+
+#else
+
+int main(int argc, const char **argv) {
+  return cindextest_main(argc, argv);
+}
+
+#endif