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