[PATCH] [ASan tests] Make simple pthread tests compile and pass on Windows

Timur Iskhodzhanov timurrrr at google.com
Mon May 12 07:54:27 PDT 2014


Hi kcc,

http://reviews.llvm.org/D3725

Files:
  lib/asan/tests/asan_test_utils.h

Index: lib/asan/tests/asan_test_utils.h
===================================================================
--- lib/asan/tests/asan_test_utils.h
+++ lib/asan/tests/asan_test_utils.h
@@ -14,6 +14,11 @@
 #ifndef ASAN_TEST_UTILS_H
 #define ASAN_TEST_UTILS_H
 
+#if defined(_WIN32)
+// <windows.h> should always be the first include on Windows.
+# include <windows.h>
+#endif
+
 #if !defined(ASAN_EXTERNAL_TEST_CONFIG)
 # define INCLUDED_FROM_ASAN_TEST_UTILS_H
 # include "asan_test_config.h"
@@ -30,10 +35,52 @@
 #include <algorithm>
 
 #if !defined(_WIN32)
-#include <strings.h>
-#include <pthread.h>
-#include <sys/mman.h>
-#include <setjmp.h>
+# include <strings.h>
+# include <sys/mman.h>
+# include <setjmp.h>
+
+# include <pthread.h>
+// Check that pthread_create/pthread_join return success.
+# define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
+# define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
+#else
+typedef HANDLE pthread_t;
+
+struct ThreadStartData {
+  void *(*start_routine)(void *);
+  void *arg;
+};
+
+inline DWORD WINAPI ThreadProc(void *arg) {
+  ThreadStartData *start_data = reinterpret_cast<ThreadStartData*>(arg);
+  void *ret = (start_data->start_routine)(start_data->arg);
+  delete start_data;
+  return (DWORD)ret;
+}
+
+inline void my_pthread_create(pthread_t *thread, void *attr,
+                              void *(*start_routine)(void *), void *arg) {
+  ASSERT_EQ(0, attr) << "Thread attributes are not supported yet.";
+  ThreadStartData *data = new ThreadStartData;
+  data->start_routine = start_routine;
+  data->arg = arg;
+  *thread = CreateThread(0, 0, ThreadProc, data, 0, 0);
+  ASSERT_NE(*thread, nullptr) << "Failed to create a thread.";
+}
+
+inline void my_pthread_join(pthread_t thread, void **value_ptr) {
+  ASSERT_EQ(0, value_ptr) << "Nonzero value_ptr is not supported yet.";
+  ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE));
+  CloseHandle(thread);
+}
+
+inline void pthread_exit(void *retval) {
+  ASSERT_EQ(0, retval) << "Nonzero retval is not supported yet.";
+  ExitThread((DWORD)retval);
+}
+
+# define PTHREAD_CREATE(a, b, c, d) my_pthread_create(a, b, c, d)
+# define PTHREAD_JOIN(a, b) my_pthread_join(a, b)
 #endif
 
 #ifdef __linux__
@@ -48,10 +95,6 @@
 #include <malloc.h>
 #endif
 
-// Check that pthread_create/pthread_join return success.
-#define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
-#define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
-
 #if ASAN_HAS_EXCEPTIONS
 # define ASAN_THROW(x) throw (x)
 #else
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3725.9311.patch
Type: text/x-patch
Size: 2565 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140512/56f2bbae/attachment.bin>


More information about the llvm-commits mailing list