1 //===-- sanitizer_pthread_wrappers.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
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))
31 typedef HANDLE pthread_t
;
33 struct PthreadHelperCreateThreadInfo
{
34 void *(*start_routine
)(void *);
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
);
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
;
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
);
68 #endif // SANITIZER_PTHREAD_WRAPPERS_H