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. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #ifndef SANITIZER_PTHREAD_WRAPPERS_H 19 #define SANITIZER_PTHREAD_WRAPPERS_H 20 21 #include "sanitizer_test_utils.h" 22 23 #if !defined(_WIN32) 24 # include <pthread.h> 25 // Simply forward the arguments and check that the pthread functions succeed. 26 # define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d)) 27 # define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b)) 28 #else 29 typedef HANDLE pthread_t; 30 31 struct PthreadHelperCreateThreadInfo { 32 void *(*start_routine)(void *); 33 void *arg; 34 }; 35 36 inline DWORD WINAPI PthreadHelperThreadProc(void *arg) { 37 PthreadHelperCreateThreadInfo *start_data = 38 reinterpret_cast<PthreadHelperCreateThreadInfo*>(arg); 39 void *ret = (start_data->start_routine)(start_data->arg); 40 delete start_data; 41 return (DWORD)ret; 42 } 43 44 inline void PTHREAD_CREATE(pthread_t *thread, void *attr, 45 void *(*start_routine)(void *), void *arg) { 46 ASSERT_EQ(0, attr) << "Thread attributes are not supported yet."; 47 PthreadHelperCreateThreadInfo *data = new PthreadHelperCreateThreadInfo; 48 data->start_routine = start_routine; 49 data->arg = arg; 50 *thread = CreateThread(0, 0, PthreadHelperThreadProc, data, 0, 0); 51 ASSERT_NE(nullptr, *thread) << "Failed to create a thread."; 52 } 53 54 inline void PTHREAD_JOIN(pthread_t thread, void **value_ptr) { 55 ASSERT_EQ(0, value_ptr) << "Nonzero value_ptr is not supported yet."; 56 ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE)); 57 ASSERT_NE(0, CloseHandle(thread)); 58 } 59 60 inline void pthread_exit(void *retval) { 61 ASSERT_EQ(0, retval) << "Nonzero retval is not supported yet."; 62 ExitThread((DWORD)retval); 63 } 64 #endif // _WIN32 65 66 #endif // SANITIZER_PTHREAD_WRAPPERS_H 67