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) +// should always be the first include on Windows. +# include +#endif + #if !defined(ASAN_EXTERNAL_TEST_CONFIG) # define INCLUDED_FROM_ASAN_TEST_UTILS_H # include "asan_test_config.h" @@ -30,10 +35,52 @@ #include #if !defined(_WIN32) -#include -#include -#include -#include +# include +# include +# include + +# include +// 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(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 #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