1 //===-- sanitizer_pthread_wrappers.h ----------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef SANITIZER_PTHREAD_WRAPPERS_H
17 #define SANITIZER_PTHREAD_WRAPPERS_H
19 #include "sanitizer_test_utils.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))
27 typedef HANDLE pthread_t
;
29 struct PthreadHelperCreateThreadInfo
{
30 void *(*start_routine
)(void *);
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
);
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
;
48 *thread
= CreateThread(0, 0, PthreadHelperThreadProc
, data
, 0, 0);
49 ASSERT_NE(nullptr, *thread
) << "Failed to create a thread.";
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
));
58 inline void pthread_exit(void *retval
) {
59 ASSERT_EQ(0, retval
) << "Nonzero retval is not supported yet.";
60 ExitThread((DWORD
)retval
);
64 #endif // SANITIZER_PTHREAD_WRAPPERS_H