[Sanitizer tests] Make simple pthread tests compile and pass on Windows
[blocksruntime.git] / lib / sanitizer_common / tests / sanitizer_pthread_wrappers.h
bloba59ac5c7c9436b0d3904416790951468bad4f1b9
1 //===-- sanitizer_pthread_wrappers.h ----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of *Sanitizer runtime.
11 // It provides handy wrappers for thread manipulation, that:
12 // a) assert on any failure rather than returning an error code
13 // b) defines pthread-like interface on platforms where where <pthread.h>
14 // is not supplied by default.
16 //===----------------------------------------------------------------------===//
18 #ifndef SANITIZER_PTHREAD_WRAPPERS_H
19 #define SANITIZER_PTHREAD_WRAPPERS_H
21 #include "sanitizer_test_utils.h"
23 #include "gtest/gtest.h"
25 #if !defined(_WIN32)
26 # include <pthread.h>
27 // Simply forward the arguments and check that the pthread functions succeed.
28 # define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
29 # define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
30 #else
31 typedef HANDLE pthread_t;
33 struct PthreadHelperCreateThreadInfo {
34 void *(*start_routine)(void *);
35 void *arg;
38 inline DWORD WINAPI PthreadHelperThreadProc(void *arg) {
39 PthreadHelperCreateThreadInfo *start_data =
40 reinterpret_cast<PthreadHelperCreateThreadInfo*>(arg);
41 void *ret = (start_data->start_routine)(start_data->arg);
42 delete start_data;
43 return (DWORD)ret;
46 inline void PTHREAD_CREATE(pthread_t *thread, void *attr,
47 void *(*start_routine)(void *), void *arg) {
48 ASSERT_EQ(0, attr) << "Thread attributes are not supported yet.";
49 PthreadHelperCreateThreadInfo *data = new PthreadHelperCreateThreadInfo;
50 data->start_routine = start_routine;
51 data->arg = arg;
52 *thread = CreateThread(0, 0, PthreadHelperThreadProc, data, 0, 0);
53 ASSERT_NE(nullptr, *thread) << "Failed to create a thread.";
56 inline void PTHREAD_JOIN(pthread_t thread, void **value_ptr) {
57 ASSERT_EQ(0, value_ptr) << "Nonzero value_ptr is not supported yet.";
58 ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE));
59 ASSERT_NE(0, CloseHandle(thread));
62 inline void pthread_exit(void *retval) {
63 ASSERT_EQ(0, retval) << "Nonzero retval is not supported yet.";
64 ExitThread((DWORD)retval);
66 #endif // _WIN32
68 #endif // SANITIZER_PTHREAD_WRAPPERS_H